def test_import(): m = mock_provider m.reset_mocks() keys = HDKey.from_path(master, "m/44'/0'/0'") m.hd_master_key = master m.set_num_used_accounts(0) m.set_txn_side_effect_for_hd_discovery() wallet = Two1Wallet.import_from_mnemonic( data_provider=m, mnemonic=master_seed, passphrase=passphrase, account_type="BIP44BitcoinMainnet") assert wallet._root_keys[1].to_b58check() == keys[1].to_b58check() assert wallet._root_keys[2].to_b58check() == keys[2].to_b58check() assert wallet._accounts[0].key.to_b58check() == keys[3].to_b58check() assert len(wallet._accounts) == 1 # Now test where the first account has transactions m.reset_mocks() m.set_num_used_accounts(2) # Set this to 2 so that we get the side effects m.set_num_used_addresses(account_index=0, n=10, change=0) m.set_num_used_addresses(account_index=0, n=20, change=1) m.set_txn_side_effect_for_hd_discovery() wallet = Two1Wallet.import_from_mnemonic( data_provider=m, mnemonic=master_seed, passphrase=passphrase, account_type="BIP44BitcoinMainnet") assert len(wallet._accounts) == 1 assert wallet._accounts[0].has_txns() # Test where multiple accounts have transactions m.reset_mocks() m.set_num_used_accounts(5) for i in range(4): m.set_num_used_addresses(account_index=i, n=1, change=0) m.set_num_used_addresses(account_index=i, n=2, change=1) m.set_txn_side_effect_for_hd_discovery() wallet = Two1Wallet.import_from_mnemonic( data_provider=m, mnemonic=master_seed, passphrase=passphrase, account_type="BIP44BitcoinMainnet") assert len(wallet._accounts) == 4 for i in range(4): assert wallet._accounts[i].has_txns()
def test_import(): m = mock_provider m.reset_mocks() keys = HDKey.from_path(master, "m/44'/0'/0'") m.hd_master_key = master m.set_num_used_accounts(0) m.set_txn_side_effect_for_hd_discovery() wallet = Two1Wallet.import_from_mnemonic(data_provider=m, mnemonic=master_seed, passphrase=passphrase, account_type="BIP44BitcoinMainnet") assert wallet._root_keys[1].to_b58check() == keys[1].to_b58check() assert wallet._root_keys[2].to_b58check() == keys[2].to_b58check() assert wallet._accounts[0].key.to_b58check() == keys[3].to_b58check() assert len(wallet._accounts) == 1 # Now test where the first account has transactions m.reset_mocks() m.set_num_used_accounts(2) # Set this to 2 so that we get the side effects m.set_num_used_addresses(account_index=0, n=10, change=0) m.set_num_used_addresses(account_index=0, n=20, change=1) m.set_txn_side_effect_for_hd_discovery() wallet = Two1Wallet.import_from_mnemonic(data_provider=m, mnemonic=master_seed, passphrase=passphrase, account_type="BIP44BitcoinMainnet") assert len(wallet._accounts) == 1 assert wallet._accounts[0].has_txns() # Test where multiple accounts have transactions m.reset_mocks() m.set_num_used_accounts(5) for i in range(4): m.set_num_used_addresses(account_index=i, n=1, change=0) m.set_num_used_addresses(account_index=i, n=2, change=1) m.set_txn_side_effect_for_hd_discovery() wallet = Two1Wallet.import_from_mnemonic(data_provider=m, mnemonic=master_seed, passphrase=passphrase, account_type="BIP44BitcoinMainnet") assert len(wallet._accounts) == 4 for i in range(4): assert wallet._accounts[i].has_txns()
def login_21(): """ Restore wallet to disk and log in to 21. """ mnemonic = os.environ["TWO1_WALLET_MNEMONIC"] provider = TwentyOneProvider() wallet = Two1Wallet.import_from_mnemonic(provider, mnemonic) if not os.path.exists(os.path.dirname(Two1Wallet.DEFAULT_WALLET_PATH)): os.makedirs(os.path.dirname(Two1Wallet.DEFAULT_WALLET_PATH)) wallet.to_file(Two1Wallet.DEFAULT_WALLET_PATH) # login config = Config() machine_auth = machine_auth_wallet.MachineAuthWallet(wallet) username = os.environ["TWO1_USERNAME"] password = os.environ["TWO1_PASSWORD"] rest_client = _rest_client.TwentyOneRestClient(two1.TWO1_HOST, machine_auth, username) machine_auth_pubkey_b64 = base64.b64encode( machine_auth.public_key.compressed_bytes).decode() payout_address = machine_auth.wallet.current_address rest_client.login(payout_address=payout_address, password=password) config.set("username", username) config.set("mining_auth_pubkey", machine_auth_pubkey_b64) config.save()
def login_21(): """ Restore wallet to disk and log in to 21. """ mnemonic = os.environ["TWO1_WALLET_MNEMONIC"] provider = TwentyOneProvider() wallet = Two1Wallet.import_from_mnemonic(provider, mnemonic) if not os.path.exists(os.path.dirname(Two1Wallet.DEFAULT_WALLET_PATH)): os.makedirs(os.path.dirname(Two1Wallet.DEFAULT_WALLET_PATH)) wallet.to_file(Two1Wallet.DEFAULT_WALLET_PATH) # login config = Config() machine_auth = machine_auth_wallet.MachineAuthWallet(wallet) username = os.environ["TWO1_USERNAME"] password = os.environ["TWO1_PASSWORD"] rest_client = _rest_client.TwentyOneRestClient(two1.TWO1_HOST, machine_auth, username) machine_auth_pubkey_b64 = base64.b64encode(machine_auth.public_key.compressed_bytes).decode() payout_address = machine_auth.wallet.current_address rest_client.login(payout_address=payout_address, password=password) config.set("username", username) config.set("mining_auth_pubkey", machine_auth_pubkey_b64) config.save()
def restore(ctx): """ Restore a wallet from a mnemonic \b If you accidently deleted your wallet file or the file became corrupted, use this command to restore your wallet. You must have your 12 word phrase (mnemonic) that was displayed when you created your wallet. """ # Stop daemon if it's running. d = None try: d = get_daemonizer() except OSError as e: pass if d: try: d.stop() except exceptions.DaemonizerError as e: click.echo("ERROR: Couldn't stop daemon: %s" % e) ctx.exit(code=4) # Check to see if the current wallet path exists if os.path.exists(ctx.obj['wallet_path']): if click.confirm("Wallet file already exists and may have a balance. Do you want to delete it?"): os.remove(ctx.obj['wallet_path']) else: click.echo("Not continuing.") ctx.exit(code=4) # Ask for mnemonic mnemonic = click.prompt("Please enter the wallet's 12 word mnemonic").strip() # Sanity check the mnemonic def check_mnemonic(mnemonic): try: return Mnemonic(language='english').check(mnemonic) except ConfigurationError: return False if not check_mnemonic(mnemonic): click.echo("ERROR: Invalid mnemonic.") ctx.exit(code=5) # Try creating the wallet click.echo("\nRestoring...") wallet = Two1Wallet.import_from_mnemonic( data_provider=ctx.obj['data_provider'], mnemonic=mnemonic, ) wallet.to_file(ctx.obj['wallet_path']) if Two1Wallet.check_wallet_file(ctx.obj['wallet_path']): click.echo("Wallet successfully restored. Run '21 login' to connect this wallet to your 21 account.") else: click.echo("Wallet not restored.") ctx.exit(code=6)
def restore(ctx): """ Restore a wallet from a mnemonic \b If you accidently deleted your wallet file or the file became corrupted, use this command to restore your wallet. You must have your 12 word phrase (mnemonic) that was displayed when you created your wallet. """ # Check to see if the current wallet path exists if os.path.exists(ctx.obj['wallet_path']): if click.confirm( "Wallet file already exists and may have a balance. Do you want to delete it?" ): os.remove(ctx.obj['wallet_path']) else: click.echo("Not continuing.") ctx.exit(code=4) # Ask for mnemonic mnemonic = click.prompt( "Please enter the wallet's 12 word mnemonic").strip() # Sanity check the mnemonic def check_mnemonic(mnemonic): try: return Mnemonic(language='english').check(mnemonic) except ConfigurationError: return False if not check_mnemonic(mnemonic): click.echo("ERROR: Invalid mnemonic.") ctx.exit(code=5) # Try creating the wallet click.echo("\nRestoring...") wallet = Two1Wallet.import_from_mnemonic( data_provider=ctx.obj['data_provider'], mnemonic=mnemonic, ) wallet.to_file(ctx.obj['wallet_path']) if Two1Wallet.check_wallet_file(ctx.obj['wallet_path']): click.echo( "Wallet successfully restored. Run '21 login' to connect this wallet to your 21 account." ) else: click.echo("Wallet not restored.") ctx.exit(code=6)
import random from two1.blockchain.twentyone_provider import TwentyOneProvider from two1.wallet.two1_wallet import Two1Wallet mnemonics = ['absent paddle capable spell bag reflect rally there swear swallow cook rubber', 'stairs art mirror spoon clap talk exclude tuna absurd exact grape relief', 'poem into dune liar already rain swear thunder spread kangaroo monster wise', 'business lyrics news image duty stone clerk salad harvest shallow follow evoke', 'another student leg ladder jeans hello cluster type network wrist before sense'] cp = TwentyOneProvider() # Create wallet objects wallets = [Two1Wallet.import_from_mnemonic(data_provider=cp, mnemonic=m, account_type='BIP32') for m in mnemonics] max_balance_index = -1 max_balance = 0 for i, w in enumerate(wallets): balance = w.balances if balance['confirmed'] > max_balance: max_balance = balance['confirmed'] max_balance_index = i print("\nWallet %d:" % i) print("----------") print("Num accounts: %d" % (len(w._accounts)))