-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextrakeys.go
111 lines (84 loc) · 2.48 KB
/
extrakeys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//go:build libsecp256k1
package gosecp256k1
/*
#cgo CFLAGS: -I${SRCDIR}/libsecp256k1/include -I${SRCDIR}/libsecp256k1/src
#cgo CFLAGS: -DECMULT_GEN_PREC_BITS=4
#cgo CFLAGS: -DECMULT_WINDOW_SIZE=15
#cgo CFLAGS: -DENABLE_MODULE_SCHNORRSIG=1
#cgo CFLAGS: -DENABLE_MODULE_EXTRAKEYS=1
#include "./libsecp256k1/include/secp256k1_extrakeys.h"
#include "./libsecp256k1/include/secp256k1.h"
*/
import "C"
import "unsafe"
// todo::: use internal context.
type (
XonlyPubkey struct {
Data [64]byte
}
Pubkey struct {
Data [64]byte
}
Seckey struct {
Data [32]byte
}
Keypair struct {
Data [96]byte
}
)
func NewKeypairFromSeckey(context *C.secp256k1_context, seckey [32]byte) *Keypair {
var keypair C.secp256k1_keypair
kp := new(Keypair)
if C.secp256k1_keypair_create(context, &keypair, (*C.uchar)(unsafe.Pointer(&seckey[0]))) == 1 {
copy(kp.Data[:], (*[64]byte)(unsafe.Pointer(&keypair.data[0]))[:])
return nil
}
return kp
}
func (kp *Keypair) Seckey(context *C.secp256k1_context) *Seckey {
var seckey [32]byte
var keypair C.secp256k1_keypair
copy((*[96]byte)(unsafe.Pointer(&keypair.data[0]))[:], kp.Data[:])
if C.secp256k1_keypair_sec(context, (*C.uchar)(unsafe.Pointer(&seckey[0])), &keypair) == 1 {
return &Seckey{
Data: seckey,
}
}
return nil
}
func (kp *Keypair) Pubkey(context *C.secp256k1_context) *Pubkey {
var pubkey C.secp256k1_pubkey
var keypair C.secp256k1_keypair
copy((*[96]byte)(unsafe.Pointer(&keypair.data[0]))[:], kp.Data[:])
pk := new(Pubkey)
if C.secp256k1_keypair_pub(context, &pubkey, &keypair) == 1 {
copy((*[64]byte)(unsafe.Pointer(&pubkey.data[0]))[:], pk.Data[:])
return pk
}
return nil
}
func (kp *Keypair) XonlyPubkey(context *C.secp256k1_context) *XonlyPubkey {
var xonly C.secp256k1_xonly_pubkey
var keypair C.secp256k1_keypair
copy((*[96]byte)(unsafe.Pointer(&keypair.data[0]))[:], kp.Data[:])
xopk := new(XonlyPubkey)
// todo::: getting int *pk_parity from caller.
if C.secp256k1_keypair_xonly_pub(context, &xonly, nil, &keypair) == 1 {
copy((*[64]byte)(unsafe.Pointer(&xonly.data[0]))[:], xopk.Data[:])
return xopk
}
return nil
}
func NewXonlyPubkey() *XonlyPubkey {
return &XonlyPubkey{
Data: [64]byte{},
}
}
func (xp *XonlyPubkey) XonlyPubkeyParse(context *C.secp256k1_context, pk [32]byte) bool {
var xonly C.secp256k1_xonly_pubkey
if C.secp256k1_xonly_pubkey_parse(context, &xonly, (*C.uchar)(unsafe.Pointer(&pk[0]))) == 1 {
copy(xp.Data[:], (*[64]byte)(unsafe.Pointer(&xonly.data[0]))[:])
return true
}
return false
}