MCPcopy Index your code
hub / github.com/jimmysong/programmingbitcoin / parse_segwit

Method parse_segwit

code-ch13/tx.py:135–160  ·  view source on GitHub ↗
(cls, s, testnet=False)

Source from the content-addressed store, hash-verified

133 # tag::source3[]
134 @classmethod
135 def parse_segwit(cls, s, testnet=False):
136 version = little_endian_to_int(s.read(4))
137 marker = s.read(2)
138 if marker != b'\x00\x01': # <1>
139 raise RuntimeError('Not a segwit transaction {}'.format(marker))
140 num_inputs = read_varint(s)
141 inputs = []
142 for _ in range(num_inputs):
143 inputs.append(TxIn.parse(s))
144 num_outputs = read_varint(s)
145 outputs = []
146 for _ in range(num_outputs):
147 outputs.append(TxOut.parse(s))
148 for tx_in in inputs: # <2>
149 num_items = read_varint(s)
150 items = []
151 for _ in range(num_items):
152 item_len = read_varint(s)
153 if item_len == 0:
154 items.append(0)
155 else:
156 items.append(s.read(item_len))
157 tx_in.witness = items
158 locktime = little_endian_to_int(s.read(4))
159 return cls(version, inputs, outputs, locktime,
160 testnet=testnet, segwit=True)
161 # end::source3[]
162
163 # tag::source4[]

Callers

nothing calls this directly

Calls 4

little_endian_to_intFunction · 0.90
read_varintFunction · 0.90
readMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected