def test_from_master_key(): keystore = from_master_key('xprv9xpBW4EdWnv4PEASBsu3VuPNAcxRiSMXTjAfZ9dkP5FCrKWCacKZBhS3cJVGCe' 'gAUNEp1uXXEncSAyro5CaJFwv7wYFcBQrF6MfWYoAXsTw') assert keystore.xprv == ('xprv9xpBW4EdWnv4PEASBsu3VuPNAcxRiSMXTjAfZ9dkP5FCrKWCacKZBhS3cJVGCe' 'gAUNEp1uXXEncSAyro5CaJFwv7wYFcBQrF6MfWYoAXsTw') assert keystore.xpub == ('xpub6BoXuZmXMAUMbiEuHuS3s3L6ienv7u5Npx6GMY3MwQnBj7qM89dojV' 'kXTZtbpEvAzxSKAxnnsVDuwSAAvvXHWVncpX46V3LGj5SaKHtNNnc')
def test_get_pubkey_derivation(self): mpk_hex = ("08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a688" "63b37df75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d0") keystore = from_master_key(mpk_hex) assert keystore.get_pubkey_derivation(XPublicKey( 'fe08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68' '863b37df75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d000000400' )) == [0, 4] assert keystore.get_pubkey_derivation(XPublicKey( 'fe08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68863b37d' 'f75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d001000301' )) == [1, 259] with pytest.raises(ValueError): keystore.get_pubkey_derivation(XPublicKey( 'fe18863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68863b37d' 'f75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d001000301' ))
def test_hex_master_public_key(self): # An uncompressed public key in hex form without the 04 prefix mpk_hex = ("08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a688" "63b37df75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d0") keystore = from_master_key(mpk_hex) assert isinstance(keystore, Old_KeyStore) assert keystore.get_master_public_key() == mpk_hex assert keystore.dump() == {'mpk': mpk_hex, 'type': 'old'} assert keystore.is_watching_only() assert keystore.get_xpubkey(False, 4).to_hex() == ( 'fe08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68' '863b37df75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d000000400' ) assert keystore.get_xpubkey(True, 259).to_hex() == ( 'fe08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68863b37d' 'f75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d001000301' )
def test_is_signature_candidate(self): mpk_hex = ("08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a688" "63b37df75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d0") keystore = from_master_key(mpk_hex) assert keystore.is_signature_candidate(XPublicKey.from_hex( 'fe08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68' '863b37df75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d000000400' )) assert keystore.is_signature_candidate(XPublicKey.from_hex( 'fe08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68863b37d' 'f75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d001000301' )) with pytest.raises(ValueError): keystore.is_signature_candidate(XPublicKey.from_hex( 'fe18863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68863b37d' 'f75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d001000301' ))
def test_hex_master_public_key(self): # An uncompressed public key in hex form without the 04 prefix mpk_hex = ("08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a688" "63b37df75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d0") keystore = from_master_key(mpk_hex) assert isinstance(keystore, Old_KeyStore) assert keystore.get_master_public_key() == mpk_hex assert keystore.to_derivation_data() == {'mpk': mpk_hex, 'subpaths':[]} assert keystore.is_watching_only() assert keystore.get_xpubkey((0, 4)) == XPublicKey.from_hex( 'fe08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68' '863b37df75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d000000400' ) assert keystore.get_xpubkey((1, 259)) == XPublicKey.from_hex( 'fe08863ac1de668decc6406880c4c8d9a74e9986a5e8d9f2be262ac4af8a68863b37d' 'f75ac48afcbb68bdd6a00f58a648bda9e5eb5e73bd51ef130a6e72dc698d001000301' )
def run_non_RPC(config): cmdname = config.get('cmd') storage = WalletStorage(config.get_wallet_path()) if storage.file_exists(): sys.exit("Error: Remove the existing wallet first!") def password_dialog(): return prompt_password( "Password (hit return if you do not wish to encrypt your wallet):") if cmdname == 'restore': text = config.get('text').strip() passphrase = config.get('passphrase', '') password = password_dialog() if keystore.is_private(text) else None if keystore.is_address_list(text): wallet = ImportedAddressWallet.from_text(storage, text) elif keystore.is_private_key_list(text): wallet = ImportedPrivkeyWallet.from_text(storage, text, password) else: if keystore.is_seed(text): k = keystore.from_seed(text, passphrase, False) elif keystore.is_master_key(text): k = keystore.from_master_key(text) else: sys.exit("Error: Seed or key not recognized") if password: k.update_password(None, password) storage.put('keystore', k.dump()) storage.put('wallet_type', 'standard') storage.put('use_encryption', bool(password)) storage.write() wallet = Wallet(storage) if not config.get('offline'): network = Network(config) network.start() wallet.start_threads(network) print("Recovering wallet...") wallet.synchronize() wallet.wait_until_synchronized() msg = ("Recovery successful" if wallet.is_found() else "Found no history for this wallet") else: msg = ("This wallet was restored offline. " "It may contain more addresses than displayed.") print(msg) elif cmdname == 'create': password = password_dialog() passphrase = config.get('passphrase', '') seed_type = 'standard' seed = Mnemonic('en').make_seed(seed_type) k = keystore.from_seed(seed, passphrase, False) storage.put('keystore', k.dump()) storage.put('wallet_type', 'standard') wallet = Wallet(storage) wallet.update_password(None, password, True) wallet.synchronize() print("Your wallet generation seed is:\n\"%s\"" % seed) print("Please keep it in a safe place; if you lose it, " "you will not be able to restore your wallet.") wallet.storage.write() print("Wallet saved in '%s'" % wallet.storage.path) sys.exit(0)
def run_non_RPC(config): cmdname = config.get('cmd') storage = WalletStorage(config.get_wallet_path()) if storage.file_exists(): sys.exit("Error: Remove the existing wallet first!") def password_dialog(): return prompt_password( "Password (hit return if you do not wish to encrypt your wallet):") if cmdname == 'restore': text = config.get('text').strip() passphrase = config.get('passphrase', '') password = password_dialog() if keystore.is_private(text) else None parent_wallet = ParentWallet.as_legacy_wallet_container(storage) if keystore.is_address_list(text): legacy_wallet = ImportedAddressWallet.from_text( parent_wallet, text) elif keystore.is_private_key_list(text): legacy_wallet = ImportedPrivkeyWallet.from_text( parent_wallet, text) else: if keystore.is_seed(text): k = keystore.from_seed(text, passphrase, False) elif keystore.is_master_key(text): k = keystore.from_master_key(text) else: sys.exit("Error: Seed or key not recognized") keystore_usage = parent_wallet.add_keystore(k.dump()) Standard_Wallet.create_within_parent( parent_wallet, keystore_usage=[keystore_usage]) if password: parent_wallet.update_password(None, password) if not config.get('offline'): network = Network() network.add_wallet(parent_wallet) print("Recovering wallet...") parent_wallet.synchronize() msg = ("Recovery successful" if parent_wallet.has_usage() else "Found no history for this wallet") else: msg = ("This wallet was restored offline. " "It may contain more addresses than displayed.") print(msg) elif cmdname == 'create': password = password_dialog() passphrase = config.get('passphrase', '') seed_type = 'standard' seed = Mnemonic('en').make_seed(seed_type) k = keystore.from_seed(seed, passphrase, False) parent_wallet = ParentWallet.as_legacy_wallet_container(storage) keystore_usage = parent_wallet.add_keystore(k.dump()) Standard_Wallet.create_within_parent(parent_wallet, keystore_usage=[keystore_usage]) parent_wallet.update_password(None, password) parent_wallet.synchronize() print("Your wallet generation seed is:\n\"%s\"" % seed) print("Please keep it in a safe place; if you lose it, " "you will not be able to restore your wallet.") parent_wallet.save_storage() print("Wallet saved in '%s'" % parent_wallet.get_storage_path()) sys.exit(0)