-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinding.c
458 lines (338 loc) · 20.1 KB
/
binding.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <node_api.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "macros.h"
#include "secp256k1.h"
#include "secp256k1_ecdh.h"
#include "secp256k1_preallocated.h"
#include "secp256k1_recovery.h"
// #include "secp256k1/src/secp256k1.c"
static uint8_t typedarray_width(napi_typedarray_type type) {
switch (type) {
case napi_int8_array: return 1;
case napi_uint8_array: return 1;
case napi_uint8_clamped_array: return 1;
case napi_int16_array: return 2;
case napi_uint16_array: return 2;
case napi_int32_array: return 4;
case napi_uint32_array: return 4;
case napi_float32_array: return 4;
case napi_float64_array: return 8;
case napi_bigint64_array: return 8;
case napi_biguint64_array: return 8;
default: return 0;
}
}
static void sn_secp256k1_context_preallocated_destroy (napi_env env, void* finalise_data, void* finalise_hint) {
secp256k1_context_preallocated_destroy(finalise_data);
}
napi_value sn_secp256k1_context_create (napi_env env, napi_callback_info info) {
SN_ARGV(1, secp256_context_create)
SN_ARGV_UINT32(flags, 0)
SN_THROWS(flags != SECP256K1_CONTEXT_VERIFY && flags != SECP256K1_CONTEXT_SIGN && flags != SECP256K1_CONTEXT_DECLASSIFY && flags != SECP256K1_CONTEXT_NONE, "must use one of the 'secp26k1_context'' flags")
secp256k1_context *ctx = secp256k1_context_create(flags);
napi_value buf;
SN_STATUS_THROWS(napi_create_external_buffer(env, sizeof(ctx), ctx, &sn_secp256k1_context_preallocated_destroy, NULL, &buf), "failed to create a n-api buffer")
return buf;
}
napi_value sn_secp256k1_context_randomize (napi_env env, napi_callback_info info) {
SN_ARGV(2, secp256k1_context_randomize)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_OPTS_TYPEDARRAY(seed32, 1)
if (seed32_data != NULL) {
SN_THROWS(seed32_size != 32, "seed must be 'secp256k1_context_SEEDBYTES' bytes")
}
SN_RETURN(secp256k1_context_randomize(ctx, seed32_data), "context could not be randomized")
}
napi_value sn_secp256k1_ec_pubkey_parse (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ec_pubkey_parse)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubkey, 1)
SN_ARGV_TYPEDARRAY(input, 2)
SN_RETURN(secp256k1_ec_pubkey_parse(ctx, pubkey, input_data, input_size), "pubkey could not be parsed")
}
napi_value sn_secp256k1_ec_pubkey_serialize (napi_env env, napi_callback_info info) {
SN_ARGV(4, secp256k1_ec_pubkey_serialize)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(output, 1)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubkey, 2)
SN_ARGV_UINT32(flags, 3)
size_t min_len = flags == SECP256K1_EC_COMPRESSED ? 33 : 65;
SN_THROWS(output_size < min_len, "output buffer must be at least 33/65 bytes for compressed/uncompressed serialisation")
SN_THROWS(pubkey_size != sizeof(secp256k1_pubkey), "pubkey must be 'secp256k1_PUBKEYBYTES' bytes")
SN_CALL(secp256k1_ec_pubkey_serialize(ctx, output_data, &output_size, pubkey, flags), "pubkey could not be serialised")
napi_value result;
SN_STATUS_THROWS(napi_create_uint32(env, (uint32_t) output_size, &result), "")
return result;
}
napi_value sn_secp256k1_ec_seckey_verify (napi_env env, napi_callback_info info) {
SN_ARGV(2, secp256k1_ec_seckey_verify)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(seckey, 1)
SN_THROWS(seckey_size != 32, "seckey should be 'secp256k1_SECKEYBYTES' bytes")
SN_RETURN_BOOLEAN_FROM_1(secp256k1_ec_seckey_verify(ctx, seckey_data))
}
napi_value sn_secp256k1_ec_privkey_negate (napi_env env, napi_callback_info info) {
SN_ARGV(2, secp256k1_ec_privkey_negate)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(seckey, 1)
SN_THROWS(seckey_size != 32, "seckey should be 'secp256k1_SECKEYBYTES' bytes")
SN_RETURN(secp256k1_ec_privkey_negate(ctx, seckey_data), "privkey could not be negated")
}
napi_value sn_secp256k1_ec_pubkey_negate (napi_env env, napi_callback_info info) {
SN_ARGV(2, secp256k1_ec_pubkey_negate)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubkey, 1)
SN_THROWS(pubkey_size != sizeof(secp256k1_pubkey), "pubkey must be 'secp256k1_PUBKEYBYTES' bytes")
SN_RETURN(secp256k1_ec_pubkey_negate(ctx, pubkey), "pubkey could not be negated")
}
napi_value sn_secp256k1_ec_pubkey_create (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ec_pubkey_create)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubkey, 1)
SN_ARGV_TYPEDARRAY(seckey, 2)
SN_THROWS(seckey_size != 32, "seckey should be 'secp256k1_SECKEYBYTES' bytes")
SN_THROWS(pubkey_size != sizeof(secp256k1_pubkey), "pubkey must be 'secp256k1_PUBKEYBYTES' bytes")
SN_RETURN(secp256k1_ec_pubkey_create(ctx, pubkey, seckey_data), "could not generate public key")
}
napi_value sn_secp256k1_ec_privkey_tweak_add (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ec_privkey_tweaka_add)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(seckey, 1)
SN_ARGV_TYPEDARRAY(tweak, 2)
SN_THROWS(seckey_size != 32, "pubkey must be 'secp256k1_SECKEYBYTES' bytes")
SN_THROWS(tweak_size != 32, "tweak must be 'secp256k1_ec_TWEAKBYTES' bytes")
SN_RETURN(secp256k1_ec_privkey_tweak_add(ctx, seckey_data, tweak_data), "could not generate public key")
}
napi_value sn_secp256k1_ec_pubkey_tweak_add (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ec_pubkey_tweak_add)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubkey, 1)
SN_ARGV_TYPEDARRAY(tweak, 2)
SN_THROWS(pubkey_size != sizeof(secp256k1_pubkey), "pubkey must be 'secp256k1_PUBKEYBYTES' bytes")
SN_THROWS(tweak_size != 32, "tweak must be 'secp256k1_ec_TWEAKBYTES' bytes")
SN_RETURN(secp256k1_ec_pubkey_tweak_add(ctx, pubkey, tweak_data), "could not generate public key")
}
napi_value sn_secp256k1_ec_privkey_tweak_mul (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ec_privkey_tweaka_mul)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(seckey, 1)
SN_ARGV_TYPEDARRAY(tweak, 2)
SN_THROWS(seckey_size != 32, "pubkey must be 'secp256k1_SECKEYBYTES' bytes")
SN_THROWS(tweak_size != 32, "tweak must be 'secp256k1_ec_TWEAKBYTES' bytes")
SN_RETURN(secp256k1_ec_privkey_tweak_mul(ctx, seckey_data, tweak_data), "could not generate public key")
}
napi_value sn_secp256k1_ec_pubkey_tweak_mul (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ec_pubkey_tweak_mul)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubkey, 1)
SN_ARGV_TYPEDARRAY(tweak, 2)
SN_THROWS(pubkey_size != sizeof(secp256k1_pubkey), "pubkey must be 'secp256k1_PUBKEYBYTES' bytes")
SN_THROWS(tweak_size != 32, "tweak must be 'secp256k1_ec_TWEAKBYTES' bytes")
SN_RETURN(secp256k1_ec_pubkey_tweak_mul(ctx, pubkey, tweak_data), "could not generate public key")
}
napi_value sn_secp256k1_ec_pubkey_combine (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ec_pubkey_tweak_mul)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubnonce, 1)
uint32_t batch_length;
napi_get_array_length(env, argv[2], &batch_length);
SN_THROWS(batch_length > 100, "batch too long, number of keys should be <= 100 ")
const secp256k1_pubkey* pubkeys[100];
for (uint32_t i = 0; i < batch_length; i++) {
napi_value element;
napi_get_element(env, argv[2], i, &element);
SN_TYPEDARRAY_ASSERT(pubkey, element, "pubkey should be passed as a TypedArray")
SN_BUFFER_CAST(secp256k1_pubkey *, pubkey, element)
SN_THROWS(pubkey_size != sizeof(secp256k1_pubkey), "pubkey must be 'secp256k1_PUBKEYBYTES' bytes")
pubkeys[i] = pubkey;
}
int success = secp256k1_ec_pubkey_combine(ctx, pubnonce, pubkeys, batch_length);
SN_THROWS(success != 1, "could not combine public keys")
return NULL;
}
napi_value sn_secp256k1_ecdsa_signature_parse_der (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ecdsa_signature_parse_der)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sig, 1)
SN_ARGV_TYPEDARRAY(input, 2)
SN_RETURN(secp256k1_ecdsa_signature_parse_der(ctx, sig, input_data, input_size), "signature could not be parsed")
}
napi_value sn_secp256k1_ecdsa_signature_parse_compact (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ecdsa_signature_parse_compact)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sig, 1)
SN_ARGV_TYPEDARRAY(input64, 2)
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_signature), "sig must be 'secp256k1_ecdsa_SIGBYTES' bytes")
SN_THROWS(input64_size != 64, "input64 must be 'secp256k1_ecdsa_COMPACTBYTES' bytes")
SN_RETURN(secp256k1_ecdsa_signature_parse_compact(ctx, sig, input64_data), "signature could not be parsed")
}
napi_value sn_secp256k1_ecdsa_signature_serialize_der (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ecdsa_signature_serialize_der)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(output, 1)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sig, 2)
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_signature), "sig must be 'secp256k1_ecdsa_SIGBYTES' bytes")
SN_RETURN(secp256k1_ecdsa_signature_serialize_der(ctx, output_data, &output_size, sig), "could not serialise signature")
}
napi_value sn_secp256k1_ecdsa_signature_serialize_compact (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ecdsa_signature_serialize_compact)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(output64, 1)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sig, 2)
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_signature), "sig must be 'secp256k1_ecdsa_SIGBYTES' bytes")
SN_THROWS(output64_size != 64, "output must be 'secp256k1_ecdsa_COMPACTBYTES'")
SN_RETURN(secp256k1_ecdsa_signature_serialize_compact(ctx, output64_data, sig), "could not serialise signature")
}
napi_value sn_secp256k1_ecdsa_signature_normalize (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ecdsa_signature_normalize)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sigout, 1)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sigin, 2)
SN_THROWS(sigout_size != sizeof(secp256k1_ecdsa_signature), "sigout must be 'secp256k1_ecdsa_SIGBYTES' bytes")
SN_THROWS(sigin_size != sizeof(secp256k1_ecdsa_signature), "sigin must be 'secp256k1_ecdsa_SIGBYTES' bytes")
SN_RETURN_BOOLEAN_FROM_1(secp256k1_ecdsa_signature_normalize(ctx, sigout, sigin))
}
napi_value sn_secp256k1_ecdsa_verify (napi_env env, napi_callback_info info) {
SN_ARGV(4, secp256k1_ecdsa_verify)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sig, 1)
SN_ARGV_TYPEDARRAY(msg32, 2)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubkey, 3)
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_signature), "sig must be 'secp256k1_ecdsa_SIGBYTES' bytes")
SN_THROWS(msg32_size != 32, "msg32 should be 'secp256k1_ecdsa_MSGBYTES' bytes")
SN_THROWS(pubkey_size != sizeof(secp256k1_pubkey), "pubkey must be 'secp256k1_PUBKEYBYTES' bytes")
SN_RETURN_BOOLEAN_FROM_1(secp256k1_ecdsa_verify(ctx, sig, msg32_data, pubkey))
}
napi_value sn_secp256k1_ecdsa_sign (napi_env env, napi_callback_info info) {
SN_ARGV(4, secp256k1_ecdsa_sign)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sig, 1)
SN_ARGV_TYPEDARRAY(msg32, 2)
SN_ARGV_TYPEDARRAY(seckey, 3)
SN_THROWS(seckey_size != 32, "seckey should be 'secp256k1_SECKEYBYTES' bytes")
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_signature), "sig must be 'secp256k1_ecdsa_SIGBYTES' bytes")
SN_THROWS(msg32_size != 32, "msg32 should be 'secp256k1_ecdsa_MSGBYTES' bytes")
SN_RETURN(secp256k1_ecdsa_sign(ctx, sig, msg32_data, seckey_data, NULL, NULL), "signature could not be generated")
}
napi_value sn_secp256k1_ecdsa_recoverable_signature_parse_compact (napi_env env, napi_callback_info info) {
SN_ARGV(4, secp256k1_ecdsa_recoverable_signature_parse_compact)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_recoverable_signature *, sig, 1)
SN_ARGV_TYPEDARRAY(input64, 2)
SN_ARGV_UINT32(recid, 2)
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_recoverable_signature), "sig must be 'secp256k1_ecdsa_recoverable_SIGBYTES' bytes")
SN_THROWS(input64_size != 64, "input64 must be 'secp256k1_ecdsa_COMPACTBYTES' bytes")
SN_RETURN(secp256k1_ecdsa_recoverable_signature_parse_compact(ctx, sig, input64_data, recid), "signature could not be parsed")
}
napi_value sn_secp256k1_ecdsa_recoverable_signature_serialize_compact (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ecdsa_recoverable_signature_serialize_compact)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(output64, 1)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_recoverable_signature *, sig, 2)
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_recoverable_signature), "sig must be 'secp256k1_ecdsa_recoverable_SIGBYTES' bytes")
SN_THROWS(output64_size != 64, "output must be 'secp256k1_ecdsa_COMPACTBYTES'")
int recid;
SN_CALL(secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, output64_data, &recid, sig), "could not serialise signature")
napi_value result;
SN_STATUS_THROWS(napi_create_uint32(env, (uint32_t) recid, &result), "")
return result;
}
napi_value sn_secp256k1_ecdsa_recoverable_signature_convert (napi_env env, napi_callback_info info) {
SN_ARGV(3, secp256k1_ecdsa_recoverable_signature_convert)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_signature *, sig, 1)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_recoverable_signature *, sigin, 2)
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_signature), "sig must be 'secp256k1_ecdsa_SIGBYTES' bytes")
SN_THROWS(sigin_size != sizeof(secp256k1_ecdsa_recoverable_signature), "sig must be 'secp256k1_ecdsa_recoverable_SIGBYTES' bytes")
SN_RETURN(secp256k1_ecdsa_recoverable_signature_convert(ctx, sig, sigin), "could not convert signature")
}
napi_value sn_secp256k1_ecdsa_sign_recoverable (napi_env env, napi_callback_info info) {
SN_ARGV(4, secp256k1_ecdsa_sign_recoverable)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_recoverable_signature *, sig, 1)
SN_ARGV_TYPEDARRAY(msg32, 2)
SN_ARGV_TYPEDARRAY(seckey, 3)
SN_THROWS(seckey_size != 32, "seckey should be 'secp256k1_SECKEYBYTES' bytes")
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_recoverable_signature), "sig must be 'secp256k1_ecdsa_recoverable_SIGBYTES' bytes")
SN_THROWS(msg32_size != 32, "msg32 should be 'secp256k1_ecdsa_MSGBYTES' bytes")
SN_RETURN(secp256k1_ecdsa_sign_recoverable(ctx, sig, msg32_data, seckey_data, NULL, NULL), "signature could not be generated")
}
napi_value sn_secp256k1_ecdsa_recover (napi_env env, napi_callback_info info) {
SN_ARGV(4, secp256k1_ecdsa_recover)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, pubkey, 1)
SN_ARGV_BUFFER_CAST(secp256k1_ecdsa_recoverable_signature *, sig, 2)
SN_ARGV_TYPEDARRAY(msg32, 3)
SN_THROWS(pubkey_size != sizeof(secp256k1_pubkey), "pubkey should be 'secp256k1_PUBKEYBYTES' bytes")
SN_THROWS(sig_size != sizeof(secp256k1_ecdsa_recoverable_signature), "sig must be 'secp256k1_ecdsa_recoverable_SIGBYTES' bytes")
SN_THROWS(msg32_size != 32, "msg32 should be 'secp256k1_ecdsa_MSGBYTES' bytes")
SN_RETURN(secp256k1_ecdsa_recover(ctx, pubkey, sig, msg32_data), "public key could not be recovered")
}
napi_value sn_secp256k1_ecdh (napi_env env, napi_callback_info info) {
SN_ARGV(5, secp256k1_ecdh)
SN_ARGV_BUFFER_CAST(secp256k1_context *, ctx, 0)
SN_ARGV_TYPEDARRAY(output, 1)
SN_ARGV_BUFFER_CAST(secp256k1_pubkey *, point, 2)
SN_ARGV_TYPEDARRAY(scalar, 3)
SN_ARGV_OPTS_TYPEDARRAY_PTR(data, 4)
SN_THROWS(output_size != 32, "output should be 'secp256k1_ecdh_BYTES' bytes")
SN_THROWS(point_size != sizeof(secp256k1_pubkey), "pubkey must be 'secp256k1_PUBKEYBYTES' bytes")
SN_THROWS(scalar_size != 32, "scalar should be 'secp256k1_ecdh_SCALARBYTES' bytes")
SN_RETURN(secp256k1_ecdh(ctx, output_data, point, scalar_data, NULL, data), "ecdh could not be completed")
}
static napi_value create_secp256k1_native(napi_env env) {
napi_value exports;
assert(napi_create_object(env, &exports) == napi_ok);
SN_EXPORT_UINT32(secp256k1_SECKEYBYTES, 32)
SN_EXPORT_UINT32(secp256k1_ec_TWEAKBYTES, 32)
SN_EXPORT_UINT32(secp256k1_PUBKEYBYTES, sizeof(secp256k1_pubkey))
SN_EXPORT_UINT32(secp256k1_ecdsa_SIGBYTES, sizeof(secp256k1_ecdsa_signature))
SN_EXPORT_UINT32(secp256k1_ecdsa_recoverable_SIGBYTES, sizeof(secp256k1_ecdsa_recoverable_signature))
SN_EXPORT_UINT32(secp256k1_ecdsa_COMPACTBYTES, 64)
SN_EXPORT_UINT32(secp256k1_ecdsa_MSGYBYTES, 32)
SN_EXPORT_UINT32(secp256k1_context_VERIFY, SECP256K1_CONTEXT_VERIFY)
SN_EXPORT_UINT32(secp256k1_context_SIGN, SECP256K1_CONTEXT_SIGN)
SN_EXPORT_UINT32(secp256k1_context_DECLASSIFY, SECP256K1_CONTEXT_DECLASSIFY)
SN_EXPORT_UINT32(secp256k1_context_NONE, SECP256K1_CONTEXT_NONE)
SN_EXPORT_UINT32(secp256k1_context_SEEDBYTES, 32)
SN_EXPORT_UINT32(secp256k1_ec_COMPRESSED, SECP256K1_EC_COMPRESSED)
SN_EXPORT_UINT32(secp256k1_ec_UNCOMPRESSED, SECP256K1_EC_UNCOMPRESSED)
SN_EXPORT_UINT32(secp256k1_tag_pubkey_EVEN, SECP256K1_TAG_PUBKEY_EVEN)
SN_EXPORT_UINT32(secp256k1_tag_pubkey_ODD, SECP256K1_TAG_PUBKEY_ODD)
SN_EXPORT_UINT32(secp256k1_tag_pubkey_UNCOMPRESSED, SECP256K1_TAG_PUBKEY_UNCOMPRESSED)
SN_EXPORT_UINT32(secp256k1_tag_pubkey_HYBRID_EVEN, SECP256K1_TAG_PUBKEY_HYBRID_EVEN)
SN_EXPORT_UINT32(secp256k1_tag_pubkey_HYBRID_ODD, SECP256K1_TAG_PUBKEY_HYBRID_ODD)
SN_EXPORT_FUNCTION(secp256k1_context_create, sn_secp256k1_context_create)
SN_EXPORT_FUNCTION(secp256k1_context_randomize, sn_secp256k1_context_randomize)
SN_EXPORT_FUNCTION(secp256k1_ec_seckey_verify, sn_secp256k1_ec_seckey_verify)
SN_EXPORT_FUNCTION(secp256k1_ec_pubkey_create, sn_secp256k1_ec_pubkey_create)
SN_EXPORT_FUNCTION(secp256k1_ec_pubkey_parse, sn_secp256k1_ec_pubkey_parse)
SN_EXPORT_FUNCTION(secp256k1_ec_pubkey_serialize, sn_secp256k1_ec_pubkey_serialize)
SN_EXPORT_FUNCTION(secp256k1_ec_privkey_negate, sn_secp256k1_ec_privkey_negate)
SN_EXPORT_FUNCTION(secp256k1_ec_pubkey_negate, sn_secp256k1_ec_pubkey_negate)
SN_EXPORT_FUNCTION(secp256k1_ec_privkey_tweak_add, sn_secp256k1_ec_privkey_tweak_add)
SN_EXPORT_FUNCTION(secp256k1_ec_pubkey_tweak_add, sn_secp256k1_ec_pubkey_tweak_add)
SN_EXPORT_FUNCTION(secp256k1_ec_privkey_tweak_mul, sn_secp256k1_ec_privkey_tweak_mul)
SN_EXPORT_FUNCTION(secp256k1_ec_pubkey_tweak_mul, sn_secp256k1_ec_pubkey_tweak_mul)
SN_EXPORT_FUNCTION(secp256k1_ec_pubkey_combine, sn_secp256k1_ec_pubkey_combine)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_signature_parse_der, sn_secp256k1_ecdsa_signature_parse_der)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_signature_parse_compact, sn_secp256k1_ecdsa_signature_parse_compact)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_signature_serialize_der, sn_secp256k1_ecdsa_signature_serialize_der)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_signature_serialize_compact, sn_secp256k1_ecdsa_signature_serialize_compact)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_signature_normalize, sn_secp256k1_ecdsa_signature_normalize)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_verify, sn_secp256k1_ecdsa_verify)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_sign, sn_secp256k1_ecdsa_sign)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_recoverable_signature_parse_compact, sn_secp256k1_ecdsa_recoverable_signature_parse_compact)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_recoverable_signature_serialize_compact, sn_secp256k1_ecdsa_recoverable_signature_serialize_compact)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_recoverable_signature_convert, sn_secp256k1_ecdsa_recoverable_signature_convert)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_sign_recoverable, sn_secp256k1_ecdsa_sign_recoverable)
SN_EXPORT_FUNCTION(secp256k1_ecdsa_recover, sn_secp256k1_ecdsa_recover)
SN_EXPORT_FUNCTION(secp256k1_ecdh, sn_secp256k1_ecdh)
return exports;
}
static napi_value Init(napi_env env, napi_value exports) {
return create_secp256k1_native(env);
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)