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 __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()
def get_private_key(self, change, n): """ Returns a private key in the chain for use in signing messages or transactions. Args: change (bool): If True, returns an address for change purposes, otherwise returns an address for payment. n (int): index of address in chain. Returns: HDPrivateKey: A private key in this account's chain. """ # We only use public key derivation per BIP44 k = self._chain_priv_keys[change] if k is None: raise ValueError("No private key provided for account.") return HDPrivateKey.from_parent(k, n)
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()