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
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 __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_for_public(public_key): """ RPC method to get the private key for the given public_key, if it is a part of this wallet. Args: public_key (str): Base58Check encoded serialization of the public key. Returns: str: A Base58Check encoded serialization of the private key object or None. """ w = wallet['obj'] try: pub_key = HDPublicKey.from_b58check(public_key) except ValueError: pub_key = PublicKey.from_base64(public_key) priv_key = w.get_private_key(pub_key.address(testnet=w._testnet)) return priv_key.to_b58check() if priv_key is not None else None
import base64 import io import uuid # change the receiving_key in config.py in the root folder. from config import receiving_key, SATOSHIS_PER_MINUTE # logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.ERROR) auth_app = Flask(__name__, static_folder="static") auth_app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/wifiportal21.db" db = SQLAlchemy(auth_app) K = HDPublicKey.from_b58check(receiving_key) blockchain_provider = TwentyOneProvider() cache = CacheManager() receiving_account = HDAccount( hd_key=K, name="hotspot receiving", index=0, data_provider=blockchain_provider, cache_manager=cache ) SATOSHIS_PER_MBTC = 100 * 10 ** 3 SATOSHIS_PER_BTC = 100 * 10 ** 6 STATUS_NONE = 0 STATUS_PAYREQ = 1 STATUS_PAID = 2 class Guest(db.Model):
import base64 import io import uuid # change the receiving_key in config.py in the root folder. from config import receiving_key, SATOSHIS_PER_MINUTE # logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.ERROR) auth_app = Flask(__name__, static_folder='static') auth_app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/wifiportal21.db' db = SQLAlchemy(auth_app) K = HDPublicKey.from_b58check(receiving_key) blockchain_provider = TwentyOneProvider() cache = CacheManager() receiving_account = HDAccount(hd_key=K, name="hotspot receiving", index=0, data_provider=blockchain_provider, cache_manager=cache) SATOSHIS_PER_MBTC = 100 * 10**3 SATOSHIS_PER_BTC = 100 * 10**6 STATUS_NONE = 0 STATUS_PAYREQ = 1 STATUS_PAID = 2