From c4c3dd9cb17e8745e78d9f8711219731ddeb3aeb Mon Sep 17 00:00:00 2001 From: J0J0XMR <163790330+J0J0XMR@users.noreply.github.com> Date: Mon, 9 Dec 2024 08:44:00 -0600 Subject: [PATCH] mrtnetwork: fix empty segwit deserialization https://github.com/mrtnetwork/bitcoin_base/commit/7929af9c44e66e9542aa48915eb08014b8390aae --- lib/src/bitcoin/script/transaction.dart | 37 ++++++++++++++----------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/src/bitcoin/script/transaction.dart b/lib/src/bitcoin/script/transaction.dart index 4e53e71..6759624 100644 --- a/lib/src/bitcoin/script/transaction.dart +++ b/lib/src/bitcoin/script/transaction.dart @@ -95,7 +95,7 @@ class BtcTransaction { } cursor += 2; } - final vi = IntUtils.decodeVarint(rawtx.sublist(cursor, cursor + 9)); + final vi = IntUtils.decodeVarint(rawtx.sublist(cursor)); cursor += vi.item2; bool canReplaceByFee = false; @@ -114,7 +114,7 @@ class BtcTransaction { } List 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 = TxOutput.fromRaw(raw: raw, hasSegwit: hasSegwit, cursor: cursor); @@ -123,24 +123,25 @@ class BtcTransaction { } List witnesses = []; if (hasSegwit) { - for (int n = 0; n < inputs.length; n++) { - final input = inputs[n]; - if (input.scriptSig.script.isNotEmpty) continue; - - final wVi = IntUtils.decodeVarint(rawtx.sublist(cursor, cursor + 9)); - cursor += wVi.item2; - List witnessesTmp = []; - for (int n = 0; n < wVi.item1; n++) { - List witness = []; - 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 witnessesTmp = []; + for (int n = 0; n < wVi.item1; n++) { + List witness = []; + 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)); } witnesses.add(TxWitnessInput(stack: witnessesTmp)); + } } } List? mwebBytes; @@ -148,7 +149,11 @@ class BtcTransaction { mwebBytes = rawtx.sublist(cursor, rawtx.length - 4); } cursor = rawtx.length - 4; - List lock = rawtx.sublist(cursor, cursor + 4); + List? lock; + if ((rawtx.length - cursor) >= 4) { + lock = rawtx.sublist(cursor, cursor + 4); + } + print("lock $lock"); return BtcTransaction( inputs: inputs, outputs: outputs,