@@ -11,6 +11,14 @@ if (!secp256k1.utils.sha256Sync) {
11
11
secp256k1 . utils . sha256Sync = ( ...msgs ) => sha256 ( secp256k1 . utils . concatBytes ( ...msgs ) )
12
12
}
13
13
14
+ function readPublicKey ( pubkey ) {
15
+ try {
16
+ return secp256k1 . Point . fromHex ( pubkey )
17
+ } catch ( err ) {
18
+ return undefined
19
+ }
20
+ }
21
+
14
22
function writePublicKey ( output , point ) {
15
23
const buf = point . toRawBytes ( output . length === 33 )
16
24
if ( output . length !== buf . length ) return 1
@@ -76,11 +84,8 @@ module.exports = {
76
84
} ,
77
85
78
86
publicKeyVerify ( pubkey ) {
79
- try {
80
- return secp256k1 . Point . fromHex ( pubkey ) ? 0 : 1
81
- } catch ( err ) {
82
- return 1
83
- }
87
+ const P = readPublicKey ( pubkey )
88
+ return P ? 0 : 1
84
89
} ,
85
90
86
91
publicKeyCreate ( output , seckey ) {
@@ -95,36 +100,23 @@ module.exports = {
95
100
} ,
96
101
97
102
publicKeyConvert ( output , pubkey ) {
98
- try {
99
- const publicKey = secp256k1 . Point . fromHex ( pubkey ) . toRawBytes ( output . length === 33 )
100
- if ( output . length !== publicKey . length ) return 1
101
- output . set ( publicKey )
102
- return 0
103
- } catch ( err ) {
104
- return 1
105
- }
103
+ const P = readPublicKey ( pubkey )
104
+ if ( ! P ) return 1
105
+ return writePublicKey ( output , P )
106
106
} ,
107
107
108
108
publicKeyNegate ( output , pubkey ) {
109
- let P
110
- try {
111
- P = secp256k1 . Point . fromHex ( pubkey )
112
- } catch ( err ) {
113
- return 1
114
- }
115
-
109
+ const P = readPublicKey ( pubkey )
110
+ if ( ! P ) return 1
116
111
const point = P . negate ( )
117
112
return writePublicKey ( output , point )
118
113
} ,
119
114
120
115
publicKeyCombine ( output , pubkeys ) {
121
116
const points = new Array ( pubkeys . length )
122
117
for ( let i = 0 ; i < pubkeys . length ; ++ i ) {
123
- try {
124
- points [ i ] = secp256k1 . Point . fromHex ( pubkeys [ i ] )
125
- } catch ( err ) {
126
- return 1
127
- }
118
+ points [ i ] = readPublicKey ( pubkeys [ i ] )
119
+ if ( ! points [ i ] ) return 1
128
120
}
129
121
130
122
let point = points [ 0 ]
@@ -134,12 +126,8 @@ module.exports = {
134
126
} ,
135
127
136
128
publicKeyTweakAdd ( output , pubkey , tweak ) {
137
- let P
138
- try {
139
- P = secp256k1 . Point . fromHex ( pubkey )
140
- } catch ( err ) {
141
- return 1
142
- }
129
+ const P = readPublicKey ( pubkey )
130
+ if ( ! P ) return 1
143
131
144
132
tweak = toBig ( tweak )
145
133
if ( tweak >= secp256k1 . CURVE . n ) return 2
@@ -151,12 +139,8 @@ module.exports = {
151
139
} ,
152
140
153
141
publicKeyTweakMul ( output , pubkey , tweak ) {
154
- let P
155
- try {
156
- P = secp256k1 . Point . fromHex ( pubkey )
157
- } catch ( err ) {
158
- return 1
159
- }
142
+ const P = readPublicKey ( pubkey )
143
+ if ( ! P ) return 1
160
144
161
145
tweak = toBig ( tweak )
162
146
if ( tweak >= secp256k1 . CURVE . n || tweak === _0n ) return 2
@@ -241,14 +225,10 @@ module.exports = {
241
225
}
242
226
if ( signature . hasHighS ( ) ) return 3
243
227
244
- let pub
245
- try {
246
- pub = secp256k1 . Point . fromHex ( pubkey )
247
- } catch ( err ) {
248
- return 2
249
- }
228
+ const P = readPublicKey ( pubkey )
229
+ if ( ! P ) return 2
250
230
251
- return secp256k1 . verify ( sig , msg32 , pub ) ? 0 : 3
231
+ return secp256k1 . verify ( sig , msg32 , P ) ? 0 : 3
252
232
} ,
253
233
254
234
// Complex logic to return correct error codes
@@ -276,18 +256,14 @@ module.exports = {
276
256
} ,
277
257
278
258
ecdh ( output , pubkey , seckey , data , hashfn , xbuf , ybuf ) {
279
- let pub
280
- try {
281
- pub = secp256k1 . Point . fromHex ( pubkey )
282
- } catch ( err ) {
283
- return 1
284
- }
259
+ const P = readPublicKey ( pubkey )
260
+ if ( ! P ) return 1
285
261
286
262
const compressed = hashfn === undefined
287
263
288
264
let point
289
265
try {
290
- point = secp256k1 . getSharedSecret ( seckey , pub , compressed )
266
+ point = secp256k1 . getSharedSecret ( seckey , P , compressed )
291
267
} catch ( err ) {
292
268
return 2
293
269
}
0 commit comments