Skip to content

Commit

Permalink
fix empty segwit deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnetwork committed Oct 28, 2024
1 parent 3b7db06 commit 7929af9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
41 changes: 24 additions & 17 deletions lib/src/bitcoin/script/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class BtcTransaction {
}
cursor += 2;
}
final vi = IntUtils.decodeVarint(rawtx.sublist(cursor, cursor + 9));
final vi = IntUtils.decodeVarint(rawtx.sublist(cursor));
cursor += vi.item2;

List<TxInput> inputs = [];
Expand All @@ -96,7 +96,7 @@ class BtcTransaction {
}

List<TxOutput> outputs = [];
final viOut = IntUtils.decodeVarint(rawtx.sublist(cursor, cursor + 9));
final viOut = IntUtils.decodeVarint(rawtx.sublist(cursor));
cursor += viOut.item2;
for (int index = 0; index < viOut.item1; index++) {
final inp =
Expand All @@ -106,25 +106,32 @@ class BtcTransaction {
}
List<TxWitnessInput> witnesses = [];
if (hasSegwit) {
for (int n = 0; n < inputs.length; n++) {
final wVi = IntUtils.decodeVarint(rawtx.sublist(cursor, cursor + 9));
cursor += wVi.item2;
List<String> witnessesTmp = [];
for (int n = 0; n < wVi.item1; n++) {
List<int> witness = <int>[];
final wtVi = IntUtils.decodeVarint(rawtx.sublist(cursor, cursor + 9));
if (wtVi.item1 != 0) {
witness = rawtx.sublist(
cursor + wtVi.item2, cursor + wtVi.item1 + wtVi.item2);
if (cursor + 4 < rawtx.length) {
// in this case the tx contains wintness data.
for (int n = 0; n < inputs.length; n++) {
final wVi = IntUtils.decodeVarint(rawtx.sublist(cursor));
cursor += wVi.item2;
List<String> witnessesTmp = [];
for (int n = 0; n < wVi.item1; n++) {
List<int> witness = <int>[];
final wtVi = IntUtils.decodeVarint(rawtx.sublist(cursor));
if (wtVi.item1 != 0) {
witness = rawtx.sublist(
cursor + wtVi.item2, cursor + wtVi.item1 + wtVi.item2);
}
cursor += wtVi.item1 + wtVi.item2;
witnessesTmp.add(BytesUtils.toHexString(witness));
}
cursor += wtVi.item1 + wtVi.item2;
witnessesTmp.add(BytesUtils.toHexString(witness));
}

witnesses.add(TxWitnessInput(stack: witnessesTmp));
witnesses.add(TxWitnessInput(stack: witnessesTmp));
}
}
}
List<int> lock = rawtx.sublist(cursor, cursor + 4);
List<int>? lock;
if ((rawtx.length - cursor) >= 4) {
lock = rawtx.sublist(cursor, cursor + 4);
}
print("lock $lock");
return BtcTransaction(
inputs: inputs,
outputs: outputs,
Expand Down
6 changes: 6 additions & 0 deletions test/p2wsh_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:test/test.dart';

void main() {
final sx =
"020000000001019361bb41042ba1b84bfd0adf549fd71d0100d523c96f235c0303a820ebe97b6f00000000000100000002f3e0010000000000225120eeef21c790e883138874de40105c2900e95d03b5f2964f6164afde5dfacec01d33e4e902000000001600142d8db3142cf97e996977fed9aec74021395c036900000000";
final tx = BtcTransaction.fromRaw(sx);
print(tx.inputs.length);
print(tx.serialize() == sx);
return;
group("P2WSH", () {
final sk1 = ECPrivate.fromWif(
"cTALNpTpRbbxTCJ2A5Vq88UxT44w1PE2cYqiB3n4hRvzyCev1Wwo",
Expand Down

0 comments on commit 7929af9

Please sign in to comment.