def tx_from_string(s): vds = BCDataStream() vds.write(s) #vds.write(raw.decode('hex')) d = {} d['version'] = vds.read_int32() n_vin = vds.read_compact_size() d['vin'] = [] for i in xrange(n_vin): txin = {} # dirty hack: add outpoint structure to get correct txid later outpoint_pos = vds.read_cursor txin['coinbase'] = vds.read_bytes(vds.read_compact_size()).encode('hex') txin['sequence'] = vds.read_uint32() d['vin'].append(txin) n_vout = vds.read_compact_size() d['vout'] = [] for i in xrange(n_vout): txout = {} txout['value'] = vds.read_int64() txout['scriptPubKey'] = vds.read_bytes(vds.read_compact_size()).encode('hex') d['vout'].append(txout) d['lockTime'] = vds.read_uint32() # compute txid # dirty hack to insert coinbase outpoint structure before hashing raw = s[0:outpoint_pos] COINBASE_OP = '0' * 64 + 'F' * 8 raw += (COINBASE_OP).decode('hex') raw += s[outpoint_pos:vds.read_cursor] d['txid'] = Hash(raw)[::-1].encode('hex') return d, s[vds.read_cursor:] # +1?
def merkle_branch_from_string(s): vds = BCDataStream() vds.write(s) #vds.write(raw.decode('hex')) hashes = [] n_hashes = vds.read_compact_size() for i in xrange(n_hashes): _hash = vds.read_bytes(32) hashes.append(hash_encode(_hash)) index = vds.read_int32() return hashes, index, s[vds.read_cursor:]