def add_op_return(unsigned_raw_tx, message): deserialized_tx = pybitcointools.deserialize(unsigned_raw_tx) newscript = make_op_return_script(message) newoutput = {} newoutput['value'] = 0 newoutput['script'] = newscript deserialized_tx['outs'].append(newoutput) reserialized_tx = pybitcointools.serialize(deserialized_tx) return reserialized_tx
def verify_vin_old(txid, index): # get raw transaction (txid) <-- vin[0]: scriptSig rpc = BtcRpc("http://*****:*****@127.0.0.1:8332") rawtx = rpc.rpc.getrawtransaction(txid) jsontxverbose = rpc.rpc.getrawtransaction(txid, 1) #pprint.pprint(jsontxverbose) jsontx = pybitcointools.deserialize(rawtx) pprint.pprint(jsontx) scriptSigasm = jsontxverbose['vin'][index]['scriptSig']['asm'] logger.debug(scriptSigasm) scriptSig = jsontx['ins'][index]['script'] sigpubdecoded = asn1der.decode( scriptSig.decode("hex")[1:]) # skip first push sig = long(sigpubdecoded[0][0]), long(sigpubdecoded[0][1]) pubkey = sigpubdecoded[1] sighash_type = pubkey[0] logger.debug("sighash type: %r" % sighash_type) push = pubkey[1] btc_pubkey_type = pubkey[2] pubkey = pubkey[3:] logger.debug("r %s s %s" % (hex(sig[0]), hex(sig[1]))) logger.debug(pubkey.encode("hex")) # generate signdata # replace input script with funding script of 17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ for txin in jsontx['ins']: txin['script'] = '' funding_txid = jsontxverbose['vin'][index]['txid'] funding_tx = rpc.rpc.getrawtransaction(funding_txid, 1) #pprint.pprint(funding_tx) funding_script = funding_tx['vout'][0]['scriptPubKey']['hex'] jsontx['ins'][index]['script'] = funding_script signdata = pybitcointools.serialize(jsontx) + "01000000" #SIGHASH ALL import hashlib digest = hashlib.sha256(hashlib.sha256( signdata.decode("hex")).digest()).digest() logger.debug(digest[::-1].encode("hex")) vk = VerifyingKey.from_string(pubkey, curve=curve) logger.debug("verify --> %s " % (vk.pubkey.verifies( int(digest.encode("hex"), 16), Signature(sig[0], sig[1])))) #print vk.verify_digest(scriptSigasm.split("[ALL]",1)[0].decode("hex"), digest, sigdecode=ecdsa.util.sigdecode_der) return BTCSignature( sig=Signature(sig[0], sig[1]), h=int(digest.encode("hex"), 16), pubkey=pubkey, )
def dump_tx_ecdsa(txid, i): tx = getrawtx(txid) vin = tx['vin'][i] if 'coinbase' in vin: return prev_tx = getrawtx(vin['txid']) prev_vout = prev_tx['vout'][vin['vout']] prev_type = prev_vout['scriptPubKey']['type'] script = prev_vout['scriptPubKey']['hex'] if prev_type == 'pubkeyhash': sig, pub = vin['scriptSig']['asm'].split(' ') elif prev_type == 'pubkey': sig = vin['scriptSig']['asm'] pub, _ = prev_vout['scriptPubKey']['asm'].split(' ') else: logger.warning("%6d %s %4d ERROR_UNHANDLED_SCRIPT_TYPE" % (txid, i)) raise x = pub[2:66] #print sig if sig[-1] == ']': sig, hashcode_txt = sig.strip(']').split('[') if hashcode_txt == 'ALL': hashcode = 1 elif hashcode_txt == 'SINGLE': hashcode = 3 else: print hashcode_txt logger.warning("xx %s %4d ERROR_UNHANDLED_HASHCODE" % (txid, hashcode_txt)) raise else: hashcode = int(sig[-2:], 16) sig = sig[:-2] modtx = pybitcointools.serialize( pybitcointools.signature_form(pybitcointools.deserialize(tx['hex']), i, script, hashcode)) z = hexlify(pybitcointools.txhash(modtx, hashcode)) _, r, s = pybitcointools.der_decode_sig(sig) r = pybitcointools.encode(r, 16, 64) s = pybitcointools.encode(s, 16, 64) #print verify_tx_input(tx['hex'], i, script, sig, pub) return {'txid': txid, 'i': i, 'x': x, 'r': r, 's': s, 'z': z, 'pub': pub}
if len(sigRtx) % 2: sigRtx = "0" + sigRtx sigDtx = {} try: sigDtx = pbt.deserialize(sigRtx) except: pass if sigDtx["ins"][0]["script"]: break except (IndexError, KeyError): pass else: raise time.sleep(0.5) base.display_dtx(sigDtx, data) sigRtx = pbt.serialize(sigDtx) print "About to publish tx" print "Press <enter> to proceed, <ctrl-c> to cancel." raw_input() print print pbt.pushtx(sigRtx) except: import traceback traceback.print_exc() print "Good bye - <enter>" raw_input()