Skip to content

Commit 3455ffb

Browse files
committed
Add set_serialized_witness method to Input, publish serialized_witness
1 parent a7e67e7 commit 3455ffb

3 files changed

Lines changed: 69 additions & 10 deletions

File tree

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Revision history for Perl extension Bitcoin::Crypto.
22

33
{{$NEXT}}
4+
[Public interface changes]
5+
- Bitcoin::Crypto::Transaction::Input:
6+
- added serialized_witness method
7+
- added set_serialized_witness method
48

59
4.005 Thu Apr 23, 2026
610
[Public interface changes]

lib/Bitcoin/Crypto/Transaction.pm

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,7 @@ sub from_serialized
219219

220220
if ($witness_flag) {
221221
foreach my $input (@inputs) {
222-
my $input_witness = unpack_compactsize $serialized, \$pos;
223-
my @witness;
224-
for (1 .. $input_witness) {
225-
my $witness_count = unpack_compactsize $serialized, \$pos;
226-
227-
push @witness, substr $serialized, $pos, $witness_count;
228-
$pos += $witness_count;
229-
}
230-
231-
$input->set_witness(\@witness);
222+
$input->set_serialized_witness($serialized, pos => \$pos);
232223
}
233224
}
234225

lib/Bitcoin/Crypto/Transaction/Input.pm

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,43 @@ sub serialized_witness
213213
;
214214
}
215215

216+
signature_for set_serialized_witness => (
217+
method => !!1,
218+
head => [ByteStr],
219+
named => [
220+
pos => Maybe [ScalarRef [PositiveOrZeroInt]],
221+
{default => undef},
222+
],
223+
bless => !!0,
224+
);
225+
226+
sub set_serialized_witness
227+
{
228+
my ($self, $serialized, $args) = @_;
229+
my $partial = !!$args->{pos};
230+
my $pos = $partial ? ${$args->{pos}} : 0;
231+
232+
my $input_witness = unpack_compactsize $serialized, \$pos;
233+
my @witness;
234+
for (1 .. $input_witness) {
235+
my $witness_count = unpack_compactsize $serialized, \$pos;
236+
237+
push @witness, substr $serialized, $pos, $witness_count;
238+
$pos += $witness_count;
239+
}
240+
241+
$self->set_witness(\@witness);
242+
243+
Bitcoin::Crypto::Exception::Transaction->raise(
244+
'serialized witness data is corrupted'
245+
) if !$partial && $pos != length $serialized;
246+
247+
${$args->{pos}} = $pos
248+
if $partial;
249+
250+
return;
251+
}
252+
216253
sub script_base
217254
{
218255
my ($self) = @_;
@@ -431,6 +468,33 @@ start decoding. It will be set to the next byte after end of input stream.
431468
432469
=back
433470
471+
=head3 serialized_witness
472+
473+
$bytestring = $object->serialized_witness()
474+
475+
Serializes witness data of the input as it would appear in the serialized
476+
transaction, and returns it as a bytestring.
477+
478+
=head3 set_serialized_witness
479+
480+
$object->set_serialized_witness($bytestring, %params)
481+
482+
Sets L</witness> to data obtained by deserializing C<$bytestring>.
483+
484+
C<%params> can be any of:
485+
486+
=over
487+
488+
=item * C<pos>
489+
490+
Position for partial string decoding. Optional. If passed, must be a scalar
491+
reference to an integer value.
492+
493+
This integer will mark the starting position of C<$bytestring> from which to
494+
start decoding. It will be set to the next byte after end of input stream.
495+
496+
=back
497+
434498
=head3 is_segwit
435499
436500
$boolean = $object->is_segwit()

0 commit comments

Comments
 (0)