Skip to content

Commit b89a5e3

Browse files
committed
Merge branch 'master' of github.com:bitcoinjs/bitcoinjs-lib
2 parents 34e1644 + 68ed198 commit b89a5e3

File tree

11 files changed

+75
-10
lines changed

11 files changed

+75
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 6.1.7
2+
__added__
3+
- skip ecc library verification via DANGER_DO_NOT_VERIFY_ECCLIB flag
4+
15
# 6.1.6
26
__fixed__
37
- Fix sighash treatment when signing taproot script sign scripts using Psbt (#2104)

src/cjs/ecc_lib.cjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ const _ECCLIB_CACHE = {};
5454
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
5555
*
5656
* @param eccLib The instance of the ECC library to initialize.
57+
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
5758
*/
58-
function initEccLib(eccLib) {
59+
function initEccLib(eccLib, opts) {
5960
if (!eccLib) {
6061
// allow clearing the library
6162
_ECCLIB_CACHE.eccLib = eccLib;
6263
} else if (eccLib !== _ECCLIB_CACHE.eccLib) {
63-
// new instance, verify it
64-
verifyEcc(eccLib);
64+
if (!opts?.DANGER_DO_NOT_VERIFY_ECCLIB)
65+
// new instance, verify it
66+
verifyEcc(eccLib);
6567
_ECCLIB_CACHE.eccLib = eccLib;
6668
}
6769
}

src/cjs/ecc_lib.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import { TinySecp256k1Interface } from './types.js';
55
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
66
*
77
* @param eccLib The instance of the ECC library to initialize.
8+
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
89
*/
9-
export declare function initEccLib(eccLib: TinySecp256k1Interface | undefined): void;
10+
export declare function initEccLib(eccLib: TinySecp256k1Interface | undefined, opts?: {
11+
DANGER_DO_NOT_VERIFY_ECCLIB: boolean;
12+
}): void;
1013
/**
1114
* Retrieves the ECC Library instance.
1215
* Throws an error if the ECC Library is not provided.

src/cjs/transaction.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ class Transaction {
203203
return x.witness.length !== 0;
204204
});
205205
}
206+
stripWitnesses() {
207+
this.ins.forEach(input => {
208+
input.witness = EMPTY_WITNESS; // Set witness data to an empty array
209+
});
210+
}
206211
weight() {
207212
const base = this.byteLength(false);
208213
const total = this.byteLength(true);

src/cjs/transaction.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export declare class Transaction {
3434
addInput(hash: Uint8Array, index: number, sequence?: number, scriptSig?: Uint8Array): number;
3535
addOutput(scriptPubKey: Uint8Array, value: bigint): number;
3636
hasWitnesses(): boolean;
37+
stripWitnesses(): void;
3738
weight(): number;
3839
virtualSize(): number;
3940
byteLength(_ALLOW_WITNESS?: boolean): number;

src/esm/ecc_lib.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ const _ECCLIB_CACHE = {};
66
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
77
*
88
* @param eccLib The instance of the ECC library to initialize.
9+
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
910
*/
10-
export function initEccLib(eccLib) {
11+
export function initEccLib(eccLib, opts) {
1112
if (!eccLib) {
1213
// allow clearing the library
1314
_ECCLIB_CACHE.eccLib = eccLib;
1415
} else if (eccLib !== _ECCLIB_CACHE.eccLib) {
15-
// new instance, verify it
16-
verifyEcc(eccLib);
16+
if (!opts?.DANGER_DO_NOT_VERIFY_ECCLIB)
17+
// new instance, verify it
18+
verifyEcc(eccLib);
1719
_ECCLIB_CACHE.eccLib = eccLib;
1820
}
1921
}

src/esm/transaction.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ export class Transaction {
161161
return x.witness.length !== 0;
162162
});
163163
}
164+
stripWitnesses() {
165+
this.ins.forEach(input => {
166+
input.witness = EMPTY_WITNESS; // Set witness data to an empty array
167+
});
168+
}
164169
weight() {
165170
const base = this.byteLength(false);
166171
const total = this.byteLength(true);

test/ecc_lib.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { initEccLib } from 'bitcoinjs-lib';
2+
import { describe, test } from 'mocha';
3+
import * as assert from 'assert';
4+
5+
describe(`initEccLib`, () => {
6+
beforeEach(() => {
7+
initEccLib(undefined);
8+
});
9+
10+
test('initEccLib should fail when invalid', () => {
11+
assert.throws(() => {
12+
initEccLib({ isXOnlyPoint: () => false } as any);
13+
}, 'Error: ecc library invalid');
14+
});
15+
16+
test('initEccLib should not fail when DANGER_DO_NOT_VERIFY_ECCLIB = true', () => {
17+
initEccLib({ isXOnlyPoint: () => false } as any, {
18+
DANGER_DO_NOT_VERIFY_ECCLIB: true,
19+
});
20+
assert.ok('it does not fail, verification is excluded');
21+
});
22+
});

test/transaction.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ describe('Transaction', () => {
135135
});
136136
});
137137

138+
describe('stripWitnesses', () => {
139+
fixtures.valid.forEach(f => {
140+
it('removes witness from the transaction if it exists', () => {
141+
const T = Transaction.fromHex(f.whex ? f.whex : f.hex);
142+
T.stripWitnesses();
143+
assert.strictEqual(T.hasWitnesses(), false);
144+
});
145+
});
146+
});
147+
138148
describe('weight/virtualSize', () => {
139149
it('computes virtual size', () => {
140150
fixtures.valid.forEach(f => {

ts_src/ecc_lib.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ const _ECCLIB_CACHE: { eccLib?: TinySecp256k1Interface } = {};
99
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
1010
*
1111
* @param eccLib The instance of the ECC library to initialize.
12+
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
1213
*/
13-
export function initEccLib(eccLib: TinySecp256k1Interface | undefined): void {
14+
export function initEccLib(
15+
eccLib: TinySecp256k1Interface | undefined,
16+
opts?: { DANGER_DO_NOT_VERIFY_ECCLIB: boolean },
17+
): void {
1418
if (!eccLib) {
1519
// allow clearing the library
1620
_ECCLIB_CACHE.eccLib = eccLib;
1721
} else if (eccLib !== _ECCLIB_CACHE.eccLib) {
18-
// new instance, verify it
19-
verifyEcc(eccLib!);
22+
if (!opts?.DANGER_DO_NOT_VERIFY_ECCLIB)
23+
// new instance, verify it
24+
verifyEcc(eccLib!);
2025
_ECCLIB_CACHE.eccLib = eccLib;
2126
}
2227
}

0 commit comments

Comments
 (0)