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

Class Script

code-ch08/script.py:33–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31
32
33class Script:
34
35 def __init__(self, cmds=None):
36 if cmds is None:
37 self.cmds = []
38 else:
39 self.cmds = cmds
40
41 def __repr__(self):
42 result = []
43 for cmd in self.cmds:
44 if type(cmd) == int:
45 if OP_CODE_NAMES.get(cmd):
46 name = OP_CODE_NAMES.get(cmd)
47 else:
48 name = 'OP_[{}]'.format(cmd)
49 result.append(name)
50 else:
51 result.append(cmd.hex())
52 return ' '.join(result)
53
54 def __add__(self, other):
55 return Script(self.cmds + other.cmds)
56
57 @classmethod
58 def parse(cls, s):
59 # get the length of the entire field
60 length = read_varint(s)
61 # initialize the cmds array
62 cmds = []
63 # initialize the number of bytes we've read to 0
64 count = 0
65 # loop until we've read length bytes
66 while count < length:
67 # get the current byte
68 current = s.read(1)
69 # increment the bytes we've read
70 count += 1
71 # convert the current byte to an integer
72 current_byte = current[0]
73 # if the current byte is between 1 and 75 inclusive
74 if current_byte >= 1 and current_byte <= 75:
75 # we have an cmd set n to be the current byte
76 n = current_byte
77 # add the next n bytes as an cmd
78 cmds.append(s.read(n))
79 # increase the count by n
80 count += n
81 elif current_byte == 76:
82 # op_pushdata1
83 data_length = little_endian_to_int(s.read(1))
84 cmds.append(s.read(data_length))
85 count += data_length + 1
86 elif current_byte == 77:
87 # op_pushdata2
88 data_length = little_endian_to_int(s.read(2))
89 cmds.append(s.read(data_length))
90 count += data_length + 2

Callers 5

sign_inputMethod · 0.90
__init__Method · 0.90
p2pkh_scriptFunction · 0.70
p2sh_scriptFunction · 0.70
__add__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected