-
Notifications
You must be signed in to change notification settings - Fork 19
/
hd_master_key_pair.go
69 lines (57 loc) · 1.34 KB
/
hd_master_key_pair.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
package wallet
import (
"crypto/ed25519"
"encoding/hex"
"code.vegaprotocol.io/vega/wallet/crypto"
)
type HDMasterKeyPair struct {
publicKey *key
privateKey *key
algo crypto.SignatureAlgorithm
}
func NewHDMasterKeyPair(
publicKey ed25519.PublicKey,
privateKey ed25519.PrivateKey,
) (*HDMasterKeyPair, error) {
algo, err := crypto.NewSignatureAlgorithm(crypto.Ed25519, 1)
if err != nil {
return nil, err
}
return &HDMasterKeyPair{
publicKey: &key{
bytes: publicKey,
encoded: hex.EncodeToString(publicKey),
},
privateKey: &key{
bytes: privateKey,
encoded: hex.EncodeToString(privateKey),
},
algo: algo,
}, nil
}
func (k *HDMasterKeyPair) PublicKey() string {
return k.publicKey.encoded
}
func (k *HDMasterKeyPair) PrivateKey() string {
return k.privateKey.encoded
}
func (k *HDMasterKeyPair) AlgorithmVersion() uint32 {
return k.algo.Version()
}
func (k *HDMasterKeyPair) AlgorithmName() string {
return k.algo.Name()
}
func (k *HDMasterKeyPair) SignAny(data []byte) ([]byte, error) {
return k.algo.Sign(k.privateKey.bytes, data)
}
func (k *HDMasterKeyPair) Sign(data []byte) (*Signature, error) {
sig, err := k.algo.Sign(k.privateKey.bytes, data)
if err != nil {
return nil, err
}
return &Signature{
Value: hex.EncodeToString(sig),
Algo: k.algo.Name(),
Version: k.algo.Version(),
}, nil
}