MCPcopy
hub / github.com/petertodd/python-bitcoinlib / parse_script

Function parse_script

bitcoin/tests/test_scripteval.py:23–49  ·  view source on GitHub ↗
(s)

Source from the content-addressed store, hash-verified

21from bitcoin.core.scripteval import *
22
23def parse_script(s):
24 def ishex(s):
25 return set(s).issubset(set('0123456789abcdefABCDEF'))
26
27 r = []
28
29 # Create an opcodes_by_name table with both OP_ prefixed names and
30 # shortened ones with the OP_ dropped.
31 opcodes_by_name = {}
32 for name, code in OPCODES_BY_NAME.items():
33 opcodes_by_name[name] = code
34 opcodes_by_name[name[3:]] = code
35
36 for word in s.split():
37 if word.isdigit() or (word[0] == '-' and word[1:].isdigit()):
38 r.append(CScript([int(word)]))
39 elif word.startswith('0x') and ishex(word[2:]):
40 # Raw ex data, inserted NOT pushed onto stack:
41 r.append(unhexlify(word[2:].encode('utf8')))
42 elif len(word) >= 2 and word[0] == "'" and word[-1] == "'":
43 r.append(CScript([bytes(word[1:-1].encode('utf8'))]))
44 elif word in opcodes_by_name:
45 r.append(CScript([opcodes_by_name[word]]))
46 else:
47 raise ValueError("Error parsing script: %r" % s)
48
49 return CScript(b''.join(r))
50
51
52def load_test_vectors(name):

Callers 2

load_test_vectorsFunction · 0.90
load_test_vectorsFunction · 0.85

Calls 3

CScriptClass · 0.85
ishexFunction · 0.85
joinMethod · 0.80

Tested by

no test coverage detected