1 | <?php |
||
14 | class ScriptCreator |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $script = ''; |
||
20 | |||
21 | /** |
||
22 | * @var Opcodes |
||
23 | */ |
||
24 | private $opcodes; |
||
25 | |||
26 | /** |
||
27 | * @var Math |
||
28 | */ |
||
29 | private $math; |
||
30 | |||
31 | /** |
||
32 | * @param Math $math |
||
33 | * @param Opcodes $opcodes |
||
34 | * @param BufferInterface|null $buffer |
||
35 | */ |
||
36 | 2618 | public function __construct(Math $math, Opcodes $opcodes, BufferInterface $buffer = null) |
|
37 | { |
||
38 | 2618 | if ($buffer !== null) { |
|
39 | 42 | $this->script = $buffer->getBinary(); |
|
40 | } |
||
41 | |||
42 | 2618 | $this->math = $math; |
|
43 | 2618 | $this->opcodes = $opcodes; |
|
44 | 2618 | } |
|
45 | |||
46 | /** |
||
47 | * @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence |
||
48 | * @return $this |
||
49 | */ |
||
50 | 2554 | public function sequence(array $sequence) |
|
51 | { |
||
52 | 2554 | $new = new self($this->math, $this->opcodes, null); |
|
53 | 2554 | foreach ($sequence as $operation) { |
|
54 | 2548 | if (is_int($operation)) { |
|
55 | 2508 | if (!$this->opcodes->offsetExists($operation)) { |
|
56 | throw new \RuntimeException('Unknown opcode'); |
||
57 | } |
||
58 | |||
59 | 2508 | $new->script .= chr($operation); |
|
60 | 286 | } elseif ($operation instanceof Number) { |
|
61 | $new->push($operation->getBuffer()); |
||
62 | 286 | } elseif ($operation instanceof BufferInterface) { |
|
63 | 286 | $new->push($operation); |
|
64 | } elseif ($operation instanceof ScriptInterface) { |
||
65 | $new->concat($operation); |
||
66 | } else { |
||
67 | 2548 | throw new \RuntimeException('Value must be an opcode/BufferInterface/Number'); |
|
68 | } |
||
69 | } |
||
70 | |||
71 | 2554 | $this->concat($new->getScript()); |
|
72 | 2554 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Add an opcode to the script |
||
77 | * |
||
78 | * @param string $name |
||
79 | * @return $this |
||
80 | */ |
||
81 | 66 | public function op($name) |
|
87 | |||
88 | /** |
||
89 | * Push data into the stack. |
||
90 | * |
||
91 | * @param $data |
||
92 | * @return $this |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | 316 | public function push(BufferInterface $data) |
|
96 | { |
||
97 | 316 | $length = $data->getSize(); |
|
98 | |||
99 | 316 | if ($length < Opcodes::OP_PUSHDATA1) { |
|
100 | 310 | $this->script .= pack('C', $length) . $data->getBinary(); |
|
101 | } else { |
||
102 | 8 | if ($length <= 0xff) { |
|
103 | 4 | $lengthSize = 1; |
|
104 | 4 | $code = 'C'; |
|
105 | 4 | } elseif ($length <= 0xffff) { |
|
106 | 2 | $lengthSize = 2; |
|
107 | 2 | $code = 'S'; |
|
108 | } else { |
||
109 | 2 | $lengthSize = 4; |
|
110 | 2 | $code = 'V'; |
|
111 | } |
||
112 | |||
113 | 8 | $opCode = constant("BitWasp\\Bitcoin\\Script\\Opcodes::OP_PUSHDATA" . $lengthSize); |
|
114 | 8 | $this->script .= pack('C', $opCode) . pack($code, $length) . $data->getBinary(); |
|
115 | } |
||
116 | |||
117 | 316 | return $this; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param int $n |
||
122 | * @return $this |
||
123 | */ |
||
124 | 32 | public function int($n) |
|
136 | |||
137 | /** |
||
138 | * @param Serializable $object |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function pushSerializable(Serializable $object) |
||
146 | |||
147 | /** |
||
148 | * @param ScriptInterface $script |
||
149 | * @return $this |
||
150 | */ |
||
151 | 2554 | public function concat(ScriptInterface $script) |
|
156 | |||
157 | /** |
||
158 | * @return ScriptInterface |
||
159 | */ |
||
160 | 2616 | public function getScript() |
|
164 | } |
||
165 |