def create_transaction(self, node, coinbase, to_address, amount): from_txid = node.getblock(coinbase)["tx"][0] inputs = [{"txid": from_txid, "vout": 0}] outputs = {to_address: amount} rawtx = node.createrawtransaction(inputs, outputs) signresult = node.signrawtransaction(rawtx) tx = ctransaction() f = cstringio.stringio(unhexlify(signresult["hex"])) tx.deserialize(f) return tx
def signaturehash(script, txto, inidx, hashtype): """consensus-correct signaturehash returns (hash, err) to precisely match the consensus-critical behavior of the sighash_single bug. (inidx is *not* checked for validity) """ hash_one = b'\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' if inidx >= len(txto.vin): return (hash_one, "inidx %d out of range (%d)" % (inidx, len(txto.vin))) txtmp = ctransaction(txto) for txin in txtmp.vin: txin.scriptsig = b'' txtmp.vin[inidx].scriptsig = findanddelete(script, cscript([op_codeseparator])) if (hashtype & 0x1f) == sighash_none: txtmp.vout = [] for i in range(len(txtmp.vin)): if i != inidx: txtmp.vin[i].nsequence = 0 elif (hashtype & 0x1f) == sighash_single: outidx = inidx if outidx >= len(txtmp.vout): return (hash_one, "outidx %d out of range (%d)" % (outidx, len(txtmp.vout))) tmp = txtmp.vout[outidx] txtmp.vout = [] for i in range(outidx): txtmp.vout.append(ctxout()) txtmp.vout.append(tmp) for i in range(len(txtmp.vin)): if i != inidx: txtmp.vin[i].nsequence = 0 if hashtype & sighash_anyonecanpay: tmp = txtmp.vin[inidx] txtmp.vin = [] txtmp.vin.append(tmp) s = txtmp.serialize() s += struct.pack(b"<i", hashtype) hash = hash256(s) return (hash, none)