Skip to content

Commit

Permalink
Merge pull request #2170 from JacksonDMiller/JacksonDMiller-addStripW…
Browse files Browse the repository at this point in the history
…itnessToTransactionMaster

Add stripWitness to Transaction Issue 2169
  • Loading branch information
junderw authored Sep 30, 2024
2 parents 34e1644 + 1538f8c commit 32e08aa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cjs/transaction.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/cjs/transaction.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/esm/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions test/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.strictEqual(T.hasWitnesses(), false);
});
});
});

describe('weight/virtualSize', () => {
it('computes virtual size', () => {
fixtures.valid.forEach(f => {
Expand Down
6 changes: 6 additions & 0 deletions ts_src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 32e08aa

Please sign in to comment.