1212
1313class BIP32 :
1414 def __init__ (self , chaincode , privkey = None , pubkey = None , fingerprint = None ,
15- depth = 0 , index = 0 ):
15+ depth = 0 , index = 0 , network = "main" ):
1616 """
1717 :param chaincode: The master chaincode, used to derive keys. As bytes.
1818 :param privkey: The master private key for this index (default 0).
@@ -28,6 +28,7 @@ def __init__(self, chaincode, privkey=None, pubkey=None, fingerprint=None,
2828 need this for serialization.
2929 :param index: If we are instanciated from an existing extended key, we
3030 need this for serialization.
31+ :param network: Either "main" or "test".
3132 """
3233 assert isinstance (chaincode , bytes )
3334 assert privkey is not None or pubkey is not None
@@ -43,6 +44,7 @@ def __init__(self, chaincode, privkey=None, pubkey=None, fingerprint=None,
4344 self .parent_fingerprint = fingerprint
4445 self .depth = depth
4546 self .index = index
47+ self .network = network
4648
4749 def get_extended_privkey_from_path (self , path ):
4850 """Get an extended privkey from a list of indexes (path).
@@ -124,7 +126,8 @@ def get_xpriv_from_path(self, path):
124126 chaincode , privkey = self .get_extended_privkey_from_path (path )
125127 extended_key = _serialize_extended_key (privkey , self .depth + len (path ),
126128 parent_pubkey ,
127- path [- 1 ], chaincode )
129+ path [- 1 ], chaincode ,
130+ self .network )
128131 return base58 .b58encode_check (extended_key ).decode ()
129132
130133 def get_xpub_from_path (self , path ):
@@ -143,23 +146,26 @@ def get_xpub_from_path(self, path):
143146 chaincode , pubkey = self .get_extended_pubkey_from_path (path )
144147 extended_key = _serialize_extended_key (pubkey , self .depth + len (path ),
145148 parent_pubkey ,
146- path [- 1 ], chaincode )
149+ path [- 1 ], chaincode ,
150+ self .network )
147151 return base58 .b58encode_check (extended_key ).decode ()
148152
149153 def get_master_xpriv (self ):
150154 """Get the encoded extended private key of the master private key"""
151155 extended_key = _serialize_extended_key (self .master_privkey , self .depth ,
152156 self .parent_fingerprint ,
153157 self .index ,
154- self .master_chaincode )
158+ self .master_chaincode ,
159+ self .network )
155160 return base58 .b58encode_check (extended_key ).decode ()
156161
157162 def get_master_xpub (self ):
158163 """Get the encoded extended public key of the master public key"""
159164 extended_key = _serialize_extended_key (self .master_pubkey , self .depth ,
160165 self .parent_fingerprint ,
161166 self .index ,
162- self .master_chaincode )
167+ self .master_chaincode ,
168+ self .network )
163169 return base58 .b58encode_check (extended_key ).decode ()
164170
165171 @classmethod
@@ -171,8 +177,6 @@ def from_xpriv(cls, xpriv):
171177 extended_key = base58 .b58decode_check (xpriv )
172178 (prefix , depth , fingerprint ,
173179 index , chaincode , key ) = _unserialize_extended_key (extended_key )
174- serialized = _serialize_extended_key (key [1 :], depth , fingerprint , index ,
175- chaincode )
176180 # We need to remove the trailing `0` before the actual private key !!
177181 return BIP32 (chaincode , key [1 :], None , fingerprint , depth , index )
178182
@@ -188,11 +192,11 @@ def from_xpub(cls, xpub):
188192 return BIP32 (chaincode , None , key , fingerprint , depth , index )
189193
190194 @classmethod
191- def from_seed (cls , seed ):
195+ def from_seed (cls , seed , network = "main" ):
192196 """Get a BIP32 "wallet" out of this seed (maybe after BIP39?)
193197
194198 :param seed: The seed as bytes.
195199 """
196200 secret = hmac .new ("Bitcoin seed" .encode (), seed ,
197201 hashlib .sha512 ).digest ()
198- return BIP32 (secret [32 :], secret [:32 ])
202+ return BIP32 (secret [32 :], secret [:32 ], network = network )
0 commit comments