def helloblock_fetchtx(txhash, network="btc"): if isinstance(txhash, list): return [helloblock_fetchtx(h) for h in txhash] if not re.match("^[0-9a-fA-F]*$", txhash): txhash = txhash.encode("hex") if network == "testnet": url = "https://testnet.helloblock.io/v1/transactions/" elif network == "btc": url = "https://mainnet.helloblock.io/v1/transactions/" else: raise Exception("Unsupported network {0} for helloblock_fetchtx".format(network)) data = json.loads(make_request(url + txhash).decode("utf-8"))["data"]["transaction"] o = {"locktime": data["locktime"], "version": data["version"], "ins": [], "outs": []} for inp in data["inputs"]: o["ins"].append( { "script": inp["scriptSig"], "outpoint": {"index": inp["prevTxoutIndex"], "hash": inp["prevTxHash"]}, "sequence": 4294967295, } ) for outp in data["outputs"]: o["outs"].append({"value": outp["value"], "script": outp["scriptPubKey"]}) from bitcoin.transaction import serialize from bitcoin.transaction import txhash as TXHASH tx = serialize(o) assert TXHASH(tx) == txhash return tx
def helloblock_fetchtx(txhash, network='btc'): if not re.match('^[0-9a-fA-F]*$', txhash): txhash = safe_hexlify(txhash) if network == 'testnet': url = 'https://testnet.helloblock.io/v1/transactions/' elif network == 'btc': url = 'https://mainnet.helloblock.io/v1/transactions/' else: raise Exception('Unsupported network {0} for helloblock_fetchtx'.format(network)) data = request(url + txhash)["data"]["transaction"] o = { "locktime": data["locktime"], "version": data["version"], "ins": [], "outs": [] } for inp in data["inputs"]: o["ins"].append({ "script": inp["scriptSig"], "outpoint": { "index": inp["prevTxoutIndex"], "hash": inp["prevTxHash"], }, "sequence": 4294967295 }) for outp in data["outputs"]: o["outs"].append({ "value": outp["value"], "script": outp["scriptPubKey"] }) from bitcoin.transaction import serialize from bitcoin.transaction import txhash as TXHASH tx = serialize(o) assert TXHASH(tx) == txhash return tx
def build_unsigned_single_tx(amount, fees, outputs): ''' Builds an unsigned tx and returns it in a list ''' txs = build_valid_single_tx(amount, fees, outputs) txjson = deserialize(txs[0]) for inp in txjson["ins"]: inp['script'] = '' return [serialize(txjson)]
o["ins"].append({ "script": inp["scriptSig"], "outpoint": { "index": inp["prevTxoutIndex"], "hash": inp["prevTxHash"], }, "sequence": 4294967295 }) for outp in data["outputs"]: o["outs"].append({ "value": outp["value"], "script": outp["scriptPubKey"] }) from bitcoin.transaction import serialize from bitcoin.transaction import txhash as TXHASH tx = serialize(o) assert TXHASH(tx) == txhash return tx def webbtc_fetchtx(txhash, network='btc'): if not re.match('^[0-9a-fA-F]*$', txhash): txhash = safe_hexlify(txhash) if network == 'testnet': webbtc_url = 'http://test.webbtc.com/tx/' elif network == 'btc': webbtc_url = 'http://webbtc.com/tx/' else: raise Exception('Unsupported network {0} for webbtc_fetchtx'.format(network)) hexdata = make_request(webbtc_url + txhash + ".hex") return st(hexdata)
'sequence': UINT_MAX }], 'outs': [{ 'script': opReturnScript, 'value': 0 }, { 'script': lockScriptChangeAddr, 'value': amount }], 'locktime': 0, 'version': 1 } rawMainTX = bitcoinlibtransaction.serialize(mainTX) ## uncomment the next 3 lines if need to debug the transaction # deserializedRawMainTX = bitcoinlibtransaction.deserialize(rawMainTX) # print(deserializedRawMainTX) # print("\n") # # sign raw transaction # signedRawMainTX = rpc._call( "signrawtransaction", rawMainTX ) # I directly call the rpc method instead of using the lib rpc.signrawtransaction function as that function is expecting a transaction object. For the sake of the exercise, I construct the transaction manually. There are other methods to make the transaction, for instance: https://github.com/petertodd/python-bitcoinlib/blob/master/examples/spend-p2pkh-txout.py #