Exemplo n.º 1
0
    def __init__(self, mnemonic, child):
        master_key = HDPrivateKey.master_key_from_mnemonic(mnemonic)
        root_keys = HDKey.from_path(master_key,"m/44'/60'/0'")
        acct_priv_key = root_keys[-1]
        keys = HDKey.from_path(acct_priv_key,'{change}/{index}'.format(change=0, index=child))

        self.address = ethereumAddressFromBytes(bytes(keys[-1].public_key._key))
        self.private_key = keys[-1]._key.to_hex()
Exemplo n.º 2
0
    def hd_master_key(self, k):
        self._hd_master_key = k
        self._acct_keys = {}

        keys = HDKey.from_path(self._hd_master_key,
                               self.account_type.account_derivation_prefix)
        for i in range(self.max_accounts):
            acct_key = HDPrivateKey.from_parent(keys[-1], 0x80000000 | i)
            payout_key = HDPrivateKey.from_parent(acct_key, 0)
            change_key = HDPrivateKey.from_parent(acct_key, 1)

            payout_addresses = [HDPublicKey.from_parent(payout_key.public_key, i).address()
                                for i in range(self.max_address)]
            change_addresses = [HDPublicKey.from_parent(change_key.public_key, i).address()
                                for i in range(self.max_address)]

            self._acct_keys[i] = {'acct_key': acct_key,
                                  'payout_key': payout_key,
                                  'change_key': change_key,
                                  'payout_addresses': payout_addresses,
                                  'change_addresses': change_addresses}

            self._num_used_addresses[i][0] = 0
            self._num_used_addresses[i][1] = 0

        self._setup_balances()
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()
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
    def create(cls, mnemonic=None):
        """
        Create Minter wallet
            @param mnemonic|string: Mnemonic phrase
            @return: dict 
        """

        # Create mnemonic phrase if None
        if not mnemonic:
            _mnemonic = Mnemonic(language='english')
            mnemonic = _mnemonic.generate(cls.BIP44_ENTROPY_BITS)

        if len(mnemonic.split(' ')) != 12:
            raise Exception('Mnemonic phrase should have 12 words.')

        # Mnemonic to seed (bytes)
        seed = Mnemonic.to_seed(mnemonic, '')

        # Generate master key from master seed
        I = hmac.new(cls.MASTER_SEED, seed, hashlib.sha512).hexdigest()
        IL = I[:64]
        IR = I[64:]

        master_key = HDPrivateKey(key=int.from_bytes(binascii.unhexlify(IL),
                                                     'big'),
                                  chain_code=binascii.unhexlify(IR),
                                  index=0,
                                  depth=0)

        # Get child keys from master key by path
        keys = HDKey.from_path(master_key, cls.BIP44_SEED_ADDRESS_PATH)

        # Get private key
        private_key = binascii.hexlify(bytes(keys[-1]._key))

        # Get public key
        public_key = cls.get_public_from_private(private_key)

        # Get address
        address = cls.get_address_from_public_key(public_key)

        return {
            'address': address,
            'private_key': private_key.decode(),
            'mnemonic': mnemonic,
            'seed': binascii.hexlify(seed).decode()
        }
from two1.bitcoin.crypto import HDKey, HDPrivateKey
from two1.blockchain.mock_provider import MockProvider
from two1.wallet.account_types import account_types
from two1.wallet.cache_manager import CacheManager
from two1.wallet.hd_account import HDAccount


master_key_mnemonic = 'cage minimum apology region aspect wrist demise gravity another bulb tail invest'
master_key_passphrase = "test"

account_type = account_types['BIP44BitcoinMainnet']

master_key = HDPrivateKey.master_key_from_mnemonic(mnemonic=master_key_mnemonic,
                                                   passphrase=master_key_passphrase)
acct0_key = HDKey.from_path(master_key, account_type.account_derivation_prefix + "/0'")[-1]

mock_provider = MockProvider(account_type, master_key)


def test_init():
    with pytest.raises(TypeError):
        HDAccount(master_key_passphrase, "default", 0, mock_provider,
                  CacheManager())


'''
The MockProvider is designed in a strange way that explains how the parameters
are chosen for test_all: the get_transactions side effects are set so that for
each DISCOVERY_INCREMENT block of addresses, a single transaction is returned
worth a fixed amount of satoshis (10k).
Exemplo n.º 7
0
import pytest

from two1.bitcoin.crypto import HDKey, HDPrivateKey
from two1.blockchain.mock_provider import MockProvider
from two1.wallet.account_types import account_types
from two1.wallet.cache_manager import CacheManager
from two1.wallet.hd_account import HDAccount

master_key_mnemonic = 'cage minimum apology region aspect wrist demise gravity another bulb tail invest'
master_key_passphrase = "test"

account_type = account_types['BIP44BitcoinMainnet']

master_key = HDPrivateKey.master_key_from_mnemonic(
    mnemonic=master_key_mnemonic, passphrase=master_key_passphrase)
acct0_key = HDKey.from_path(master_key,
                            account_type.account_derivation_prefix + "/0'")[-1]

mock_provider = MockProvider(account_type, master_key)


def test_init():
    with pytest.raises(TypeError):
        HDAccount(master_key_passphrase, "default", 0, mock_provider,
                  CacheManager())


'''
The MockProvider is designed in a strange way that explains how the parameters
are chosen for test_all: the get_transactions side effects are set so that for
each DISCOVERY_INCREMENT block of addresses, a single transaction is returned
worth a fixed amount of satoshis (10k).