Esempio n. 1
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
Esempio n. 2
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()