@@ -2,8 +2,8 @@ import { sha256 } from "@noble/hashes/sha256";
2
2
import * as secp from "./secp256k1" ;
3
3
import { assertBool , assertBytes , hexToBytes , toHex } from "./utils" ;
4
4
5
- // Legacy compatibility layer for elliptic via noble-secp256k1
6
- // Use `secp256k1` module directly instead
5
+ // Use `secp256k1` module directly.
6
+ // This is a legacy compatibility layer for the npm package `secp256k1` via noble-secp256k1
7
7
8
8
// Copy-paste from secp256k1, maybe export it?
9
9
const bytesToNumber = ( bytes : Uint8Array ) => hexToNumber ( toHex ( bytes ) ) ;
@@ -116,7 +116,6 @@ export function ecdsaSign(
116
116
}
117
117
const [ signature , recid ] = secp . signSync ( msgHash , privateKey , {
118
118
recovered : true ,
119
- canonical : true ,
120
119
der : false
121
120
} ) ;
122
121
return { signature : output ( out , 64 , signature ) , recid } ;
@@ -150,13 +149,14 @@ export function ecdsaVerify(
150
149
if ( r >= ORDER || s >= ORDER ) {
151
150
throw new Error ( "Cannot parse signature" ) ;
152
151
}
152
+ const pub = secp . Point . fromHex ( publicKey ) ; // should not throw error
153
153
let sig ;
154
154
try {
155
155
sig = getSignature ( signature ) ;
156
156
} catch ( error ) {
157
157
return false ;
158
158
}
159
- return secp . verify ( sig , msgHash , publicKey ) ;
159
+ return secp . verify ( sig , msgHash , pub ) ;
160
160
}
161
161
162
162
export function privateKeyTweakAdd (
@@ -234,10 +234,10 @@ export function publicKeyTweakAdd(
234
234
assertBool ( compressed ) ;
235
235
const p1 = secp . Point . fromHex ( publicKey ) ;
236
236
const p2 = secp . Point . fromPrivateKey ( tweak ) ;
237
- if ( p2 . equals ( secp . Point . ZERO ) ) {
237
+ const point = p1 . add ( p2 ) ;
238
+ if ( p2 . equals ( secp . Point . ZERO ) || point . equals ( secp . Point . ZERO ) ) {
238
239
throw new Error ( "Tweak must not be zero" ) ;
239
240
}
240
- const point = p1 . add ( p2 ) ;
241
241
return output ( out , compressed ? 33 : 65 , point . toRawBytes ( compressed ) ) ;
242
242
}
243
243
@@ -254,7 +254,7 @@ export function publicKeyTweakMul(
254
254
if ( bn === 0n ) {
255
255
throw new Error ( "Tweak must not be zero" ) ;
256
256
}
257
- if ( bn <= 0 || bn >= ORDER ) {
257
+ if ( bn <= 1 || bn >= ORDER ) {
258
258
throw new Error ( "Tweak is zero or bigger than curve order" ) ;
259
259
}
260
260
const point = secp . Point . fromHex ( publicKey ) . multiply ( bn ) ;
@@ -267,23 +267,17 @@ export function privateKeyTweakMul(
267
267
) : Uint8Array {
268
268
assertBytes ( privateKey , 32 ) ;
269
269
assertBytes ( tweak , 32 ) ;
270
- let bn = bytesToNumber ( tweak ) ;
271
- if ( bn === 0n ) {
272
- throw new Error ( "Tweak must not be zero" ) ;
273
- }
274
- if ( bn >= ORDER ) {
275
- throw new Error ( "Tweak bigger than curve order" ) ;
276
- }
277
- bn = mod ( bn * bytesToNumber ( privateKey ) , ORDER ) ;
278
- if ( bn >= ORDER ) {
279
- bn -= ORDER ;
270
+ const bn = bytesToNumber ( tweak ) ;
271
+ if ( bn <= 1 || bn >= ORDER ) {
272
+ throw new Error ( "Tweak is zero or bigger than curve order" ) ;
280
273
}
281
- if ( bn === 0n ) {
274
+ const res = mod ( bn * bytesToNumber ( privateKey ) , ORDER ) ;
275
+ if ( res === 0n ) {
282
276
throw new Error (
283
277
"The tweak was out of range or the resulted private key is invalid"
284
278
) ;
285
279
}
286
- privateKey . set ( hexToBytes ( numberToHex ( bn ) ) ) ;
280
+ privateKey . set ( hexToBytes ( numberToHex ( res ) ) ) ;
287
281
return privateKey ;
288
282
}
289
283
// internal -> DER
0 commit comments