-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinternal.go
79 lines (61 loc) · 2.15 KB
/
internal.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
package bip32
import (
"encoding/binary"
"github.com/btcsuite/btcd/btcec"
"github.com/sammyne/base58"
)
// appendMeta serialize the meta part of the given public key
// into bytes sequence and then append them to the buf, the address
// of which will be return.
func appendMeta(buf []byte, pub *PublicKey) []byte {
var childIndex [ChildIndexLen]byte
binary.BigEndian.PutUint32(childIndex[:], pub.ChildIndex)
// The serialized format of meta is:
// depth (1) || parent fingerprint (4)) || child num (4) || chain code (32)
// note the missing version and data fields
//str := make([]byte, 0, KeyLen-VersionLen)
//str = append(str, pub.Version...)
buf = append(buf, pub.Level)
buf = append(buf, pub.ParentFP...)
buf = append(buf, childIndex[:]...)
buf = append(buf, pub.ChainCode...)
return buf
}
// decodePublicKey decodes a public key out of the given base58-check encoded
// key string.
// Note: the decoded key goes through format check only, no on-curve check
func decodePublicKey(data58 string) (*PublicKey, error) {
decoded, version, err := base58.CheckDecodeX(data58, VersionLen)
if nil != err {
return nil, err
}
if KeyLen != len(decoded)+VersionLen {
return nil, ErrInvalidKeyLen
}
pub := new(PublicKey)
// The serialized format is:
// version (4) || depth (1) || parent fingerprint (4)) ||
// child num (4) || chain code (32) || key data (33)
// where the version has separated from decoded
// decompose the decoded payload into fields
pub.Version = version
a, b := 0, DepthLen
pub.Level = decoded[a:b][0]
a, b = b, b+FingerprintLen
pub.ParentFP = decoded[a:b]
a, b = b, b+ChildIndexLen
pub.ChildIndex = binary.BigEndian.Uint32(decoded[a:b])
a, b = b, b+ChainCodeLen
pub.ChainCode = decoded[a:b]
a, b = b, b+KeyDataLen
pub.Data = decoded[a:b]
return pub, nil
}
// derivePublicKey calculates the public key corresponding to the input
// private key, and output the public key data in compressed form.
func derivePublicKey(priv []byte) []byte {
// load the public key data eagerly
x, y := secp256k1Curve.ScalarBaseMult(priv)
pubKey := btcec.PublicKey{Curve: secp256k1Curve, X: x, Y: y}
return pubKey.SerializeCompressed()
}