-
Notifications
You must be signed in to change notification settings - Fork 2
/
hdprivatekey.go
executable file
·163 lines (125 loc) · 4.17 KB
/
hdprivatekey.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package libwallet
import (
"crypto/sha256"
"errors"
"fmt"
"strings"
"github.com/muun/libwallet/hdpath"
"github.com/btcsuite/btcutil/hdkeychain"
)
// HDPrivateKey is an HD capable priv key
type HDPrivateKey struct {
key hdkeychain.ExtendedKey
Network *Network
Path string
}
// NewHDPrivateKey builds an HD priv key from a seed for a given network
func NewHDPrivateKey(seed []byte, network *Network) (*HDPrivateKey, error) {
key, err := hdkeychain.NewMaster(seed, network.network)
if err != nil {
return nil, err
}
return &HDPrivateKey{key: *key, Network: network, Path: "m"}, nil
}
// NewHDPrivateKeyFromBytes builds an HD priv key from the compress priv and chain code for a given network
func NewHDPrivateKeyFromBytes(rawKey, chainCode []byte, network *Network) (*HDPrivateKey, error) {
parentFP := []byte{0, 0, 0, 0}
key := hdkeychain.NewExtendedKey(network.network.HDPrivateKeyID[:],
rawKey, chainCode, parentFP, 0, 0, true)
return &HDPrivateKey{key: *key, Network: network, Path: "m"}, nil
}
// NewHDPrivateKeyFromString creates an HD priv key from a base58-encoded string
// If the parsed key is public, it returns an error
func NewHDPrivateKeyFromString(str, path string, network *Network) (*HDPrivateKey, error) {
key, err := hdkeychain.NewKeyFromString(str)
if err != nil {
return nil, err
}
if !key.IsPrivate() {
return nil, errors.New("encoded key was not a private key")
}
return &HDPrivateKey{key: *key, Network: network, Path: path}, nil
}
// PublicKey returns the matching pub key
func (p *HDPrivateKey) PublicKey() *HDPublicKey {
key, err := p.key.Neuter()
if err != nil {
panic("original key was invalid")
}
return &HDPublicKey{key: *key, Network: p.Network, Path: p.Path}
}
// String return the key base58-encoded
func (p *HDPrivateKey) String() string {
return p.key.String()
}
// DerivedAt derives a new child priv key, which may be hardened
// index should be uint32 but for java compat we use int64
func (p *HDPrivateKey) DerivedAt(index int64, hardened bool) (*HDPrivateKey, error) {
var modifier uint32
if hardened {
modifier = hdkeychain.HardenedKeyStart
}
child, err := p.key.Child(uint32(index) | modifier)
if err != nil {
return nil, err
}
parentPath, err := hdpath.Parse(p.Path)
if err != nil {
return nil, err
}
path := parentPath.Child(uint32(index) | modifier)
return &HDPrivateKey{key: *child, Network: p.Network, Path: path.String()}, nil
}
func (p *HDPrivateKey) DeriveTo(path string) (*HDPrivateKey, error) {
if !strings.HasPrefix(path, p.Path) {
return nil, fmt.Errorf("derivation path %v is not prefix of the keys path %v", path, p.Path)
}
firstPath, err := hdpath.Parse(p.Path)
if err != nil {
return nil, fmt.Errorf("couldn't parse derivation path %v: %w", p.Path, err)
}
secondPath, err := hdpath.Parse(path)
if err != nil {
return nil, fmt.Errorf("couldn't parse derivation path %v: %w", path, err)
}
indexes := secondPath.IndexesFrom(firstPath)
derivedKey := p
for depth, index := range indexes {
derivedKey, err = derivedKey.DerivedAt(int64(index.Index), index.Hardened)
if err != nil {
return nil, fmt.Errorf("failed to derive key at path %v on depth %v: %w", path, depth, err)
}
}
// The generated path has no names in it, so replace it
derivedKey.Path = path
return derivedKey, nil
}
// Sign a payload using the backing EC key
func (p *HDPrivateKey) Sign(data []byte) ([]byte, error) {
signingKey, err := p.key.ECPrivKey()
if err != nil {
return nil, err
}
hash := sha256.Sum256(data)
sig, err := signingKey.Sign(hash[:])
if err != nil {
return nil, err
}
return sig.Serialize(), nil
}
func (p *HDPrivateKey) Decrypter() Decrypter {
return &hdPrivKeyDecrypter{p, nil, true}
}
func (p *HDPrivateKey) DecrypterFrom(senderKey *PublicKey) Decrypter {
return &hdPrivKeyDecrypter{p, senderKey, false}
}
func (p *HDPrivateKey) Encrypter() Encrypter {
return &hdPubKeyEncrypter{p.PublicKey(), p}
}
func (p *HDPrivateKey) EncrypterTo(receiver *HDPublicKey) Encrypter {
return &hdPubKeyEncrypter{receiver, p}
}
// What follows is a workaround for https://github.com/golang/go/issues/46893
func SignWithPrivateKey(key *HDPrivateKey, data []byte) ([]byte, error) {
return key.Sign(data)
}