Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/core/nuts/nut01/nut01.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,61 @@ pub const KeysResponse = struct {
.keysets = try arraylist.toOwnedSlice(),
};
}

pub fn sort(self: KeysResponse, allocator: std.mem.Allocator) !KeysResponse {
var sorted_keysets = std.ArrayList(KeySet).init(allocator);
defer sorted_keysets.deinit();

for (self.keysets) |keyset| {
const keys = keyset.keys;

var key_array = std.ArrayList([]const u8).init(allocator);
defer key_array.deinit();

var it = keys.inner.iterator();
while (it.next()) |kv| {
const key = kv.key_ptr.*;
try key_array.append(key);
}

const Context = struct {
items: [][]const u8,
};
var context = Context{ .items = key_array.items };

std.sort.insertion(
[]const u8,
key_array.items,
&context,
struct {
fn lessThan(_: *Context, a: []const u8, b: []const u8) bool {
const keyA = std.fmt.parseInt(u64, a, 10) catch 0;
const keyB = std.fmt.parseInt(u64, b, 10) catch 0;
return keyA < keyB;
}
}.lessThan,
);

var sorted_keys = std.StringHashMap(secp256k1.PublicKey).init(allocator);
defer sorted_keys.deinit();

for (key_array.items) |key| {
const value = keys.inner.get(key).?;
try sorted_keys.put(key, value);
}

const sorted_keyset = KeySet{
.id = keyset.id,
.unit = keyset.unit,
.keys = Keys{ .inner = sorted_keys },
};

try sorted_keysets.append(sorted_keyset);
}

const sorted_pubkeys = KeysResponse{ .keysets = try sorted_keysets.toOwnedSlice() };
return sorted_pubkeys;
}
};

/// Mint Keys [NUT-01]
Expand Down
12 changes: 9 additions & 3 deletions src/router/router_handlers.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
const std = @import("std");

const bitcoin_primitives = @import("bitcoin-primitives");
const httpz = @import("httpz");
const core = @import("../core/lib.zig");
const secp256k1 = bitcoin_primitives.secp256k1;
const zul = @import("zul");
const ln_invoice = @import("../lightning_invoices/invoice.zig");

const core = @import("../core/lib.zig");
const ln_invoice = @import("../lightning_invoices/invoice.zig");
const MintLightning = core.lightning.MintLightning;
const MintState = @import("router.zig").MintState;
const LnKey = @import("router.zig").LnKey;
const KeySet = @import("../core/nuts/nut02/nut02.zig").KeySet;
const KeysResponse = @import("../core/nuts/nut01/nut01.zig").KeysResponse;
const Keys = @import("../core/nuts/nut01/nut01.zig").Keys;

pub fn getKeys(state: MintState, req: *httpz.Request, res: *httpz.Response) !void {
const pubkeys = try state.mint.pubkeys(req.arena);

return try res.json(pubkeys, .{});
return try res.json(try pubkeys.sort(req.arena), .{});
}

pub fn getKeysets(state: MintState, req: *httpz.Request, res: *httpz.Response) !void {
Expand Down
Loading