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_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, 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)
def key(network, key): network_name = network_code_translation[network] try: k = HDKey(key, network=network_name) except Exception as e: flash(_('Invalid key: %s' % e), category='error') return redirect(url_for('main.index')) if k.is_private: flash(_( 'Never post your private key online. Only use this for test keys or in an offline environment!' ), category='error') return render_template('explorer/key.html', title=_('Key'), subtitle=k.wif(), key=k, network=network)
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) hdk = HDKey().from_seed(seed) print("Seed for HD Key %s" % to_hexstring(seed)) print("HD Key WIF is %s" % hdk.wif()) # Generate a key from a Mnemonic sentence print("\nGenerate a key from a Mnemonic sentence") words = "type fossil omit food supply enlist move perfect direct grape clean diamond" print("Your Mnemonic is %s" % words) seed = Mnemonic().to_seed(words) hdk = HDKey().from_seed(seed) print("Seed for HD Key %s" % to_hexstring(seed)) print("HD Key WIF is %s" % hdk.wif()) # Let's talk Spanish print("\nGenerate a key from a Spanish Mnemonic sentence") words = "laguna afirmar talón resto peldaño deuda guerra dorado catorce avance oasis barniz" print("Your Mnemonic is %s" % words) seed = Mnemonic().to_seed(words)
print("Private key %s" % k.wif()) print("Encrypted pk %s " % k.bip38_encrypt('TestingOneTwoThree')) print("Is Compressed %s\n" % k.compressed) print("\n=== Import and Decrypt BIP38 Key ===") k = Key('6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg', passphrase='TestingOneTwoThree') print("Private key %s" % k.wif()) print("Is Compressed %s\n" % k.compressed) # # Hierarchical Deterministic Key Class and Child Key Derivation Examples # print("\n=== Generate random HD Key on testnet ===") hdk = HDKey(network='testnet') print("Random BIP32 HD Key on testnet %s" % hdk.wif()) print("\n=== Import HD Key from seed ===") k = HDKey.from_seed('000102030405060708090a0b0c0d0e0f') print("HD Key WIF for seed 000102030405060708090a0b0c0d0e0f: %s" % k.wif()) print("Key type is : %s" % k.key_type) print("\n=== Generate random Litecoin key ===") lk = HDKey(network='litecoin') lk.info() print("\n=== Generate random Dash key ===") lk = HDKey(network='dash') lk.info() print("\n=== Import simple private key as HDKey ===")
"\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) wallet = BulkPaperWallet.create(name=wallet_name, network=network, key=hdkey.wif()) wallet.new_key("Input") wallet.new_account("Outputs", account_id=OUTPUT_ACCOUNT_ID) if args.recover_wallet_passphrase: print("Wallet recovered, now updating keys and balances...") stuff_updated = True while stuff_updated: for kn in range(0, 10): wallet.new_key(account_id=OUTPUT_ACCOUNT_ID) wallet.new_key_change(account_id=OUTPUT_ACCOUNT_ID) stuff_updated = wallet.utxos_update() wallet.info() sys.exit() # --- Create array with outputs ---
def main(): print("Command Line Wallet for BitcoinLib\n") # --- Parse commandline arguments --- args = parse_args() databasefile = DEFAULT_DATABASE if args.database: databasefile = DEFAULT_DATABASEDIR + 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.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']: 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_remove) 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.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) 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: res = wt.send() if res: 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 and 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()
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) wallet = BulkPaperWallet.create(name=wallet_name, network=network, key=hdkey.wif()) wallet.new_key("Input") wallet.new_account("Outputs", account_id=OUTPUT_ACCOUNT_ID) if args.recover_wallet_passphrase: print("Wallet recovered, now updating keys and balances...") stuff_updated = True while stuff_updated: for kn in range(0, 10): wallet.new_key(account_id=OUTPUT_ACCOUNT_ID) wallet.new_key_change(account_id=OUTPUT_ACCOUNT_ID) stuff_updated = wallet.utxos_update() wallet.info() sys.exit() # --- Create array with outputs ---
# --- 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']: continue print("[%d] %s (%s) %s" % (w['id'], w['name'], w['network'], w['owner'])) clw_exit()