serialize value of incomplete inputs in the witness field. fix p2wsh-in-p2sh deserialization
This commit is contained in:
parent
fcea2a065d
commit
c12c1685d7
|
@ -305,11 +305,8 @@ def parse_scriptSig(d, _bytes):
|
||||||
if match_decoded(decoded, match):
|
if match_decoded(decoded, match):
|
||||||
item = decoded[0][1]
|
item = decoded[0][1]
|
||||||
if item[0] == 0:
|
if item[0] == 0:
|
||||||
redeemScript = bh2u(item)
|
|
||||||
d['address'] = bitcoin.hash160_to_p2sh(bitcoin.hash_160(item))
|
d['address'] = bitcoin.hash160_to_p2sh(bitcoin.hash_160(item))
|
||||||
d['type'] = 'p2wpkh-p2sh'
|
d['type'] = 'p2wpkh-p2sh' if len(item) == 21 else 'p2wsh-p2sh'
|
||||||
d['redeemScript'] = redeemScript
|
|
||||||
d['num_sig'] = 1
|
|
||||||
else:
|
else:
|
||||||
# payto_pubkey
|
# payto_pubkey
|
||||||
d['type'] = 'p2pk'
|
d['type'] = 'p2pk'
|
||||||
|
@ -421,20 +418,19 @@ def parse_input(vds):
|
||||||
d['type'] = 'unknown'
|
d['type'] = 'unknown'
|
||||||
d['num_sig'] = 0
|
d['num_sig'] = 0
|
||||||
if scriptSig:
|
if scriptSig:
|
||||||
if scriptSig[0] == 255:
|
d['scriptSig'] = bh2u(scriptSig)
|
||||||
d['value'] = struct.unpack_from('<Q', scriptSig, 1)[0]
|
parse_scriptSig(d, scriptSig)
|
||||||
scriptSig = scriptSig[9:]
|
else:
|
||||||
if scriptSig:
|
d['scriptSig'] = ''
|
||||||
d['scriptSig'] = bh2u(scriptSig)
|
|
||||||
parse_scriptSig(d, scriptSig)
|
|
||||||
else:
|
|
||||||
d['scriptSig'] = ''
|
|
||||||
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def parse_witness(vds, txin):
|
def parse_witness(vds, txin):
|
||||||
n = vds.read_compact_size()
|
n = vds.read_compact_size()
|
||||||
|
if n == 0:
|
||||||
|
txin['value'] = vds.read_uint64()
|
||||||
|
n = vds.read_compact_size()
|
||||||
w = list(bh2u(vds.read_bytes(vds.read_compact_size())) for i in range(n))
|
w = list(bh2u(vds.read_bytes(vds.read_compact_size())) for i in range(n))
|
||||||
if n > 2:
|
if n > 2:
|
||||||
txin['num_sig'] = n - 2
|
txin['num_sig'] = n - 2
|
||||||
|
@ -497,9 +493,7 @@ def p2wpkh_nested_script(pubkey):
|
||||||
return '00' + push_script(pkh)
|
return '00' + push_script(pkh)
|
||||||
|
|
||||||
def p2wsh_nested_script(witness_script):
|
def p2wsh_nested_script(witness_script):
|
||||||
print('wit', witness_script)
|
|
||||||
wsh = bh2u(sha256(bfh(witness_script)))
|
wsh = bh2u(sha256(bfh(witness_script)))
|
||||||
print(len(wsh)//2)
|
|
||||||
return '00' + push_script(wsh)
|
return '00' + push_script(wsh)
|
||||||
|
|
||||||
|
|
||||||
|
@ -650,13 +644,15 @@ class Transaction:
|
||||||
def serialize_witness(self, txin):
|
def serialize_witness(self, txin):
|
||||||
pubkeys, sig_list = self.get_siglist(txin)
|
pubkeys, sig_list = self.get_siglist(txin)
|
||||||
if txin['type'] in ['p2wpkh', 'p2wpkh-p2sh']:
|
if txin['type'] in ['p2wpkh', 'p2wpkh-p2sh']:
|
||||||
n = 2
|
witness = var_int(2) + push_script(sig_list[0]) + push_script(pubkeys[0])
|
||||||
return var_int(n) + push_script(sig_list[0]) + push_script(pubkeys[0])
|
|
||||||
elif txin['type'] in ['p2wsh', 'p2wsh-p2sh']:
|
elif txin['type'] in ['p2wsh', 'p2wsh-p2sh']:
|
||||||
n = len(sig_list) + 2
|
n = len(sig_list) + 2
|
||||||
# fixme: witness script must be decided by wallet
|
|
||||||
witness_script = multisig_script(pubkeys, txin['num_sig'])
|
witness_script = multisig_script(pubkeys, txin['num_sig'])
|
||||||
return var_int(n) + '00' + ''.join(var_int(len(x)//2) + x for x in sig_list) + var_int(len(witness_script)//2) + witness_script
|
witness = var_int(n) + '00' + ''.join(var_int(len(x)//2) + x for x in sig_list) + var_int(len(witness_script)//2) + witness_script
|
||||||
|
else:
|
||||||
|
raise BaseException('wrong txin type')
|
||||||
|
value_field = '' if self.is_txin_complete(txin) else var_int(0) + int_to_hex(txin['value'], 8)
|
||||||
|
return value_field + witness
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_segwit_input(self, txin):
|
def is_segwit_input(self, txin):
|
||||||
|
@ -669,8 +665,6 @@ class Transaction:
|
||||||
return txin['scriptSig']
|
return txin['scriptSig']
|
||||||
pubkeys, sig_list = self.get_siglist(txin, estimate_size)
|
pubkeys, sig_list = self.get_siglist(txin, estimate_size)
|
||||||
script = ''.join(push_script(x) for x in sig_list)
|
script = ''.join(push_script(x) for x in sig_list)
|
||||||
if self.is_segwit_input(txin):
|
|
||||||
segwit_value = '' if self.is_txin_complete(txin) or estimate_size else 'ff' + int_to_hex(txin['value'], 8)
|
|
||||||
if _type == 'p2pk':
|
if _type == 'p2pk':
|
||||||
pass
|
pass
|
||||||
elif _type == 'p2sh':
|
elif _type == 'p2sh':
|
||||||
|
@ -681,14 +675,14 @@ class Transaction:
|
||||||
elif _type == 'p2pkh':
|
elif _type == 'p2pkh':
|
||||||
script += push_script(pubkeys[0])
|
script += push_script(pubkeys[0])
|
||||||
elif _type in ['p2wpkh', 'p2wsh']:
|
elif _type in ['p2wpkh', 'p2wsh']:
|
||||||
return segwit_value
|
return ''
|
||||||
elif _type == 'p2wpkh-p2sh':
|
elif _type == 'p2wpkh-p2sh':
|
||||||
scriptSig = p2wpkh_nested_script(pubkeys[0])
|
scriptSig = p2wpkh_nested_script(pubkeys[0])
|
||||||
return segwit_value + 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 = p2wsh_nested_script(witness_script)
|
||||||
return segwit_value + push_script(scriptSig)
|
return push_script(scriptSig)
|
||||||
elif _type == 'address':
|
elif _type == 'address':
|
||||||
script += push_script(pubkeys[0])
|
script += push_script(pubkeys[0])
|
||||||
elif _type == 'unknown':
|
elif _type == 'unknown':
|
||||||
|
|
Loading…
Reference in New Issue