Browse Source

simplify tx.sign()

283
ThomasV 12 years ago
parent
commit
e91e02f2ce
  1. 73
      lib/bitcoin.py

73
lib/bitcoin.py

@ -526,17 +526,17 @@ class Transaction:
s += int_to_hex(txin['index'],4) # prev index s += int_to_hex(txin['index'],4) # prev index
if for_sig is None: if for_sig is None:
pubkeysig = txin.get('pubkeysig')
if pubkeysig:
pubkey, sig = pubkeysig[0]
sig = sig + chr(1) # hashtype
script = op_push( len(sig))
script += sig.encode('hex')
script += op_push( len(pubkey))
script += pubkey.encode('hex')
else:
signatures = txin['signatures'] signatures = txin['signatures']
pubkeys = txin['pubkeys'] pubkeys = txin['pubkeys']
if not txin.get('redeemScript'):
pubkey = pubkeys[0]
sig = signatures[0]
sig = sig + '01' # hashtype
script = op_push(len(sig)/2)
script += sig
script += op_push(len(pubkey)/2)
script += pubkey
else:
script = '00' # op_0 script = '00' # op_0
for sig in signatures: for sig in signatures:
sig = sig + '01' sig = sig + '01'
@ -595,39 +595,35 @@ class Transaction:
def sign(self, private_keys): def sign(self, private_keys):
import deserialize import deserialize
is_complete = True is_complete = True
for i, txin in enumerate(self.inputs): for i, txin in enumerate(self.inputs):
tx_for_sig = self.serialize( self.inputs, self.outputs, for_sig = i ) # build list of public/private keys
txin_pk = private_keys.get( txin.get('address') ) keypairs = {}
redeem_script = txin.get('redeemScript') for sec in private_keys.get( txin.get('address') ):
compressed = is_compressed(sec)
pkey = regenerate_key(sec)
pubkey = GetPubKey(pkey.pubkey, compressed)
keypairs[ pubkey.encode('hex') ] = sec
redeem_script = txin.get('redeemScript')
if redeem_script: if redeem_script:
# 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 else:
num = 1
redeem_pubkeys = keypairs.keys()
# list of already existing signatures # get list of already existing signatures
signatures = txin.get("signatures",[]) signatures = txin.get("signatures",[])
# continue if this txin is complete # continue if this txin is complete
if len(signatures) == num: if len(signatures) == num:
continue continue
# build list of public/private keys tx_for_sig = self.serialize( self.inputs, self.outputs, for_sig = i )
keypairs = {}
for sec in txin_pk:
compressed = is_compressed(sec)
pkey = regenerate_key(sec)
pubkey = GetPubKey(pkey.pubkey, compressed)
keypairs[ pubkey.encode('hex') ] = sec
for pubkey in redeem_pubkeys: for pubkey in redeem_pubkeys:
# check if we have the corresponding private key
# check if we have a key corresponding to the redeem script
if pubkey in keypairs.keys(): if pubkey in keypairs.keys():
# add signature # add signature
sec = keypairs[pubkey] sec = keypairs[pubkey]
@ -640,32 +636,11 @@ class Transaction:
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)
signatures.append( sig.encode('hex') ) signatures.append( sig.encode('hex') )
# for p2sh, pubkeysig is a tuple (may be incomplete)
txin["signatures"] = signatures txin["signatures"] = signatures
txin["pubkeys"] = redeem_pubkeys
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:
if txin.get("pubkeysig"):
continue
if not txin_pk:
is_complete = False
continue
sec = txin_pk[0]
compressed = is_compressed(sec)
pkey = regenerate_key(sec)
secexp = pkey.secret
private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
public_key = private_key.get_verifying_key()
pkey = EC_KEY(secexp)
pubkey = GetPubKey(pkey.pubkey, compressed)
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)
txin["pubkeysig"] = [(pubkey, sig)]
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 )

Loading…
Cancel
Save