Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 test_mnemonic_generate_eror(self):
self.assertRaisesRegexp(ValueError, 'Strenght should be divisible by 32', Mnemonic().generate, 11)
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)
def test_wallet_multisig_sign_with_external_single_key(self):
self.db_remove()
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, multisig=True).public_master(),
HDKey(network=network),
hdkey.public()
]
wallet = HDWallet.create('Multisig-2-of-3-example', key_list, sigs_required=2, network=network,
db_uri=self.DATABASE_URI)
wallet.new_key()
wallet.utxos_update()
wt = wallet.send_to('21A6yyUPRL9hZZo1Rw4qP5G6h9idVVLUncE', 10000000)
self.assertFalse(wt.verify())
wt.sign(hdkey)
self.assertTrue(wt.verify())
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: "
" [ ... ]")
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))
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
print("\n=== Create Dash wallet ===")
dash_wallet = HDWallet.create(
db_uri=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(), db_uri=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',
db_uri=test_database)
try:
w.import_key(key='T43gB4F6k1Ly3YWbMuddq13xLb56hevUDP3RthKArr7FPHjQiXpp', network='litecoin')
except WalletError as e:
print("Import litecoin key in bitcoin wallet gives an EXPECTED error: %s" % e)
if not wallet_exists(WALLET_NAME):
cosign_names = ['This PC', 'Offline PC', 'Paper backup']
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 PC is created"
"\n- Use private key 2 to create a wallet on an Offline PC"
"\n- Store key 3 on a Paper in a safe in case one of the PC's is not available anymore"
)
key_list = []
for cosigner in cosign_names:
words = Mnemonic().generate(KEY_STRENGHT)
print("\nKey for cosigner '%s' generated. Please store both passphrase and password carefully!" % cosigner)
password = ''
if cosigner != 'Paper backup':
password = input("Enter password for this key (or enter for no password): ")
seed = Mnemonic().to_seed(words, password)
hdkey = HDKey.from_seed(seed, network=NETWORK)
public_account_wif = hdkey.account_multisig_key().wif_public()
print("Passphrase: %s" % words)
print("Share this public key below with other cosigner")
print("Public key: %s" % public_account_wif)
key_list.append(hdkey)
thispc_keylist = [
HDKey(key_list[0].wif(), network=NETWORK),
HDKey(key_list[1].account_multisig_key().wif_public(), network=NETWORK),
HDKey(key_list[2].wif_public(), network=NETWORK, key_type='single')
]
thispc_wallet = HDWallet.create_multisig(WALLET_NAME, thispc_keylist, 2, sort_keys=True,
network=NETWORK, databasefile=test_database)
thispc_wallet.new_key()
input = raw_input
except NameError:
pass
#
# Create Wallets
#
# First recreate database to avoid already exist errors
test_databasefile = BCL_DATABASE_DIR + 'bitcoinlib.test.sqlite'
test_database = 'sqlite:///' + test_databasefile
if os.path.isfile(test_databasefile):
os.remove(test_databasefile)
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', keys=passphrase, password=password, network='bitcoinlib_test',
db_uri=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()
print("\n- Successfully send, updated wallet info:")
wlt.info()
# Multisig 3-of-5 wallet with Mnemonic passphrase keys
#
# © 2017 November - 1200 Web Development
#
from bitcoinlib.mnemonic import Mnemonic
from bitcoinlib.keys import HDKey
NETWORK = 'testnet'
KEY_STRENGHT = 128
words = Mnemonic().generate(KEY_STRENGHT)
print("A Mnemonic passphrase has been generated. Please write down and store carefully: \n%s" % words)
password = input("\nEnter a password if you would like to protect passphrase []: ")
seed = Mnemonic().to_seed(words, password)
hdkey = HDKey.from_seed(seed, network=NETWORK)
public_account_wif = hdkey.public_master_multisig()
print("\nPrivate key: \n%s" % hdkey.wif_private())
# print("Public key: \n%s" % hdkey.wif_public())
print("Public account key to share with other cosigners for a multisig BIP45 wallet: \n%s" % public_account_wif.wif())