4
4
* @_subsection : api/crypto:Signing [about-signing]
5
5
*/
6
6
7
- import * as secp256k1 from "@noble/secp256k1" ;
7
+ import { secp256k1 } from "@noble/curves /secp256k1" ;
8
8
9
9
import {
10
10
concat , dataLength , getBytes , getBytesCopy , hexlify , toBeHex ,
11
11
assertArgument
12
12
} from "../utils/index.js" ;
13
13
14
- import { computeHmac } from "./hmac.js" ;
15
14
import { Signature } from "./signature.js" ;
16
15
17
16
import type { BytesLike } from "../utils/index.js" ;
18
17
19
18
import type { SignatureLike } from "./index.js" ;
20
19
21
20
22
- //const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
23
-
24
- // Make noble-secp256k1 sync
25
- secp256k1 . utils . hmacSha256Sync = function ( key : Uint8Array , ...messages : Array < Uint8Array > ) : Uint8Array {
26
- return getBytes ( computeHmac ( "sha256" , key , concat ( messages ) ) ) ;
27
- }
28
-
29
21
/**
30
22
* A **SigningKey** provides high-level access to the elliptic curve
31
23
* cryptography (ECC) operations and key management.
@@ -69,16 +61,14 @@ export class SigningKey {
69
61
sign ( digest : BytesLike ) : Signature {
70
62
assertArgument ( dataLength ( digest ) === 32 , "invalid digest length" , "digest" , digest ) ;
71
63
72
- const [ sigDer , recid ] = secp256k1 . signSync ( getBytesCopy ( digest ) , getBytesCopy ( this . #privateKey) , {
73
- recovered : true ,
74
- canonical : true
64
+ const sig = secp256k1 . sign ( getBytesCopy ( digest ) , getBytesCopy ( this . #privateKey) , {
65
+ lowS : true
75
66
} ) ;
76
67
77
- const sig = secp256k1 . Signature . fromHex ( sigDer ) ;
78
68
return Signature . from ( {
79
- r : toBeHex ( "0x" + sig . r . toString ( 16 ) , 32 ) ,
80
- s : toBeHex ( "0x" + sig . s . toString ( 16 ) , 32 ) ,
81
- v : ( recid ? 0x1c : 0x1b )
69
+ r : toBeHex ( sig . r , 32 ) ,
70
+ s : toBeHex ( sig . s , 32 ) ,
71
+ v : ( sig . recovery ? 0x1c : 0x1b )
82
72
} ) ;
83
73
}
84
74
@@ -106,7 +96,7 @@ export class SigningKey {
106
96
*/
107
97
computeSharedSecret ( other : BytesLike ) : string {
108
98
const pubKey = SigningKey . computePublicKey ( other ) ;
109
- return hexlify ( secp256k1 . getSharedSecret ( getBytesCopy ( this . #privateKey) , getBytes ( pubKey ) ) ) ;
99
+ return hexlify ( secp256k1 . getSharedSecret ( getBytesCopy ( this . #privateKey) , getBytes ( pubKey ) , false ) ) ;
110
100
}
111
101
112
102
/**
@@ -151,7 +141,7 @@ export class SigningKey {
151
141
bytes = pub ;
152
142
}
153
143
154
- const point = secp256k1 . Point . fromHex ( bytes ) ;
144
+ const point = secp256k1 . ProjectivePoint . fromHex ( bytes ) ;
155
145
return hexlify ( point . toRawBytes ( compressed ) ) ;
156
146
}
157
147
@@ -177,12 +167,14 @@ export class SigningKey {
177
167
assertArgument ( dataLength ( digest ) === 32 , "invalid digest length" , "digest" , digest ) ;
178
168
179
169
const sig = Signature . from ( signature ) ;
180
- const der = secp256k1 . Signature . fromCompact ( getBytesCopy ( concat ( [ sig . r , sig . s ] ) ) ) . toDERRawBytes ( ) ;
181
170
182
- const pubKey = secp256k1 . recoverPublicKey ( getBytesCopy ( digest ) , der , sig . yParity ) ;
183
- assertArgument ( pubKey != null , "invalid signature for digest" , "signature" , signature ) ;
171
+ let secpSig = secp256k1 . Signature . fromCompact ( getBytesCopy ( concat ( [ sig . r , sig . s ] ) ) ) ;
172
+ secpSig = secpSig . addRecoveryBit ( sig . yParity ) ;
173
+
174
+ const pubKey = secpSig . recoverPublicKey ( getBytesCopy ( digest ) ) ;
175
+ assertArgument ( pubKey != null , "invalid signautre for digest" , "signature" , signature ) ;
184
176
185
- return hexlify ( pubKey ) ;
177
+ return "0x" + pubKey . toHex ( false ) ;
186
178
}
187
179
188
180
/**
@@ -196,8 +188,8 @@ export class SigningKey {
196
188
* addresses from parent public keys and chain codes.
197
189
*/
198
190
static addPoints ( p0 : BytesLike , p1 : BytesLike , compressed ?: boolean ) : string {
199
- const pub0 = secp256k1 . Point . fromHex ( SigningKey . computePublicKey ( p0 ) . substring ( 2 ) ) ;
200
- const pub1 = secp256k1 . Point . fromHex ( SigningKey . computePublicKey ( p1 ) . substring ( 2 ) ) ;
191
+ const pub0 = secp256k1 . ProjectivePoint . fromHex ( SigningKey . computePublicKey ( p0 ) . substring ( 2 ) ) ;
192
+ const pub1 = secp256k1 . ProjectivePoint . fromHex ( SigningKey . computePublicKey ( p1 ) . substring ( 2 ) ) ;
201
193
return "0x" + pub0 . add ( pub1 ) . toHex ( ! ! compressed )
202
194
}
203
195
}
0 commit comments