From e026d9358a7dd6deb3e3f5ad1fb09eeeb2fad217 Mon Sep 17 00:00:00 2001 From: Nikita Orlov Date: Tue, 17 Sep 2024 19:13:19 +0200 Subject: [PATCH] Database interface rm unused interface codes from old impl --- src/core/database/database.zig | 428 +++++++++++++++++++ src/core/database/mint_memory.zig | 8 +- src/core/lib.zig | 2 +- src/core/mint/database/database.zig | 567 -------------------------- src/core/mint/lightning/lightning.zig | 61 --- src/core/mint/mint.zig | 17 +- src/core/mint/types.zig | 9 +- src/mint.zig | 19 +- src/router/router_handlers.zig | 2 +- 9 files changed, 459 insertions(+), 654 deletions(-) create mode 100644 src/core/database/database.zig delete mode 100644 src/core/mint/database/database.zig delete mode 100644 src/core/mint/lightning/lightning.zig diff --git a/src/core/database/database.zig b/src/core/database/database.zig new file mode 100644 index 0000000..fb2389c --- /dev/null +++ b/src/core/database/database.zig @@ -0,0 +1,428 @@ +const std = @import("std"); +const nuts = @import("../nuts/lib.zig"); +const dhke = @import("../dhke.zig"); +const zul = @import("zul"); +const bitcoin_primitives = @import("bitcoin-primitives"); +const secp256k1 = bitcoin_primitives.secp256k1; + +const Arened = @import("../../helper/helper.zig").Parsed; +const MintKeySetInfo = @import("../mint/mint.zig").MintKeySetInfo; +const MintQuote = @import("../mint/mint.zig").MintQuote; +const MeltQuote = @import("../mint/mint.zig").MeltQuote; + +pub const MintMemoryDatabase = @import("mint_memory.zig").MintMemoryDatabase; + +pub const MintDatabase = struct { + const Self = @This(); + + allocator: std.mem.Allocator, + ptr: *anyopaque, + size: usize, + align_of: usize, + + deinitFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator) void, + + setActiveKeysetFn: *const fn (ptr: *anyopaque, unit: nuts.CurrencyUnit, id: nuts.Id) anyerror!void, + getActiveKeysetIdFn: *const fn (ptr: *anyopaque, unit: nuts.CurrencyUnit) ?nuts.Id, + getActiveKeysetsFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator) anyerror!std.AutoHashMap(nuts.CurrencyUnit, nuts.Id), + addKeysetInfoFn: *const fn (ptr: *anyopaque, keyset: MintKeySetInfo) anyerror!void, + getKeysetInfoFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, keyset_id: nuts.Id) anyerror!?MintKeySetInfo, + getKeysetInfosFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator) anyerror!Arened(std.ArrayList(MintKeySetInfo)), + addMintQuoteFn: *const fn (ptr: *anyopaque, quote: MintQuote) anyerror!void, + getMintQuoteFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, quote_id: zul.UUID) anyerror!?MintQuote, + updateMintQuoteStateFn: *const fn (ptr: *anyopaque, quote_id: zul.UUID, state: nuts.nut04.QuoteState) anyerror!nuts.nut04.QuoteState, + getMintQuotesFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator) anyerror!std.ArrayList(MintQuote), + getMintQuoteByRequestLookupIdFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, request_lookup_id: zul.UUID) anyerror!?MintQuote, + getMintQuoteByRequestFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, request: []const u8) anyerror!?MintQuote, + removeMintQuoteStateFn: *const fn (ptr: *anyopaque, quote_id: zul.UUID) anyerror!void, + addMeltQuoteFn: *const fn (ptr: *anyopaque, quote: MeltQuote) anyerror!void, + getMeltQuoteFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, quote_id: zul.UUID) anyerror!?MeltQuote, + updateMeltQuoteStateFn: *const fn (ptr: *anyopaque, quote_id: zul.UUID, state: nuts.nut05.QuoteState) anyerror!nuts.nut05.QuoteState, + getMeltQuotesFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator) anyerror!std.ArrayList(MeltQuote), + getMeltQuoteByRequestLookupIdFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, request_lookup_id: zul.UUID) anyerror!?MeltQuote, + getMeltQuoteByRequestFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, request: []const u8) anyerror!?MeltQuote, + removeMeltQuoteStateFn: *const fn (ptr: *anyopaque, quote_id: zul.UUID) anyerror!void, + addProofsFn: *const fn (ptr: *anyopaque, proofs: []const nuts.Proof) anyerror!void, + + getProofsByYsFn: *const fn ( + ptr: *anyopaque, + gpa: std.mem.Allocator, + ys: []const secp256k1.PublicKey, + ) anyerror!std.ArrayList(?nuts.Proof), + + updateProofsStatesFn: *const fn ( + ptr: *anyopaque, + gpa: std.mem.Allocator, + ys: []const secp256k1.PublicKey, + state: nuts.nut07.State, + ) anyerror!std.ArrayList(?nuts.nut07.State), + getProofsStatesFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, ys: []const secp256k1.PublicKey) anyerror!std.ArrayList(?nuts.nut07.State), + + getProofsByKeysetIdFn: *const fn ( + ptr: *anyopaque, + gpa: std.mem.Allocator, + keyset_id: nuts.Id, + ) anyerror!Arened(std.meta.Tuple(&.{ + std.ArrayList(nuts.Proof), + std.ArrayList(?nuts.nut07.State), + })), + addBlindSignaturesFn: *const fn ( + ptr: *anyopaque, + blinded_messages: []const secp256k1.PublicKey, + blind_signatures: []const nuts.BlindSignature, + ) anyerror!void, + getBlindSignaturesFn: *const fn ( + ptr: *anyopaque, + gpa: std.mem.Allocator, + blinded_messages: []const secp256k1.PublicKey, + ) anyerror!std.ArrayList(?nuts.BlindSignature), + getBlindSignaturesForKeysetFn: *const fn ( + ptr: *anyopaque, + gpa: std.mem.Allocator, + keyset_id: nuts.Id, + ) anyerror!std.ArrayList(nuts.BlindSignature), + + // interface for generating clojure to ptr: anytype for this zig interface + pub fn initFrom(comptime T: type, _allocator: std.mem.Allocator, value: T) !Self { + + // implement gen structure + const gen = struct { + pub fn setActiveKeyset(pointer: *anyopaque, unit: nuts.CurrencyUnit, id: nuts.Id) anyerror!void { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.setActiveKeyset(unit, id); + } + + pub fn getActiveKeysetId(pointer: *anyopaque, unit: nuts.CurrencyUnit) ?nuts.Id { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getActiveKeysetId(unit); + } + + pub fn getActiveKeysets(pointer: *anyopaque, gpa: std.mem.Allocator) anyerror!std.AutoHashMap(nuts.CurrencyUnit, nuts.Id) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getActiveKeysets(gpa); + } + pub fn addKeysetInfo(pointer: *anyopaque, keyset: MintKeySetInfo) anyerror!void { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.addKeysetInfo(keyset); + } + pub fn getKeysetInfo(pointer: *anyopaque, gpa: std.mem.Allocator, keyset_id: nuts.Id) anyerror!?MintKeySetInfo { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getKeysetInfo(gpa, keyset_id); + } + pub fn getKeysetInfos(pointer: *anyopaque, gpa: std.mem.Allocator) anyerror!Arened(std.ArrayList(MintKeySetInfo)) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getKeysetInfos(gpa); + } + pub fn addMintQuote(pointer: *anyopaque, quote: MintQuote) anyerror!void { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.addMintQuote(quote); + } + pub fn getMintQuote(pointer: *anyopaque, gpa: std.mem.Allocator, quote_id: zul.UUID) anyerror!?MintQuote { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getMintQuote(gpa, quote_id); + } + pub fn updateMintQuoteState(pointer: *anyopaque, quote_id: zul.UUID, state: nuts.nut04.QuoteState) anyerror!nuts.nut04.QuoteState { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.updateMintQuoteState(quote_id, state); + } + pub fn getMintQuotes(pointer: *anyopaque, gpa: std.mem.Allocator) anyerror!std.ArrayList(MintQuote) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getMintQuotes(gpa); + } + pub fn getMintQuoteByRequestLookupId(pointer: *anyopaque, gpa: std.mem.Allocator, request_lookup_id: zul.UUID) anyerror!?MintQuote { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getMintQuoteByRequestLookupId(gpa, request_lookup_id); + } + pub fn getMintQuoteByRequest(pointer: *anyopaque, gpa: std.mem.Allocator, request: []const u8) anyerror!?MintQuote { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getMintQuoteByRequest(gpa, request); + } + pub fn removeMintQuoteState(pointer: *anyopaque, quote_id: zul.UUID) anyerror!void { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.removeMintQuoteState(quote_id); + } + pub fn addMeltQuote(pointer: *anyopaque, quote: MeltQuote) anyerror!void { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.addMeltQuote(quote); + } + pub fn getMeltQuote(pointer: *anyopaque, gpa: std.mem.Allocator, quote_id: zul.UUID) anyerror!?MeltQuote { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getMeltQuote(gpa, quote_id); + } + pub fn updateMeltQuoteState(pointer: *anyopaque, quote_id: zul.UUID, state: nuts.nut05.QuoteState) anyerror!nuts.nut05.QuoteState { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.updateMeltQuoteState(quote_id, state); + } + pub fn getMeltQuotes(pointer: *anyopaque, gpa: std.mem.Allocator) anyerror!std.ArrayList(MeltQuote) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getMeltQuotes(gpa); + } + pub fn getMeltQuoteByRequestLookupId(pointer: *anyopaque, gpa: std.mem.Allocator, request_lookup_id: zul.UUID) anyerror!?MeltQuote { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getMeltQuoteByRequestLookupId(gpa, request_lookup_id); + } + pub fn getMeltQuoteByRequest(pointer: *anyopaque, gpa: std.mem.Allocator, request: []const u8) anyerror!?MeltQuote { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getMeltQuoteByRequest(gpa, request); + } + pub fn removeMeltQuoteState(pointer: *anyopaque, quote_id: zul.UUID) anyerror!void { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.removeMeltQuoteState(quote_id); + } + pub fn addProofs(pointer: *anyopaque, proofs: []const nuts.Proof) anyerror!void { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.addProofs(proofs); + } + + pub fn getProofsByYs( + pointer: *anyopaque, + gpa: std.mem.Allocator, + ys: []const secp256k1.PublicKey, + ) anyerror!std.ArrayList(?nuts.Proof) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getProofsByYs(gpa, ys); + } + + pub fn updateProofsStates( + pointer: *anyopaque, + gpa: std.mem.Allocator, + ys: []const secp256k1.PublicKey, + state: nuts.nut07.State, + ) anyerror!std.ArrayList(?nuts.nut07.State) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.updateProofsStates(gpa, ys, state); + } + + pub fn getProofsStates( + pointer: *anyopaque, + gpa: std.mem.Allocator, + ys: []const secp256k1.PublicKey, + ) anyerror!std.ArrayList(?nuts.nut07.State) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getProofsStates(gpa, ys); + } + + pub fn getProofsByKeysetId( + pointer: *anyopaque, + gpa: std.mem.Allocator, + keyset_id: nuts.Id, + ) anyerror!Arened(std.meta.Tuple(&.{ + std.ArrayList(nuts.Proof), + std.ArrayList(?nuts.nut07.State), + })) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getProofsByKeysetId(gpa, keyset_id); + } + + pub fn addBlindSignatures( + pointer: *anyopaque, + blinded_messages: []const secp256k1.PublicKey, + blind_signatures: []const nuts.BlindSignature, + ) anyerror!void { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.addBlindSignatures(blinded_messages, blind_signatures); + } + + pub fn getBlindSignatures( + pointer: *anyopaque, + gpa: std.mem.Allocator, + blinded_messages: []const secp256k1.PublicKey, + ) anyerror!std.ArrayList(?nuts.BlindSignature) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getBlindSignatures(gpa, blinded_messages); + } + + pub fn getBlindSignaturesForKeyset( + pointer: *anyopaque, + gpa: std.mem.Allocator, + keyset_id: nuts.Id, + ) anyerror!std.ArrayList(nuts.BlindSignature) { + const self: *T = @ptrCast(@alignCast(pointer)); + return self.getBlindSignaturesForKeyset(gpa, keyset_id); + } + pub fn deinit(pointer: *anyopaque, allocator: std.mem.Allocator) void { + const self: *T = @ptrCast(@alignCast(pointer)); + + if (std.meta.hasFn(T, "deinit")) { + self.deinit(); + } + + allocator.destroy(self); + } + }; + + const ptr: *T align(1) = try _allocator.create(T); + ptr.* = value; + + return .{ + .ptr = ptr, + .allocator = _allocator, + .size = @sizeOf(T), + .align_of = @alignOf(T), + + .getBlindSignaturesForKeysetFn = gen.getBlindSignaturesForKeyset, + + .getBlindSignaturesFn = gen.getBlindSignatures, + .addBlindSignaturesFn = gen.addBlindSignatures, + .getProofsByKeysetIdFn = gen.getProofsByKeysetId, + .getProofsStatesFn = gen.getProofsStates, + .updateProofsStatesFn = gen.updateProofsStates, + .deinitFn = gen.deinit, + .getProofsByYsFn = gen.getProofsByYs, + + .setActiveKeysetFn = gen.setActiveKeyset, + .getActiveKeysetIdFn = gen.getActiveKeysetId, + .getActiveKeysetsFn = gen.getActiveKeysets, + .addKeysetInfoFn = gen.addKeysetInfo, + .getKeysetInfoFn = gen.getKeysetInfo, + .getKeysetInfosFn = gen.getKeysetInfos, + .addMintQuoteFn = gen.addMintQuote, + .getMintQuoteFn = gen.getMintQuote, + .updateMintQuoteStateFn = gen.updateMintQuoteState, + .getMintQuotesFn = gen.getMintQuotes, + .getMintQuoteByRequestLookupIdFn = gen.getMintQuoteByRequestLookupId, + .getMintQuoteByRequestFn = gen.getMintQuoteByRequest, + .removeMintQuoteStateFn = gen.removeMintQuoteState, + .addMeltQuoteFn = gen.addMeltQuote, + .getMeltQuoteFn = gen.getMeltQuote, + .updateMeltQuoteStateFn = gen.updateMeltQuoteState, + .getMeltQuotesFn = gen.getMeltQuotes, + .getMeltQuoteByRequestLookupIdFn = gen.getMeltQuoteByRequestLookupId, + .getMeltQuoteByRequestFn = gen.getMeltQuoteByRequest, + .removeMeltQuoteStateFn = gen.removeMeltQuoteState, + .addProofsFn = gen.addProofs, + }; + } + + /// free resources of database + pub fn deinit(self: Self) void { + self.deinitFn(self.ptr, self.allocator); + // clearing pointer + } + + pub fn setActiveKeyset(self: Self, unit: nuts.CurrencyUnit, id: nuts.Id) anyerror!void { + return self.setActiveKeysetFn(self.ptr, unit, id); + } + + pub fn getActiveKeysetId(self: Self, unit: nuts.CurrencyUnit) ?nuts.Id { + return self.getActiveKeysetIdFn(self.ptr, unit); + } + + pub fn getActiveKeysets(self: Self, gpa: std.mem.Allocator) anyerror!std.AutoHashMap(nuts.CurrencyUnit, nuts.Id) { + return self.getActiveKeysetsFn(self.ptr, gpa); + } + pub fn addKeysetInfo(self: Self, keyset: MintKeySetInfo) anyerror!void { + return self.addKeysetInfoFn(self.ptr, keyset); + } + pub fn getKeysetInfo(self: Self, gpa: std.mem.Allocator, keyset_id: nuts.Id) anyerror!?MintKeySetInfo { + return self.getKeysetInfoFn(self.ptr, gpa, keyset_id); + } + pub fn getKeysetInfos(self: Self, gpa: std.mem.Allocator) anyerror!Arened(std.ArrayList(MintKeySetInfo)) { + return self.getKeysetInfosFn(self.ptr, gpa); + } + pub fn addMintQuote(self: Self, quote: MintQuote) anyerror!void { + return self.addMintQuoteFn(self.ptr, quote); + } + pub fn getMintQuote(self: Self, gpa: std.mem.Allocator, quote_id: zul.UUID) anyerror!?MintQuote { + return self.getMintQuoteFn(self.ptr, gpa, quote_id); + } + pub fn updateMintQuoteState(self: Self, quote_id: zul.UUID, state: nuts.nut04.QuoteState) anyerror!nuts.nut04.QuoteState { + return self.updateMintQuoteStateFn(self.ptr, quote_id, state); + } + pub fn getMintQuotes(self: Self, allocator: std.mem.Allocator) anyerror!std.ArrayList(MintQuote) { + return self.getMintQuotesFn(self.ptr, allocator); + } + pub fn getMintQuoteByRequestLookupId(self: Self, gpa: std.mem.Allocator, request_lookup_id: zul.UUID) anyerror!?MintQuote { + return self.getMintQuoteByRequestLookupIdFn(self.ptr, gpa, request_lookup_id); + } + pub fn getMintQuoteByRequest(self: Self, gpa: std.mem.Allocator, request: []const u8) anyerror!?MintQuote { + return self.getMintQuoteByRequestFn(self.ptr, gpa, request); + } + pub fn removeMintQuoteState(self: Self, quote_id: zul.UUID) anyerror!void { + return self.removeMintQuoteStateFn(self.ptr, quote_id); + } + pub fn addMeltQuote(self: Self, quote: MeltQuote) anyerror!void { + return self.addMeltQuoteFn(self.ptr, quote); + } + pub fn getMeltQuote(self: Self, gpa: std.mem.Allocator, quote_id: zul.UUID) anyerror!?MeltQuote { + return self.getMeltQuoteFn(self.ptr, gpa, quote_id); + } + pub fn updateMeltQuoteState(self: Self, quote_id: zul.UUID, state: nuts.nut05.QuoteState) anyerror!nuts.nut05.QuoteState { + return self.updateMeltQuoteStateFn(self.ptr, quote_id, state); + } + pub fn getMeltQuotes(self: Self, gpa: std.mem.Allocator) anyerror!std.ArrayList(MeltQuote) { + return self.getMeltQuotesFn(self.ptr, gpa); + } + pub fn getMeltQuoteByRequestLookupId(self: Self, gpa: std.mem.Allocator, request_lookup_id: zul.UUID) anyerror!?MeltQuote { + return self.getMeltQuoteByRequestLookupIdFn(self.ptr, gpa, request_lookup_id); + } + pub fn getMeltQuoteByRequest(self: Self, gpa: std.mem.Allocator, request: []const u8) anyerror!?MeltQuote { + return self.getMeltQuoteByRequestFn(self.ptr, gpa, request); + } + pub fn removeMeltQuoteState(self: Self, quote_id: zul.UUID) anyerror!void { + return self.removeMeltQuoteStateFn(self.ptr, quote_id); + } + pub fn addProofs(self: Self, proofs: []const nuts.Proof) anyerror!void { + return self.addProofsFn(self.ptr, proofs); + } + + pub fn getProofsByYs( + self: Self, + gpa: std.mem.Allocator, + ys: []const secp256k1.PublicKey, + ) anyerror!std.ArrayList(?nuts.Proof) { + return self.getProofsByYsFn(self.ptr, gpa, ys); + } + + pub fn updateProofsStates( + self: Self, + gpa: std.mem.Allocator, + ys: []const secp256k1.PublicKey, + state: nuts.nut07.State, + ) anyerror!std.ArrayList(?nuts.nut07.State) { + return self.updateProofsStatesFn(self.ptr, gpa, ys, state); + } + + pub fn getProofsStates( + self: Self, + gpa: std.mem.Allocator, + ys: []const secp256k1.PublicKey, + ) anyerror!std.ArrayList(?nuts.nut07.State) { + return self.getProofsStatesFn(self.ptr, gpa, ys); + } + + pub fn getProofsByKeysetId( + self: Self, + gpa: std.mem.Allocator, + keyset_id: nuts.Id, + ) anyerror!Arened(std.meta.Tuple(&.{ + std.ArrayList(nuts.Proof), + std.ArrayList(?nuts.nut07.State), + })) { + return self.getProofsByKeysetIdFn(self.ptr, gpa, keyset_id); + } + + pub fn addBlindSignatures( + self: Self, + blinded_messages: []const secp256k1.PublicKey, + blind_signatures: []const nuts.BlindSignature, + ) anyerror!void { + return self.addBlindSignaturesFn(self.ptr, blinded_messages, blind_signatures); + } + + pub fn getBlindSignatures( + self: Self, + gpa: std.mem.Allocator, + blinded_messages: []const secp256k1.PublicKey, + ) anyerror!std.ArrayList(?nuts.BlindSignature) { + return self.getBlindSignaturesFn(self.ptr, gpa, blinded_messages); + } + + pub fn getBlindSignaturesForKeyset( + self: Self, + gpa: std.mem.Allocator, + keyset_id: nuts.Id, + ) anyerror!std.ArrayList(nuts.BlindSignature) { + return self.getBlindSignaturesForKeysetFn(self.ptr, gpa, keyset_id); + } +}; diff --git a/src/core/database/mint_memory.zig b/src/core/database/mint_memory.zig index e1b48a4..a1db251 100644 --- a/src/core/database/mint_memory.zig +++ b/src/core/database/mint_memory.zig @@ -285,7 +285,7 @@ pub const MintMemoryDatabase = struct { pub fn getMintQuoteByRequestLookupId( self: *Self, allocator: std.mem.Allocator, - request: []const u8, + request: zul.UUID, ) !?MintQuote { var arena = std.heap.ArenaAllocator.init(allocator); defer arena.deinit(); @@ -294,7 +294,7 @@ pub const MintMemoryDatabase = struct { const quotes = try self.getMintQuotes(arena.allocator()); for (quotes.items) |q| { // if we found, cloning with allocator, so caller responsible on free resources - if (std.mem.eql(u8, q.request_lookup_id, request)) return try q.clone(allocator); + if (q.request_lookup_id.eql(request)) return try q.clone(allocator); } return null; @@ -386,7 +386,7 @@ pub const MintMemoryDatabase = struct { pub fn getMeltQuoteByRequestLookupId( self: *Self, allocator: std.mem.Allocator, - request: []const u8, + request: zul.UUID, ) !?MeltQuote { var arena = std.heap.ArenaAllocator.init(allocator); defer arena.deinit(); @@ -395,7 +395,7 @@ pub const MintMemoryDatabase = struct { const quotes = try self.getMeltQuotes(arena.allocator()); for (quotes.items) |q| { // if we found, cloning with allocator, so caller responsible on free resources - if (std.mem.eql(u8, q.request_lookup_id, request)) return try q.clone(allocator); + if (std.mem.eql(u8, q.request_lookup_id, &request.bin)) return try q.clone(allocator); } return null; diff --git a/src/core/lib.zig b/src/core/lib.zig index f9a216c..5749d71 100644 --- a/src/core/lib.zig +++ b/src/core/lib.zig @@ -3,5 +3,5 @@ pub const secret = @import("secret.zig"); pub const amount = @import("amount.zig"); pub const nuts = @import("nuts/lib.zig"); pub const mint = @import("mint/mint.zig"); -pub const mint_memory = @import("database/mint_memory.zig"); +pub const mint_memory = @import("database/database.zig"); pub const lightning = @import("lightning/lightning.zig"); diff --git a/src/core/mint/database/database.zig b/src/core/mint/database/database.zig deleted file mode 100644 index 384b55f..0000000 --- a/src/core/mint/database/database.zig +++ /dev/null @@ -1,567 +0,0 @@ -const std = @import("std"); -const core = @import("../../core/lib.zig"); -const model = @import("../model.zig"); -const zul = @import("zul"); - -// TODO remove deinit from Database and Tx(?) -pub const Tx = struct { - // These two fields are the same as before - ptr: *anyopaque, - commitFn: *const fn (ptr: *anyopaque) anyerror!void, - rollbackFn: *const fn (ptr: *anyopaque) anyerror!void, - deinitFn: *const fn (ptr: *anyopaque) void, - - // This is new - fn init(ptr: anytype) Tx { - const T = @TypeOf(ptr); - const ptr_info = @typeInfo(T); - - const gen = struct { - pub fn commit(pointer: *anyopaque) anyerror!void { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.commit(self); - } - - pub fn rollback(pointer: *anyopaque) anyerror!void { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.rollback(self); - } - - pub fn deinit(pointer: *anyopaque) void { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.deinit(self); - } - }; - - return .{ - .ptr = ptr, - .commitFn = gen.commit, - .rollbackFn = gen.rollback, - .deinitFn = gen.deinit, - }; - } - - // This is the same as before - pub fn commit(self: *Tx) !void { - return self.commitFn(self.ptr); - } - - pub fn rollback(self: *Tx) !void { - return self.rollbackFn(self.ptr); - } - - pub fn deinit(self: *Tx) void { - return self.deinitFn(self.ptr); - } -}; - -pub const Database = struct { - // These two fields are the same as before - ptr: *anyopaque, - beginTxFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator) anyerror!Tx, - - deinitFn: *const fn (ptr: *anyopaque) void, - addUsedProofsFn: *const fn (ptr: *anyopaque, tx: Tx, proofs: []const core.proof.Proof) anyerror!void, - getUsedProofsFn: *const fn (ptr: *anyopaque, tx: Tx, allocator: std.mem.Allocator) anyerror![]core.proof.Proof, - - addPendingInvoiceFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, tx: Tx, key: []const u8, invoice: model.Invoice) anyerror!void, - getPendingInvoiceFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, tx: Tx, key: []const u8) anyerror!model.Invoice, - deletePendingInvoiceFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, tx: Tx, key: []const u8) anyerror!void, - - getBolt11MintQuoteFn: *const fn (ptr: *anyopaque, _: std.mem.Allocator, _: Tx, id: zul.UUID) anyerror!core.primitives.Bolt11MintQuote, - - updateBolt11MintQuoteFn: *const fn (ptr: *anyopaque, _: std.mem.Allocator, _: Tx, quote: core.primitives.Bolt11MintQuote) anyerror!void, - - addBolt11MintQuoteFn: *const fn (ptr: *anyopaque, _: std.mem.Allocator, _: Tx, _: core.primitives.Bolt11MintQuote) anyerror!void, - - getBolt11MeltQuoteFn: *const fn (ptr: *anyopaque, _: std.mem.Allocator, _: Tx, id: zul.UUID) anyerror!core.primitives.Bolt11MeltQuote, - - updateBolt11MeltQuoteFn: *const fn (ptr: *anyopaque, _: std.mem.Allocator, _: Tx, quote: core.primitives.Bolt11MeltQuote) anyerror!void, - - addBolt11MeltQuoteFn: *const fn (ptr: *anyopaque, _: std.mem.Allocator, _: Tx, _: core.primitives.Bolt11MeltQuote) anyerror!void, - - // This is new - fn init(ptr: anytype) Database { - const T = @TypeOf(ptr); - const ptr_info = @typeInfo(T); - - const gen = struct { - pub fn deinit(pointer: *anyopaque) void { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.deinit(self); - } - - pub fn beginTx(pointer: *anyopaque, allocator: std.mem.Allocator) anyerror!Tx { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.beginTx(self, allocator); - } - - pub fn addUsedProofs(pointer: *anyopaque, tx: Tx, proofs: []const core.proof.Proof) !void { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.addUsedProofs(self, tx, proofs); - } - - pub fn getUsedProofs(pointer: *anyopaque, tx: Tx, allocator: std.mem.Allocator) ![]core.proof.Proof { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.getUsedProofs(self, tx, allocator); - } - - pub fn addPendingInvoice(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, key: []const u8, invoice: model.Invoice) anyerror!void { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.addPendingInvoice(self, allocator, tx, key, invoice); - } - - pub fn getPendingInvoice(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, key: []const u8) anyerror!model.Invoice { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.getPendingInvoice(self, allocator, tx, key); - } - - pub fn deletePendingInvoice(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, key: []const u8) anyerror!void { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.deletePendingInvoice(self, allocator, tx, key); - } - - pub fn updateBolt11MintQuote(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, quote: core.primitives.Bolt11MintQuote) anyerror!void { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.updateBolt11MintQuote(self, allocator, tx, quote); - } - - pub fn getBolt11MintQuote(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, id: zul.UUID) !core.primitives.Bolt11MintQuote { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.getBolt11MintQuote(self, allocator, tx, id); - } - - pub fn addBolt11MintQuote(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, quote: core.primitives.Bolt11MintQuote) !void { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.addBolt11MintQuote(self, allocator, tx, quote); - } - - pub fn updateBolt11MeltQuote(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, quote: core.primitives.Bolt11MeltQuote) anyerror!void { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.updateBolt11MeltQuote(self, allocator, tx, quote); - } - - pub fn getBolt11MeltQuote(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, id: zul.UUID) !core.primitives.Bolt11MeltQuote { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.getBolt11MeltQuote(self, allocator, tx, id); - } - - pub fn addBolt11MeltQuote(pointer: *anyopaque, allocator: std.mem.Allocator, tx: Tx, quote: core.primitives.Bolt11MeltQuote) !void { - const self: T = @ptrCast(@alignCast(pointer)); - - return ptr_info.Pointer.child.addBolt11MeltQuote(self, allocator, tx, quote); - } - }; - - return .{ - .ptr = @ptrCast(ptr), - .beginTxFn = gen.beginTx, - .deinitFn = gen.deinit, - .addUsedProofsFn = gen.addUsedProofs, - .getUsedProofsFn = gen.getUsedProofs, - .addPendingInvoiceFn = gen.addPendingInvoice, - .getPendingInvoiceFn = gen.getPendingInvoice, - .deletePendingInvoiceFn = gen.deletePendingInvoice, - .updateBolt11MintQuoteFn = gen.updateBolt11MintQuote, - .getBolt11MintQuoteFn = gen.getBolt11MintQuote, - .addBolt11MintQuoteFn = gen.addBolt11MintQuote, - - .updateBolt11MeltQuoteFn = gen.updateBolt11MeltQuote, - .getBolt11MeltQuoteFn = gen.getBolt11MeltQuote, - .addBolt11MeltQuoteFn = gen.addBolt11MeltQuote, - }; - } - - // This is the same as before - pub fn beginTx(self: Database, allocator: std.mem.Allocator) !Tx { - return self.beginTxFn(self.ptr, allocator); - } - - pub fn addUsedProofs(self: Database, tx: Tx, proofs: []const core.proof.Proof) !void { - return self.addUsedProofsFn(self.ptr, tx, proofs); - } - - pub fn getUsedProofs(self: Database, tx: Tx, allocator: std.mem.Allocator) ![]core.proof.Proof { - return self.getUsedProofsFn(self.ptr, tx, allocator); - } - - pub fn addPendingInvoice(self: Database, allocator: std.mem.Allocator, tx: Tx, key: []const u8, invoice: model.Invoice) !void { - return self.addPendingInvoiceFn(self.ptr, allocator, tx, key, invoice); - } - - pub fn getPendingInvoice(self: Database, allocator: std.mem.Allocator, tx: Tx, key: []const u8) !model.Invoice { - return self.getPendingInvoiceFn(self.ptr, allocator, tx, key); - } - - pub fn deletePendingInvoice(self: Database, allocator: std.mem.Allocator, tx: Tx, key: []const u8) !void { - return self.deletePendingInvoiceFn(self.ptr, allocator, tx, key); - } - - pub fn getBolt11MintQuote(self: Database, allocator: std.mem.Allocator, tx: Tx, id: zul.UUID) !core.primitives.Bolt11MintQuote { - return self.getBolt11MintQuoteFn(self.ptr, allocator, tx, id); - } - - pub fn updateBolt11MintQuote(self: Database, allocator: std.mem.Allocator, tx: Tx, quote: core.primitives.Bolt11MintQuote) !void { - return self.updateBolt11MintQuoteFn(self.ptr, allocator, tx, quote); - } - - pub fn addBolt11MintQuote(self: Database, allocator: std.mem.Allocator, tx: Tx, quote: core.primitives.Bolt11MintQuote) !void { - return self.addBolt11MintQuoteFn(self.ptr, allocator, tx, quote); - } - - pub fn getBolt11MeltQuote(self: Database, allocator: std.mem.Allocator, tx: Tx, id: zul.UUID) !core.primitives.Bolt11MeltQuote { - return self.getBolt11MeltQuoteFn(self.ptr, allocator, tx, id); - } - - pub fn updateBolt11MeltQuote(self: Database, allocator: std.mem.Allocator, tx: Tx, quote: core.primitives.Bolt11MeltQuote) !void { - return self.updateBolt11MeltQuoteFn(self.ptr, allocator, tx, quote); - } - - pub fn addBolt11MeltQuote(self: Database, allocator: std.mem.Allocator, tx: Tx, quote: core.primitives.Bolt11MeltQuote) !void { - return self.addBolt11MeltQuoteFn(self.ptr, allocator, tx, quote); - } - - pub fn deinit(self: Database) void { - return self.deinitFn(self.ptr); - } -}; - -pub const InMemory = struct { - const Self = @This(); - - const BaseTx = struct { - allocator: std.mem.Allocator, - // TODO make it better - - pub fn init(allocator: std.mem.Allocator) !*BaseTx { - // allocator. - var self = try allocator.create(@This()); - self.allocator = allocator; - return self; - } - - pub fn commit(self: *@This()) !void { - _ = self; - } - - pub fn rollback(self: *@This()) !void { - _ = self; - } - - pub fn deinit(self: *@This()) void { - self.allocator.destroy(self); - } - }; - - const Bolt11MintQuotes = struct { - const Bolt11MintQuote = struct { - id: zul.UUID, - payment_request: []const u8, - expiry: u64, - paid: bool, - - pub fn deinit(self: @This(), allocator: std.mem.Allocator) void { - allocator.free(self.payment_request); - } - }; - - quotes: std.ArrayList(Bolt11MintQuote), - allocator: std.mem.Allocator, - - fn init(allocator: std.mem.Allocator) !Bolt11MintQuotes { - return .{ - .quotes = std.ArrayList(Bolt11MintQuote).init(allocator), - .allocator = allocator, - }; - } - - fn deinit(self: @This()) void { - for (self.quotes.items) |q| q.deinit(); - } - - fn update(self: *@This(), quote: core.primitives.Bolt11MintQuote) !void { - if (self.getPtr(quote.quote_id)) |q| { - const q_old = q.*; - const q_cloned = try quote.clone(self.allocator); - - q.* = Bolt11MintQuote{ - .id = q_cloned.quote_id, - .payment_request = q_cloned.payment_request, - .expiry = q_cloned.expiry, - .paid = q_cloned.paid, - }; - q_old.deinit(self.allocator); - } else return error.QuoteNotFound; - } - - fn add(self: *@This(), quote: core.primitives.Bolt11MintQuote) !void { - if (self.get(quote.quote_id) != null) return error.QuoteAlreadyExist; - - const payment_request = try self.allocator.alloc(u8, quote.payment_request.len); - errdefer self.allocator.free(payment_request); - - try self.quotes.append(.{ - .id = quote.quote_id, - .payment_request = payment_request, - .expiry = quote.expiry, - .paid = quote.paid, - }); - } - - fn get(self: *@This(), id: zul.UUID) ?Bolt11MintQuote { - for (self.quotes.items) |i| if (id.eql(i.id)) return i; - - return null; - } - - fn getPtr(self: *@This(), id: zul.UUID) ?*Bolt11MintQuote { - for (self.quotes.items) |*i| if (id.eql(i.id)) return i; - - return null; - } - - fn delete(self: *@This(), id: zul.UUID) !void { - for (0.., self.quotes.items) |idx, i| { - if (i.eql(id)) { - self.quotes.orderedRemove(idx); - return; - } - } - } - }; - - const Bolt11MeltQuotes = struct { - quotes: std.ArrayList(core.primitives.Bolt11MeltQuote), - allocator: std.mem.Allocator, - - fn init(allocator: std.mem.Allocator) !Bolt11MeltQuotes { - return .{ - .quotes = std.ArrayList(core.primitives.Bolt11MeltQuote).init(allocator), - .allocator = allocator, - }; - } - - fn deinit(self: @This()) void { - for (self.quotes.items) |q| q.deinit(); - } - - fn update(self: *@This(), quote: core.primitives.Bolt11MeltQuote) !void { - if (self.getPtr(quote.quote_id)) |q| { - const q_old = q.*; - q.* = try quote.clone(self.allocator); - q_old.deinit(self.allocator); - } else return error.QuoteNotFound; - } - - fn add(self: *@This(), quote: core.primitives.Bolt11MeltQuote) !void { - if (self.get(quote.quote_id) != null) return error.QuoteAlreadyExist; - - const new = try quote.clone(self.allocator); - errdefer new.deinit(self.allocator); - - try self.quotes.append(new); - } - - fn get(self: *@This(), id: zul.UUID) ?core.primitives.Bolt11MeltQuote { - for (self.quotes.items) |i| if (id.eql(i.quote_id)) return i; - - return null; - } - - fn getPtr(self: *@This(), id: zul.UUID) ?*core.primitives.Bolt11MeltQuote { - for (self.quotes.items) |*i| if (id.eql(i.quote_id)) return i; - - return null; - } - - fn delete(self: *@This(), id: zul.UUID) !void { - for (0.., self.quotes.items) |idx, i| { - if (i.quote_id.eql(id)) { - self.quotes.orderedRemove(idx); - return; - } - } - } - }; - - const PendingInvoices = struct { - const PendingInvoice = struct { - key: []const u8, - amount: u64, - payment_request: []const u8, - - fn deinit(self: PendingInvoice, allocator: std.mem.Allocator) void { - allocator.free(self.key); - allocator.free(self.payment_request); - } - }; - - invoices: std.ArrayList(PendingInvoice), - allocator: std.mem.Allocator, - - fn deinit(self: PendingInvoices) void { - for (self.invoices.items) |i| i.deinit(); - } - - fn init(allocator: std.mem.Allocator) !PendingInvoices { - return .{ - .invoices = std.ArrayList(PendingInvoice).init(allocator), - .allocator = allocator, - }; - } - - fn add(self: *@This(), invoice: PendingInvoice) !void { - if (self.get(invoice.key) != null) return error.InvoiceDuplicate; - - const key = try self.allocator.alloc(u8, invoice.key.len); - errdefer self.allocator.free(key); - - const payment_request = try self.allocator.alloc(u8, invoice.payment_request.len); - errdefer self.allocator.free(payment_request); - - try self.invoices.append(.{ .key = key, .payment_request = payment_request, .amount = invoice.amount }); - } - - fn get(self: *@This(), key: []const u8) ?PendingInvoice { - for (self.invoices.items) |i| if (std.mem.eql(u8, i.key, key)) return i; - return null; - } - - fn delete(self: *@This(), key: []const u8) !void { - for (0.., self.invoices.items) |idx, i| { - if (std.mem.eql(u8, i.key, key)) { - _ = self.invoices.orderedRemove(idx); - return; - } - } - } - }; - - allocator: std.mem.Allocator, - proofs: std.ArrayList(core.proof.Proof), - pending_invoices: PendingInvoices, - mint_quotes: Bolt11MintQuotes, - melt_quotes: Bolt11MeltQuotes, - - pub fn init(allocator: std.mem.Allocator) !Database { - var self = try allocator.create(Self); - errdefer allocator.destroy(self); - - self.allocator = allocator; - self.proofs = std.ArrayList(core.proof.Proof).init(allocator); - - self.pending_invoices = try PendingInvoices.init(allocator); - errdefer self.pending_invoices.deinit(); - - self.mint_quotes = try Bolt11MintQuotes.init(allocator); - errdefer self.mint_quotes.deinit(); - - self.melt_quotes = try Bolt11MeltQuotes.init(allocator); - errdefer self.melt_quotes.deinit(); - - return Database.init(self); - } - - pub fn deinit(self: *Self) void { - self.proofs.deinit(); - self.allocator.destroy(self); - } - - pub fn beginTx(self: *Self, allocator: std.mem.Allocator) !Tx { - _ = self; // autofix - return Tx.init(try BaseTx.init(allocator)); - } - - pub fn addUsedProofs(self: *Self, tx: Tx, proofs: []const core.proof.Proof) !void { - _ = tx; // autofix - - try self.proofs.appendSlice(proofs); - } - - pub fn getUsedProofs(self: *Self, tx: Tx, allocator: std.mem.Allocator) ![]core.proof.Proof { - _ = tx; // autofix - const res = try allocator.alloc(core.proof.Proof, self.proofs.items.len); - errdefer allocator.free(res); - - @memcpy(res, self.proofs.items); - - return res; - } - - pub fn addPendingInvoice(self: *Self, _: std.mem.Allocator, _: Tx, key: []const u8, invoice: model.Invoice) anyerror!void { - try self.pending_invoices.add(.{ .key = key, .amount = invoice.amount, .payment_request = invoice.payment_request }); - } - - pub fn getPendingInvoice(self: *Self, allocator: std.mem.Allocator, _: Tx, key: []const u8) !model.Invoice { - const invoice = self.pending_invoices.get(key) orelse return error.PendingInvoiceNotFound; - - const inv = model.Invoice{ .amount = invoice.amount, .payment_request = invoice.payment_request }; - - return inv.clone(allocator); - } - - pub fn deletePendingInvoice(self: *Self, _: std.mem.Allocator, _: Tx, key: []const u8) anyerror!void { - return try self.pending_invoices.delete(key); - } - - pub fn getBolt11MintQuote(self: *Self, allocator: std.mem.Allocator, _: Tx, id: zul.UUID) !core.primitives.Bolt11MintQuote { - const q = self.mint_quotes.get(id) orelse return error.NotFound; - - const qq = core.primitives.Bolt11MintQuote{ - .quote_id = q.id, - .payment_request = q.payment_request, - .expiry = q.expiry, - .paid = q.paid, - }; - - return qq.clone(allocator); - } - - pub fn addBolt11MintQuote(self: *Self, _: std.mem.Allocator, _: Tx, quote: core.primitives.Bolt11MintQuote) !void { - return self.mint_quotes.add(quote); - } - - pub fn updateBolt11MintQuote(self: *Self, _: std.mem.Allocator, _: Tx, quote: core.primitives.Bolt11MintQuote) !void { - return self.mint_quotes.update(quote); - } - - pub fn getBolt11MeltQuote(self: *Self, allocator: std.mem.Allocator, _: Tx, id: zul.UUID) !core.primitives.Bolt11MeltQuote { - const q = self.melt_quotes.get(id) orelse return error.NotFound; - - return q.clone(allocator); - } - - pub fn addBolt11MeltQuote(self: *Self, _: std.mem.Allocator, _: Tx, quote: core.primitives.Bolt11MeltQuote) !void { - return self.melt_quotes.add(quote); - } - - pub fn updateBolt11MeltQuote(self: *Self, _: std.mem.Allocator, _: Tx, quote: core.primitives.Bolt11MeltQuote) !void { - return self.melt_quotes.update(quote); - } -}; - -test "dfd" { - var db = try InMemory.init(std.testing.allocator); - defer db.deinit(); - - var tx = try db.beginTx(std.testing.allocator); - defer tx.deinit(); - - const pr: core.proof.Proof = undefined; - - try db.addUsedProofs(tx, &.{pr}); - - const proofs = try db.getUsedProofs(tx, std.testing.allocator); - defer std.testing.allocator.free(proofs); - - try std.testing.expectEqual(1, proofs.len); - try std.testing.expectEqual(pr, proofs[0]); -} diff --git a/src/core/mint/lightning/lightning.zig b/src/core/mint/lightning/lightning.zig deleted file mode 100644 index 81d2ef6..0000000 --- a/src/core/mint/lightning/lightning.zig +++ /dev/null @@ -1,61 +0,0 @@ -const std = @import("std"); -const invoice = @import("invoices/lib.zig"); -const model = @import("../model.zig"); -const Self = @This(); - -// These two fields are the same as before -ptr: *anyopaque, - -createInvoiceFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, amount: u64) anyerror!model.CreateInvoiceResult, - -payInvoiceFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, payment_request: []const u8) anyerror!model.PayInvoiceResult, - -isInvoicePaidFn: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, invoice: []const u8) anyerror!bool, - -// This is new -pub fn init(ptr: anytype) Self { - const T = @TypeOf(ptr); - const ptr_info = @typeInfo(T); - - const gen = struct { - pub fn createInvoice(pointer: *anyopaque, allocator: std.mem.Allocator, amount: u64) !model.CreateInvoiceResult { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.createInvoice(self, allocator, amount); - } - pub fn payInvoice(pointer: *anyopaque, allocator: std.mem.Allocator, payment_request: []const u8) !model.PayInvoiceResult { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.payInvoice(self, allocator, payment_request); - } - - pub fn isInvoicePaid(pointer: *anyopaque, allocator: std.mem.Allocator, inv: []const u8) !bool { - const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.Pointer.child.isInvoicePaid(self, allocator, inv); - } - }; - - return .{ - .ptr = ptr, - - .createInvoiceFn = gen.createInvoice, - .isInvoicePaidFn = gen.isInvoicePaid, - .payInvoiceFn = gen.payInvoice, - }; -} - -pub fn isInvoicePaid(self: Self, allocator: std.mem.Allocator, inv: []const u8) anyerror!bool { - return self.isInvoicePaidFn(self.ptr, allocator, inv); -} - -pub fn payInvoice(self: Self, allocator: std.mem.Allocator, payment_request: []const u8) !model.PayInvoiceResult { - return self.payInvoiceFn(self.ptr, allocator, payment_request); -} - -/// Caller is own [model.CreateInvoiceResult], so he responsible to call deinit on it -pub fn createInvoice(self: Self, allocator: std.mem.Allocator, amount: u64) anyerror!model.CreateInvoiceResult { - return self.createInvoiceFn(self.ptr, allocator, amount); -} - -/// Decoding invoice from payment request to [Bolt11Invoice], caller is responsible to call deinit on succ result over [Bolt11Invoice] -pub fn decodeInvoice(_: Self, allocator: std.mem.Allocator, payment_request: []const u8) !invoice.Bolt11Invoice { - return invoice.Bolt11Invoice.fromStr(allocator, payment_request); -} diff --git a/src/core/mint/mint.zig b/src/core/mint/mint.zig index 23dc429..2ebdfcc 100644 --- a/src/core/mint/mint.zig +++ b/src/core/mint/mint.zig @@ -18,6 +18,8 @@ const CurrencyUnit = core.nuts.CurrencyUnit; pub const MintQuote = @import("types.zig").MintQuote; pub const MeltQuote = @import("types.zig").MeltQuote; + +pub const MintDatabase = core.mint_memory.MintDatabase; pub const MintMemoryDatabase = core.mint_memory.MintMemoryDatabase; // TODO implement tests @@ -77,7 +79,7 @@ pub const Mint = struct { /// Mint Info mint_info: MintInfo, /// Mint Storage backend - localstore: helper.RWMutex(*MintMemoryDatabase), + localstore: helper.RWMutex(MintDatabase), /// Active Mint Keysets keysets: RWMutex(std.AutoHashMap(nuts.Id, nuts.MintKeySet)), secp_ctx: secp256k1.Secp256k1, @@ -102,7 +104,7 @@ pub const Mint = struct { mint_url: []const u8, seed: []const u8, mint_info: MintInfo, - localstore: *MintMemoryDatabase, + localstore: MintDatabase, // Hashmap where the key is the unit and value is (input fee ppk, max_order) supported_units: std.AutoHashMap(core.nuts.CurrencyUnit, std.meta.Tuple(&.{ u64, u8 })), ) !Mint { @@ -286,7 +288,7 @@ pub const Mint = struct { unit: nuts.CurrencyUnit, amount: core.amount.Amount, expiry: u64, - ln_lookup: []const u8, + ln_lookup: zul.UUID, ) !MintQuote { const nut04 = self.mint_info.nuts.nut04; if (nut04.disabled) return error.MintingDisabled; @@ -577,7 +579,7 @@ pub const Mint = struct { /// Flag mint quote as paid pub fn payMintQuoteForRequestId( self: *Mint, - request_lookup_id: []const u8, + request_lookup_id: zul.UUID, ) !void { const mint_quote = (try self.localstore.value.getMintQuoteByRequestLookupId(self.allocator, request_lookup_id)) orelse return; @@ -1329,15 +1331,16 @@ const MintConfig = struct { }; fn createMint(arena: std.mem.Allocator, config: MintConfig) !Mint { - const db_ptr = try arena.create(MintMemoryDatabase); - db_ptr.* = try MintMemoryDatabase.initFrom(arena, config.active_keysets, config.keysets, config.mint_quotes, config.melt_quotes, config.pending_proofs, config.spent_proofs, config.blinded_signatures); + const db = try MintMemoryDatabase.initFrom(arena, config.active_keysets, config.keysets, config.mint_quotes, config.melt_quotes, config.pending_proofs, config.spent_proofs, config.blinded_signatures); + + const _db = try MintDatabase.initFrom(MintMemoryDatabase, arena, db); return Mint.init( arena, config.mint_url, config.seed, config.mint_info, - db_ptr, + _db, config.supported_units, ); } diff --git a/src/core/mint/types.zig b/src/core/mint/types.zig index 1366279..76c40ed 100644 --- a/src/core/mint/types.zig +++ b/src/core/mint/types.zig @@ -24,7 +24,7 @@ pub const MintQuote = struct { /// Expiration time of quote expiry: u64, /// Value used by ln backend to look up state of request - request_lookup_id: []const u8, + request_lookup_id: zul.UUID, /// formatting mint quote pub fn format( @@ -45,7 +45,7 @@ pub const MintQuote = struct { unit: CurrencyUnit, amount: amount_lib.Amount, expiry: u64, - request_lookup_id: []const u8, + request_lookup_id: zul.UUID, ) !MintQuote { const id = zul.UUID.v4(); @@ -64,14 +64,10 @@ pub const MintQuote = struct { } pub fn deinit(self: *const MintQuote, allocator: std.mem.Allocator) void { - allocator.free(self.request_lookup_id); allocator.free(self.request); } pub fn clone(self: *const MintQuote, allocator: std.mem.Allocator) !MintQuote { - const request_lookup = try allocator.dupe(u8, self.request_lookup_id); - errdefer allocator.free(request_lookup); - const request = try allocator.dupe(u8, self.request); errdefer allocator.free(request); @@ -81,7 +77,6 @@ pub const MintQuote = struct { var cloned = self.*; cloned.request = request; - cloned.request_lookup_id = request_lookup; cloned.mint_url = mint_url; return cloned; diff --git a/src/mint.zig b/src/mint.zig index b1011e0..2c3682a 100644 --- a/src/mint.zig +++ b/src/mint.zig @@ -8,6 +8,7 @@ const os = std.os; const builtin = @import("builtin"); const config = @import("mintd/config.zig"); const clap = @import("clap"); +const zul = @import("zul"); const MintLightning = core.lightning.MintLightning; const MintState = @import("router/router.zig").MintState; @@ -15,7 +16,8 @@ const LnKey = @import("router/router.zig").LnKey; const FakeWallet = @import("fake_wallet/fake_wallet.zig").FakeWallet; const Mint = core.mint.Mint; const FeeReserve = core.mint.FeeReserve; -const MintDatabase = core.mint_memory.MintMemoryDatabase; +const MintDatabase = core.mint_memory.MintDatabase; +const MintMemoryDatabase = core.mint_memory.MintMemoryDatabase; const ContactInfo = core.nuts.ContactInfo; const MintVersion = core.nuts.MintVersion; const MintInfo = core.nuts.MintInfo; @@ -25,9 +27,11 @@ const default_quote_ttl_secs: u64 = 1800; /// Update mint quote when called for a paid invoice fn handlePaidInvoice(mint: *Mint, request_lookup_id: []const u8) !void { - std.log.debug("Invoice with lookup id paid: {s}", .{request_lookup_id}); + const request_lookup = try zul.UUID.parse(request_lookup_id); - try mint.payMintQuoteForRequestId(request_lookup_id); + std.log.debug("Invoice with lookup id paid: {s}", .{request_lookup}); + + try mint.payMintQuoteForRequestId(request_lookup); } pub fn main() !void { @@ -78,8 +82,8 @@ pub fn main() !void { defer parsed_settings.deinit(); var localstore = switch (parsed_settings.value.database.engine) { - .in_memory => v: { - break :v try MintDatabase.initFrom( + inline .in_memory => v: { + var db = try MintMemoryDatabase.initFrom( gpa.allocator(), .init(gpa.allocator()), &.{}, @@ -89,6 +93,9 @@ pub fn main() !void { &.{}, .init(gpa.allocator()), ); + errdefer db.deinit(); + + break :v try MintDatabase.initFrom(MintMemoryDatabase, gpa.allocator(), db); }, else => { // not implemented engine @@ -237,7 +244,7 @@ pub fn main() !void { const mnemonic = try bip39.Mnemonic.parseInNormalized(.english, parsed_settings.value.info.mnemonic); - var mint = try Mint.init(gpa.allocator(), parsed_settings.value.info.url, &try mnemonic.toSeedNormalized(&.{}), mint_info, &localstore, supported_units); + var mint = try Mint.init(gpa.allocator(), parsed_settings.value.info.url, &try mnemonic.toSeedNormalized(&.{}), mint_info, localstore, supported_units); defer mint.deinit(); // Check the status of any mint quotes that are pending diff --git a/src/router/router_handlers.zig b/src/router/router_handlers.zig index 0014352..4c930c5 100644 --- a/src/router/router_handlers.zig +++ b/src/router/router_handlers.zig @@ -121,7 +121,7 @@ pub fn getMintBolt11Quote( payload.unit, payload.amount, create_invoice_response.expiry orelse 0, - create_invoice_response.request_lookup_id, + try zul.UUID.parse(create_invoice_response.request_lookup_id), ) catch |err| { std.log.err("could not create new mint quote: {any}", .{err}); return error.InternalError;