Repository navigation
fix: export asyncAggregateWithRandomness through napi binding #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -660,13 +660,13 @@ pub fn aggregateWithRandomness(sets: js.Array) !js.Value { | |
| /// All input data should be copied into this struct so the worker thread doesn't depend on | ||
| /// any JS-managed memory staying alive. | ||
| const AsyncAggRandData = struct { | ||
| pks: []PublicKey, | ||
| sigs: []Signature, | ||
| pk_ptrs: []*const PublicKey, | ||
| sig_ptrs: []*const Signature, | ||
| pks: []NativePublicKey, | ||
| sigs: []NativeSignature, | ||
| pk_ptrs: []*const NativePublicKey, | ||
| sig_ptrs: []*const NativeSignature, | ||
| randomness: []u8, | ||
| pk_out: PublicKey, | ||
| sig_out: Signature, | ||
| pk_out: NativePublicKey, | ||
| sig_out: NativeSignature, | ||
| err: ?anyerror, | ||
| deferred: napi.Deferred, | ||
| work: napi.c.napi_async_work, | ||
|
|
@@ -710,23 +710,23 @@ fn asyncAggRand_execute(_: napi.Env, data: *AsyncAggRandData) void { | |
| /// Ran on the JS thread once the worker has finished. Always settles the | ||
| /// promise — `settle` does the resolve/reject; if it errors we fall back to a | ||
| /// bare reject so callers never see a dangling Promise. | ||
| fn asyncAggRand_complete(env: napi.Env, status: napi.status.Status, data: *AsyncAggRandData) void { | ||
| fn asyncAggRand_complete(env: napi.Env, stat: napi.status.Status, data: *AsyncAggRandData) void { | ||
| defer { | ||
| napi.status.check(napi.c.napi_delete_async_work(env.env, data.work)) catch {}; | ||
| data.destroy(); | ||
| } | ||
|
|
||
| settle(env, status, data) catch { | ||
| settle(env, stat, data) catch { | ||
| // Catch all rejection: if we reach this point we might want | ||
| // better errors upstream | ||
| rejectWithError(env, data.deferred, "asyncAggregateWithRandomness", "InternalError") catch {}; | ||
| }; | ||
| } | ||
|
|
||
| fn settle(env: napi.Env, status: napi.status.Status, data: *AsyncAggRandData) !void { | ||
| if (status != .ok) { | ||
| fn settle(env: napi.Env, stat: napi.status.Status, data: *AsyncAggRandData) !void { | ||
| if (stat != .ok) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable name |
||
| // libuv's async work itself failed (e.g. cancelled), not crypto. | ||
| return rejectWithError(env, data.deferred, "asyncAggregateWithRandomness/asyncWork", @tagName(status)); | ||
| return rejectWithError(env, data.deferred, "asyncAggregateWithRandomness/asyncWork", @tagName(stat)); | ||
| } | ||
| if (data.err) |err| { | ||
| // Worker captured a Zig error — surface its name as the JS Error.code | ||
|
|
@@ -735,13 +735,8 @@ fn settle(env: napi.Env, status: napi.status.Status, data: *AsyncAggRandData) !v | |
| return rejectWithError(env, data.deferred, "asyncAggregateWithRandomness", @errorName(err)); | ||
| } | ||
|
|
||
| const pk_value: NativePublicKey = .{}; | ||
| const pk_unwrapped = try env.unwrap(PublicKey, pk_value); | ||
| pk_unwrapped.* = data.pk_out; | ||
|
|
||
| const sig_value: NativeSignature = .{}; | ||
| const sig_unwrapped = try env.unwrap(Signature, sig_value); | ||
| sig_unwrapped.* = data.sig_out; | ||
| const pk_value = napi.Value{ .env = env.env, .value = js.convertReturn(PublicKey, .{ .raw = data.pk_out }, env.env) }; | ||
| const sig_value = napi.Value{ .env = env.env, .value = js.convertReturn(Signature, .{ .raw = data.sig_out }, env.env) }; | ||
|
|
||
| const result = try env.createObject(); | ||
| try result.setNamedProperty("pk", pk_value); | ||
|
|
@@ -777,24 +772,25 @@ fn rejectWithError(env: napi.Env, deferred: napi.Deferred, where: []const u8, co | |
| /// 1) sets: Array of {pk: PublicKey, sig: Uint8Array} | ||
| /// | ||
| /// Returns: Promise<{pk: PublicKey, sig: Signature}> | ||
| pub fn asyncAggregateWithRandomness(env: napi.Env, cb: napi.CallbackInfo(1)) !napi.Value { | ||
| const sets = cb.arg(0); | ||
| const n = try sets.getArrayLength(); | ||
| pub fn asyncAggregateWithRandomness(sets: js.Array) !js.Value { | ||
| const n = try sets.length(); | ||
|
Comment on lines
+775
to
+776
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per Rule 51 of the repository style guide, all function arguments must be asserted. Please add an assertion for the References
|
||
|
|
||
| if (n == 0) return error.EmptyArray; | ||
| if (n > MAX_AGGREGATE_PER_JOB) return error.TooManySets; | ||
| if (thread_pool == null) return error.PoolNotInitialized; | ||
|
|
||
| const env = js.env(); | ||
|
|
||
| const data = try allocator.create(AsyncAggRandData); | ||
| errdefer allocator.destroy(data); | ||
|
|
||
| data.pks = try allocator.alloc(PublicKey, n); | ||
| data.pks = try allocator.alloc(NativePublicKey, n); | ||
| errdefer allocator.free(data.pks); | ||
| data.sigs = try allocator.alloc(Signature, n); | ||
| data.sigs = try allocator.alloc(NativeSignature, n); | ||
| errdefer allocator.free(data.sigs); | ||
| data.pk_ptrs = try allocator.alloc(*const PublicKey, n); | ||
| data.pk_ptrs = try allocator.alloc(*const NativePublicKey, n); | ||
| errdefer allocator.free(data.pk_ptrs); | ||
| data.sig_ptrs = try allocator.alloc(*const Signature, n); | ||
| data.sig_ptrs = try allocator.alloc(*const NativeSignature, n); | ||
| errdefer allocator.free(data.sig_ptrs); | ||
| data.randomness = try allocator.alloc(u8, n * 32); | ||
| errdefer allocator.free(data.randomness); | ||
|
|
@@ -807,16 +803,16 @@ pub fn asyncAggregateWithRandomness(env: napi.Env, cb: napi.CallbackInfo(1)) !na | |
| napi_io.get().random(data.randomness); | ||
|
|
||
| for (0..n) |i| { | ||
| const set_value = try sets.getElement(@intCast(i)); | ||
| const set = (try sets.get(@intCast(i))).toValue(); | ||
|
|
||
| const pk_value = try set_value.getNamedProperty("pk"); | ||
| const unwrapped_pk = try env.unwrap(PublicKey, pk_value); | ||
| data.pks[i] = unwrapped_pk.*; | ||
| const pk_napi = try set.getNamedProperty("pk"); | ||
| const wrapped_pk = try env.unwrap(PublicKey, pk_napi); | ||
| data.pks[i] = wrapped_pk.raw; | ||
| data.pk_ptrs[i] = &data.pks[i]; | ||
|
|
||
| const sig_value = try set_value.getNamedProperty("sig"); | ||
| const sig_bytes = try sig_value.getTypedarrayInfo(); | ||
| data.sigs[i] = Signature.deserialize(sig_bytes.data[0..]) catch return error.DeserializationFailed; | ||
| const sig_napi = try set.getNamedProperty("sig"); | ||
| const sig_bytes = try uint8SliceFromValue(.{ .val = sig_napi }); | ||
| data.sigs[i] = NativeSignature.deserialize(sig_bytes[0..]) catch return error.DeserializationFailed; | ||
| data.sig_ptrs[i] = &data.sigs[i]; | ||
| } | ||
|
|
||
|
|
@@ -835,5 +831,5 @@ pub fn asyncAggregateWithRandomness(env: napi.Env, cb: napi.CallbackInfo(1)) !na | |
|
|
||
| try work.queue(); | ||
|
|
||
| return data.deferred.getPromise(); | ||
| return .{ .val = data.deferred.getPromise() }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name
statis an abbreviation ofstatus, which violates the repository style guide (Rule 221). Additionally, the function nameasyncAggRand_completeuses an underscore, violating the camelCase requirement for function names (Rule 216). Finally, per Rule 51, function arguments should be asserted to ensure they are valid before use.References