commit b856336f8ce868717457676112a453b370eb9408
parent bab22c8c53a274b74e3a17f6c7bc9d3eba1ddb10
Author: SomberNight <somber.night@protonmail.com>
Date: Thu, 18 Feb 2021 00:36:20 +0100
bip32: add new test vectors from BIP
also yet-to-be-merged test from https://github.com/bitcoin/bips/pull/1030
Diffstat:
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/electrum/tests/test_bitcoin.py b/electrum/tests/test_bitcoin.py
@@ -463,23 +463,23 @@ class Test_xprv_xpub(ElectrumTestCase):
'xtype': 'p2wpkh'},
)
- def _do_test_bip32(self, seed: str, sequence):
+ def _do_test_bip32(self, seed: str, sequence: str):
node = BIP32Node.from_rootseed(bfh(seed), xtype='standard')
xprv, xpub = node.to_xprv(), node.to_xpub()
- self.assertEqual("m/", sequence[0:2])
- sequence = sequence[2:]
- for n in sequence.split('/'):
- if n[-1] != "'":
- xpub2 = BIP32Node.from_xkey(xpub).subkey_at_public_derivation(n).to_xpub()
- node = BIP32Node.from_xkey(xprv).subkey_at_private_derivation(n)
+ int_path = convert_bip32_path_to_list_of_uint32(sequence)
+ for n in int_path:
+ if n & bip32.BIP32_PRIME == 0:
+ xpub2 = BIP32Node.from_xkey(xpub).subkey_at_public_derivation([n]).to_xpub()
+ node = BIP32Node.from_xkey(xprv).subkey_at_private_derivation([n])
xprv, xpub = node.to_xprv(), node.to_xpub()
- if n[-1] != "'":
+ if n & bip32.BIP32_PRIME == 0:
self.assertEqual(xpub, xpub2)
return xpub, xprv
def test_bip32(self):
# see https://en.bitcoin.it/wiki/BIP_0032_TestVectors
+ # and https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Test_Vectors
xpub, xprv = self._do_test_bip32("000102030405060708090a0b0c0d0e0f", "m/0'/1/2'/2/1000000000")
self.assertEqual("xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy", xpub)
self.assertEqual("xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76", xprv)
@@ -488,6 +488,14 @@ class Test_xprv_xpub(ElectrumTestCase):
self.assertEqual("xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt", xpub)
self.assertEqual("xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j", xprv)
+ xpub, xprv = self._do_test_bip32("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be", "m/0h")
+ self.assertEqual("xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y", xpub)
+ self.assertEqual("xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L", xprv)
+
+ xpub, xprv = self._do_test_bip32("3ddd5602285899a946114506157c7997e5444528f3003f6134712147db19b678", "m/0h/1h")
+ self.assertEqual("xpub6BJA1jSqiukeaesWfxe6sNK9CCGaujFFSJLomWHprUL9DePQ4JDkM5d88n49sMGJxrhpjazuXYWdMf17C9T5XnxkopaeS7jGk1GyyVziaMt", xpub)
+ self.assertEqual("xprv9xJocDuwtYCMNAo3Zw76WENQeAS6WGXQ55RCy7tDJ8oALr4FWkuVoHJeHVAcAqiZLE7Je3vZJHxspZdFHfnBEjHqU5hG1Jaj32dVoS6XLT1", xprv)
+
def test_xpub_from_xprv(self):
"""We can derive the xpub key from a xprv."""
for xprv_details in self.xprv_xpub: