示例#1
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()
示例#2
0
    def get_public_key(self, change, n=-1):
        """ Returns a public key in the chain

        Args:
            change (bool): If True, returns an address for change purposes,
               otherwise returns an address for payment.
            n (int): index of address in chain. If n == -1, a new key
               is created with index = self.last_[change|payout]_index + 1

        Returns:
            HDPublicKey: A public key in this account's chain.
        """
        # We only use public key derivation per BIP44
        c = int(change)
        k = self._chain_pub_keys[c]
        if n < 0:
            self.last_indices[c] += 1
            i = self.last_indices[c]
            pub_key = HDPublicKey.from_parent(k, i)
            addr = pub_key.address(True, self.testnet)
            self._cache_manager.insert_address(self.index, change, i, addr)
        else:
            pub_key = HDPublicKey.from_parent(k, n)

        return pub_key
示例#3
0
    def get_public_key(self, change, n=-1):
        """ Returns a public key in the chain

        Args:
            change (bool): If True, returns an address for change purposes,
               otherwise returns an address for payment.
            n (int): index of address in chain. If n == -1, a new key
               is created with index = self.last_[change|payout]_index + 1

        Returns:
            HDPublicKey: A public key in this account's chain.
        """
        # We only use public key derivation per BIP44
        c = int(change)
        k = self._chain_pub_keys[c]
        if n < 0:
            self.last_indices[c] += 1
            i = self.last_indices[c]
            pub_key = HDPublicKey.from_parent(k, i)
            addr = pub_key.address(True, self.testnet)
            self._cache_manager.insert_address(self.index, change, i, addr)
        else:
            pub_key = HDPublicKey.from_parent(k, n)

        return pub_key
示例#4
0
    def __init__(self,
                 hd_key,
                 name,
                 index,
                 data_provider,
                 cache_manager,
                 testnet=False,
                 last_state=None,
                 skip_discovery=False):
        # Take in either public or private key for this account as we
        # can derive everything from it.
        if not isinstance(hd_key, HDKey):
            raise TypeError("hd_key must be a HDKey object")

        self.key = hd_key
        self.name = name
        self.index = index
        self.data_provider = data_provider
        self.testnet = testnet

        self.last_indices = [-1, -1]
        self._cache_manager = cache_manager
        self._last_update = 0
        self._last_full_update = 0

        if last_state is not None and isinstance(last_state, dict):
            if "last_payout_index" in last_state:
                self.last_indices[
                    self.PAYOUT_CHAIN] = last_state["last_payout_index"]
            if "last_change_index" in last_state:
                self.last_indices[
                    self.CHANGE_CHAIN] = last_state["last_change_index"]

        # Check to see that the address cache has up to last_indices
        for change in [self.PAYOUT_CHAIN, self.CHANGE_CHAIN]:
            k = self._cache_manager.get_chain_indices(self.index, change)
            for i in range(self.last_indices[change] + 1):
                if i not in k or k[i] != i:
                    self.last_indices[change] = -1
                    break

        self._chain_priv_keys = [None, None]
        self._chain_pub_keys = [None, None]

        for change in [0, 1]:
            if isinstance(self.key, HDPrivateKey):
                self._chain_priv_keys[change] = HDPrivateKey.from_parent(
                    self.key, change)
                self._chain_pub_keys[change] = self._chain_priv_keys[
                    change].public_key
            else:
                self._chain_pub_keys[change] = HDPublicKey.from_parent(
                    self.key, change)

        if not skip_discovery:
            self._sync_txns(check_all=True)
            self._update_balance()
示例#5
0
    def __init__(self, hd_key, name, index, data_provider, cache_manager,
                 testnet=False, last_state=None, skip_discovery=False):
        # Take in either public or private key for this account as we
        # can derive everything from it.
        if not isinstance(hd_key, HDKey):
            raise TypeError("hd_key must be a HDKey object")

        self.key = hd_key
        self.name = name
        self.index = index
        self.data_provider = data_provider
        self.testnet = testnet

        self.last_indices = [-1, -1]
        self._cache_manager = cache_manager
        self._last_update = 0
        self._last_full_update = 0

        if last_state is not None and isinstance(last_state, dict):
            if "last_payout_index" in last_state:
                self.last_indices[self.PAYOUT_CHAIN] = last_state["last_payout_index"]
            if "last_change_index" in last_state:
                self.last_indices[self.CHANGE_CHAIN] = last_state["last_change_index"]

        # Check to see that the address cache has up to last_indices
        for change in [self.PAYOUT_CHAIN, self.CHANGE_CHAIN]:
            k = self._cache_manager.get_chain_indices(self.index, change)
            for i in range(self.last_indices[change] + 1):
                if i not in k or k[i] != i:
                    self.last_indices[change] = -1
                    break

        self._chain_priv_keys = [None, None]
        self._chain_pub_keys = [None, None]

        for change in [0, 1]:
            if isinstance(self.key, HDPrivateKey):
                self._chain_priv_keys[change] = HDPrivateKey.from_parent(self.key, change)
                self._chain_pub_keys[change] = self._chain_priv_keys[change].public_key
            else:
                self._chain_pub_keys[change] = HDPublicKey.from_parent(self.key, change)

        if not skip_discovery:
            self._sync_txns(check_all=True)
            self._update_balance()
示例#6
0
import pytest
from two1.bitcoin.crypto import HDPublicKey
from two1.bitcoin.txn import Transaction
from two1.blockchain.twentyone_provider import TwentyOneProvider
from two1.blockchain.exceptions import DataProviderError

acct_pub_key = HDPublicKey.from_b58check(
    "xpub68YdQASJ3w2RYS7XNT8HkLVjWqKeMD5uAxJR2vqXAh65j7izto1cVSwCNm7awAjjeYExqneCAZzt5xGETXZz1EXa9HntM5HzwdQ9551UErA"
)

twentyone_provider = TwentyOneProvider()
stage_twentyone_provider = TwentyOneProvider("http://blockchain.21-stage.co")


@pytest.mark.parametrize("provider, testnet", [
    (twentyone_provider, False),
    (twentyone_provider, True),
    (stage_twentyone_provider, False),
])
def test_get_transactions(provider, testnet):
    cp = provider
    cp.testnet = testnet
    if testnet:
        address_list = ["myTpteaBCwuHsDsoBQfrN4YjKEBpmoLBii"]
        data = cp.get_transactions(address_list)
        exp = (1, 2)
    else:
        address_list = ["1K4nPxBMy6sv7jssTvDLJWk1ADHBZEoUVb"]
        data = cp.get_transactions(address_list)
        exp = (1, 9)
示例#7
0
import pytest
from two1.bitcoin.crypto import HDPublicKey
from two1.bitcoin.txn import Transaction
from two1.blockchain.twentyone_provider import TwentyOneProvider
from two1.blockchain.exceptions import DataProviderError


acct_pub_key = HDPublicKey.from_b58check(
    "xpub68YdQASJ3w2RYS7XNT8HkLVjWqKeMD5uAxJR2vqXAh65j7izto1cVSwCNm7awAjjeYExqneCAZzt5xGETXZz1EXa9HntM5HzwdQ9551UErA")

twentyone_provider = TwentyOneProvider()
stage_twentyone_provider = TwentyOneProvider("http://blockchain.21-stage.co")


@pytest.mark.parametrize("provider, testnet",
                         [
                             (twentyone_provider, False),
                             (twentyone_provider, True),
                             (stage_twentyone_provider, False),
                         ])
def test_get_transactions(provider, testnet):
    cp = provider
    cp.testnet = testnet
    if testnet:
        address_list = ["myTpteaBCwuHsDsoBQfrN4YjKEBpmoLBii"]
        data = cp.get_transactions(address_list)
        exp = (1, 2)
    else:
        address_list = ["1K4nPxBMy6sv7jssTvDLJWk1ADHBZEoUVb"]
        data = cp.get_transactions(address_list)
        exp = (1, 9)
示例#8
0
wallet = None
wallet = wallet or Two1Wallet.import_from_mnemonic(
    mnemonic=wallet_data['master_seed'])
provider = TwentyOneProvider()


def execute(wallet_method):
    methodToCall = getattr(wallet, wallet_method)
    result = json.dumps({wallet_method: methodToCall()})
    print(result)


# Loop through methods
del sys.argv[0]
if sys.argv[0] == 'sign':
    pubkey = HDPublicKey.from_hex(sys.argv[1])
    server_privkey = wallet.get_private_for_public(pubkey)
    tx = Transaction.from_hex(sys.argv[2])
    script = Script.from_hex(sys.argv[3])

    for i, inp in enumerate(tx.inputs):
        tx.sign_input(i, Transaction.SIG_HASH_ALL, server_privkey, script)

    tx_id = provider.broadcast_transaction(tx.to_hex())

    print(json.dumps({'tx_id': tx_id, 'hex': tx.to_hex()}))
else:
    for arg in sys.argv:
        if arg == 'USD':
            rate = Price(wallet.balance())._get_usd_rate()
            print(rate)