Skip to content
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

feat: initEccLib skip verification (v6) #2184

Merged
merged 4 commits into from
Dec 3, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 6.1.7
__added__
- skip ecc library verification via DANGER_DO_NOT_VERIFY_ECCLIB flag

# 6.1.6
__fixed__
- Fix sighash treatment when signing taproot script sign scripts using Psbt (#2104)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bitcoinjs-lib",
"version": "6.1.6",
"version": "6.1.7",
"description": "Client-side Bitcoin JavaScript library",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/ecc_lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { TinySecp256k1Interface } from './types';
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
*
* @param eccLib The instance of the ECC library to initialize.
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
*/
export declare function initEccLib(eccLib: TinySecp256k1Interface | undefined): void;
export declare function initEccLib(eccLib: TinySecp256k1Interface | undefined, opts?: {
DANGER_DO_NOT_VERIFY_ECCLIB: boolean;
}): void;
/**
* Retrieves the ECC Library instance.
* Throws an error if the ECC Library is not provided.
Expand Down
8 changes: 5 additions & 3 deletions src/ecc_lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ const _ECCLIB_CACHE = {};
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
*
* @param eccLib The instance of the ECC library to initialize.
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
*/
function initEccLib(eccLib) {
function initEccLib(eccLib, opts) {
if (!eccLib) {
// allow clearing the library
_ECCLIB_CACHE.eccLib = eccLib;
} else if (eccLib !== _ECCLIB_CACHE.eccLib) {
// new instance, verify it
verifyEcc(eccLib);
if (!opts?.DANGER_DO_NOT_VERIFY_ECCLIB)
// new instance, verify it
verifyEcc(eccLib);
_ECCLIB_CACHE.eccLib = eccLib;
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/ecc_lib.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { initEccLib } from '../src';
import { describe, test } from 'mocha';
import * as assert from 'assert';

describe(`initEccLib`, () => {
beforeEach(() => {
initEccLib(undefined);
});

test('initEccLib should fail when invalid', () => {
assert.throws(() => {
initEccLib({ isXOnlyPoint: () => false } as any);
}, 'Error: ecc library invalid');
});

test('initEccLib should not fail when DANGER_DO_NOT_VERIFY_ECCLIB = true', () => {
initEccLib({ isXOnlyPoint: () => false } as any, {
DANGER_DO_NOT_VERIFY_ECCLIB: true,
});
assert.ok('it does not fail, verification is excluded');
});
});
11 changes: 8 additions & 3 deletions ts_src/ecc_lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ const _ECCLIB_CACHE: { eccLib?: TinySecp256k1Interface } = {};
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
*
* @param eccLib The instance of the ECC library to initialize.
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
*/
export function initEccLib(eccLib: TinySecp256k1Interface | undefined): void {
export function initEccLib(
eccLib: TinySecp256k1Interface | undefined,
opts?: { DANGER_DO_NOT_VERIFY_ECCLIB: boolean },
): void {
if (!eccLib) {
// allow clearing the library
_ECCLIB_CACHE.eccLib = eccLib;
} else if (eccLib !== _ECCLIB_CACHE.eccLib) {
// new instance, verify it
verifyEcc(eccLib!);
if (!opts?.DANGER_DO_NOT_VERIFY_ECCLIB)
// new instance, verify it
verifyEcc(eccLib!);
_ECCLIB_CACHE.eccLib = eccLib;
}
}
Expand Down
Loading