def test_mnemonic_to_mnemonic(self): self.assertEqual(Mnemonic().to_mnemonic('28acfc94465fd2f6774759d6897ec122'), 'chunk gun celery million wood kite tackle twenty story episode raccoon dutch') self.assertEqual(Mnemonic().to_mnemonic('28acfc94465fd2f6774759d6897ec122', add_checksum=False), 'action filter venture match garlic nut oven modify output dwarf wild cattle') self.assertRaisesRegexp(ValueError, "Integer value of data should be in secp256k1 domain between 1 and " "secp256k1_n-1", Mnemonic().to_mnemonic, '28acfc94465fd2f6774759d6897ec12228acfc94465fd2f6774759d6897ec12228acfc94465fd2f6774')
def get_passphrase(args): inp_passphrase = Mnemonic('english').generate(args.passphrase_strength) print("\nYour mnemonic private key sentence is: %s" % inp_passphrase) print("\nPlease write down on paper and backup. With this key you can restore your wallet and all keys") passphrase = inp_passphrase.split(' ') inp = input("\nType 'yes' if you understood and wrote down your key: ") if inp not in ['yes', 'Yes', 'YES']: clw_exit("Exiting...") return passphrase
def test_mnemonic_exceptions(self): self.assertRaisesRegexp(ValueError, "Strength should be divisible by 32", Mnemonic().generate, 20) self.assertRaisesRegexp(ValueError, "Data length in bits should be divisible by 32", Mnemonic().checksum, 'aabbccddeeff') self.assertRaisesRegexp(Warning, "Unrecognised word", Mnemonic().sanitize_mnemonic, 'action filter venture match garlic nut oven modify output dwarf wild fiets') self.assertRaisesRegexp(Warning, "Could not detect language", Mnemonic().detect_language, 'floep fliep')
def create_wallet(wallet_name, args, databasefile): if args.network is None: args.network = DEFAULT_NETWORK print("\nCREATE wallet '%s' (%s network)" % (wallet_name, args.network)) if args.create_multisig: if not isinstance(args.create_multisig, list) or len(args.create_multisig) < 2: clw_exit("Please enter multisig creation parameter in the following format: " "<number-of-signatures> <number-of-signatures-required> " "<key-0> <key-1> [<key-2> ... <key-n>]") try: sigs_total = int(args.create_multisig[0]) except ValueError: clw_exit("Number of total signatures (first argument) must be a numeric value. %s" % args.create_multisig[0]) try: sigs_required = int(args.create_multisig[1]) except ValueError: clw_exit("Number of signatures required (second argument) must be a numeric value. %s" % args.create_multisig[1]) key_list = args.create_multisig[2:] keys_missing = sigs_total - len(key_list) assert(keys_missing >= 0) if keys_missing: print("Not all keys provided, creating %d additional keys" % keys_missing) for _ in range(keys_missing): passphrase = get_passphrase(args) passphrase = ' '.join(passphrase) seed = binascii.hexlify(Mnemonic().to_seed(passphrase)) key_list.append(HDKey().from_seed(seed, network=args.network)) return HDWallet.create_multisig(name=wallet_name, keys=key_list, sigs_required=sigs_required, network=args.network, databasefile=databasefile, sort_keys=True, witness_type=args.witness_type) elif args.create_from_key: return HDWallet.create(name=wallet_name, network=args.network, keys=args.create_from_key, databasefile=databasefile, witness_type=args.witness_type) else: passphrase = args.passphrase if passphrase is None: passphrase = get_passphrase(args) elif not passphrase: passphrase = input("Enter Passphrase: ") if not isinstance(passphrase, list): passphrase = passphrase.split(' ') elif len(passphrase) == 1: passphrase = passphrase[0].split(' ') if len(passphrase) < 12: clw_exit("Please specify passphrase with 12 words or more") passphrase = ' '.join(passphrase) seed = binascii.hexlify(Mnemonic().to_seed(passphrase)) hdkey = HDKey().from_seed(seed, network=args.network) return HDWallet.create(name=wallet_name, network=args.network, keys=hdkey, witness_type=args.witness_type, databasefile=databasefile)
def _check_list(self, language, vectors): mnemo = Mnemonic(language) for v in vectors: if v[0]: phrase = mnemo.to_mnemonic(v[0]) else: phrase = v[1] seed = change_base(mnemo.to_seed(phrase, v[4]), 256, 16) print("Test %s => %s" % (v[0], phrase)) self.assertEqual(v[1], phrase) self.assertEqual(v[2], seed) k = HDKey.from_seed(seed) self.assertEqual(k.wif(), v[3])
def _check_list(self, language, vectors): mnemo = Mnemonic(language) for v in vectors: if v[0]: phrase = mnemo.to_mnemonic(v[0], check_on_curve=False) else: phrase = v[1] seed = change_base(mnemo.to_seed(phrase, v[4], validate=False), 256, 16) # print("Test %s => %s" % (v[0], phrase)) self.assertEqual(v[1], phrase) self.assertEqual(v[2], seed) k = HDKey.from_seed(seed) self.assertEqual(k.wif(is_private=True), v[3])
def _check_list(self, language, vectors): mnemo = Mnemonic(language) for v in vectors: if v[0]: phrase = mnemo.to_mnemonic(v[0]) else: phrase = v[1] seed = change_base(mnemo.to_seed(phrase, v[4]), 256, 16) print("Test %s => %s" % (v[0], phrase)) self.assertEqual(v[1], phrase) self.assertEqual(v[2], seed) k = HDKey.from_seed(seed) self.assertEqual(k.extended_wif(), v[3])
def create_wallet(wallet_name, args, databasefile): print("\nCREATE wallet '%s' (%s network)" % (wallet_name, args.network)) if args.create_multisig: if not isinstance(args.create_multisig, list) or len(args.create_multisig) < 3: clw_exit( "Please enter multisig creation parameter in the following format: " "<number-of-signatures-required> <key-0> <key-1> [<key-2> ... <key-n>]" ) try: sigs_required = int(args.create_multisig[0]) except ValueError: clw_exit( "Number of signatures required (first argument) must be a numeric value. %s" % args.create_multisig[0]) key_list = args.create_multisig[1:] return HDWallet.create_multisig(name=wallet_name, key_list=key_list, sigs_required=sigs_required, network=args.network, databasefile=databasefile) else: passphrase = args.passphrase if passphrase is None: inp_passphrase = Mnemonic('english').generate( args.passphrase_strength) print("\nYour mnemonic private key sentence is: %s" % inp_passphrase) print( "\nPlease write down on paper and backup. With this key you can restore your wallet and all keys" ) passphrase = inp_passphrase.split(' ') inp = input( "\nType 'yes' if you understood and wrote down your key: ") if inp not in ['yes', 'Yes', 'YES']: clw_exit("Exiting...") elif not passphrase: passphrase = input("Enter Passphrase: ") if not isinstance(passphrase, list): passphrase = passphrase.split(' ') elif len(passphrase) == 1: passphrase = passphrase[0].split(' ') if len(passphrase) < 12: clw_exit("Please specify passphrase with 12 words or more") passphrase = ' '.join(passphrase) seed = binascii.hexlify(Mnemonic().to_seed(passphrase)) hdkey = HDKey().from_seed(seed, network=args.network) return HDWallet.create(name=wallet_name, network=args.network, key=hdkey.wif(), databasefile=databasefile)
def create_paper_wallets(self, output_keys, style_file, template_file, image_size_factor=1): count = 0 for wallet_key in output_keys: address_img = qrcode.make(wallet_key.address) filename_pre = "%s/%d-" % (WALLET_DIR, wallet_key.key_id) address_img.save(filename_pre+'address.png', 'PNG') private_wif = wallet_key.key().key.wif() priv_img = qrcode.make(private_wif) priv_img.save(filename_pre+'privatekey.png', 'PNG') passphrase = Mnemonic().to_mnemonic(wallet_key._hdkey_object.private_byte) f = open('templates/'+template_file, 'r') template = Template(f.read()) wallet_name = "%s %s %d" % (self.name, wallet_key.name, wallet_key.key_id) wallet_str = template.render( install_dir=INSTALL_DIR, filename_pre=filename_pre, wallet_name=wallet_name, private_key=private_wif, passphrase=passphrase, address=wallet_key.address, currency_name=self.network.currency_name, currency_name_plural=self.network.currency_name_plural, image_size_factor=image_size_factor) print("Generate wallet %d" % wallet_key.key_id) pdfkit.from_string(wallet_str, filename_pre+'wallet.pdf', options=pdfkit_options, css=style_file) count += 1 print("A total of %d paper wallets have been created" % count)
def test_wallet_multisig_sign_with_external_single_key(self): if os.path.isfile(DATABASEFILE_UNITTESTS): os.remove(DATABASEFILE_UNITTESTS) NETWORK = 'bitcoinlib_test' words = 'square innocent drama' seed = Mnemonic().to_seed(words, 'password') hdkey = HDKey.from_seed(seed, network=NETWORK) hdkey.key_type = 'single' key_list = [ HDKey(network=NETWORK).account_multisig_key().public(), HDKey(network=NETWORK), hdkey.public() ] wallet = HDWallet.create_multisig('Multisig-2-of-3-example', key_list, 2, network=NETWORK, databasefile=DATABASEFILE_UNITTESTS) wallet.new_key() wallet.utxos_update() res = wallet.send_to('n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi', 10000000) t = res['transaction'].raw_hex() t2 = wallet.transaction_import(t) self.assertFalse(t2.verify()) t2 = wallet.transaction_sign(t2, hdkey) self.assertTrue(t2.verify())
def test_wallet_key_import_and_sign_multisig(self): if os.path.isfile(DATABASEFILE_UNITTESTS): os.remove(DATABASEFILE_UNITTESTS) network = 'bitcoinlib_test' words = 'square innocent drama' seed = Mnemonic().to_seed(words, 'password') hdkey = HDKey.from_seed(seed, network=network) hdkey.key_type = 'single' key_list = [ HDKey(network=network).account_multisig_key().public(), HDKey(network=network), hdkey.public() ] wallet = HDWallet.create_multisig('Multisig-2-of-3-example', key_list, 2, sort_keys=True, network=network, databasefile=DATABASEFILE_UNITTESTS) wallet.new_key() wallet.utxos_update() wallet.import_key(hdkey) wt = wallet.send_to('n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi', 10000000) self.assertIsNone(wt.error)
def test_mnemonic_wordlists(self): self.assertEqual(Mnemonic().word(2047), 'zoo') self.assertEqual(Mnemonic(language='spanish').word(2047), 'zurdo') self.assertEqual(Mnemonic(language='french').word(2047), 'zoologie') self.assertEqual(Mnemonic(language='italian').word(2047), 'zuppa') self.assertEqual(Mnemonic(language='japanese').word(2047), 'われる') self.assertEqual(Mnemonic(language='chinese_simplified').word(2047), '歇') self.assertEqual(Mnemonic(language='chinese_traditional').word(2047), '歇') self.assertEqual(len(Mnemonic().wordlist()), 2048)
def from_passphrase(passphrase, password='', network=DEFAULT_NETWORK): """ Create key from Mnemonic passphrase :param passphrase: Mnemonic passphrase, list of words as string seperated with a space character :type passphrase: str :param password: Password to protect passphrase :type password: str :param network: Network to use :type network: str, Network :return HDKey: """ return HDKey().from_seed(Mnemonic().to_seed(passphrase, password), network=network)
def create_wallet(wallet_name, args, databasefile): if args.network is None: args.network = DEFAULT_NETWORK print("\nCREATE wallet '%s' (%s network)" % (wallet_name, args.network)) if args.create_multisig: if not isinstance(args.create_multisig, list) or len(args.create_multisig) < 3: clw_exit( "Please enter multisig creation parameter in the following format: " "<number-of-signatures-required> <key-0> <key-1> [<key-2> ... <key-n>]" ) try: sigs_required = int(args.create_multisig[0]) except ValueError: clw_exit( "Number of signatures required (first argument) must be a numeric value. %s" % args.create_multisig[0]) key_list = args.create_multisig[1:] return HDWallet.create_multisig(name=wallet_name, key_list=key_list, sigs_required=sigs_required, network=args.network, databasefile=databasefile, sort_keys=True) elif args.create_from_key: return HDWallet.create(name=wallet_name, network=args.network, key=args.create_from_key, databasefile=databasefile) else: passphrase = args.passphrase if passphrase is None: passphrase = get_passphrase(args) elif not passphrase: passphrase = input("Enter Passphrase: ") if not isinstance(passphrase, list): passphrase = passphrase.split(' ') elif len(passphrase) == 1: passphrase = passphrase[0].split(' ') if len(passphrase) < 12: clw_exit("Please specify passphrase with 12 words or more") passphrase = ' '.join(passphrase) seed = binascii.hexlify(Mnemonic().to_seed(passphrase)) hdkey = HDKey().from_seed(seed, network=args.network) return HDWallet.create(name=wallet_name, network=args.network, key=hdkey.wif(), databasefile=databasefile)
print( "\nWallet %s already exists. Please specify (not existing) wallet name for wallet to recover" % wallet_name) sys.exit() wallet = BulkPaperWallet(wallet_name) if wallet.network.network_name != args.network: print( "\nNetwork setting (%s) ignored. Using network from defined wallet instead: %s" % (args.network, wallet.network.network_name)) network = wallet.network.network_name network_obj = Network(network) print("\nOpen wallet '%s' (%s network)" % (wallet_name, network)) else: print("\nCREATE wallet '%s' (%s network)" % (wallet_name, network)) if not args.recover_wallet_passphrase: words = Mnemonic('english').generate(args.passphrase_strength) print("\nYour mnemonic private key sentence is: %s" % words) print( "\nPlease write down on paper and backup. With this key you can restore all paper wallets if " "something goes wrong during this process. You can / have to throw away this private key after " "the paper wallets are distributed.") inp = input( "\nType 'yes' if you understood and wrote down your key: ") if inp not in ['yes', 'Yes', 'YES']: print("Exiting...") sys.exit() else: words = args.recover_wallet_passphrase seed = binascii.hexlify(Mnemonic().to_seed(words)) hdkey = HDKey().from_seed(seed, network=network)
def is_word(word): print(word in Mnemonic().wordlist())
def test_mnemonic_to_entropy(self): phrase = 'usage grid neither voice worry armor sudden core excuse keen stand pudding' self.assertEqual(Mnemonic().to_entropy(phrase), b'\xef\x8c\xceP\xfa\xbf\xdc\x17\xf6!\x82O\x0f7RV')
def test_mnemonic_generate_eror(self): self.assertRaisesRegexp(ValueError, 'Strenght should be divisible by 32', Mnemonic().generate, 11)
input = raw_input except NameError: pass # # Create Wallets # # First recreate database to avoid already exist errors test_databasefile = 'bitcoinlib.test.sqlite' test_database = DEFAULT_DATABASEDIR + test_databasefile if os.path.isfile(test_database): os.remove(test_database) print("\n=== Create a simple Mnemonic wallet ===") passphrase = Mnemonic().generate() print("Your private key passphrase is:", passphrase) password = input("Enter password to protect passphrase: ") wlt = HDWallet.create('mnwlttest1', key=passphrase, password=password, network='bitcoinlib_test', databasefile=test_database) wlt.get_key() wlt.utxos_update() # Create some test UTXOs wlt.info() to_key = wlt.get_key() print("\n- Create transaction (send to own wallet)") t = wlt.send_to(to_key.address, 50000000) t.info()
def main(): print("Command Line Wallet for BitcoinLib\n") # --- Parse commandline arguments --- args = parse_args() databasefile = DEFAULT_DATABASE if args.database: databasefile = os.path.join(BCL_DATABASE_DIR, args.database) if args.generate_key: passphrase = get_passphrase(args) passphrase = ' '.join(passphrase) seed = binascii.hexlify(Mnemonic().to_seed(passphrase)) hdkey = HDKey.from_seed(seed, network=args.network) print( "Private master key, to create multisig wallet on this machine: %s" % hdkey.wif()) print( "Public account key, to share with other cosigner multisig wallets: %s" % hdkey.public_master(witness_type=args.witness_type, multisig=True)) print("Network: %s" % hdkey.network.name) clw_exit() # List wallets, then exit if args.list_wallets: print("BitcoinLib wallets:") for w in wallets_list(databasefile=databasefile): if 'parent_id' in w and w['parent_id']: continue print("[%d] %s (%s) %s" % (w['id'], w['name'], w['network'], w['owner'])) clw_exit() # Delete specified wallet, then exit if args.wallet_remove: if not wallet_exists(args.wallet_name, databasefile=databasefile): clw_exit("Wallet '%s' not found" % args.wallet_name) inp = input( "\nWallet '%s' with all keys and will be removed, without private key it cannot be restored." "\nPlease retype exact name of wallet to proceed: " % args.wallet_name) if inp == args.wallet_name: if wallet_delete(args.wallet_name, force=True, databasefile=databasefile): clw_exit("\nWallet %s has been removed" % args.wallet_name) else: clw_exit("\nError when deleting wallet") else: clw_exit("\nSpecified wallet name incorrect") wlt = None if args.wallet_name and not args.wallet_name.isdigit( ) and not wallet_exists(args.wallet_name, databasefile=databasefile): if not args.create_from_key and input( "Wallet %s does not exist, create new wallet [yN]? " % args.wallet_name).lower() != 'y': clw_exit('Aborted') wlt = create_wallet(args.wallet_name, args, databasefile) args.wallet_info = True else: try: wlt = HDWallet(args.wallet_name, databasefile=databasefile) if args.passphrase is not None: print( "WARNING: Using passphrase option for existing wallet ignored" ) if args.create_from_key is not None: print( "WARNING: Using create_from_key option for existing wallet ignored" ) except WalletError as e: clw_exit("Error: %s" % e.msg) if wlt is None: clw_exit("Could not open wallet %s" % args.wallet_name) if args.import_private: if wlt.import_key(args.import_private): clw_exit("Private key imported") else: clw_exit("Failed to import key") if args.wallet_recreate: wallet_empty(args.wallet_name) print("Removed transactions and generated keys from this wallet") if args.update_utxos: wlt.utxos_update() if args.update_transactions: wlt.scan(scan_gap_limit=5) if args.export_private: if wlt.scheme == 'multisig': for w in wlt.cosigner: if w.main_key and w.main_key.is_private: print(w.main_key.wif) elif not wlt.main_key or not wlt.main_key.is_private: print("No private key available for this wallet") else: print(wlt.main_key.wif) clw_exit() if args.network is None: args.network = wlt.network.name tx_import = None if args.import_tx_file: try: fn = args.import_tx_file f = open(fn, "r") except FileNotFoundError: clw_exit("File %s not found" % args.import_tx_file) try: tx_import = ast.literal_eval(f.read()) except (ValueError, SyntaxError): tx_import = f.read() if args.import_tx: try: tx_import = ast.literal_eval(args.import_tx) except (ValueError, SyntaxError): tx_import = args.import_tx if tx_import: if isinstance(tx_import, dict): wt = wlt.transaction_import(tx_import) else: wt = wlt.transaction_import_raw(tx_import, network=args.network) wt.sign() if args.push: res = wt.send() if res: print("Transaction pushed to network. Transaction ID: %s" % wt.hash) else: print("Error creating transaction: %s" % wt.error) wt.info() print("Signed transaction:") print_transaction(wt) clw_exit() if args.receive: keys = wlt.get_key(network=args.network, number_of_keys=args.receive) if args.receive != 1: keys += wlt.get_key_change(network=args.network, number_of_keys=args.receive) keys = [keys] if not isinstance(keys, list) else keys print("Receive address(es):") for key in keys: addr = key.address print(addr) if QRCODES_AVAILABLE and args.receive == 1: qrcode = pyqrcode.create(addr) print(qrcode.terminal()) if not QRCODES_AVAILABLE and args.receive == 1: print( "Install qr code module to show QR codes: pip install pyqrcode" ) clw_exit() if args.create_transaction == []: clw_exit("Missing arguments for --create-transaction/-t option") if args.create_transaction: if args.fee_per_kb: clw_exit("Fee-per-kb option not allowed with --create-transaction") try: wt = create_transaction(wlt, args.create_transaction, args) except WalletError as e: clw_exit("Cannot create transaction: %s" % e.msg) wt.sign() print("Transaction created") wt.info() if args.push: wt.send() if wt.pushed: print("Transaction pushed to network. Transaction ID: %s" % wt.hash) else: print("Error creating transaction: %s" % wt.error) else: print( "\nTransaction created but not send yet. Transaction dictionary for export: " ) print_transaction(wt) clw_exit() if args.sweep: if args.fee: clw_exit("Fee option not allowed with --sweep") offline = True print("Sweep wallet. Send all funds to %s" % args.sweep) if args.push: offline = False wt = wlt.sweep(args.sweep, offline=offline, network=args.network, fee_per_kb=args.fee_per_kb) if not wt: clw_exit( "Error occurred when sweeping wallet: %s. Are UTXO's available and updated?" % wt) wt.info() if args.push: if wt.pushed: print("Transaction pushed to network. Transaction ID: %s" % wt.hash) elif not wt: print("Cannot sweep wallet, are UTXO's updated and available?") else: print("Error sweeping wallet: %s" % wt.error) else: print( "\nTransaction created but not send yet. Transaction dictionary for export: " ) print_transaction(wt) clw_exit() # print("Updating wallet") if args.network == 'bitcoinlib_test': wlt.utxos_update() print("Wallet info for %s" % wlt.name) wlt.info()
def test_mnemonic_small_entropy_bug(self): for i in range(50): m = Mnemonic().generate(256) e = Mnemonic().to_entropy(m) self.assertEqual(len(e), 32)
if __name__ == '__main__': print("Command Line Wallet for BitcoinLib\n") # --- Parse commandline arguments --- args = parse_args() # network_obj = Network(args.network) databasefile = DEFAULT_DATABASE if args.database: databasefile = DEFAULT_DATABASEDIR + args.database if args.generate_key: passphrase = get_passphrase() passphrase = ' '.join(passphrase) seed = binascii.hexlify(Mnemonic().to_seed(passphrase)) hdkey = HDKey().from_seed(seed, network=args.network) print( "Private master key, to create multisig wallet on this machine: %s" % hdkey.wif()) print( "Public account key, to share with other cosigner multisig wallets: %s" % hdkey.account_multisig_key().wif_public()) print("Network: %s" % hdkey.network.network_name) clw_exit() # List wallets, then exit if args.list_wallets: print("Bitcoinlib wallets:") for w in wallets_list(databasefile=databasefile): if 'parent_id' in w and w['parent_id']:
('Offline PC', 'bip32', 'password'), ('Online PC', 'bip32', ''), ('Paper backup', 'single', ''), ] print( "We will generate 3 private keys, to sign and send a transaction 2 keys are needed:" "\n- With 1 private key a wallet on this Offline PC is created" "\n- Use private key 2 to create a wallet on the Online PC" "\n- Store key 3 on a Paper in a safe in case one of the PC's is not available anymore" ) key_lists = {} w_id = 0 for cosigner in cosigners: print("\n") words = Mnemonic().generate(KEY_STRENGTH) password = '' if cosigner[2] == 'password': password = input("Please give password for cosigner '%s': " % cosigner[0]) seed = Mnemonic().to_seed(words, password) hdkey = HDKey.from_seed(seed, network=NETWORK, key_type=cosigner[1], witness_type=WITNESS_TYPE) if cosigner[1] == 'bip32': public_account = hdkey.public_master_multisig( witness_type=WITNESS_TYPE) else: public_account = hdkey print(
def test_mnemonic_to_seed_invalid_checksum(self): phrase = "runway truly foil future recall scatter garage over floor clutch shy boat" self.assertRaisesRegexp(ValueError, "Invalid checksum 0110 for entropy", Mnemonic().to_seed, phrase)
input = raw_input except NameError: pass WALLET_NAME = "Multisig_3of5" wlt = HDWallet(WALLET_NAME) # If you want to sign on an offline PC, export utxo dictionary to offline PC # utxos = {...} # wlt.utxos_update(utxos=utxos) wlt.utxos_update() wlt.info() # Paste your raw transaction here or enter in default input raw_tx = '' if not raw_tx: raw_tx = input("Paste raw transaction hex: ") passphrase = input("Enter passphrase: ") password = input("Enter password []:") seed = Mnemonic().to_seed(passphrase, password) hdkey = HDKey.from_seed(seed, network=wlt.network.network_name) t = wlt.transaction_import_raw(raw_tx) t.sign(hdkey) print("Raw signed transaction: ") print(t.raw_hex())
del litecoin_wallet print("\n=== Create Dash wallet ===") dash_wallet = HDWallet.create(databasefile=test_database, name='Dash Wallet', network='dash') dash_wallet.new_key() dash_wallet.info(detail=3) del dash_wallet print("\n=== Create Litecoin testnet Wallet from Mnemonic Passphrase ===") words = 'blind frequent camera goddess pottery repair skull year mistake wrist lonely mix' # Or use generate method: # words = Mnemonic('english').generate() print("Generated Passphrase: %s" % words) seed = Mnemonic().to_seed(words) hdkey = HDKey().from_seed(seed, network='litecoin_testnet') wallet = HDWallet.create(name='Mnemonic Wallet', network='litecoin_testnet', keys=hdkey.wif(), databasefile=test_database) wallet.new_key("Input", 0) wallet.utxos_update() wallet.info(detail=3) print( "\n=== Test import Litecoin key in Bitcoin wallet (should give error) ===") w = HDWallet.create(name='Wallet Error', databasefile=test_database) try: w.import_key(key='T43gB4F6k1Ly3YWbMuddq13xLb56hevUDP3RthKArr7FPHjQiXpp', network='litecoin')
def test_mnemonic_generate(self): phrase = Mnemonic(language='dutch').generate() self.assertEqual(len(phrase.split(' ')), 12) self.assertEqual(Mnemonic.detect_language(phrase), 'dutch')
# EXAMPLES - Mnemonic class examples # # © 2017 - 2019 January - 1200 Web Development <http://1200wd.com/> # from bitcoinlib.encoding import to_hexstring from bitcoinlib.keys import HDKey from bitcoinlib.mnemonic import Mnemonic # # Mnemonic examples # # Convert hexadecimal to Mnemonic and back again to hex print("\nConvert hexadecimal to Mnemonic and back again to hex") pk = '7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f' words = Mnemonic().to_mnemonic(pk) print("Hex %s" % pk) print("Checksum bin %s" % Mnemonic().checksum(pk)) print("Mnemonic %s" % words) print("Seed for HD Key %s" % to_hexstring(Mnemonic().to_seed(words, 'test'))) print("Back to Entropy %s" % to_hexstring(Mnemonic().to_entropy(words))) # Generate a random Mnemonic HD Key print("\nGenerate a random Mnemonic HD Key") entsize = 128 words = Mnemonic('english').generate(entsize) print("Your Mnemonic is %s" % words) print(" (An avarage of %d tries is needed to brute-force this password)" % ((2**entsize) // 2)) seed = Mnemonic().to_seed(words)
# w.info() # t = w.sweep(w.new_key().address, 10000, fee=1000) # raw_tx = t.raw_hex() # t.info() # Raw partially signed transaction transaction raw_tx = '' if not raw_tx: raw_tx = input("Paste raw transaction hex: ") t = Transaction.import_raw(raw_tx) key_str = input("Enter private key or mnemonic passphrase: ") if len(key_str.split(" ")) < 2: hdkey = HDKey(key_str) else: password = input("Enter password []:") seed = Mnemonic().to_seed(key_str, password) hdkey = HDKey.from_seed(seed, network=network) t.sign(hdkey) t.info() print("Raw signed transaction: ") print(t.raw_hex()) if input("Try to send transaction [y/n] ") in ['y', 'Y']: srv = Service(network=network) res = srv.sendrawtransaction(t.raw()) pprint(res)