| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Bitcoin::Crypto::Script; |
|
2
|
|
|
|
|
|
|
$Bitcoin::Crypto::Script::VERSION = '2.000_01'; # TRIAL |
|
3
|
|
|
|
|
|
|
$Bitcoin::Crypto::Script::VERSION = '2.00001'; |
|
4
|
29
|
|
|
29
|
|
82592
|
use v5.10; |
|
|
29
|
|
|
|
|
150
|
|
|
5
|
29
|
|
|
29
|
|
192
|
use strict; |
|
|
29
|
|
|
|
|
85
|
|
|
|
29
|
|
|
|
|
657
|
|
|
6
|
29
|
|
|
29
|
|
169
|
use warnings; |
|
|
29
|
|
|
|
|
88
|
|
|
|
29
|
|
|
|
|
846
|
|
|
7
|
29
|
|
|
29
|
|
1139
|
use Moo; |
|
|
29
|
|
|
|
|
13090
|
|
|
|
29
|
|
|
|
|
227
|
|
|
8
|
29
|
|
|
29
|
|
20176
|
use Crypt::Digest::SHA256 qw(sha256); |
|
|
29
|
|
|
|
|
15299
|
|
|
|
29
|
|
|
|
|
1920
|
|
|
9
|
29
|
|
|
29
|
|
1445
|
use Mooish::AttributeBuilder -standard; |
|
|
29
|
|
|
|
|
5257
|
|
|
|
29
|
|
|
|
|
261
|
|
|
10
|
29
|
|
|
29
|
|
5213
|
use Try::Tiny; |
|
|
29
|
|
|
|
|
4000
|
|
|
|
29
|
|
|
|
|
1838
|
|
|
11
|
29
|
|
|
29
|
|
222
|
use Scalar::Util qw(blessed); |
|
|
29
|
|
|
|
|
81
|
|
|
|
29
|
|
|
|
|
1605
|
|
|
12
|
29
|
|
|
29
|
|
1291
|
use Type::Params -sigs; |
|
|
29
|
|
|
|
|
215749
|
|
|
|
29
|
|
|
|
|
290
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
29
|
|
|
29
|
|
15484
|
use Bitcoin::Crypto::Constants; |
|
|
29
|
|
|
|
|
108
|
|
|
|
29
|
|
|
|
|
1019
|
|
|
15
|
29
|
|
|
29
|
|
12085
|
use Bitcoin::Crypto::Base58 qw(encode_base58check decode_base58check); |
|
|
29
|
|
|
|
|
156
|
|
|
|
29
|
|
|
|
|
1968
|
|
|
16
|
29
|
|
|
29
|
|
14162
|
use Bitcoin::Crypto::Bech32 qw(encode_segwit decode_segwit get_hrp); |
|
|
29
|
|
|
|
|
129
|
|
|
|
29
|
|
|
|
|
2042
|
|
|
17
|
29
|
|
|
29
|
|
236
|
use Bitcoin::Crypto::Constants; |
|
|
29
|
|
|
|
|
67
|
|
|
|
29
|
|
|
|
|
775
|
|
|
18
|
29
|
|
|
29
|
|
182
|
use Bitcoin::Crypto::Helpers qw(carp_once); |
|
|
29
|
|
|
|
|
72
|
|
|
|
29
|
|
|
|
|
1471
|
|
|
19
|
29
|
|
|
29
|
|
223
|
use Bitcoin::Crypto::Util qw(hash160 hash256); |
|
|
29
|
|
|
|
|
79
|
|
|
|
29
|
|
|
|
|
1183
|
|
|
20
|
29
|
|
|
29
|
|
208
|
use Bitcoin::Crypto::Exception; |
|
|
29
|
|
|
|
|
95
|
|
|
|
29
|
|
|
|
|
1038
|
|
|
21
|
29
|
|
|
29
|
|
334
|
use Bitcoin::Crypto::Types qw(Maybe ArrayRef HashRef Str Object ByteStr Any ScriptType ScriptDesc); |
|
|
29
|
|
|
|
|
164
|
|
|
|
29
|
|
|
|
|
216
|
|
|
22
|
29
|
|
|
29
|
|
126798
|
use Bitcoin::Crypto::Script::Opcode; |
|
|
29
|
|
|
|
|
196
|
|
|
|
29
|
|
|
|
|
1701
|
|
|
23
|
29
|
|
|
29
|
|
11176
|
use Bitcoin::Crypto::Script::Runner; |
|
|
29
|
|
|
|
|
109
|
|
|
|
29
|
|
|
|
|
2956
|
|
|
24
|
29
|
|
|
29
|
|
200
|
use Bitcoin::Crypto::Script::Common; |
|
|
29
|
|
|
|
|
65
|
|
|
|
29
|
|
|
|
|
917
|
|
|
25
|
29
|
|
|
29
|
|
15838
|
use Bitcoin::Crypto::Script::Recognition; |
|
|
29
|
|
|
|
|
140
|
|
|
|
29
|
|
|
|
|
2943
|
|
|
26
|
|
|
|
|
|
|
|
|
27
|
29
|
|
|
29
|
|
255
|
use namespace::clean; |
|
|
29
|
|
|
|
|
82
|
|
|
|
29
|
|
|
|
|
268
|
|
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has field '_serialized' => ( |
|
30
|
|
|
|
|
|
|
isa => Str, |
|
31
|
|
|
|
|
|
|
writer => 1, |
|
32
|
|
|
|
|
|
|
default => '', |
|
33
|
|
|
|
|
|
|
); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has field 'type' => ( |
|
36
|
|
|
|
|
|
|
isa => Maybe [ScriptType], |
|
37
|
|
|
|
|
|
|
lazy => 1, |
|
38
|
|
|
|
|
|
|
); |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has field '_address' => ( |
|
41
|
|
|
|
|
|
|
isa => Maybe [ByteStr], |
|
42
|
|
|
|
|
|
|
lazy => 1, |
|
43
|
|
|
|
|
|
|
); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
with qw(Bitcoin::Crypto::Role::Network); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _build_type |
|
48
|
|
|
|
|
|
|
{ |
|
49
|
173
|
|
|
173
|
|
2036
|
my ($self) = @_; |
|
50
|
|
|
|
|
|
|
|
|
51
|
173
|
|
|
|
|
2952
|
my $rec = Bitcoin::Crypto::Script::Recognition->new(script => $self); |
|
52
|
173
|
|
|
|
|
4623
|
return $rec->get_type; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _build_address |
|
56
|
|
|
|
|
|
|
{ |
|
57
|
10
|
|
|
10
|
|
80
|
my ($self) = @_; |
|
58
|
|
|
|
|
|
|
|
|
59
|
10
|
|
|
|
|
122
|
my $rec = Bitcoin::Crypto::Script::Recognition->new(script => $self); |
|
60
|
10
|
|
|
|
|
193
|
return $rec->get_address; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _build |
|
64
|
|
|
|
|
|
|
{ |
|
65
|
58
|
|
|
58
|
|
169
|
my ($self, $type, $address) = @_; |
|
66
|
|
|
|
|
|
|
|
|
67
|
58
|
|
|
|
|
121
|
state $types = do { |
|
68
|
|
|
|
|
|
|
my $legacy = sub { |
|
69
|
29
|
|
|
29
|
|
88
|
my ($self, $address, $type) = @_; |
|
70
|
|
|
|
|
|
|
|
|
71
|
29
|
|
|
|
|
135
|
my $decoded = decode_base58check($address); |
|
72
|
29
|
|
|
|
|
115
|
my $network_byte = substr $decoded, 0, 1, ''; |
|
73
|
|
|
|
|
|
|
|
|
74
|
29
|
|
|
|
|
113
|
my $byte_method = lc "p2${type}_byte"; |
|
75
|
29
|
50
|
|
|
|
219
|
Bitcoin::Crypto::Exception::NetworkCheck->raise( |
|
76
|
|
|
|
|
|
|
"provided address $address does not belong to network " . $self->network->name |
|
77
|
|
|
|
|
|
|
) if $network_byte ne $self->network->$byte_method; |
|
78
|
|
|
|
|
|
|
|
|
79
|
29
|
|
|
|
|
223
|
Bitcoin::Crypto::Script::Common->fill($type => $self, $decoded); |
|
80
|
6
|
|
|
|
|
42
|
}; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $witness = sub { |
|
83
|
9
|
|
|
9
|
|
40
|
my ($self, $address, $name, $version, $length) = @_; |
|
84
|
|
|
|
|
|
|
|
|
85
|
9
|
|
|
|
|
49
|
my $data = decode_segwit $address; |
|
86
|
9
|
|
|
|
|
57
|
my $this_version = substr $data, 0, 1, ''; |
|
87
|
|
|
|
|
|
|
|
|
88
|
9
|
50
|
|
|
|
42
|
Bitcoin::Crypto::Exception::SegwitProgram->raise( |
|
89
|
|
|
|
|
|
|
"$name script only handles witness version $version" |
|
90
|
|
|
|
|
|
|
) unless $this_version eq chr $version; |
|
91
|
|
|
|
|
|
|
|
|
92
|
9
|
50
|
|
|
|
56
|
Bitcoin::Crypto::Exception::SegwitProgram->raise( |
|
93
|
|
|
|
|
|
|
"$name script should contain $length bytes" |
|
94
|
|
|
|
|
|
|
) unless length $data eq $length; |
|
95
|
|
|
|
|
|
|
|
|
96
|
9
|
100
|
|
|
|
83
|
Bitcoin::Crypto::Exception::NetworkCheck->raise( |
|
97
|
|
|
|
|
|
|
"provided address $address does not belong to network " . $self->network->name |
|
98
|
|
|
|
|
|
|
) if get_hrp($address) ne $self->network->segwit_hrp; |
|
99
|
|
|
|
|
|
|
|
|
100
|
8
|
|
|
|
|
69
|
$self |
|
101
|
|
|
|
|
|
|
->add("OP_$version") |
|
102
|
|
|
|
|
|
|
->push($data); |
|
103
|
6
|
|
|
|
|
64
|
}; |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
{ |
|
106
|
|
|
|
|
|
|
P2PK => sub { |
|
107
|
9
|
|
|
9
|
|
23
|
my ($self, $pubkey) = @_; |
|
108
|
|
|
|
|
|
|
|
|
109
|
9
|
|
|
|
|
26
|
$self |
|
110
|
|
|
|
|
|
|
->push($pubkey) |
|
111
|
|
|
|
|
|
|
->add('OP_CHECKSIG'); |
|
112
|
|
|
|
|
|
|
}, |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
P2PKH => sub { |
|
115
|
15
|
|
|
15
|
|
56
|
$legacy->(@_, 'PKH'); |
|
116
|
|
|
|
|
|
|
}, |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
P2SH => sub { |
|
119
|
14
|
|
|
14
|
|
74
|
$legacy->(@_, 'SH'); |
|
120
|
|
|
|
|
|
|
}, |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
P2MS => sub { |
|
123
|
5
|
|
|
5
|
|
28
|
my ($self, $data) = @_; |
|
124
|
|
|
|
|
|
|
|
|
125
|
5
|
50
|
|
|
|
26
|
Bitcoin::Crypto::Exception::ScriptPush->raise( |
|
126
|
|
|
|
|
|
|
'P2MS script argument must be an array reference' |
|
127
|
|
|
|
|
|
|
) unless ref $data eq 'ARRAY'; |
|
128
|
|
|
|
|
|
|
|
|
129
|
5
|
|
|
|
|
25
|
my ($signatures_num, @pubkeys) = @$data; |
|
130
|
|
|
|
|
|
|
|
|
131
|
5
|
50
|
33
|
|
|
55
|
Bitcoin::Crypto::Exception::ScriptPush->raise( |
|
132
|
|
|
|
|
|
|
'P2MS script first element must be a number between 1 and 15' |
|
133
|
|
|
|
|
|
|
) unless $signatures_num >= 0 && $signatures_num <= 15; |
|
134
|
|
|
|
|
|
|
|
|
135
|
5
|
50
|
33
|
|
|
40
|
Bitcoin::Crypto::Exception::ScriptPush->raise( |
|
136
|
|
|
|
|
|
|
'P2MS script remaining elements number should be between the number of signatures and 15' |
|
137
|
|
|
|
|
|
|
) unless @pubkeys >= $signatures_num && @pubkeys <= 15; |
|
138
|
|
|
|
|
|
|
|
|
139
|
5
|
|
|
|
|
29
|
$self->push(chr $signatures_num); |
|
140
|
|
|
|
|
|
|
|
|
141
|
5
|
|
|
|
|
19
|
foreach my $pubkey (@pubkeys) { |
|
142
|
13
|
|
|
|
|
36
|
$self->push($pubkey); |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$self |
|
146
|
5
|
|
|
|
|
36
|
->push(chr scalar @pubkeys) |
|
147
|
|
|
|
|
|
|
->add('OP_CHECKMULTISIG'); |
|
148
|
|
|
|
|
|
|
}, |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
P2WPKH => sub { |
|
151
|
4
|
|
|
4
|
|
22
|
$witness->(@_, 'P2WPKH', 0, 20); |
|
152
|
|
|
|
|
|
|
}, |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
P2WSH => sub { |
|
155
|
5
|
|
|
5
|
|
28
|
$witness->(@_, 'P2WSH', 0, 32); |
|
156
|
|
|
|
|
|
|
}, |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
NULLDATA => sub { |
|
159
|
6
|
|
|
6
|
|
14
|
my ($self, $data) = @_; |
|
160
|
|
|
|
|
|
|
|
|
161
|
6
|
|
|
|
|
22
|
$self |
|
162
|
|
|
|
|
|
|
->add('OP_RETURN') |
|
163
|
|
|
|
|
|
|
->push($data); |
|
164
|
|
|
|
|
|
|
}, |
|
165
|
6
|
|
|
|
|
197
|
}; |
|
166
|
|
|
|
|
|
|
}; |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::ScriptType->raise( |
|
169
|
|
|
|
|
|
|
"unknown standard script type $type" |
|
170
|
58
|
50
|
|
|
|
210
|
) if !$types->{$type}; |
|
171
|
|
|
|
|
|
|
|
|
172
|
58
|
|
|
|
|
243
|
$types->{$type}->($self, $address); |
|
173
|
57
|
|
|
|
|
1379
|
return; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub BUILD |
|
177
|
|
|
|
|
|
|
{ |
|
178
|
645
|
|
|
645
|
0
|
33011
|
my ($self, $args) = @_; |
|
179
|
|
|
|
|
|
|
|
|
180
|
645
|
100
|
|
|
|
3821
|
if ($args->{type}) { |
|
181
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::ScriptPush->raise( |
|
182
|
|
|
|
|
|
|
'script with a "type" also requires an "address"' |
|
183
|
58
|
50
|
|
|
|
241
|
) unless $args->{address}; |
|
184
|
|
|
|
|
|
|
|
|
185
|
58
|
|
|
|
|
218
|
$self->_build($args->{type}, $args->{address}); |
|
186
|
|
|
|
|
|
|
} |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
signature_for operations => ( |
|
190
|
|
|
|
|
|
|
method => Object, |
|
191
|
|
|
|
|
|
|
positional => [], |
|
192
|
|
|
|
|
|
|
); |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub operations |
|
195
|
|
|
|
|
|
|
{ |
|
196
|
|
|
|
|
|
|
my ($self) = @_; |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
my $serialized = $self->_serialized; |
|
199
|
|
|
|
|
|
|
my @ops; |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
my $data_push = sub { |
|
202
|
|
|
|
|
|
|
my ($size) = @_; |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::ScriptSyntax->raise( |
|
205
|
|
|
|
|
|
|
'not enough bytes of data in the script' |
|
206
|
|
|
|
|
|
|
) if length $serialized < $size; |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
return substr $serialized, 0, $size, ''; |
|
209
|
|
|
|
|
|
|
}; |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
my %context = ( |
|
212
|
|
|
|
|
|
|
op_if => undef, |
|
213
|
|
|
|
|
|
|
op_else => undef, |
|
214
|
|
|
|
|
|
|
previous_context => undef, |
|
215
|
|
|
|
|
|
|
); |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
my %special_ops = ( |
|
218
|
|
|
|
|
|
|
OP_PUSHDATA1 => sub { |
|
219
|
|
|
|
|
|
|
my ($op) = @_; |
|
220
|
|
|
|
|
|
|
my $raw_size = substr $serialized, 0, 1, ''; |
|
221
|
|
|
|
|
|
|
my $size = unpack 'C', $raw_size; |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
push @$op, $data_push->($size); |
|
224
|
|
|
|
|
|
|
$op->[1] .= $raw_size . $op->[2]; |
|
225
|
|
|
|
|
|
|
}, |
|
226
|
|
|
|
|
|
|
OP_PUSHDATA2 => sub { |
|
227
|
|
|
|
|
|
|
my ($op) = @_; |
|
228
|
|
|
|
|
|
|
my $raw_size = substr $serialized, 0, 2, ''; |
|
229
|
|
|
|
|
|
|
my $size = unpack 'v', $raw_size; |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
push @$op, $data_push->($size); |
|
232
|
|
|
|
|
|
|
$op->[1] .= $raw_size . $op->[2]; |
|
233
|
|
|
|
|
|
|
}, |
|
234
|
|
|
|
|
|
|
OP_PUSHDATA4 => sub { |
|
235
|
|
|
|
|
|
|
my ($op) = @_; |
|
236
|
|
|
|
|
|
|
my $raw_size = substr $serialized, 0, 4, ''; |
|
237
|
|
|
|
|
|
|
my $size = unpack 'V', $raw_size; |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
push @$op, $data_push->($size); |
|
240
|
|
|
|
|
|
|
$op->[1] .= $raw_size . $op->[2]; |
|
241
|
|
|
|
|
|
|
}, |
|
242
|
|
|
|
|
|
|
OP_IF => sub { |
|
243
|
|
|
|
|
|
|
my ($op) = @_; |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
if ($context{op_if}) { |
|
246
|
|
|
|
|
|
|
%context = ( |
|
247
|
|
|
|
|
|
|
previous_context => {%context}, |
|
248
|
|
|
|
|
|
|
); |
|
249
|
|
|
|
|
|
|
} |
|
250
|
|
|
|
|
|
|
$context{op_if} = $op; |
|
251
|
|
|
|
|
|
|
}, |
|
252
|
|
|
|
|
|
|
OP_ELSE => sub { |
|
253
|
|
|
|
|
|
|
my ($op, $pos) = @_; |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::ScriptSyntax->raise( |
|
256
|
|
|
|
|
|
|
'OP_ELSE found but no previous OP_IF or OP_NOTIF' |
|
257
|
|
|
|
|
|
|
) if !$context{op_if}; |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::ScriptSyntax->raise( |
|
260
|
|
|
|
|
|
|
'multiple OP_ELSE for a single OP_IF' |
|
261
|
|
|
|
|
|
|
) if @{$context{op_if}} > 2; |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
$context{op_else} = $op; |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
push @{$context{op_if}}, $pos; |
|
266
|
|
|
|
|
|
|
}, |
|
267
|
|
|
|
|
|
|
OP_ENDIF => sub { |
|
268
|
|
|
|
|
|
|
my ($op, $pos) = @_; |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::ScriptSyntax->raise( |
|
271
|
|
|
|
|
|
|
'OP_ENDIF found but no previous OP_IF or OP_NOTIF' |
|
272
|
|
|
|
|
|
|
) if !$context{op_if}; |
|
273
|
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
push @{$context{op_if}}, undef |
|
275
|
|
|
|
|
|
|
if @{$context{op_if}} == 2; |
|
276
|
|
|
|
|
|
|
push @{$context{op_if}}, $pos; |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
if ($context{op_else}) { |
|
279
|
|
|
|
|
|
|
push @{$context{op_else}}, $pos; |
|
280
|
|
|
|
|
|
|
} |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
if ($context{previous_context}) { |
|
283
|
|
|
|
|
|
|
%context = %{$context{previous_context}}; |
|
284
|
|
|
|
|
|
|
} |
|
285
|
|
|
|
|
|
|
else { |
|
286
|
|
|
|
|
|
|
%context = (); |
|
287
|
|
|
|
|
|
|
} |
|
288
|
|
|
|
|
|
|
}, |
|
289
|
|
|
|
|
|
|
); |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
$special_ops{OP_NOTIF} = $special_ops{OP_IF}; |
|
292
|
|
|
|
|
|
|
my @debug_ops; |
|
293
|
|
|
|
|
|
|
my $position = 0; |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
try { |
|
296
|
|
|
|
|
|
|
while (length $serialized) { |
|
297
|
|
|
|
|
|
|
my $this_byte = substr $serialized, 0, 1, ''; |
|
298
|
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
try { |
|
300
|
|
|
|
|
|
|
my $opcode = Bitcoin::Crypto::Script::Opcode->get_opcode_by_code($this_byte); |
|
301
|
|
|
|
|
|
|
push @debug_ops, $opcode->name; |
|
302
|
|
|
|
|
|
|
my $to_push = [$opcode, $this_byte]; |
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
if (exists $special_ops{$opcode->name}) { |
|
305
|
|
|
|
|
|
|
$special_ops{$opcode->name}->($to_push, $position); |
|
306
|
|
|
|
|
|
|
} |
|
307
|
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
push @ops, $to_push; |
|
309
|
|
|
|
|
|
|
} |
|
310
|
|
|
|
|
|
|
catch { |
|
311
|
|
|
|
|
|
|
my $err = $_; |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
my $opcode_num = ord($this_byte); |
|
314
|
|
|
|
|
|
|
unless ($opcode_num > 0 && $opcode_num <= 75) { |
|
315
|
|
|
|
|
|
|
push @debug_ops, unpack 'H*', $this_byte; |
|
316
|
|
|
|
|
|
|
die $err; |
|
317
|
|
|
|
|
|
|
} |
|
318
|
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
# NOTE: compiling standard data push into PUSHDATA1 for now |
|
320
|
|
|
|
|
|
|
my $opcode = Bitcoin::Crypto::Script::Opcode->get_opcode_by_name('OP_PUSHDATA1'); |
|
321
|
|
|
|
|
|
|
push @debug_ops, $opcode->name; |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
my $raw_data = $data_push->($opcode_num); |
|
324
|
|
|
|
|
|
|
push @ops, [$opcode, $this_byte . $raw_data, $raw_data]; |
|
325
|
|
|
|
|
|
|
}; |
|
326
|
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
$position += 1; |
|
328
|
|
|
|
|
|
|
} |
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::ScriptSyntax->raise( |
|
331
|
|
|
|
|
|
|
'some OP_IFs were not closed' |
|
332
|
|
|
|
|
|
|
) if $context{op_if}; |
|
333
|
|
|
|
|
|
|
} |
|
334
|
|
|
|
|
|
|
catch { |
|
335
|
|
|
|
|
|
|
my $ex = $_; |
|
336
|
|
|
|
|
|
|
if (blessed $ex && $ex->isa('Bitcoin::Crypto::Exception::ScriptSyntax')) { |
|
337
|
|
|
|
|
|
|
$ex->set_script(\@debug_ops); |
|
338
|
|
|
|
|
|
|
$ex->set_error_position($position); |
|
339
|
|
|
|
|
|
|
} |
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
die $ex; |
|
342
|
|
|
|
|
|
|
}; |
|
343
|
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
return \@ops; |
|
345
|
|
|
|
|
|
|
} |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
signature_for is_pushes_only => ( |
|
348
|
|
|
|
|
|
|
method => Object, |
|
349
|
|
|
|
|
|
|
positional => [], |
|
350
|
|
|
|
|
|
|
); |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
sub is_pushes_only |
|
353
|
|
|
|
|
|
|
{ |
|
354
|
|
|
|
|
|
|
my ($self) = @_; |
|
355
|
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
foreach my $op (@{$self->operations}) { |
|
357
|
|
|
|
|
|
|
return !!0 unless $op->[0]->pushes; |
|
358
|
|
|
|
|
|
|
} |
|
359
|
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
return !!1; |
|
361
|
|
|
|
|
|
|
} |
|
362
|
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
signature_for add_raw => ( |
|
364
|
|
|
|
|
|
|
method => Object, |
|
365
|
|
|
|
|
|
|
positional => [ByteStr], |
|
366
|
|
|
|
|
|
|
); |
|
367
|
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
sub add_raw |
|
369
|
|
|
|
|
|
|
{ |
|
370
|
|
|
|
|
|
|
my ($self, $bytes) = @_; |
|
371
|
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
$self->_set_serialized($self->_serialized . $bytes); |
|
373
|
|
|
|
|
|
|
return $self; |
|
374
|
|
|
|
|
|
|
} |
|
375
|
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
signature_for add_operation => ( |
|
377
|
|
|
|
|
|
|
method => Object, |
|
378
|
|
|
|
|
|
|
positional => [Str], |
|
379
|
|
|
|
|
|
|
); |
|
380
|
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
sub add_operation |
|
382
|
|
|
|
|
|
|
{ |
|
383
|
|
|
|
|
|
|
my ($self, $name) = @_; |
|
384
|
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
my $opcode = Bitcoin::Crypto::Script::Opcode->get_opcode_by_name($name); |
|
386
|
|
|
|
|
|
|
$self->add_raw($opcode->code); |
|
387
|
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
return $self; |
|
389
|
|
|
|
|
|
|
} |
|
390
|
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
sub add |
|
392
|
|
|
|
|
|
|
{ |
|
393
|
556
|
|
|
556
|
1
|
3070
|
goto \&add_operation; |
|
394
|
|
|
|
|
|
|
} |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
signature_for push_bytes => ( |
|
397
|
|
|
|
|
|
|
method => Object, |
|
398
|
|
|
|
|
|
|
positional => [ByteStr], |
|
399
|
|
|
|
|
|
|
); |
|
400
|
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
sub push_bytes |
|
402
|
|
|
|
|
|
|
{ |
|
403
|
|
|
|
|
|
|
my ($self, $bytes) = @_; |
|
404
|
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
my $len = length $bytes; |
|
406
|
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
if ($len == 0) { |
|
408
|
|
|
|
|
|
|
$self->add_operation('OP_0'); |
|
409
|
|
|
|
|
|
|
} |
|
410
|
|
|
|
|
|
|
elsif ($len == 1 && ord($bytes) <= 0x10) { |
|
411
|
|
|
|
|
|
|
$self->add_operation('OP_' . ord($bytes)); |
|
412
|
|
|
|
|
|
|
} |
|
413
|
|
|
|
|
|
|
elsif ($len <= 75) { |
|
414
|
|
|
|
|
|
|
$self |
|
415
|
|
|
|
|
|
|
->add_raw(pack 'C', $len) |
|
416
|
|
|
|
|
|
|
->add_raw($bytes); |
|
417
|
|
|
|
|
|
|
} |
|
418
|
|
|
|
|
|
|
elsif ($len < (1 << 8)) { |
|
419
|
|
|
|
|
|
|
$self |
|
420
|
|
|
|
|
|
|
->add_operation('OP_PUSHDATA1') |
|
421
|
|
|
|
|
|
|
->add_raw(pack 'C', $len) |
|
422
|
|
|
|
|
|
|
->add_raw($bytes); |
|
423
|
|
|
|
|
|
|
} |
|
424
|
|
|
|
|
|
|
elsif ($len < (1 << 16)) { |
|
425
|
|
|
|
|
|
|
$self |
|
426
|
|
|
|
|
|
|
->add_operation('OP_PUSHDATA2') |
|
427
|
|
|
|
|
|
|
->add_raw(pack 'v', $len) |
|
428
|
|
|
|
|
|
|
->add_raw($bytes); |
|
429
|
|
|
|
|
|
|
} |
|
430
|
|
|
|
|
|
|
elsif (Bitcoin::Crypto::Constants::is_32bit || $len < (1 << 32)) { |
|
431
|
|
|
|
|
|
|
$self |
|
432
|
|
|
|
|
|
|
->add_operation('OP_PUSHDATA4') |
|
433
|
|
|
|
|
|
|
->add_raw(pack 'V', $len) |
|
434
|
|
|
|
|
|
|
->add_raw($bytes); |
|
435
|
|
|
|
|
|
|
} |
|
436
|
|
|
|
|
|
|
else { |
|
437
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::ScriptPush->raise( |
|
438
|
|
|
|
|
|
|
'too much data to push onto stack in one operation' |
|
439
|
|
|
|
|
|
|
); |
|
440
|
|
|
|
|
|
|
} |
|
441
|
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
return $self; |
|
443
|
|
|
|
|
|
|
} |
|
444
|
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
sub push |
|
446
|
|
|
|
|
|
|
{ |
|
447
|
287
|
|
|
287
|
1
|
1566
|
goto \&push_bytes; |
|
448
|
|
|
|
|
|
|
} |
|
449
|
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
# this can only detect native segwit in this context, as P2SH outputs are |
|
451
|
|
|
|
|
|
|
# indistinguishable from any other P2SH |
|
452
|
|
|
|
|
|
|
signature_for is_native_segwit => ( |
|
453
|
|
|
|
|
|
|
method => Object, |
|
454
|
|
|
|
|
|
|
positional => [], |
|
455
|
|
|
|
|
|
|
); |
|
456
|
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
sub is_native_segwit |
|
458
|
|
|
|
|
|
|
{ |
|
459
|
|
|
|
|
|
|
my ($self) = @_; |
|
460
|
|
|
|
|
|
|
my @segwit_types = qw(P2WPKH P2WSH); |
|
461
|
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
my $script_type = $self->type // ''; |
|
463
|
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
return 0 != grep { $script_type eq $_ } @segwit_types; |
|
465
|
|
|
|
|
|
|
} |
|
466
|
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
sub get_script |
|
468
|
|
|
|
|
|
|
{ |
|
469
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
|
470
|
|
|
|
|
|
|
|
|
471
|
0
|
|
|
|
|
|
carp_once "Bitcoin::Crypto::Script->get_script is deprecated. Use Bitcoin::Crypto::Script->to_serialized instead."; |
|
472
|
0
|
|
|
|
|
|
return $self->to_serialized; |
|
473
|
|
|
|
|
|
|
} |
|
474
|
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
signature_for get_hash => ( |
|
476
|
|
|
|
|
|
|
method => Object, |
|
477
|
|
|
|
|
|
|
positional => [], |
|
478
|
|
|
|
|
|
|
); |
|
479
|
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
sub get_hash |
|
481
|
|
|
|
|
|
|
{ |
|
482
|
|
|
|
|
|
|
my ($self) = @_; |
|
483
|
|
|
|
|
|
|
return hash160($self->_serialized); |
|
484
|
|
|
|
|
|
|
} |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
sub get_script_hash |
|
487
|
|
|
|
|
|
|
{ |
|
488
|
0
|
|
|
0
|
0
|
|
carp_once "Bitcoin::Crypto::Script->get_script_hash is deprecated. Use Bitcoin::Crypto::Script->get_hash instead."; |
|
489
|
0
|
|
|
|
|
|
goto \&get_hash; |
|
490
|
|
|
|
|
|
|
} |
|
491
|
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
signature_for to_serialized => ( |
|
493
|
|
|
|
|
|
|
method => Object, |
|
494
|
|
|
|
|
|
|
positional => [], |
|
495
|
|
|
|
|
|
|
); |
|
496
|
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
sub to_serialized |
|
498
|
|
|
|
|
|
|
{ |
|
499
|
|
|
|
|
|
|
my ($self) = @_; |
|
500
|
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
return $self->_serialized; |
|
502
|
|
|
|
|
|
|
} |
|
503
|
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
signature_for from_serialized => ( |
|
505
|
|
|
|
|
|
|
method => Str, |
|
506
|
|
|
|
|
|
|
positional => [Any], |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
# no need to validate ByteStr, as it will be passed to add_raw |
|
509
|
|
|
|
|
|
|
); |
|
510
|
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
sub from_serialized |
|
512
|
|
|
|
|
|
|
{ |
|
513
|
|
|
|
|
|
|
my ($class, $bytes) = @_; |
|
514
|
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
return $class->new->add_raw($bytes); |
|
516
|
|
|
|
|
|
|
} |
|
517
|
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
signature_for from_standard => ( |
|
519
|
|
|
|
|
|
|
method => Str, |
|
520
|
|
|
|
|
|
|
positional => [ScriptDesc, {slurpy => 1}], |
|
521
|
|
|
|
|
|
|
); |
|
522
|
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
sub from_standard |
|
524
|
|
|
|
|
|
|
{ |
|
525
|
|
|
|
|
|
|
my ($class, $desc) = @_; |
|
526
|
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
return $class->new( |
|
528
|
|
|
|
|
|
|
type => $desc->[0], |
|
529
|
|
|
|
|
|
|
address => $desc->[1], |
|
530
|
|
|
|
|
|
|
); |
|
531
|
|
|
|
|
|
|
} |
|
532
|
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
signature_for run => ( |
|
534
|
|
|
|
|
|
|
method => Object, |
|
535
|
|
|
|
|
|
|
positional => [ArrayRef [ByteStr], {default => []}], |
|
536
|
|
|
|
|
|
|
); |
|
537
|
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
sub run |
|
539
|
|
|
|
|
|
|
{ |
|
540
|
|
|
|
|
|
|
my ($self, $initial_stack) = @_; |
|
541
|
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
my $runner = Bitcoin::Crypto::Script::Runner->new(); |
|
543
|
|
|
|
|
|
|
return $runner->execute($self, $initial_stack); |
|
544
|
|
|
|
|
|
|
} |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
signature_for witness_program => ( |
|
547
|
|
|
|
|
|
|
method => Object, |
|
548
|
|
|
|
|
|
|
positional => [], |
|
549
|
|
|
|
|
|
|
); |
|
550
|
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
sub witness_program |
|
552
|
|
|
|
|
|
|
{ |
|
553
|
|
|
|
|
|
|
my ($self) = @_; |
|
554
|
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
my $program = Bitcoin::Crypto::Script->new(network => $self->network); |
|
556
|
|
|
|
|
|
|
$program |
|
557
|
|
|
|
|
|
|
->add_operation('OP_' . Bitcoin::Crypto::Constants::segwit_witness_version) |
|
558
|
|
|
|
|
|
|
->push_bytes(sha256($self->to_serialized)); |
|
559
|
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
return $program; |
|
561
|
|
|
|
|
|
|
} |
|
562
|
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
signature_for get_legacy_address => ( |
|
564
|
|
|
|
|
|
|
method => Object, |
|
565
|
|
|
|
|
|
|
positional => [], |
|
566
|
|
|
|
|
|
|
); |
|
567
|
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
sub get_legacy_address |
|
569
|
|
|
|
|
|
|
{ |
|
570
|
|
|
|
|
|
|
my ($self) = @_; |
|
571
|
|
|
|
|
|
|
return encode_base58check($self->network->p2sh_byte . $self->get_hash); |
|
572
|
|
|
|
|
|
|
} |
|
573
|
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
signature_for get_compat_address => ( |
|
575
|
|
|
|
|
|
|
method => Object, |
|
576
|
|
|
|
|
|
|
positional => [], |
|
577
|
|
|
|
|
|
|
); |
|
578
|
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
sub get_compat_address |
|
580
|
|
|
|
|
|
|
{ |
|
581
|
|
|
|
|
|
|
my ($self) = @_; |
|
582
|
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
# network field is not required, lazy check for completeness |
|
584
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::NetworkConfig->raise( |
|
585
|
|
|
|
|
|
|
'this network does not support segregated witness' |
|
586
|
|
|
|
|
|
|
) unless $self->network->supports_segwit; |
|
587
|
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
return $self->witness_program->get_legacy_address; |
|
589
|
|
|
|
|
|
|
} |
|
590
|
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
signature_for get_segwit_address => ( |
|
592
|
|
|
|
|
|
|
method => Object, |
|
593
|
|
|
|
|
|
|
positional => [], |
|
594
|
|
|
|
|
|
|
); |
|
595
|
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
sub get_segwit_address |
|
597
|
|
|
|
|
|
|
{ |
|
598
|
|
|
|
|
|
|
my ($self) = @_; |
|
599
|
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
# network field is not required, lazy check for completeness |
|
601
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::NetworkConfig->raise( |
|
602
|
|
|
|
|
|
|
'this network does not support segregated witness' |
|
603
|
|
|
|
|
|
|
) unless $self->network->supports_segwit; |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
return encode_segwit($self->network->segwit_hrp, $self->witness_program->run->stack_serialized); |
|
606
|
|
|
|
|
|
|
} |
|
607
|
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
signature_for get_address => ( |
|
609
|
|
|
|
|
|
|
method => Object, |
|
610
|
|
|
|
|
|
|
positional => [], |
|
611
|
|
|
|
|
|
|
); |
|
612
|
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
sub get_address |
|
614
|
|
|
|
|
|
|
{ |
|
615
|
|
|
|
|
|
|
my ($self) = @_; |
|
616
|
|
|
|
|
|
|
my $address = $self->_address; |
|
617
|
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
return undef |
|
619
|
|
|
|
|
|
|
unless $self->has_type && defined $address; |
|
620
|
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
if ($self->is_native_segwit) { |
|
622
|
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
# network field is not required, lazy check for completeness |
|
624
|
|
|
|
|
|
|
Bitcoin::Crypto::Exception::NetworkConfig->raise( |
|
625
|
|
|
|
|
|
|
'this network does not support segregated witness' |
|
626
|
|
|
|
|
|
|
) unless $self->network->supports_segwit; |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
my $version = pack 'C', Bitcoin::Crypto::Constants::segwit_witness_version; |
|
629
|
|
|
|
|
|
|
return encode_segwit($self->network->segwit_hrp, $version . $address); |
|
630
|
|
|
|
|
|
|
} |
|
631
|
|
|
|
|
|
|
elsif ($self->type eq 'P2PKH') { |
|
632
|
|
|
|
|
|
|
return encode_base58check($self->network->p2pkh_byte . $address); |
|
633
|
|
|
|
|
|
|
} |
|
634
|
|
|
|
|
|
|
elsif ($self->type eq 'P2SH') { |
|
635
|
|
|
|
|
|
|
return encode_base58check($self->network->p2sh_byte . $address); |
|
636
|
|
|
|
|
|
|
} |
|
637
|
|
|
|
|
|
|
elsif ($self->type eq 'NULLDATA') { |
|
638
|
|
|
|
|
|
|
return qq("$address"); |
|
639
|
|
|
|
|
|
|
} |
|
640
|
|
|
|
|
|
|
} |
|
641
|
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
signature_for has_type => ( |
|
643
|
|
|
|
|
|
|
method => Object, |
|
644
|
|
|
|
|
|
|
positional => [], |
|
645
|
|
|
|
|
|
|
); |
|
646
|
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
sub has_type |
|
648
|
|
|
|
|
|
|
{ |
|
649
|
|
|
|
|
|
|
my ($self) = @_; |
|
650
|
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
return defined $self->type; |
|
652
|
|
|
|
|
|
|
} |
|
653
|
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
signature_for is_empty => ( |
|
655
|
|
|
|
|
|
|
method => Object, |
|
656
|
|
|
|
|
|
|
positional => [], |
|
657
|
|
|
|
|
|
|
); |
|
658
|
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
sub is_empty |
|
660
|
|
|
|
|
|
|
{ |
|
661
|
|
|
|
|
|
|
my ($self) = @_; |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
return length $self->_serialized == 0; |
|
664
|
|
|
|
|
|
|
} |
|
665
|
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
1; |
|
667
|
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
__END__ |
|
669
|
|
|
|
|
|
|
=head1 NAME |
|
670
|
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
Bitcoin::Crypto::Script - Bitcoin script instance |
|
672
|
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
674
|
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
use Bitcoin::Crypto::Script; |
|
676
|
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
my $script = Bitcoin::Crypto::Script->from_standard( |
|
678
|
|
|
|
|
|
|
[P2WPKH => $my_segwit_address] |
|
679
|
|
|
|
|
|
|
); |
|
680
|
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
# getting serialized script |
|
682
|
|
|
|
|
|
|
my $serialized = $script->to_serialized(); |
|
683
|
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
# getting P2WSH address from script |
|
685
|
|
|
|
|
|
|
my $sh_address = $script->get_segwit_adress(); |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
# getting back the address encoded in P2WPKH script |
|
688
|
|
|
|
|
|
|
my $address = $script->get_address(); |
|
689
|
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
692
|
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
This class allows you to create Perl representation of a Bitcoin script. |
|
694
|
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
You can use a script object to: |
|
696
|
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
=over 2 |
|
698
|
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
=item * create a script from opcodes |
|
700
|
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
=item * serialize a script into byte string |
|
702
|
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
=item * deserialize a script into a sequence of opcodes |
|
704
|
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
=item * create legacy (p2sh), compat (p2sh(p2wsh)) and segwit (p2wsh) adresses |
|
706
|
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
=item * execute the script |
|
708
|
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
=back |
|
710
|
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
712
|
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
=head2 type |
|
714
|
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
Contains the type of the script, if the script is standard and the type is |
|
716
|
|
|
|
|
|
|
known. Otherwise, contains C<undef>. |
|
717
|
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
I<predicate>: C<has_type> |
|
719
|
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
=head1 METHODS |
|
721
|
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
=head2 new |
|
723
|
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
$script_object = $class->new() |
|
725
|
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
A constructor. Returns a new empty script instance. |
|
727
|
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
See L</from_serialized> if you want to import a serialized script instead. |
|
729
|
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
=head2 operations |
|
731
|
|
|
|
|
|
|
|
|
732
|
|
|
|
|
|
|
$ops_aref = $object->operations |
|
733
|
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
Returns an array reference of operations contained in a script: |
|
735
|
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
[ |
|
737
|
|
|
|
|
|
|
[OP_XXX (Object), raw (String), ...], |
|
738
|
|
|
|
|
|
|
... |
|
739
|
|
|
|
|
|
|
] |
|
740
|
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
The first element of each subarray is the L<Bitcoin::Crypto::Script::Opcode> |
|
742
|
|
|
|
|
|
|
object. The second element is the raw opcode string, usually single byte. The |
|
743
|
|
|
|
|
|
|
rest of elements are metadata and is dependant on the op type. This metadata is |
|
744
|
|
|
|
|
|
|
used during script execution. |
|
745
|
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
=head2 add_operation, add |
|
747
|
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
$script_object = $object->add_operation($opcode) |
|
749
|
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
Adds a new opcode at the end of a script. Returns the object instance for chaining. |
|
751
|
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
C<add> is a shorter alias for C<add_operation>. |
|
753
|
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
Throws an exception for unknown opcodes. |
|
755
|
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
=head2 add_raw |
|
757
|
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
$script_object = $object->add_raw($bytes) |
|
759
|
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
Adds C<$bytes> at the end of the script without processing them at all. |
|
761
|
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
Returns the object instance for chaining. |
|
763
|
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
=head2 push_bytes, push |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
$script_object = $object->push_bytes($bytes) |
|
767
|
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
Pushes C<$bytes> to the execution stack at the end of a script, using a minimal push opcode. |
|
769
|
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
C<push> is a shorter alias for C<push_bytes>. |
|
771
|
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
For example, running C<< $script->push_bytes("\x03") >> will have the same |
|
773
|
|
|
|
|
|
|
effect as C<< $script->add_operation('OP_3') >>. |
|
774
|
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
Throws an exception for data exceeding a 4 byte number in length. |
|
776
|
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
Note that no data longer than 520 bytes can be pushed onto the stack in one |
|
778
|
|
|
|
|
|
|
operation, but this method will not check for that. |
|
779
|
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
Returns the object instance for chaining. |
|
781
|
|
|
|
|
|
|
|
|
782
|
|
|
|
|
|
|
=head2 to_serialized |
|
783
|
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
$bytestring = $object->to_serialized() |
|
785
|
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
Returns a serialized script as byte string. |
|
787
|
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
=head2 from_serialized |
|
789
|
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
$script = Bitcoin::Crypto::Script->from_serialized($bytestring); |
|
791
|
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
Creates a new script instance from a bytestring. |
|
793
|
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
=head2 from_standard |
|
795
|
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
$object = Bitcoin::Crypto::Script->from_standard([P2PKH => '1Ehr6cNDzPCx3wQRu1sMdXWViEi2MQnFzH']) |
|
797
|
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
Creates a new object of standard type with given address. The address must |
|
799
|
|
|
|
|
|
|
match the network specified for the script (see L</set_network>). |
|
800
|
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
=head2 get_hash |
|
802
|
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
$bytestring = $object->get_hash() |
|
804
|
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
Returns a serialized script parsed with C<HASH160> (ripemd160 of sha256). |
|
806
|
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
=head2 set_network |
|
808
|
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
$script_object = $object->set_network($val) |
|
810
|
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
Change key's network state to C<$val>. It can be either network name present in |
|
812
|
|
|
|
|
|
|
L<Bitcoin::Crypto::Network> package or an instance of this class. |
|
813
|
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
Returns current object instance. |
|
815
|
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
=head2 get_legacy_address |
|
817
|
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
$address = $object->get_legacy_address() |
|
819
|
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
Returns string containing Base58Check encoded script hash (P2SH address) |
|
821
|
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
=head2 get_compat_address |
|
823
|
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
$address = $object->get_compat_address() |
|
825
|
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
Returns string containing Base58Check encoded script hash containing a witness |
|
827
|
|
|
|
|
|
|
program for compatibility purposes (P2SH(P2WSH) address) |
|
828
|
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
=head2 get_segwit_address |
|
830
|
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
$address = $object->get_segwit_address() |
|
832
|
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
Returns string containing Bech32 encoded witness program (P2WSH address) |
|
834
|
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
=head2 get_address |
|
836
|
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
$address = $object->get_address() |
|
838
|
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
This method does not return P2SH address, but instead the address encoded in |
|
840
|
|
|
|
|
|
|
the script of standard type. For example, if the script is of type C<P2WPKH>, |
|
841
|
|
|
|
|
|
|
then the contained alegacy address will be returned. If the script is not of |
|
842
|
|
|
|
|
|
|
standard type or the type does not contain an address, returns C<undef>. |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
Currently handles script of types C<P2PKH>, C<P2SH>, C<P2WPKH>, C<P2WSH>. |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
=head2 run |
|
847
|
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
$runner = $object->run(\@initial_stack) |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
Executes the script and returns L<Bitcoin::Crypto::Script::Runner> instance |
|
851
|
|
|
|
|
|
|
after running the script. |
|
852
|
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
This is a convenience method which constructs runner instance in the |
|
854
|
|
|
|
|
|
|
background. This helper is only meant to run simple scripts. |
|
855
|
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
=head2 is_native_segwit |
|
857
|
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
$boolean = $object->is_native_segwit |
|
859
|
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
Returns true if the type of the script is either C<P2WPKH> or C<P2WSH>. |
|
861
|
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
=head2 is_empty |
|
863
|
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
$boolean = $object->is_empty |
|
865
|
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
Returns true if the script is completely empty (contains no opcodes). |
|
867
|
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
=head2 is_pushes_only |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
$boolean = $object->is_pushes_only |
|
871
|
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
Returns true if the script contains only opcodes pushing to the stack. |
|
873
|
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
=head1 EXCEPTIONS |
|
875
|
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
This module throws an instance of L<Bitcoin::Crypto::Exception> if it |
|
877
|
|
|
|
|
|
|
encounters an error. It can produce the following error types from the |
|
878
|
|
|
|
|
|
|
L<Bitcoin::Crypto::Exception> namespace: |
|
879
|
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
=over 2 |
|
881
|
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
=item * ScriptOpcode - unknown opcode was specified |
|
883
|
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
=item * ScriptPush - data pushed to the execution stack is invalid |
|
885
|
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
=item * ScriptType - invalid standard script type name specified |
|
887
|
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
=item * ScriptSyntax - script syntax is invalid |
|
889
|
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
=item * ScriptRuntime - script runtime error |
|
891
|
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
=item * SegwitProgram - Segregated witness address error |
|
893
|
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
=item * NetworkConfig - incomplete or corrupted network configuration |
|
895
|
|
|
|
|
|
|
|
|
896
|
|
|
|
|
|
|
=item * NetworkCheck - address does not belong to the configured network |
|
897
|
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
=back |
|
899
|
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
901
|
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
=over |
|
903
|
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
=item L<Bitcoin::Crypto::Script::Runner> |
|
905
|
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
=item L<Bitcoin::Crypto::Script::Opcode> |
|
907
|
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
=item L<Bitcoin::Crypto::Transaction> |
|
909
|
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
=back |
|
911
|
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
=cut |
|
913
|
|
|
|
|
|
|
|