Exemplo n.º 1
1
def test_crypto():
    pts = ((0x50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352,
            0x2cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6),
           (0xa83b8de893467d3a88d959c0eb4032d9ce3bf80f175d4d9e75892a3ebb8ab7e5,
            0x370f723328c24b7a97fe34063ba68f253fb08f8645d7c8b9a4ff98e3c29e7f0d),
           (0xf680556678e25084a82fa39e1b1dfd0944f7e69fddaa4e03ce934bd6b291dca0,
            0x52c10b721d34447e173721fb0151c68de1106badb089fb661523b8302a9097f5),
           (0x241febb8e23cbd77d664a18f66ad6240aaec6ecdc813b088d5b901b2e285131f,
            0x513378d9ff94f8d3d6c420bd13981df8cd50fd0fbd0cb5afabb3e66f2750026d))

    for pt in pts:
        b = bytes([(pt[1] & 0x1) + 0x2]) + pt[0].to_bytes(32, 'big')
        b_full = bytes([0x04]) + pt[0].to_bytes(32, 'big') + pt[1].to_bytes(32, 'big')
        pk = PublicKey.from_bytes(b)
        assert pk.point.y == pt[1]
        assert b == pk.compressed_bytes
        assert b_full == bytes(pk)

        assert bytes(PublicKey.from_hex(pk.to_hex())) == b_full

    for i in range(10):
        pk = PrivateKey.from_random()
        assert PrivateKey.from_hex(pk.to_hex()).key == pk.key

    hd_priv = HDPrivateKey.master_key_from_entropy()[0]
    hd_priv2 = HDKey.from_hex(hd_priv.to_hex())
    hd_pub = hd_priv.public_key
    hd_pub2 = HDKey.from_hex(hd_pub.to_hex())

    assert isinstance(hd_priv2, HDPrivateKey)
    assert hd_priv2._key.key == hd_priv._key.key
    assert hd_priv2.chain_code == hd_priv.chain_code

    assert isinstance(hd_pub2, HDPublicKey)
    assert hd_pub2._key.point.x == hd_pub._key.point.x
    assert hd_pub2._key.point.y == hd_pub._key.point.y
    assert hd_pub2.chain_code == hd_pub.chain_code
Exemplo n.º 2
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.º 3
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.º 5
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.º 6
0
def test_crypto():
    pts = (
        (0x50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352,
         0x2cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6),
        (0xa83b8de893467d3a88d959c0eb4032d9ce3bf80f175d4d9e75892a3ebb8ab7e5,
         0x370f723328c24b7a97fe34063ba68f253fb08f8645d7c8b9a4ff98e3c29e7f0d),
        (0xf680556678e25084a82fa39e1b1dfd0944f7e69fddaa4e03ce934bd6b291dca0,
         0x52c10b721d34447e173721fb0151c68de1106badb089fb661523b8302a9097f5),
        (0x241febb8e23cbd77d664a18f66ad6240aaec6ecdc813b088d5b901b2e285131f,
         0x513378d9ff94f8d3d6c420bd13981df8cd50fd0fbd0cb5afabb3e66f2750026d))

    for pt in pts:
        b = bytes([(pt[1] & 0x1) + 0x2]) + pt[0].to_bytes(32, 'big')
        b_full = bytes([0x04]) + pt[0].to_bytes(32, 'big') + pt[1].to_bytes(
            32, 'big')
        pk = PublicKey.from_bytes(b)
        assert pk.point.y == pt[1]
        assert b == pk.compressed_bytes
        assert b_full == bytes(pk)

        assert bytes(PublicKey.from_hex(pk.to_hex())) == b_full

    for i in range(10):
        pk = PrivateKey.from_random()
        assert PrivateKey.from_hex(pk.to_hex()).key == pk.key

    hd_priv = HDPrivateKey.master_key_from_entropy()[0]
    hd_priv2 = HDKey.from_hex(hd_priv.to_hex())
    hd_pub = hd_priv.public_key
    hd_pub2 = HDKey.from_hex(hd_pub.to_hex())

    assert isinstance(hd_priv2, HDPrivateKey)
    assert hd_priv2._key.key == hd_priv._key.key
    assert hd_priv2.chain_code == hd_priv.chain_code

    assert isinstance(hd_pub2, HDPublicKey)
    assert hd_pub2._key.point.x == hd_pub._key.point.x
    assert hd_pub2._key.point.y == hd_pub._key.point.y
    assert hd_pub2.chain_code == hd_pub.chain_code
Exemplo n.º 7
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.º 9
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).