fix is_complete in tx.sign()
This commit is contained in:
@@ -591,24 +591,32 @@ class Transaction:
|
|||||||
def hash(self):
|
def hash(self):
|
||||||
return Hash(self.raw.decode('hex') )[::-1].encode('hex')
|
return Hash(self.raw.decode('hex') )[::-1].encode('hex')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def sign(self, private_keys):
|
def sign(self, private_keys):
|
||||||
import deserialize
|
import deserialize
|
||||||
|
|
||||||
is_complete = True
|
is_complete = True
|
||||||
|
|
||||||
for i in range(len(self.inputs)):
|
for i, txin in enumerate(self.inputs):
|
||||||
txin = self.inputs[i]
|
|
||||||
tx_for_sig = self.serialize( self.inputs, self.outputs, for_sig = i )
|
tx_for_sig = self.serialize( self.inputs, self.outputs, for_sig = i )
|
||||||
|
|
||||||
txin_pk = private_keys.get( txin.get('address') )
|
txin_pk = private_keys.get( txin.get('address') )
|
||||||
if not txin_pk:
|
|
||||||
continue
|
|
||||||
|
|
||||||
redeem_script = txin.get('redeemScript')
|
redeem_script = txin.get('redeemScript')
|
||||||
|
|
||||||
if redeem_script:
|
if redeem_script:
|
||||||
# 1 parse the redeem script
|
|
||||||
|
# parse the redeem script
|
||||||
num, redeem_pubkeys = deserialize.parse_redeemScript(redeem_script)
|
num, redeem_pubkeys = deserialize.parse_redeemScript(redeem_script)
|
||||||
txin["pubkeys"] = redeem_pubkeys
|
txin["pubkeys"] = redeem_pubkeys
|
||||||
|
|
||||||
|
# list of already existing signatures
|
||||||
|
signatures = txin.get("signatures",[])
|
||||||
|
|
||||||
|
# continue if this txin is complete
|
||||||
|
if len(signatures == num):
|
||||||
|
continue
|
||||||
|
|
||||||
# build list of public/private keys
|
# build list of public/private keys
|
||||||
keypairs = {}
|
keypairs = {}
|
||||||
for sec in txin_pk:
|
for sec in txin_pk:
|
||||||
@@ -617,10 +625,6 @@ class Transaction:
|
|||||||
pubkey = GetPubKey(pkey.pubkey, compressed)
|
pubkey = GetPubKey(pkey.pubkey, compressed)
|
||||||
keypairs[ pubkey.encode('hex') ] = sec
|
keypairs[ pubkey.encode('hex') ] = sec
|
||||||
|
|
||||||
# list of already existing signatures
|
|
||||||
signatures = txin.get("signatures",[])
|
|
||||||
print_error("signatures",signatures)
|
|
||||||
|
|
||||||
for pubkey in redeem_pubkeys:
|
for pubkey in redeem_pubkeys:
|
||||||
|
|
||||||
# check if we have a key corresponding to the redeem script
|
# check if we have a key corresponding to the redeem script
|
||||||
@@ -639,10 +643,18 @@ class Transaction:
|
|||||||
# for p2sh, pubkeysig is a tuple (may be incomplete)
|
# for p2sh, pubkeysig is a tuple (may be incomplete)
|
||||||
txin["signatures"] = signatures
|
txin["signatures"] = signatures
|
||||||
print_error("signatures", signatures)
|
print_error("signatures", signatures)
|
||||||
is_complete = is_complete and (len(signatures) == num)
|
is_complete = is_complete and len(signatures == num)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
sec = private_keys[txin['address']][0]
|
|
||||||
|
if txin.get("pubkeysig"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not txin_pk:
|
||||||
|
is_complete = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
sec = txin_pk[0]
|
||||||
compressed = is_compressed(sec)
|
compressed = is_compressed(sec)
|
||||||
pkey = regenerate_key(sec)
|
pkey = regenerate_key(sec)
|
||||||
secexp = pkey.secret
|
secexp = pkey.secret
|
||||||
@@ -652,9 +664,7 @@ class Transaction:
|
|||||||
pubkey = GetPubKey(pkey.pubkey, compressed)
|
pubkey = GetPubKey(pkey.pubkey, compressed)
|
||||||
sig = private_key.sign_digest( Hash( tx_for_sig.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
|
sig = private_key.sign_digest( Hash( tx_for_sig.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
|
||||||
assert public_key.verify_digest( sig, Hash( tx_for_sig.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
|
assert public_key.verify_digest( sig, Hash( tx_for_sig.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
|
||||||
|
|
||||||
txin["pubkeysig"] = [(pubkey, sig)]
|
txin["pubkeysig"] = [(pubkey, sig)]
|
||||||
is_complete = is_complete = True
|
|
||||||
|
|
||||||
self.is_complete = is_complete
|
self.is_complete = is_complete
|
||||||
self.raw = self.serialize( self.inputs, self.outputs )
|
self.raw = self.serialize( self.inputs, self.outputs )
|
||||||
|
|||||||
Reference in New Issue
Block a user