Skip to content

Commit

Permalink
Feat: LNbits backend (zig-bitcoin#27)
Browse files Browse the repository at this point in the history
* lnbits backend with webhook + router merging
  • Loading branch information
StringNick authored Sep 29, 2024
1 parent 7cdb46f commit bebd791
Show file tree
Hide file tree
Showing 18 changed files with 1,540 additions and 161 deletions.
12 changes: 6 additions & 6 deletions src/core/database/database.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ pub const MintDatabase = struct {
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,
getMintQuoteByRequestLookupIdFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, request_lookup_id: []const u8) 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,
getMeltQuoteByRequestLookupIdFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, request_lookup_id: []const u8) 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,
Expand Down Expand Up @@ -129,7 +129,7 @@ pub const MintDatabase = struct {
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 {
pub fn getMintQuoteByRequestLookupId(pointer: *anyopaque, gpa: std.mem.Allocator, request_lookup_id: []const u8) anyerror!?MintQuote {
const self: *T = @ptrCast(@alignCast(pointer));
return self.getMintQuoteByRequestLookupId(gpa, request_lookup_id);
}
Expand Down Expand Up @@ -157,7 +157,7 @@ pub const MintDatabase = struct {
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 {
pub fn getMeltQuoteByRequestLookupId(pointer: *anyopaque, gpa: std.mem.Allocator, request_lookup_id: []const u8) anyerror!?MeltQuote {
const self: *T = @ptrCast(@alignCast(pointer));
return self.getMeltQuoteByRequestLookupId(gpa, request_lookup_id);
}
Expand Down Expand Up @@ -332,7 +332,7 @@ pub const MintDatabase = struct {
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 {
pub fn getMintQuoteByRequestLookupId(self: Self, gpa: std.mem.Allocator, request_lookup_id: []const u8) 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 {
Expand All @@ -353,7 +353,7 @@ pub const MintDatabase = struct {
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 {
pub fn getMeltQuoteByRequestLookupId(self: Self, gpa: std.mem.Allocator, request_lookup_id: []const u8) 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 {
Expand Down
8 changes: 4 additions & 4 deletions src/core/database/mint_memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ pub const MintMemoryDatabase = struct {
pub fn getMintQuoteByRequestLookupId(
self: *Self,
allocator: std.mem.Allocator,
request: zul.UUID,
request: []const u8,
) !?MintQuote {
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
Expand All @@ -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 (q.request_lookup_id.eql(request)) return try q.clone(allocator);
if (std.mem.eql(u8, q.request_lookup_id, request)) return try q.clone(allocator);
}

return null;
Expand Down Expand Up @@ -386,7 +386,7 @@ pub const MintMemoryDatabase = struct {
pub fn getMeltQuoteByRequestLookupId(
self: *Self,
allocator: std.mem.Allocator,
request: zul.UUID,
request: []const u8,
) !?MeltQuote {
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
Expand All @@ -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.bin)) return try q.clone(allocator);
if (std.mem.eql(u8, q.request_lookup_id, request)) return try q.clone(allocator);
}

return null;
Expand Down
4 changes: 4 additions & 0 deletions src/core/lightning/lightning.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub const PayInvoiceResponse = struct {
/// Totoal Amount Spent
total_spent: Amount,

unit: CurrencyUnit,

pub fn deinit(self: PayInvoiceResponse, allocator: std.mem.Allocator) void {
allocator.free(self.payment_hash);

Expand All @@ -45,6 +47,8 @@ pub const PaymentQuoteResponse = struct {
amount: Amount,
/// Fee required for melt
fee: u64,
/// Status
state: MeltQuoteState,

pub fn deinit(self: PaymentQuoteResponse, allocator: std.mem.Allocator) void {
allocator.free(self.request_lookup_id);
Expand Down
8 changes: 5 additions & 3 deletions src/core/lightning/mint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const Self = @This();

const std = @import("std");
const core = @import("../lib.zig");
const ref = @import("../../sync/ref.zig");
const mpmc = @import("../../sync/mpmc.zig");

const Channel = @import("../../channels/channels.zig").Channel;
const Amount = core.amount.Amount;
Expand All @@ -21,7 +23,7 @@ ptr: *anyopaque,

deinitFn: *const fn (ptr: *anyopaque) void,
getSettingsFn: *const fn (ptr: *anyopaque) Settings,
waitAnyInvoiceFn: *const fn (ptr: *anyopaque) anyerror!Channel(std.ArrayList(u8)).Rx,
waitAnyInvoiceFn: *const fn (ptr: *anyopaque) ref.Arc(mpmc.UnboundedChannel(std.ArrayList(u8))),
getPaymentQuoteFn: *const fn (ptr: *anyopaque, alloc: std.mem.Allocator, melt_quote_request: MeltQuoteBolt11Request) anyerror!PaymentQuoteResponse,
payInvoiceFn: *const fn (ptr: *anyopaque, alloc: std.mem.Allocator, melt_quote: core.mint.MeltQuote, partial_msats: ?Amount, max_fee_msats: ?Amount) anyerror!PayInvoiceResponse,
checkInvoiceStatusFn: *const fn (ptr: *anyopaque, request_lookup_id: []const u8) anyerror!MintQuoteState,
Expand All @@ -34,7 +36,7 @@ pub fn initFrom(comptime T: type, allocator: std.mem.Allocator, value: T) !Self
return self.getSettings();
}

pub fn waitAnyInvoice(pointer: *anyopaque) anyerror!Channel(std.ArrayList(u8)).Rx {
pub fn waitAnyInvoice(pointer: *anyopaque) ref.Arc(mpmc.UnboundedChannel(std.ArrayList(u8))) {
const self: *T = @ptrCast(@alignCast(pointer));
return self.waitAnyInvoice();
}
Expand Down Expand Up @@ -94,7 +96,7 @@ pub fn getSettings(self: Self) Settings {
return self.getSettingsFn(self.ptr);
}

pub fn waitAnyInvoice(self: Self) !Channel(std.ArrayList(u8)).Rx {
pub fn waitAnyInvoice(self: Self) ref.Arc(mpmc.UnboundedChannel(std.ArrayList(u8))) {
return self.waitAnyInvoiceFn(self.ptr);
}

Expand Down
2 changes: 0 additions & 2 deletions src/core/mint/lightning/lib.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pub const lnbits = @import("lnbits.zig");
pub const invoice = @import("invoices/lib.zig");

pub const Lightning = @import("lightning.zig");
Loading

0 comments on commit bebd791

Please sign in to comment.