From ff76a797a39b497f2c5acc40fa0c252c538d1916 Mon Sep 17 00:00:00 2001 From: Jackson Miller Date: Mon, 30 Sep 2024 13:47:32 +0900 Subject: [PATCH 1/2] first crack at the function and corresponding test --- src/cjs/transaction.cjs | 5 +++++ src/cjs/transaction.d.ts | 1 + src/esm/transaction.js | 5 +++++ test/transaction.spec.ts | 10 ++++++++++ ts_src/transaction.ts | 6 ++++++ 5 files changed, 27 insertions(+) diff --git a/src/cjs/transaction.cjs b/src/cjs/transaction.cjs index f05cf7f02..eee05850f 100644 --- a/src/cjs/transaction.cjs +++ b/src/cjs/transaction.cjs @@ -203,6 +203,11 @@ class Transaction { return x.witness.length !== 0; }); } + stripWitnesses() { + this.ins.forEach(input => { + input.witness = EMPTY_WITNESS; // Set witness data to an empty array + }); + } weight() { const base = this.byteLength(false); const total = this.byteLength(true); diff --git a/src/cjs/transaction.d.ts b/src/cjs/transaction.d.ts index 6f9d61534..047d8cf2c 100644 --- a/src/cjs/transaction.d.ts +++ b/src/cjs/transaction.d.ts @@ -34,6 +34,7 @@ export declare class Transaction { addInput(hash: Uint8Array, index: number, sequence?: number, scriptSig?: Uint8Array): number; addOutput(scriptPubKey: Uint8Array, value: bigint): number; hasWitnesses(): boolean; + stripWitnesses(): void; weight(): number; virtualSize(): number; byteLength(_ALLOW_WITNESS?: boolean): number; diff --git a/src/esm/transaction.js b/src/esm/transaction.js index 3379e0cf0..d1166a540 100644 --- a/src/esm/transaction.js +++ b/src/esm/transaction.js @@ -161,6 +161,11 @@ export class Transaction { return x.witness.length !== 0; }); } + stripWitnesses() { + this.ins.forEach(input => { + input.witness = EMPTY_WITNESS; // Set witness data to an empty array + }); + } weight() { const base = this.byteLength(false); const total = this.byteLength(true); diff --git a/test/transaction.spec.ts b/test/transaction.spec.ts index e4c58cedf..3ea49a7b2 100644 --- a/test/transaction.spec.ts +++ b/test/transaction.spec.ts @@ -135,6 +135,16 @@ describe('Transaction', () => { }); }); + describe('stripWitnesses', () => { + fixtures.valid.forEach(f => { + it('removes witness from the transaction if it exists', () => { + const T = Transaction.fromHex(f.whex ? f.whex : f.hex); + T.stripWitnesses(); + assert.equal(T.hasWitnesses(), false); + }); + }); + }); + describe('weight/virtualSize', () => { it('computes virtual size', () => { fixtures.valid.forEach(f => { diff --git a/ts_src/transaction.ts b/ts_src/transaction.ts index d6e80955d..95e1194c7 100644 --- a/ts_src/transaction.ts +++ b/ts_src/transaction.ts @@ -208,6 +208,12 @@ export class Transaction { }); } + stripWitnesses(): void { + this.ins.forEach(input => { + input.witness = EMPTY_WITNESS; // Set witness data to an empty array + }); + } + weight(): number { const base = this.byteLength(false); const total = this.byteLength(true); From 1538f8c726a7666350f5e977001a2dd9dddbc085 Mon Sep 17 00:00:00 2001 From: Jackson Miller Date: Mon, 30 Sep 2024 13:55:02 +0900 Subject: [PATCH 2/2] Change to match the other tests in the file --- test/transaction.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/transaction.spec.ts b/test/transaction.spec.ts index 3ea49a7b2..08774f6f6 100644 --- a/test/transaction.spec.ts +++ b/test/transaction.spec.ts @@ -140,7 +140,7 @@ describe('Transaction', () => { it('removes witness from the transaction if it exists', () => { const T = Transaction.fromHex(f.whex ? f.whex : f.hex); T.stripWitnesses(); - assert.equal(T.hasWitnesses(), false); + assert.strictEqual(T.hasWitnesses(), false); }); }); });