Skip to content

Instantly share code, notes, and snippets.

@shirriff
Created January 26, 2014 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shirriff/b00515b8583064487ff3 to your computer and use it in GitHub Desktop.
Save shirriff/b00515b8583064487ff3 to your computer and use it in GitHub Desktop.
Transaction-generating utilities for Bitcoin. See full file at https://github.com/shirriff/bitcoin-code/blob/master/txnUtils.py
def makeSignedTransaction(privateKey, outputTransactionHash, sourceIndex, scriptPubKey, outputs):
myTxn_forSig = (makeRawTransaction(outputTransactionHash, sourceIndex, scriptPubKey, outputs)
+ "01000000") # hash code
s256 = hashlib.sha256(hashlib.sha256(myTxn_forSig.decode('hex')).digest()).digest()
sk = ecdsa.SigningKey.from_string(privateKey.decode('hex'), curve=ecdsa.SECP256k1)
sig = sk.sign_digest(s256, sigencode=ecdsa.util.sigencode_der) + '\01' # 01 is hashtype
pubKey = keyUtils.privateKeyToPublicKey(privateKey)
scriptSig = utils.varstr(sig).encode('hex') + utils.varstr(pubKey.decode('hex')).encode('hex')
signed_txn = makeRawTransaction(outputTransactionHash, sourceIndex, scriptSig, outputs)
verifyTxnSignature(signed_txn)
return signed_txn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment