def address_handlers(cls): return ScriptPubKey.PayToHandlers( address=cls.P2PKH_address_from_hash160, script_hash=cls.P2SH_address_from_hash160, pubkey=cls.P2PKH_address_from_pubkey, unspendable=lambda: None, strange=lambda script: None, )
def pay_to_address_script(cls, address): '''Return a pubkey script that pays to a pubkey hash. Pass the address (either P2PKH or P2SH) in base58 form. ''' raw = Base58.decode_check(address) # Require version byte plus hash160. verbyte = -1 if len(raw) == 21: verbyte, hash_bytes = raw[0], raw[1:] if verbyte == cls.P2PKH_VERBYTE: return ScriptPubKey.P2PKH_script(hash_bytes) if verbyte == cls.P2SH_VERBYTE: return ScriptPubKey.P2SH_script(hash_bytes) raise CoinError('invalid address: {}'.format(address))
def pay_to_address_script(cls, address): '''Return a pubkey script that pays to a pubkey hash. Pass the address (either P2PKH or P2SH) in base58 form. ''' raw = cls.DECODE_CHECK(address) # Require version byte(s) plus hash160. verbyte = -1 verlen = len(raw) - 20 if verlen > 0: verbyte, hash_bytes = raw[:verlen], raw[verlen:] if verbyte == cls.P2PKH_VERBYTE: return ScriptPubKey.P2PKH_script(hash_bytes) if verbyte in cls.P2SH_VERBYTES: return ScriptPubKey.P2SH_script(hash_bytes) raise CoinError('invalid address: {}'.format(address))
def pay_to_address_script(cls, address): '''Return a pubkey script that pays to a pubkey hash. Pass the address (either P2PKH or P2SH) in base58 form. ''' #比特币地址格式为base58(version(1byte)+hash(20bytes)+checksum(4bytes)) #这里decode_check会对比特币地址解码后,校验最后四字节,然后去掉四字节,返回21字节的地址数据 raw = Base58.decode_check(address) # Require version byte(s) plus hash160. verbyte = -1 verlen = len(raw) - 20 if verlen > 0: verbyte, hash_bytes = raw[:verlen], raw[verlen:] if verbyte == cls.P2PKH_VERBYTE: return ScriptPubKey.P2PKH_script(hash_bytes) if verbyte in cls.P2SH_VERBYTES: return ScriptPubKey.P2SH_script(hash_bytes) raise CoinError('invalid address: {}'.format(address))
def hashX_from_script(cls, script): '''Returns a hashX from a script, or None if the script is provably unspendable so the output can be dropped. ''' if script and script[0] == OpCodes.OP_RETURN: return None # BTN: make p2pk and p2pkh the same hashX if len(script) == 35 and script[0] == 0x21 and script[-1] == OpCodes.OP_CHECKSIG: pubkey = script[1:-1] script = ScriptPubKey.P2PKH_script(hash160(pubkey)) return sha256(script).digest()[:HASHX_LEN]
def address_handlers(cls): return ScriptPubKey.PayToHandlers( address=cls.P2PKH_address_from_hash160, script_hash=cls.P2SH_address_from_hash160, pubkey=cls.P2PKH_address_from_pubkey, contract_create=cls.Contract_cal_address_from_data, contract_create_native=cls.Contract_cal_address_from_data, contract_call=cls.Contract_call, contract_deposit=cls.Contract_deposit_to_contract, contract_upgrade=cls.Contract_upgrade, contract_spent=cls.Contract_spent, unspendable=lambda: None, strange=lambda script: None, )
def pay_to_pubkey_script(cls, pubkey): '''Return a pubkey script that pays to a pubkey. Pass the raw pubkey bytes (length 33 or 65). ''' return ScriptPubKey.P2PK_script(pubkey)
def pay_to_multisig_script(cls, m, pubkeys): '''Return a P2SH script for an M of N multisig transaction.''' return ScriptPubKey.multisig_script(m, pubkeys)
def address_from_script(cls, script): '''Given a pk_script, return the adddress it pays to, or None.''' return ScriptPubKey.pay_to(cls.address_handlers, script)
def hashX_from_script(cls, script): '''Returns a hashX from a script.''' script = ScriptPubKey.hashX_script(script) if script is None: return None return sha256(script).digest()[:cls.HASHX_LEN]