def test_bip32_addresses_p2sh_p2wpkh(setup_wallet, mixdepth, internal, index,
                                     address, wif):
    """
    Test with a random but fixed entropy
    """
    jm_single().config.set('BLOCKCHAIN', 'network', 'testnet')

    entropy = unhexlify(
        '2e0339ba89b4a1272cdf78b27ee62669ee01992a59e836e2807051be128ca817')
    storage = VolatileStorage()
    SegwitLegacyWallet.initialize(storage,
                                  get_network(),
                                  entropy=entropy,
                                  max_mixdepth=3)
    wallet = SegwitLegacyWallet(storage)

    # wallet needs to know about all intermediate keys
    for i in range(index + 1):
        wallet.get_new_script(mixdepth, internal)

    assert wif == wallet.get_wif(mixdepth, internal, index)
    assert address == wallet.get_addr(mixdepth, internal, index)
Esempio n. 2
0
def test_wrong_wallet_cls(setup_wallet):
    storage = VolatileStorage()
    SegwitLegacyWallet.initialize(storage, get_network())
    wallet = SegwitLegacyWallet(storage)

    wallet.save()
    data = storage.file_data

    del wallet
    del storage

    storage = VolatileStorage(data=data)

    with pytest.raises(Exception):
        LegacyWallet(storage)
def main():
    parser = OptionParser(
        usage='usage: %prog [options] wallet_file_name [password]',
        description='Create a wallet with the given wallet name and password.')
    add_base_options(parser)
    parser.add_option(
        '--recovery-seed-file',
        dest='seed_file',
        default=None,
        help=
        ('File containing a mnemonic recovery phrase. If provided, the wallet '
         'is recovered from this seed instead of being newly generated.'))
    (options, args) = parser.parse_args()
    wallet_name = args[0]
    if options.wallet_password_stdin:
        password = wallet_utils.read_password_stdin()
    else:
        assert len(
            args
        ) > 1, "must provide password via stdin (see --help), or as second argument."
        password = args[1].encode("utf-8")
    seed = options.seed_file and Path(options.seed_file).read_text().rstrip()

    load_program_config(config_path=options.datadir)
    wallet_root_path = os.path.join(jm_single().datadir, "wallets")
    wallet_path = os.path.join(wallet_root_path, wallet_name)
    if jm_single().config.get("POLICY", "native") == "true":
        walletclass = SegwitWalletFidelityBonds
    else:
        # Fidelity Bonds are not available for segwit legacy wallets
        walletclass = SegwitLegacyWallet
    entropy = seed and SegwitLegacyWallet.entropy_from_mnemonic(seed)
    wallet = create_wallet(wallet_path,
                           password,
                           wallet_utils.DEFAULT_MIXDEPTH,
                           walletclass,
                           entropy=entropy)
    jmprint("recovery_seed:{}".format(wallet.get_mnemonic_words()[0]),
            "important")
    wallet.close()
Esempio n. 4
0
def test_wallet_save(setup_wallet):
    wallet = get_populated_wallet()

    script = wallet.get_external_script(1)

    wallet.save()
    storage = wallet._storage
    data = storage.file_data

    del wallet
    del storage

    storage = VolatileStorage(data=data)
    wallet = SegwitLegacyWallet(storage)

    assert wallet.get_next_unused_index(0, True) == 3
    assert wallet.get_next_unused_index(0, False) == 0
    assert wallet.get_next_unused_index(1, True) == 0
    assert wallet.get_next_unused_index(1, False) == 1
    assert wallet.is_known_script(script)
Esempio n. 5
0
def test_signing_imported(setup_wallet, wif, type_check):
    jm_single().config.set('BLOCKCHAIN', 'network', 'testnet')
    storage = VolatileStorage()
    SegwitLegacyWallet.initialize(storage, get_network())
    wallet = SegwitLegacyWallet(storage)

    MIXDEPTH = 0
    path = wallet.import_private_key(MIXDEPTH, wif)
    utxo = fund_wallet_addr(wallet, wallet.get_address_from_path(path))
    # The dummy output is constructed as an unspendable p2sh:
    tx = btc.mktx([utxo], [{
        "address":
        str(
            btc.CCoinAddress.from_scriptPubKey(
                btc.CScript(b"\x00").to_p2sh_scriptPubKey())),
        "value":
        10**8 - 9000
    }])
    script = wallet.get_script_from_path(path)
    success, msg = wallet.sign_tx(tx, {0: (script, 10**8)})
    assert success, msg
    type_check(tx)
    txout = jm_single().bc_interface.pushtx(tx.serialize())
    assert txout
Esempio n. 6
0
def test_imported_key_removed(setup_wallet):
    wif = 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM'
    key_type = cryptoengine.TYPE_P2SH_P2WPKH

    storage = VolatileStorage()
    SegwitLegacyWallet.initialize(storage, get_network())
    wallet = SegwitLegacyWallet(storage)

    path = wallet.import_private_key(1, wif, key_type)
    script = wallet.get_script_path(path)
    assert wallet.is_known_script(script)

    wallet.remove_imported_key(path=path)
    assert not wallet.is_known_script(script)

    with pytest.raises(WalletError):
        wallet.get_script_path(path)
Esempio n. 7
0
def test_initialize_twice(setup_wallet):
    wallet = get_populated_wallet(num=0)
    storage = wallet._storage
    with pytest.raises(WalletError):
        SegwitLegacyWallet.initialize(storage, get_network())
Esempio n. 8
0
def test_import_key(setup_wallet):
    jm_single().config.set('BLOCKCHAIN', 'network', 'testnet')
    storage = VolatileStorage()
    SegwitLegacyWallet.initialize(storage, get_network())
    wallet = SegwitLegacyWallet(storage)

    wallet.import_private_key(
        0, 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM',
        cryptoengine.TYPE_P2SH_P2WPKH)
    wallet.import_private_key(
        1, 'cVqtSSoVxFyPqTRGfeESi31uCYfgTF4tGWRtGeVs84fzybiX5TPk',
        cryptoengine.TYPE_P2PKH)

    with pytest.raises(WalletError):
        wallet.import_private_key(
            1, 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM',
            cryptoengine.TYPE_P2SH_P2WPKH)

    # test persist imported keys
    wallet.save()
    data = storage.file_data

    del wallet
    del storage

    storage = VolatileStorage(data=data)
    wallet = SegwitLegacyWallet(storage)

    imported_paths_md0 = list(wallet.yield_imported_paths(0))
    imported_paths_md1 = list(wallet.yield_imported_paths(1))
    assert len(imported_paths_md0) == 1
    assert len(imported_paths_md1) == 1

    # verify imported addresses
    assert wallet.get_addr_path(
        imported_paths_md0[0]) == '2MzY5yyonUY7zpHspg7jB7WQs1uJxKafQe4'
    assert wallet.get_addr_path(
        imported_paths_md1[0]) == 'mpCX9EbdXpcrKMtjEe1fqFhvzctkfzMYTX'

    # test remove key
    wallet.remove_imported_key(path=imported_paths_md0[0])
    assert not list(wallet.yield_imported_paths(0))

    assert wallet.get_details(imported_paths_md1[0]) == (1, 'imported', 0)