FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bitcoin-php/src/Script/Factory/ScriptCreator.php at main · ProtonMail/bitcoin-php · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ProtonMail
/
bitcoin-php
Public
forked from
Bit-Wasp/bitcoin-php
Notifications
You must be signed in to change notification settings
Fork
23
Star
32
Code
Issues
3
Pull requests
1
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
bitcoin-php
/
src
/
Script
/
Factory
/
ScriptCreator.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
203 lines (180 loc) · 5.24 KB
Breadcrumbs
bitcoin-php
/
src
/
Script
/
Factory
/
ScriptCreator.php
Copy path
File metadata and controls
203 lines (180 loc) · 5.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare
(strict_types=
1
);
namespace
BitWasp
\
Bitcoin
\
Script
\
Factory
;
use
BitWasp
\
Bitcoin
\
Math
\
Math
;
use
BitWasp
\
Bitcoin
\
Script
\
Interpreter
\
Number
;
use
BitWasp
\
Bitcoin
\
Script
\
Opcodes
;
use
BitWasp
\
Bitcoin
\
Script
\
Script
;
use
BitWasp
\
Bitcoin
\
Script
\
ScriptInterface
;
use
BitWasp
\
Buffertools
\
Buffer
;
use
BitWasp
\
Buffertools
\
BufferInterface
;
class
ScriptCreator
{
/**
* @var string
*/
private
$
script
=
''
;
/**
* @var Opcodes
*/
private
$
opcodes
;
/**
* @var Math
*/
private
$
math
;
/**
* @param Math $math
* @param Opcodes $opcodes
* @param BufferInterface|null $buffer
*/
public
function
__construct
(
Math
$
math
,
Opcodes
$
opcodes
, ?
BufferInterface
$
buffer
=
null
)
{
if
(
$
buffer
!==
null
) {
$
this
->
script
=
$
buffer
->
getBinary
();
}
$
this
->
math
=
$
math
;
$
this
->
opcodes
=
$
opcodes
;
}
/**
* Add a data-push instruction to the script,
* pushing x bytes of $data from $data, with
* the appropriate marker for the different
* PUSHDATA opcodes.
*
* @param BufferInterface $data
* @return $this
*/
public
function
push
(
BufferInterface
$
data
)
{
$
length
=
$
data
->
getSize
();
if
(
$
length
< Opcodes::
OP_PUSHDATA1
) {
$
this
->
script
.=
pack
(
'
C
'
,
$
length
) .
$
data
->
getBinary
();
}
else
{
if
(
$
length
<=
0xff
) {
$
lengthSize
=
1
;
$
code
=
'
C
'
;
}
elseif
(
$
length
<=
0xffff
) {
$
lengthSize
=
2
;
$
code
=
'
S
'
;
}
else
{
$
lengthSize
=
4
;
$
code
=
'
V
'
;
}
$
opCode
=
constant
(
"
BitWasp
\\
Bitcoin
\\
Script
\\
Opcodes::OP_PUSHDATA
"
.
$
lengthSize
);
$
this
->
script
.=
pack
(
'
C
'
,
$
opCode
) .
pack
(
$
code
,
$
length
) .
$
data
->
getBinary
();
}
return
$
this
;
}
/**
* Concatenate $script onto $this.
* @param ScriptInterface $script
* @return $this
*/
public
function
concat
(
ScriptInterface
$
script
)
{
$
this
->
script
.=
$
script
->
getBinary
();
return
$
this
;
}
/**
* This function accepts an array of elements, builds
* an intermediate script composed of the items in $sequence,
* and concatenates it in one step.
*
* The allowed types are:
* - opcode (integer form)
* - script number (Number class)
* - data (BufferInterface)
* - script (ScriptInterface)
*
* @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence
* @return $this
*/
public
function
sequence
(
array
$
sequence
)
{
$
new
=
new
self
(
$
this
->
math
,
$
this
->
opcodes
,
null
);
foreach
(
$
sequence
as
$
operation
) {
if
(
is_int
(
$
operation
)) {
if
(!
$
this
->
opcodes
->
offsetExists
(
$
operation
)) {
throw
new
\
RuntimeException
(
'
Unknown opcode
'
);
}
$
new
->
script
.=
chr
(
$
operation
);
}
elseif
(
$
operation
instanceof
Number) {
$
new
->
push
(
$
operation
->
getBuffer
());
}
elseif
(
$
operation
instanceof
BufferInterface) {
$
new
->
push
(
$
operation
);
}
elseif
(
$
operation
instanceof
ScriptInterface) {
$
new
->
concat
(
$
operation
);
}
else
{
throw
new
\
RuntimeException
(
'
Value must be an opcode/BufferInterface/Number
'
);
}
}
$
this
->
concat
(
$
new
->
getScript
());
return
$
this
;
}
/**
* This function accepts an integer, and adds the appropriate
* data-push instruction to the script, minimally encoding it
* where possible.
*
* @param int $n
* @return $this
*/
public
function
int
(
int
$
n
)
{
if
(
$
n
===
0
) {
$
this
->
script
.=
chr
(Opcodes::
OP_0
);
}
else
if
(
$
n
=== -
1
|| (
$
n
>=
1
&&
$
n
<=
16
)) {
$
this
->
script
.=
chr
(
\
BitWasp
\
Bitcoin
\
Script
\encodeOpN
(
$
n
));
}
else
{
$
this
->
push
(Number::
int
(
$
n
)->
getBuffer
());
}
return
$
this
;
}
/**
* Takes a list of opcodes (the name as a string)
* and adds the opcodes to the script.
*
* @param string... $opNames
* @return $this
*/
public
function
op
(
string
...
$
opNames
)
{
$
opCodes
= [];
foreach
(
$
opNames
as
$
opName
) {
$
opCodes
[] =
$
this
->
opcodes
->
getOpByName
(
$
opName
);
}
return
$
this
->
sequence
(
$
opCodes
);
}
/**
* Takes a list of opcodes (in integer form) and
* adds them to the script.
*
* @param int ...$opcodes
* @return $this
*/
public
function
opcode
(
int
...
$
opcodes
)
{
$
this
->
sequence
(
$
opcodes
);
return
$
this
;
}
/**
* Takes a list of data elements and adds the
* push-data instructions to the script.
*
* @param BufferInterface ...$dataList
* @return $this
*/
public
function
data
(
BufferInterface
...
$
dataList
)
{
$
this
->
sequence
(
$
dataList
);
return
$
this
;
}
/**
* Generates a script based on the current state.
* @return ScriptInterface
*/
public
function
getScript
():
ScriptInterface
{
return
new
Script
(
new
Buffer
(
$
this
->
script
),
$
this
->
opcodes
);
}
}
Back
|
FazBrowse Home
|
New Git URL