Skip to content

Commit 9cb0984

Browse files
committed
bip32: unserialize and store the network prefix
1 parent 4fc61a3 commit 9cb0984

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

bip32/bip32.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,11 @@ def from_xpriv(cls, xpriv):
183183
:param xpriv: (str) The encoded serialized extended private key.
184184
"""
185185
extended_key = base58.b58decode_check(xpriv)
186-
(prefix, depth, fingerprint,
186+
(network, depth, fingerprint,
187187
index, chaincode, key) = _unserialize_extended_key(extended_key)
188188
# We need to remove the trailing `0` before the actual private key !!
189-
return BIP32(chaincode, key[1:], None, fingerprint, depth, index)
189+
return BIP32(chaincode, key[1:], None, fingerprint, depth, index,
190+
network)
190191

191192
@classmethod
192193
def from_xpub(cls, xpub):
@@ -195,9 +196,9 @@ def from_xpub(cls, xpub):
195196
:param xpub: (str) The encoded serialized extended public key.
196197
"""
197198
extended_key = base58.b58decode_check(xpub)
198-
(prefix, depth, fingerprint,
199+
(network, depth, fingerprint,
199200
index, chaincode, key) = _unserialize_extended_key(extended_key)
200-
return BIP32(chaincode, None, key, fingerprint, depth, index)
201+
return BIP32(chaincode, None, key, fingerprint, depth, index, network)
201202

202203
@classmethod
203204
def from_seed(cls, seed, network="main"):

bip32/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,20 @@ def _unserialize_extended_key(extended_key):
157157
158158
:param extended_key: The extended key to unserialize __as bytes__
159159
160-
:return: prefix (int), depth (int), fingerprint (bytes), index (int),
160+
:return: network (str), depth (int), fingerprint (bytes), index (int),
161161
chaincode (bytes), key (bytes)
162162
"""
163163
assert isinstance(extended_key, bytes) and len(extended_key) == 78
164164
prefix = int.from_bytes(extended_key[:4], "big")
165+
if prefix in list(ENCODING_PREFIX["main"].values()):
166+
network = "main"
167+
else:
168+
network = "test"
165169
depth = extended_key[4]
166170
fingerprint = extended_key[5:9]
167171
index = int.from_bytes(extended_key[9:13], "big")
168172
chaincode, key = extended_key[13:45], extended_key[45:]
169-
return prefix, depth, fingerprint, index, chaincode, key
173+
return network, depth, fingerprint, index, chaincode, key
170174

171175

172176
def _hardened_index_in_path(path):

tests/test_bip32.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,23 @@ def test_sanity_tests():
124124
assert bip32.get_master_xpriv() == xpriv2
125125
assert bip32.get_xpriv_from_path([HARDENED_INDEX, 18]) == xpriv
126126
assert bip32.get_xpub_from_path([HARDENED_INDEX, 18]) == xpub
127+
# We should recognize the networks..
128+
# .. for xprivs:
129+
bip32 = BIP32.from_xpriv("xprv9wHokC2KXdTSpEepFcu53hMDUHYfAtTaLEJEMyxBPAMf78hJg17WhL5FyeDUQH5KWmGjGgEb2j74gsZqgupWpPbZgP6uFmP8MYEy5BNbyET")
130+
assert bip32.network == "main"
131+
bip32 = BIP32.from_xpriv("tprv8ZgxMBicQKsPeCBsMzQCCb5JcW4S49MVL3EwhdZMF1RF71rgisZU4ZRvrHX6PZQEiNUABDLvYqpx8Lsccq8aGGR59qHAoLoE3iXYuDa8JTP")
132+
assert bip32.network == "test"
133+
# .. for xpubs:
134+
bip32 = BIP32.from_xpub("xpub6AHA9hZDN11k2ijHMeS5QqHx2KP9aMBRhTDqANMnwVtdyw2TDYRmF8PjpvwUFcL1Et8Hj59S3gTSMcUQ5gAqTz3Wd8EsMTmF3DChhqPQBnU")
135+
assert bip32.network == "main"
136+
bip32 = BIP32.from_xpub("tpubD6NzVbkrYhZ4WN3WiKRjeo2eGyYNiKNg8vcQ1UjLNJJaDvoFhmR1XwJsbo5S4vicSPoWQBThR3Rt8grXtP47c1AnoiXMrEmFdRZupxJzH1j")
137+
assert bip32.network == "test"
138+
# We should create valid network encoding..
139+
assert BIP32.from_seed(os.urandom(32),
140+
"test").get_master_xpub().startswith("tpub")
141+
assert BIP32.from_seed(os.urandom(32),
142+
"test").get_master_xpriv().startswith("tprv")
143+
assert BIP32.from_seed(os.urandom(32),
144+
"main").get_master_xpub().startswith("xpub")
145+
assert BIP32.from_seed(os.urandom(32),
146+
"main").get_master_xpriv().startswith("xprv")

0 commit comments

Comments
 (0)