MCPcopy
hub / github.com/jimmysong/programmingbitcoin / parse

Method parse

code-ch08/tx.py:105–127  ·  view source on GitHub ↗

Takes a byte stream and parses the transaction at the start return a Tx object

(cls, s, testnet=False)

Source from the content-addressed store, hash-verified

103
104 @classmethod
105 def parse(cls, s, testnet=False):
106 '''Takes a byte stream and parses the transaction at the start
107 return a Tx object
108 '''
109 # s.read(n) will return n bytes
110 # version is an integer in 4 bytes, little-endian
111 version = little_endian_to_int(s.read(4))
112 # num_inputs is a varint, use read_varint(s)
113 num_inputs = read_varint(s)
114 # parse num_inputs number of TxIns
115 inputs = []
116 for _ in range(num_inputs):
117 inputs.append(TxIn.parse(s))
118 # num_outputs is a varint, use read_varint(s)
119 num_outputs = read_varint(s)
120 # parse num_outputs number of TxOuts
121 outputs = []
122 for _ in range(num_outputs):
123 outputs.append(TxOut.parse(s))
124 # locktime is an integer in 4 bytes, little-endian
125 locktime = little_endian_to_int(s.read(4))
126 # return an instance of the class (see __init__ for args)
127 return cls(version, inputs, outputs, locktime, testnet=testnet)
128
129 def serialize(self):
130 '''Returns the byte serialization of the transaction'''

Callers 14

op_checksigFunction · 0.45
fetchMethod · 0.45
load_cacheMethod · 0.45
parseMethod · 0.45
parseMethod · 0.45
test_parse_versionMethod · 0.45
test_parse_inputsMethod · 0.45
test_parse_outputsMethod · 0.45
test_parse_locktimeMethod · 0.45
test_serializeMethod · 0.45
test_feeMethod · 0.45
test_sign_inputMethod · 0.45

Calls 3

little_endian_to_intFunction · 0.90
read_varintFunction · 0.90
readMethod · 0.45

Tested by 7

test_parse_versionMethod · 0.36
test_parse_inputsMethod · 0.36
test_parse_outputsMethod · 0.36
test_parse_locktimeMethod · 0.36
test_serializeMethod · 0.36
test_feeMethod · 0.36
test_sign_inputMethod · 0.36