def script_obj_from_address(address, netcodes=None): netcode, key_type, data = netcode_and_type_for_text(address) if key_type == 'pay_to_script': return ScriptPayToScript(hash160=data) if key_type == 'address': return ScriptPayToAddress(hash160=data) raise ValueError("bad text")
def script_obj_from_address(address, netcodes=None): netcode, key_type, data = netcode_and_type_for_text(address, netcodes) if key_type == 'pay_to_script': return ScriptPayToScript(hash160=data) if key_type == 'address': return ScriptPayToAddress(hash160=data) if key_type == 'address_wit': return ScriptPayToAddressWit(version=data[:1], hash160=data[2:]) if key_type == 'pay_to_script_wit': return ScriptPayToScriptWit(version=data[:1], hash256=data[2:]) raise ValueError("bad text")
def address_from_spend(self, spend): script = None """:type: ScriptType""" try: script = ScriptPayToScript.from_script(spend.script) except Exception: script = ScriptPayToAddress.from_script(spend.script) # explicitly call info() because pycoin script.address(netcode) disregards the netcode addr = script.info(self.netcode)['address'] return addr
def sign_tx_in(self, hash160_lookup, tx_in_idx, tx_out_script, hash_type=SIGHASH_ALL, **kwargs): tx_in = self.txs_in[tx_in_idx] is_p2h = (len(tx_out_script) == 23 and byte_to_int(tx_out_script[0]) == opcodes.OP_HASH160 and byte_to_int(tx_out_script[-1]) == opcodes.OP_EQUAL) script_to_hash = tx_out_script if is_p2h: hash160 = ScriptPayToScript.from_script(tx_out_script).hash160 p2sh_lookup = kwargs.get("p2sh_lookup") if p2sh_lookup is None: raise ValueError("p2sh_lookup not set") if hash160 not in p2sh_lookup: raise ValueError("hash160=%s not found in p2sh_lookup" % b2h(hash160)) script_to_hash = p2sh_lookup[hash160] signature_for_hash_type_f = lambda hash_type: self.signature_hash( tx_out_script, tx_in_idx, hash_type) if tx_in.verify(tx_out_script, signature_for_hash_type_f, lock_time=kwargs.get('lock_time')): return sign_value = self.signature_hash(script_to_hash, tx_in_idx, hash_type=hash_type) the_script = script_obj_from_script(tx_out_script) solution = the_script.solve(hash160_lookup=hash160_lookup, sign_value=sign_value, signature_type=hash_type, existing_script=self.txs_in[tx_in_idx].script, **kwargs) tx_in.script = solution
def sign_tx_in(self, hash160_lookup, tx_in_idx, tx_out_script, hash_type=SIGHASH_ALL, **kwargs): tx_in = self.txs_in[tx_in_idx] is_p2h = (len(tx_out_script) == 23 and byte_to_int(tx_out_script[0]) == opcodes.OP_HASH160 and byte_to_int(tx_out_script[-1]) == opcodes.OP_EQUAL) script_to_hash = tx_out_script if is_p2h: hash160 = ScriptPayToScript.from_script(tx_out_script).hash160 p2sh_lookup = kwargs.get("p2sh_lookup") if p2sh_lookup is None: raise ValueError("p2sh_lookup not set") if hash160 not in p2sh_lookup: raise ValueError("hash160=%s not found in p2sh_lookup" % b2h(hash160)) script_to_hash = p2sh_lookup[hash160] signature_for_hash_type_f = lambda hash_type: self.signature_hash(tx_out_script, tx_in_idx, hash_type) if tx_in.verify(tx_out_script, signature_for_hash_type_f): return sign_value = self.signature_hash(script_to_hash, tx_in_idx, hash_type=hash_type) the_script = script_obj_from_script(tx_out_script) solution = the_script.solve(hash160_lookup=hash160_lookup, sign_value=sign_value, signature_type=hash_type,existing_script=self.txs_in[tx_in_idx].script, **kwargs) tx_in.script = solution