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

Method evaluate

code-ch08/script.py:138–194  ·  view source on GitHub ↗
(self, z)

Source from the content-addressed store, hash-verified

136 return encode_varint(total) + result
137
138 def evaluate(self, z):
139 # create a copy as we may need to add to this list if we have a
140 # RedeemScript
141 cmds = self.cmds[:]
142 stack = []
143 altstack = []
144 while len(cmds) > 0:
145 cmd = cmds.pop(0)
146 if type(cmd) == int:
147 # do what the opcode says
148 operation = OP_CODE_FUNCTIONS[cmd]
149 if cmd in (99, 100):
150 # op_if/op_notif require the cmds array
151 if not operation(stack, cmds):
152 LOGGER.info('bad op: {}'.format(OP_CODE_NAMES[cmd]))
153 return False
154 elif cmd in (107, 108):
155 # op_toaltstack/op_fromaltstack require the altstack
156 if not operation(stack, altstack):
157 LOGGER.info('bad op: {}'.format(OP_CODE_NAMES[cmd]))
158 return False
159 elif cmd in (172, 173, 174, 175):
160 # these are signing operations, they need a sig_hash
161 # to check against
162 if not operation(stack, z):
163 LOGGER.info('bad op: {}'.format(OP_CODE_NAMES[cmd]))
164 return False
165 else:
166 if not operation(stack):
167 LOGGER.info('bad op: {}'.format(OP_CODE_NAMES[cmd]))
168 return False
169 # tag::source1[]
170 else:
171 stack.append(cmd)
172 if len(cmds) == 3 and cmds[0] == 0xa9 \
173 and type(cmds[1]) == bytes and len(cmds[1]) == 20 \
174 and cmds[2] == 0x87: # <1>
175 cmds.pop() # <2>
176 h160 = cmds.pop()
177 cmds.pop()
178 if not op_hash160(stack): # <3>
179 return False
180 stack.append(h160)
181 if not op_equal(stack):
182 return False
183 if not op_verify(stack): # <4>
184 LOGGER.info('bad p2sh h160')
185 return False
186 redeem_script = encode_varint(len(cmd)) + cmd # <5>
187 stream = BytesIO(redeem_script)
188 cmds.extend(Script.parse(stream).cmds) # <6>
189 # end::source1[]
190 if len(stack) == 0:
191 return False
192 if stack.pop() == b'':
193 return False
194 return True
195

Callers 2

verify_inputMethod · 0.45
verify_inputFunction · 0.45

Calls 5

op_hash160Function · 0.90
op_equalFunction · 0.90
op_verifyFunction · 0.90
encode_varintFunction · 0.90
parseMethod · 0.45

Tested by

no test coverage detected