Skip to content

Add stripWitness to Transaction Issue 2169 #2170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading