ThomasV 8 years ago
parent
commit
4273c607b7
  1. 12
      lib/bitcoin.py
  2. 12
      lib/transaction.py

12
lib/bitcoin.py

@ -332,6 +332,14 @@ def public_key_to_p2wpkh(public_key):
def script_to_p2wsh(script): def script_to_p2wsh(script):
return hash_to_segwit_addr(sha256(bfh(script))) return hash_to_segwit_addr(sha256(bfh(script)))
def p2wpkh_nested_script(pubkey):
pubkey = safe_parse_pubkey(pubkey)
pkh = bh2u(hash_160(bfh(pubkey)))
return '00' + push_script(pkh)
def p2wsh_nested_script(witness_script):
wsh = bh2u(sha256(bfh(witness_script)))
return '00' + push_script(wsh)
def pubkey_to_address(txin_type, pubkey): def pubkey_to_address(txin_type, pubkey):
if txin_type == 'p2pkh': if txin_type == 'p2pkh':
@ -339,7 +347,7 @@ def pubkey_to_address(txin_type, pubkey):
elif txin_type == 'p2wpkh': elif txin_type == 'p2wpkh':
return hash_to_segwit_addr(hash_160(bfh(pubkey))) return hash_to_segwit_addr(hash_160(bfh(pubkey)))
elif txin_type == 'p2wpkh-p2sh': elif txin_type == 'p2wpkh-p2sh':
scriptSig = transaction.p2wpkh_nested_script(pubkey) scriptSig = p2wpkh_nested_script(pubkey)
return hash160_to_p2sh(hash_160(bfh(scriptSig))) return hash160_to_p2sh(hash_160(bfh(scriptSig)))
else: else:
raise NotImplementedError(txin_type) raise NotImplementedError(txin_type)
@ -350,7 +358,7 @@ def redeem_script_to_address(txin_type, redeem_script):
elif txin_type == 'p2wsh': elif txin_type == 'p2wsh':
return script_to_p2wsh(redeem_script) return script_to_p2wsh(redeem_script)
elif txin_type == 'p2wsh-p2sh': elif txin_type == 'p2wsh-p2sh':
scriptSig = transaction.p2wsh_nested_script(redeem_script) scriptSig = p2wsh_nested_script(redeem_script)
return hash160_to_p2sh(hash_160(bfh(scriptSig))) return hash160_to_p2sh(hash_160(bfh(scriptSig)))
else: else:
raise NotImplementedError(txin_type) raise NotImplementedError(txin_type)

12
lib/transaction.py

@ -487,14 +487,6 @@ def deserialize(raw):
# pay & redeem scripts # pay & redeem scripts
def p2wpkh_nested_script(pubkey):
pubkey = safe_parse_pubkey(pubkey)
pkh = bh2u(hash_160(bfh(pubkey)))
return '00' + push_script(pkh)
def p2wsh_nested_script(witness_script):
wsh = bh2u(sha256(bfh(witness_script)))
return '00' + push_script(wsh)
def multisig_script(public_keys, m): def multisig_script(public_keys, m):
@ -677,11 +669,11 @@ class Transaction:
elif _type in ['p2wpkh', 'p2wsh']: elif _type in ['p2wpkh', 'p2wsh']:
return '' return ''
elif _type == 'p2wpkh-p2sh': elif _type == 'p2wpkh-p2sh':
scriptSig = p2wpkh_nested_script(pubkeys[0]) scriptSig = bitcoin.p2wpkh_nested_script(pubkeys[0])
return push_script(scriptSig) return push_script(scriptSig)
elif _type == 'p2wsh-p2sh': elif _type == 'p2wsh-p2sh':
witness_script = self.get_preimage_script(txin) witness_script = self.get_preimage_script(txin)
scriptSig = p2wsh_nested_script(witness_script) scriptSig = bitcoin.p2wsh_nested_script(witness_script)
return push_script(scriptSig) return push_script(scriptSig)
elif _type == 'address': elif _type == 'address':
script += push_script(pubkeys[0]) script += push_script(pubkeys[0])

Loading…
Cancel
Save