Skip to content

Commit

Permalink
Merge branch 'main' into feat/wallet_memory_methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lana-shanghai committed Sep 16, 2024
2 parents 979bb30 + 5065446 commit 70afd34
Show file tree
Hide file tree
Showing 22 changed files with 1,874 additions and 294 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [main]

env:
ZIG_VERSION: 0.14.0-dev.1409+6d2945f1f
ZIG_VERSION: 0.14.0-dev.1550+4fba7336a

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
permissions: write-all

env:
ZIG_VERSION: 0.14.0-dev.1409+6d2945f1f
ZIG_VERSION: 0.14.0-dev.1550+4fba7336a

jobs:
docs:
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
run-mint-server:
zig build
./zig-out/bin/coconut-mint --config ./zig-out/bin/config.toml
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
.hash = "1220bcc68967188de7ad5d520a4629c0d1e169c111d87e6978a3c128de5ec2b6bdd0",
},
.@"bitcoin-primitives" = .{
.url = "git+https://github.com/zig-bitcoin/bitcoin-primitives#b51ffa5b67e376a102bde0250e4235bc66e32c2e",
.hash = "1220ae99270542861a0f2cc5d9b0b2df2f11c6bd2ce431c9067c5c958cd36f66c948",
.url = "git+https://github.com/zig-bitcoin/bitcoin-primitives#1b8e7c241a1f7fc97aca8c0f953b36c0af2eea2a",
.hash = "1220c6f7e963fbc93d0095ab7ada0274aebb2e72d60a93e03ae5cfd6db552e1dc522",
},
.@"zig-toml" = .{
.url = "git+https://github.com/sam701/zig-toml#78c8512273ab83c0a71f1063d9049ce7abdb70b0",
Expand Down
29 changes: 14 additions & 15 deletions src/core/database/mint_memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub const MintMemoryDatabase = struct {

active_keysets: std.AutoHashMap(nuts.CurrencyUnit, nuts.Id),
keysets: std.AutoHashMap(nuts.Id, MintKeySetInfo),
mint_quotes: std.AutoHashMap([16]u8, MintQuote),
melt_quotes: std.AutoHashMap([16]u8, MeltQuote),
mint_quotes: std.AutoHashMap(zul.UUID, MintQuote),
melt_quotes: std.AutoHashMap(zul.UUID, MeltQuote),
proofs: std.AutoHashMap([33]u8, nuts.Proof),
proof_states: std.AutoHashMap([33]u8, nuts.nut07.State),
blinded_signatures: std.AutoHashMap([33]u8, nuts.BlindSignature),
Expand Down Expand Up @@ -107,13 +107,13 @@ pub const MintMemoryDatabase = struct {
try _keysets.put(ks.id, ks);
}

var _mint_quotes = std.AutoHashMap([16]u8, MintQuote).init(allocator);
var _mint_quotes = std.AutoHashMap(zul.UUID, MintQuote).init(allocator);
errdefer _mint_quotes.deinit();

for (mint_quotes) |q| {
try _mint_quotes.put(q.id, q);
}
var _melt_quotes = std.AutoHashMap([16]u8, MeltQuote).init(allocator);
var _melt_quotes = std.AutoHashMap(zul.UUID, MeltQuote).init(allocator);
errdefer _melt_quotes.deinit();

for (melt_quotes) |q| {
Expand Down Expand Up @@ -146,9 +146,9 @@ pub const MintMemoryDatabase = struct {

const keysets = std.AutoHashMap(nuts.Id, MintKeySetInfo).init(arena.allocator());

const mint_quotes = std.AutoHashMap([16]u8, MintQuote).init(arena.allocator());
const mint_quotes = std.AutoHashMap(zul.UUID, MintQuote).init(arena.allocator());

const melt_quotes = std.AutoHashMap([16]u8, MeltQuote).init(arena.allocator());
const melt_quotes = std.AutoHashMap(zul.UUID, MeltQuote).init(arena.allocator());

const proofs = std.AutoHashMap([33]u8, nuts.Proof).init(arena.allocator());

Expand Down Expand Up @@ -231,13 +231,12 @@ pub const MintMemoryDatabase = struct {
pub fn addMintQuote(self: *Self, quote: MintQuote) !void {
self.lock.lock();
defer self.lock.unlock();
// TODO clone quote

try self.mint_quotes.put(quote.id, quote);
try self.mint_quotes.put(quote.id, try quote.clone(self.allocator));
}

// caller must free MintQuote
pub fn getMintQuote(self: *Self, allocator: std.mem.Allocator, quote_id: [16]u8) !?MintQuote {
pub fn getMintQuote(self: *Self, allocator: std.mem.Allocator, quote_id: zul.UUID) !?MintQuote {
self.lock.lockShared();
defer self.lock.unlockShared();

Expand All @@ -248,7 +247,7 @@ pub const MintMemoryDatabase = struct {

pub fn updateMintQuoteState(
self: *Self,
quote_id: [16]u8,
quote_id: zul.UUID,
state: nuts.nut04.QuoteState,
) !nuts.nut04.QuoteState {
self.lock.lockShared();
Expand Down Expand Up @@ -321,7 +320,7 @@ pub const MintMemoryDatabase = struct {

pub fn removeMintQuoteState(
self: *Self,
quote_id: [16]u8,
quote_id: zul.UUID,
) !void {
self.lock.lock();
defer self.lock.unlock();
Expand All @@ -334,11 +333,11 @@ pub const MintMemoryDatabase = struct {
self.lock.lock();
defer self.lock.unlock();

try self.melt_quotes.put(quote.id, quote);
try self.melt_quotes.put(quote.id, try quote.clone(self.allocator));
}

// caller must free MeltQuote
pub fn getMeltQuote(self: *Self, allocator: std.mem.Allocator, quote_id: [16]u8) !?MeltQuote {
pub fn getMeltQuote(self: *Self, allocator: std.mem.Allocator, quote_id: zul.UUID) !?MeltQuote {
self.lock.lockShared();
defer self.lock.unlockShared();

Expand All @@ -349,7 +348,7 @@ pub const MintMemoryDatabase = struct {

pub fn updateMeltQuoteState(
self: *Self,
quote_id: [16]u8,
quote_id: zul.UUID,
state: nuts.nut05.QuoteState,
) !nuts.nut05.QuoteState {
self.lock.lockShared();
Expand Down Expand Up @@ -422,7 +421,7 @@ pub const MintMemoryDatabase = struct {

pub fn removeMeltQuoteState(
self: *Self,
quote_id: [16]u8,
quote_id: zul.UUID,
) void {
self.lock.lock();
defer self.lock.unlock();
Expand Down
2 changes: 2 additions & 0 deletions src/core/lightning/lightning.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const root = @import("../../lib.zig");
const std = @import("std");

pub const MintLightning = @import("mint.zig");

const Bolt11Invoice = root.lightning_invoices.Bolt11Invoice;
const Amount = root.core.amount.Amount;
const MeltQuoteState = root.core.nuts.nut05.QuoteState;
Expand Down
115 changes: 115 additions & 0 deletions src/core/lightning/mint.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/// MintLightning interface for backend
const Self = @This();

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

const Channel = @import("../../channels/channels.zig").Channel;
const Amount = core.amount.Amount;
const PaymentQuoteResponse = core.lightning.PaymentQuoteResponse;
const CreateInvoiceResponse = core.lightning.CreateInvoiceResponse;
const PayInvoiceResponse = core.lightning.PayInvoiceResponse;
const MeltQuoteBolt11Request = core.nuts.nut05.MeltQuoteBolt11Request;
const Settings = core.lightning.Settings;
const MintMeltSettings = core.lightning.MintMeltSettings;
const FeeReserve = core.mint.FeeReserve;
const MintQuoteState = core.nuts.nut04.QuoteState;

// _type: type,
allocator: std.mem.Allocator,
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,
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,
createInvoiceFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, amount: Amount, unit: core.nuts.CurrencyUnit, description: []const u8, unix_expiry: u64) anyerror!CreateInvoiceResponse,

pub fn initFrom(comptime T: type, allocator: std.mem.Allocator, value: T) !Self {
const gen = struct {
pub fn getSettings(pointer: *anyopaque) Settings {
const self: *T = @ptrCast(@alignCast(pointer));
return self.getSettings();
}

pub fn waitAnyInvoice(pointer: *anyopaque) anyerror!Channel(std.ArrayList(u8)).Rx {
const self: *T = @ptrCast(@alignCast(pointer));
return self.waitAnyInvoice();
}

pub fn getPaymentQuote(pointer: *anyopaque, arena: std.mem.Allocator, melt_quote_request: MeltQuoteBolt11Request) anyerror!PaymentQuoteResponse {
const self: *T = @ptrCast(@alignCast(pointer));
return self.getPaymentQuote(arena, melt_quote_request);
}

pub fn payInvoice(pointer: *anyopaque, arena: std.mem.Allocator, melt_quote: core.mint.MeltQuote, partial_msats: ?Amount, max_fee_msats: ?Amount) !PayInvoiceResponse {
const self: *T = @ptrCast(@alignCast(pointer));
return self.payInvoice(arena, melt_quote, partial_msats, max_fee_msats);
}

pub fn checkInvoiceStatus(pointer: *anyopaque, request_lookup_id: []const u8) !MintQuoteState {
const self: *T = @ptrCast(@alignCast(pointer));
return self.checkInvoiceStatus(request_lookup_id);
}

pub fn createInvoice(pointer: *anyopaque, arena: std.mem.Allocator, amount: Amount, unit: core.nuts.CurrencyUnit, description: []const u8, unix_expiry: u64) !CreateInvoiceResponse {
const self: *T = @ptrCast(@alignCast(pointer));
return self.createInvoice(arena, amount, unit, description, unix_expiry);
}

pub fn deinit(pointer: *anyopaque) void {
if (std.meta.hasFn(T, "deinit")) {
const self: *T = @ptrCast(@alignCast(pointer));
self.deinit();
}
}
};

const ptr = try allocator.create(T);
ptr.* = value;

return .{
// ._type = T,
.allocator = allocator,
.ptr = ptr,
.getSettingsFn = gen.getSettings,
.waitAnyInvoiceFn = gen.waitAnyInvoice,
.getPaymentQuoteFn = gen.getPaymentQuote,
.payInvoiceFn = gen.payInvoice,
.checkInvoiceStatusFn = gen.checkInvoiceStatus,
.createInvoiceFn = gen.createInvoice,
.deinitFn = gen.deinit,
};
}

pub fn deinit(self: Self) void {
self.deinitFn(self.ptr);
// clearing pointer
// self.allocator.destroy(@as(self._type, @ptrCast(self.ptr)));
}

pub fn getSettings(self: Self) Settings {
return self.getSettingsFn(self.ptr);
}

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

pub fn getPaymentQuote(self: Self, arena: std.mem.Allocator, melt_quote_request: MeltQuoteBolt11Request) !PaymentQuoteResponse {
return self.getPaymentQuoteFn(self.ptr, arena, melt_quote_request);
}

pub fn payInvoice(self: Self, arena: std.mem.Allocator, melt_quote: core.mint.MeltQuote, partial_msats: ?Amount, max_fee_msats: ?Amount) !PayInvoiceResponse {
return self.payInvoiceFn(self.ptr, arena, melt_quote, partial_msats, max_fee_msats);
}

pub fn checkInvoiceStatus(self: Self, request_lookup_id: []const u8) !MintQuoteState {
return self.checkInvoiceStatusFn(self.ptr, request_lookup_id);
}

pub fn createInvoice(self: Self, arena: std.mem.Allocator, amount: Amount, unit: core.nuts.CurrencyUnit, description: []const u8, unix_expiry: u64) !CreateInvoiceResponse {
return self.createInvoiceFn(self.ptr, arena, amount, unit, description, unix_expiry);
}
Loading

0 comments on commit 70afd34

Please sign in to comment.