def from_pubkey(self, pubkey): '''Returns a P2PKH address from a public key. The public key can be serialized as a bytes or a hex string.''' if isinstance(pubkey, PublicKey): return self.from_pubkey_hash(hash160(pubkey.to_ser())) elif isinstance(pubkey, bytes): return self.from_pubkey_hash(hash160(pubkey)) elif isinstance(pubkey, str): return self.from_pubkey_hash(hash160(bytes.fromhex(pubkey))) else: raise AddressError("wrong type of public key")
def from_pubkey(self, pubkey, coin): assert isinstance(pubkey, PublicKey) self.coin = coin if coin == "BCH": return self( hash160( pubkey.to_ser() ), coin) elif coin == "BTC": return self( hash160( pubkey.to_ser() ), coin) elif coin == "ETH": if pubkey.is_compressed(): pubkey.uncompress() return self( keccak256( pubkey.to_ser()[1:] )[-20:], coin ) else: AddressError("wrong coin")
def p2sh_unlocking_script(addr, redeem_script, pubkeys, signatures, var_args=None): assert isinstance(addr, Address) assert addr.kind == Constants.CASH_P2SH assert isinstance(pubkeys[0], PublicKey) assert isinstance(signatures[0], (bytes, bytearray)) assert isinstance(redeem_script, (bytes, bytearray)) assert hash160(redeem_script) == addr.h # TODO: parse script if redeem_script[-1] == OP_CHECKMULTISIG: dummy = var_args[0] # Multisig output to unlock assert redeem_script == multisig_locking_script( pubkeys, len(signatures)) return (multisig_unlocking_script(signatures, dummy) + push_data(redeem_script)) elif (len(redeem_script) in [75, 76, 77, 78, 79, 80]) & ( redeem_script[-38] == OP_CHECKLOCKTIMEVERIFY): # Expiring tip choice = 'refund' print("expiring tip: {} !".format(choice)) return (expiring_tip_unlocking_script('refund', signatures[0]) + push_data(redeem_script)) else: raise ScriptError("cannot parse script")
def account_from_seed(secret): seed = seed_from_human(secret) public_key = crypto.get_public_key(seed) account = crypto.hash160(public_key) account = account_to_human(account) return account
def generate_keypair(): """ Generate an address and a secret key """ seed = os.urandom(32) public_key = crypto.get_public_key(seed) account = crypto.hash160(public_key) account = address.account_to_human(account) seed = address.seed_to_human(seed) return account, seed
def get_address(self): """Get the associated Bitcoin address.""" pubkey = self.get_public_key() hash = hash160(sha256(pubkey)) return b58c_encode(hash)
def from_script(self, script): '''Initializes from a redeem script.''' return self.from_script_hash(hash160(script))