def deserialize(cls, raw): vds = BCDataStream() vds.write(raw.decode('hex')) vin = parse_input(vds) address = NetworkAddress.deserialize(vds) collateral_pubkey = vds.read_bytes( vds.read_compact_size()).encode('hex') delegate_pubkey = vds.read_bytes(vds.read_compact_size()).encode('hex') sig = vds.read_bytes(vds.read_compact_size()) sig_time = vds.read_int64() protocol_version = vds.read_uint32() last_ping = MasternodePing.deserialize(vds) last_dsq = vds.read_int64() kwargs = { 'vin': vin, 'addr': address, 'collateral_key': collateral_pubkey, 'delegate_key': delegate_pubkey, 'sig': sig, 'sig_time': sig_time, 'protocol_version': protocol_version, 'last_ping': last_ping, 'last_dsq': last_dsq } return cls(**kwargs)
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:]