Skip to content

Commit

Permalink
Merge pull request #7 from libsv/feat/derived_keys
Browse files Browse the repository at this point in the history
Extended Key Derivation helpers
  • Loading branch information
theflyingcodr authored Aug 23, 2021
2 parents 0e59664 + 61964db commit ccbb89a
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
59 changes: 59 additions & 0 deletions bip32/derivationpaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package bip32
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)

var (
numericPlusTick = regexp.MustCompile(`^[0-9]+'{0,1}$`)
)

// DerivePath given an uint64 number will generate a hardened BIP32 path 3 layers deep.
//
// This is achieved by the following process:
Expand Down Expand Up @@ -44,3 +49,57 @@ func DeriveNumber(path string) (uint64, error) {
seed += d3 - (1 << 31)
return seed, nil
}

// DeriveChildFromPath will generate a new extended key derived from the key k using the
// bip32 path provided, ie "1234/0/123"
// Child keys must be ints or hardened keys followed by '.
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
func (k *ExtendedKey) DeriveChildFromPath(derivationPath string) (*ExtendedKey, error) {
if derivationPath == "" {
return k, nil
}
key := k
children := strings.Split(derivationPath, "/")
for _, child := range children {
if !numericPlusTick.MatchString(child) {
return nil, fmt.Errorf("invalid path: %q", derivationPath)
}
childInt, err := childInt(child)
if err != nil {
return nil, fmt.Errorf("derive key failed %w", err)
}
key, err = key.Child(childInt)
if err != nil {
return nil, fmt.Errorf("derive key failed %w", err)
}
}
return key, nil
}

// DerivePublicKeyFromPath will generate a new extended key derived from the key k using the
// bip32 path provided, ie "1234/0/123". It will then transform to an bec.PublicKey before
// serialising the bytes and returning.
func (k *ExtendedKey) DerivePublicKeyFromPath(derivationPath string) ([]byte, error) {
key, err := k.DeriveChildFromPath(derivationPath)
if err != nil {
return nil, err
}
pubKey, err := key.ECPubKey()
if err != nil {
return nil, fmt.Errorf("failed to generate public key %w", err)
}
return pubKey.SerialiseCompressed(), nil
}

func childInt(child string) (uint32, error) {
var suffix uint32
if strings.HasSuffix(child, "'") {
child = strings.TrimRight(child, "'")
suffix = 2147483648 // 2^32
}
t, err := strconv.ParseUint(child, 10, 32)
if err != nil {
return 0, fmt.Errorf("failed to get child int %w", err)
}
return uint32(t) + suffix, nil
}
73 changes: 73 additions & 0 deletions bip32/derivationpaths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,76 @@ func TestDeriveSeed(t *testing.T) {
})
}
}

func Test_DeriveChildFromPath(t *testing.T) {
t.Parallel()
tests := map[string]struct {
key *ExtendedKey
path string
expPriv string
expPub string
err error
}{
"successful run, 1 level child, should return no errors": {
key: func() *ExtendedKey {
k, err := NewKeyFromString("xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi")
assert.NoError(t, err)
return k
}(),
path: "0/1",
expPriv: "xprv9ww7sMFLzJMzy7bV1qs7nGBxgKYrgcm3HcJvGb4yvNhT9vxXC7eX7WVULzCfxucFEn2TsVvJw25hH9d4mchywguGQCZvRgsiRaTY1HCqN8G",
expPub: "xpub6AvUGrnEpfvJBbfx7sQ89Q8hEMPM65UteqEX4yUbUiES2jHfjexmfJoxCGSwFMZiPBaKQT1RiKWrKfuDV4vpgVs4Xn8PpPTR2i79rwHd4Zr",
err: nil,
}, "successful run, 2 level child, should return no errors": {
key: func() *ExtendedKey {
k, err := NewKeyFromString("xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi")
assert.NoError(t, err)
return k
}(),
path: "0/1/100000",
expPriv: "xprv9xrdP7iD2MKJthXr1NiyGJ5KqmD2sLbYYFi49AMq9bXrKJGKBnjx5ivSzXRfLhXxzQNsqCi51oUjniwGemvfAZpzpAGohpzFkat42ohU5bR",
expPub: "xpub6BqyndF6risc7BcK7QFydS24Po3XGoKPuUdewYmShw4qC6bTjL4CdXEvqow6yhsfAtvU8e6kHPNFM2LzeWwKQoJm6hrYttTcxVQrk42WRE3",
err: nil,
}, "successful run, 10 level child, should return no errors": {
key: func() *ExtendedKey {
k, err := NewKeyFromString("xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi")
assert.NoError(t, err)
return k
}(),
path: "0/1/1/1/1/1/1/1/1/2147483647",
expPriv: "xprvAD89K3nZjaG8NqELN8Ce2ATWTcRADLH6JTbrXoVJT6eBRbMwbG7J75v3ym4tGC7X3Mih5krQF77pGi6GNdvxfNcr6WqYacHCSa6uzotoAx2",
expPub: "xpub6S7ViZKTZwpRbKJoU9jePJQF1eFecnzwfgXTLBtv1SBAJPh68oRYetEXq1RvGzsYnTzeikfdM5UM3WDrSZxuBrJi5nLpGxsuSE6cDE8pB2o",
err: nil,
}, "successful run, 1 level, hardened": {
key: func() *ExtendedKey {
k, err := NewKeyFromString("xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi")
assert.NoError(t, err)
return k
}(),
path: "0/1'",
expPriv: "xprv9ww7sMFVKxty8iXvY7Yn2NyvHZ2CgEoAYXmvf2a4XvkhzBUBmYmaMWyjyAhSxgyKK4zYzbJT6hT4JeGW5fFcNaYsBsBR9a8TxVX1LJQiZ1P",
expPub: "xpub6AvUGrnPALTGMCcPe95nPWveqarh5hX1ukhXTQyg6GHgryoLK65puKJDpTcMBKJKdtXQYVwbK3zMgydcTcf5qpLpJcULu9hKUxx5rzgYhrk",
err: nil,
}, "successful run, 3 level, hardened": {
key: func() *ExtendedKey {
k, err := NewKeyFromString("xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi")
assert.NoError(t, err)
return k
}(),
path: "10/1'/1000'/15'",
expPriv: "xprvA1bKm9LnkQbMvUW6kwKDLFapT9V9vTeh9D9VnVSJhRf8KmqQTc9W5YboNYcUUkZLreNq1NmeuPpw8x86C87gGyxyV6jNBV4kztFrPdSWz2t",
expPub: "xpub6EagAesgan9f8xaZrxrDhPXZ1BKeKvNYWS56asqvFmC7CaAZ19TkdLvHDrzubSMiC6tAqTMcumVFkgT2duhZncV3KieshEDHNc4jPWkRMGD",
err: nil,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
k, err := test.key.DeriveChildFromPath(test.path)
assert.NoError(t, err)
assert.Equal(t, test.expPriv, k.String())
pubKey, err := k.Neuter()
assert.NoError(t, err)
assert.Equal(t, test.expPub, pubKey.String())
})
}
}

0 comments on commit ccbb89a

Please sign in to comment.