Skip to content

Commit f40be3f

Browse files
committed
Add DeriveBIP32WithPath
1 parent a7681b2 commit f40be3f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

bitcoin/common.go

+20
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const (
4242
TransactionConfirmations = 1
4343
)
4444

45+
func BitcoinDefaultDerivationPath() []byte {
46+
return []byte{2, 0, 0, 0}
47+
}
48+
4549
func ParseSatoshi(amount string) (int64, error) {
4650
amt, err := decimal.NewFromString(amount)
4751
if err != nil {
@@ -145,6 +149,22 @@ func DeriveBIP32(public string, chainCode []byte, children ...uint32) (string, s
145149
return extPub.String(), hex.EncodeToString(pub.SerializeCompressed()), nil
146150
}
147151

152+
func DeriveBIP32WithPath(public, chaincode string, path8 []byte) (string, error) {
153+
if path8[0] > 3 {
154+
panic(path8[0])
155+
}
156+
path32 := make([]uint32, path8[0])
157+
for i := 0; i < int(path8[0]); i++ {
158+
path32[i] = uint32(path8[1+i])
159+
}
160+
cc, err := hex.DecodeString(chaincode)
161+
if err != nil {
162+
return "", err
163+
}
164+
_, sdk, err := DeriveBIP32(public, cc, path32...)
165+
return sdk, err
166+
}
167+
148168
func HashMessageForSignature(msg string, chain byte) ([]byte, error) {
149169
var buf bytes.Buffer
150170
prefix := "Bitcoin Signed Message:\n"

0 commit comments

Comments
 (0)