Skip to content

Commit 9801dda

Browse files
authored
Merge pull request #5577 from BitGo/WP-3704
fix(root): replace elliptic with noble/secp256k1
2 parents 323a310 + 50a208d commit 9801dda

File tree

21 files changed

+92
-121
lines changed

21 files changed

+92
-121
lines changed

modules/bitgo/test/v2/unit/internal/tssUtils/ecdsa.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,7 @@ describe('TSS Ecdsa Utils:', async function () {
16741674
'!@#$^0c70545b519bb7bbc7195fd4b7d5bfc873bfd38b18596e4b47a05b6a88d552e2e8319cb31e279b99dbe54115a983d35e86679af96d81b7478d1df368f76a8'; // 129 chars
16751675
should(() =>
16761676
ECDSAUtils.EcdsaUtils.validateCommonKeychainPublicKey(commonKeychainWithInvalidCharacters)
1677-
).throwError('Unknown point format');
1677+
).throwError(/^Invalid commonKeychain, error:/);
16781678
});
16791679
});
16801680

modules/sdk-coin-avaxp/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,17 @@
4444
},
4545
"devDependencies": {
4646
"@bitgo/sdk-api": "^1.58.8",
47-
"@bitgo/sdk-test": "^8.0.71",
48-
"@types/elliptic": "^6.4.18"
47+
"@bitgo/sdk-test": "^8.0.71"
4948
},
5049
"dependencies": {
5150
"@bitgo-forks/avalanchejs": "4.1.0-alpha.1",
5251
"@bitgo/sdk-core": "^28.25.0",
5352
"@bitgo/statics": "^51.0.0",
5453
"@bitgo/utxo-lib": "^11.2.2",
54+
"@noble/curves": "1.8.1",
5555
"avalanche": "3.15.3",
5656
"bignumber.js": "^9.0.0",
5757
"create-hash": "^1.2.0",
58-
"elliptic": "^6.6.1",
5958
"ethereumjs-util": "7.1.5",
6059
"lodash": "^4.17.14",
6160
"safe-buffer": "^5.2.1"

modules/sdk-coin-avaxp/src/lib/utils.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { Signature } from 'avalanche/dist/common';
2222
import { Credential } from 'avalanche/dist/common/credentials';
2323
import { NodeIDStringToBuffer } from 'avalanche/dist/utils';
2424
import * as createHash from 'create-hash';
25-
import { ec } from 'elliptic';
25+
import { secp256k1 } from '@noble/curves/secp256k1';
2626
import { ADDRESS_SEPARATOR, DeprecatedOutput, DeprecatedTx, Output } from './iface';
2727

2828
export class Utils implements BaseUtils {
@@ -101,15 +101,12 @@ export class Utils implements BaseUtils {
101101
if (pub.length === 66 && firstByte !== '02' && firstByte !== '03') return false;
102102

103103
if (!this.allHexChars(pub)) return false;
104-
105104
pubBuf = BufferAvax.from(pub, 'hex');
106105
}
107106
// validate the public key
108-
const secp256k1 = new ec('secp256k1');
109107
try {
110-
const keyPair = secp256k1.keyFromPublic(pubBuf);
111-
const { result } = keyPair.validate();
112-
return result;
108+
secp256k1.ProjectivePoint.fromHex(pubBuf.toString('hex'));
109+
return true;
113110
} catch (e) {
114111
return false;
115112
}

modules/sdk-coin-icp/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"@dfinity/agent": "^2.2.0",
4747
"@dfinity/candid": "^2.2.0",
4848
"@dfinity/principal": "^2.2.0",
49-
"crc-32": "^1.2.2",
50-
"elliptic": "^6.6.1"
49+
"@noble/curves": "1.8.1",
50+
"crc-32": "^1.2.2"
5151
},
5252
"devDependencies": {
5353
"@bitgo/sdk-api": "^1.58.8",

modules/sdk-coin-icp/src/lib/utils.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { BaseUtils, KeyPair } from '@bitgo/sdk-core';
2-
import elliptic from 'elliptic';
2+
import { secp256k1 } from '@noble/curves/secp256k1';
33
import { Principal as DfinityPrincipal } from '@dfinity/principal';
44
import * as agent from '@dfinity/agent';
55
import crypto from 'crypto';
66
import crc32 from 'crc-32';
77
import { KeyPair as IcpKeyPair } from './keyPair';
88

9-
const Secp256k1Curve = new elliptic.ec('secp256k1');
10-
119
export class Utils implements BaseUtils {
1210
isValidAddress(address: string): boolean {
1311
throw new Error('Method not implemented.');
@@ -102,11 +100,9 @@ export class Utils implements BaseUtils {
102100
}
103101

104102
derivePrincipalFromPublicKey(publicKeyHex: string): DfinityPrincipal {
105-
const publicKeyBuffer = Buffer.from(publicKeyHex, 'hex');
106-
107103
try {
108-
const ellipticKey = Secp256k1Curve.keyFromPublic(publicKeyBuffer);
109-
const uncompressedPublicKeyHex = ellipticKey.getPublic(false, 'hex');
104+
const point = secp256k1.ProjectivePoint.fromHex(publicKeyHex);
105+
const uncompressedPublicKeyHex = point.toHex(false);
110106
const derEncodedKey = agent.wrapDER(Buffer.from(uncompressedPublicKeyHex, 'hex'), agent.SECP256K1_OID);
111107
const principalId = DfinityPrincipal.selfAuthenticating(Buffer.from(derEncodedKey));
112108
const principal = DfinityPrincipal.fromUint8Array(principalId.toUint8Array());

modules/sdk-coin-stx/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,17 @@
4343
"@bitgo/sdk-core": "^28.25.0",
4444
"@bitgo/statics": "^51.0.0",
4545
"@bitgo/utxo-lib": "^11.2.2",
46+
"@noble/curves": "1.8.1",
4647
"@stacks/network": "^4.3.0",
4748
"@stacks/transactions": "2.0.1",
4849
"bignumber.js": "^9.0.0",
4950
"bn.js": "^5.2.1",
50-
"elliptic": "^6.6.1",
5151
"ethereumjs-util": "7.1.5",
5252
"lodash": "^4.17.15"
5353
},
5454
"devDependencies": {
5555
"@bitgo/sdk-api": "^1.58.8",
56-
"@bitgo/sdk-test": "^8.0.71",
57-
"@types/elliptic": "^6.4.18"
56+
"@bitgo/sdk-test": "^8.0.71"
5857
},
5958
"gitHead": "18e460ddf02de2dbf13c2aa243478188fb539f0c"
6059
}

modules/sdk-coin-stx/src/lib/utils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
TransactionVersion,
2727
validateStacksAddress,
2828
} from '@stacks/transactions';
29-
import { ec } from 'elliptic';
29+
import { secp256k1 } from '@noble/curves/secp256k1';
3030
import * as _ from 'lodash';
3131
import { InvalidTransactionError, isValidXprv, isValidXpub, SigningError, UtilsError } from '@bitgo/sdk-core';
3232
import { AddressDetails, SendParams } from './iface';
@@ -157,11 +157,9 @@ export function isValidPublicKey(pub: string): boolean {
157157
if (!allHexChars(pub)) return false;
158158

159159
// validate the public key
160-
const secp256k1 = new ec('secp256k1');
161160
try {
162-
const keyPair = secp256k1.keyFromPublic(Buffer.from(pub, 'hex'));
163-
const { result } = keyPair.validate();
164-
return result;
161+
secp256k1.ProjectivePoint.fromHex(pub);
162+
return true;
165163
} catch (e) {
166164
return false;
167165
}

modules/sdk-coin-xtz/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
"@bitgo/sdk-core": "^28.25.0",
4545
"@bitgo/statics": "^51.0.0",
4646
"@bitgo/utxo-lib": "^11.2.2",
47+
"@noble/curves": "1.8.1",
4748
"@taquito/local-forging": "6.3.5-beta.0",
4849
"@taquito/signer": "6.3.5-beta.0",
4950
"bignumber.js": "^9.0.0",
5051
"bs58check": "^2.1.2",
51-
"elliptic": "^6.6.1",
5252
"libsodium-wrappers": "^0.7.6",
5353
"lodash": "^4.17.15"
5454
},

modules/sdk-coin-xtz/src/lib/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isValidXpub, SigningError } from '@bitgo/sdk-core';
22
import { InMemorySigner } from '@taquito/signer';
33
import * as base58check from 'bs58check';
4-
import { ec as EC } from 'elliptic';
4+
import { secp256k1 } from '@noble/curves/secp256k1';
55
import sodium from 'libsodium-wrappers';
66
import { HashType, SignResponse } from './iface';
77
import { KeyPair } from './keyPair';
@@ -101,9 +101,8 @@ export async function verifySignature(
101101
signature: string,
102102
watermark: Uint8Array = DEFAULT_WATERMARK
103103
): Promise<boolean> {
104-
const rawPublicKey = decodeKey(publicKey, hashTypes.sppk);
105-
const ec = new EC('secp256k1');
106-
const key = ec.keyFromPublic(rawPublicKey);
104+
const rawPublicKey = decodeKey(publicKey, hashTypes.sppk).toString('hex');
105+
const key = secp256k1.ProjectivePoint.fromHex(rawPublicKey);
107106

108107
const messageBuffer = Uint8Array.from(Buffer.from(message, 'hex'));
109108
// Tezos signatures always have a watermark
@@ -115,7 +114,7 @@ export async function verifySignature(
115114
const bytesHash = Buffer.from(sodium.crypto_generichash(32, messageWithWatermark));
116115

117116
const rawSignature = decodeSignature(signature, hashTypes.sig);
118-
return key.verify(bytesHash, { r: rawSignature.slice(0, 32), s: rawSignature.slice(32, 64) });
117+
return secp256k1.verify(rawSignature, bytesHash, key.toHex());
119118
}
120119

121120
/**

modules/sdk-core/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
"@bitgo/sjcl": "^1.0.1",
4444
"@bitgo/statics": "^51.0.0",
4545
"@bitgo/utxo-lib": "^11.2.2",
46-
"@noble/secp256k1": "1.6.3",
46+
"@noble/curves": "1.8.1",
4747
"@stablelib/hex": "^1.0.0",
48-
"@types/elliptic": "^6.4.18",
4948
"@types/superagent": "4.1.15",
5049
"bech32": "^2.0.0",
5150
"big.js": "^3.1.3",
@@ -56,7 +55,6 @@
5655
"bs58": "^4.0.1",
5756
"create-hmac": "^1.1.7",
5857
"debug": "^3.1.0",
59-
"elliptic": "^6.6.1",
6058
"ethereumjs-util": "7.1.5",
6159
"fp-ts": "^2.12.2",
6260
"io-ts": "npm:@bitgo-forks/[email protected]",

0 commit comments

Comments
 (0)