Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
StringNick committed Oct 3, 2024
1 parent 04cb5a5 commit 81bf2f6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 100 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
// internet connectivity.
.dependencies = .{
.zul = .{
.url = "https://github.com/karlseguin/zul/archive/08c989bf6871e87807a4668232913ee245425863.zip",
.hash = "12206f5d1e5bd4793fe952bbae891b7424a19026e0d296a1381074c7d21d5d76c1a1",
.url = "https://github.com/StringNick/zul/archive/9f9acdff296918e0f81e8af39b269dad988c48f9.zip",
.hash = "122016552e83ec129489fd32d89b4122f737fcdacb22dbee34dca2fbc62981bb487d",
},
.@"zig-cli" = .{
.url = "https://github.com/StringNick/zig-cli/archive/c9b9d17b14c524785a32a5b7c930b9584a331372.zip",
Expand Down
Binary file added cocomint_db.sqlite
Binary file not shown.
2 changes: 2 additions & 0 deletions src/core/database/mint_sqlite.zig
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ pub const Database = struct {
defer row.deinit(); // must be called
const quote_json = row.blob(0);

std.log.debug("quote-json: {s}", .{quote_json});

const quote = try std.json.parseFromSlice(MintQuote, self.allocator, quote_json, .{});
defer quote.deinit();

Expand Down
140 changes: 44 additions & 96 deletions src/core/mint/lightning/lnbits.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const FeeReserve = core.mint.FeeReserve;
const Channel = @import("../../../channels/channels.zig").Channel;
const MintLightning = core.lightning.MintLightning;

pub const HttpError = std.http.Client.RequestError || std.http.Client.Request.FinishError || std.http.Client.Request.WaitError || error{ ReadBodyError, WrongJson };

pub const LightningError = HttpError || std.Uri.ParseError || std.mem.Allocator.Error || error{
pub const LightningError = error{
NotFound,
Unauthorized,
PaymentFailed,
Expand Down Expand Up @@ -159,14 +157,12 @@ pub const LnBits = struct {
const amnt = try core.lightning.toUnit(amount, unit, .sat);

const expiry = unix_expiry - @abs(time_now);
_ = expiry; // autofix

const create_invoice_response = try self.client.createInvoice(gpa, .{
.amount = amnt,
.unit = unit.toString(),
.memo = description,
// .expiry = expiry,
.expiry = null,
.expiry = expiry,
.webhook = self.webhook_url,
.internal = null,
.out = false,
Expand Down Expand Up @@ -226,17 +222,15 @@ pub const LNBitsClient = struct {
admin_key: []const u8,
invoice_api_key: []const u8,
lnbits_url: []const u8,
client: std.http.Client,
client: zul.http.Client,

pub fn init(
allocator: std.mem.Allocator,
admin_key: []const u8,
invoice_api_key: []const u8,
lnbits_url: []const u8,
) !LNBitsClient {
var client = std.http.Client{
.allocator = allocator,
};
var client = zul.http.Client.init(allocator);
errdefer client.deinit();

return .{
Expand All @@ -256,92 +250,53 @@ pub const LNBitsClient = struct {
self: *@This(),
allocator: std.mem.Allocator,
endpoint: []const u8,
) LightningError![]const u8 {
) ![]const u8 {
const uri = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ self.lnbits_url, endpoint });
defer allocator.free(uri);

const header_buf = try allocator.alloc(u8, 1024 * 1024 * 4);
defer allocator.free(header_buf);

var req = try self.client.open(.GET, try std.Uri.parse(uri), .{
.server_header_buffer = header_buf,
.extra_headers = &.{
.{
.name = "X-Api-Key",
.value = self.admin_key,
},
},
});
var req = try self.client.allocRequest(allocator, uri);
defer req.deinit();

try req.send();
try req.finish();
try req.wait();
try req.header("X-Api-Key", self.admin_key);

if (req.response.status != .ok) {
if (req.response.status == .not_found) return LightningError.NotFound;
var res = try req.getResponse(.{});

if (res.status != 200) {
if (res.status == 404) return LightningError.NotFound;
}

var rdr = req.reader();
const body = rdr.readAllAlloc(allocator, 1024 * 1024 * 4) catch |err| {
std.log.debug("read body error: {any}", .{err});
return error.ReadBodyError;
};
errdefer allocator.free(body);
const res_body = try res.allocBody(allocator, .{});

return body;
return res_body.buf;
}

pub fn post(
self: *@This(),
allocator: std.mem.Allocator,
endpoint: []const u8,
req_body: []const u8,
) LightningError![]const u8 {
) !zul.StringBuilder {
const uri = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ self.lnbits_url, endpoint });
defer allocator.free(uri);

const header_buf = try allocator.alloc(u8, 1024 * 1024 * 4);
defer allocator.free(header_buf);

std.log.debug("uri: {s}", .{uri});
var req = try self.client.allocRequest(allocator, uri);
defer req.deinit();

var req = try self.client.open(.POST, try std.Uri.parse(uri), .{
.server_header_buffer = header_buf,
.extra_headers = &.{
.{
.name = "X-Api-Key",
.value = self.admin_key,
},
req.method = .POST;

.{
.name = "accept",
.value = "*/*",
},
},
});
defer req.deinit();
req.body(req_body);

req.transfer_encoding = .{ .content_length = req_body.len };
try req.header("X-Api-Key", self.admin_key);
try req.header("accept", "*/*");

try req.send();
try req.writeAll(req_body);
try req.finish();
try req.wait();
var res = try req.getResponse(.{});

if (req.response.status != .ok) {
if (req.response.status == .not_found) return LightningError.NotFound;
if (req.response.status == .unauthorized) return LightningError.Unauthorized;
if (res.status != 200) {
if (res.status == 404) return LightningError.NotFound;
if (res.status == 401) return LightningError.Unauthorized;
}

var rdr = req.reader();
const body = rdr.readAllAlloc(allocator, 1024 * 1024 * 4) catch |err| {
std.log.debug("read post body error: {any}", .{err});
return error.ReadBodyError;
};
errdefer allocator.free(body);

return body;
return try res.allocBody(allocator, .{});
}

/// createInvoice - creating invoice
Expand All @@ -351,37 +306,20 @@ pub const LNBitsClient = struct {
.emit_null_optional_fields = false,
});

std.log.debug("request {s}", .{req_body});

const res = try self.post(allocator, "api/v1/payments", req_body);
defer res.deinit();

std.log.debug("create invoice, response : {s}", .{res});

const parsed = std.json.parseFromSlice(std.json.Value, allocator, res, .{ .allocate = .alloc_always }) catch |err| {
errdefer std.log.debug("{any}", .{err});
const parsed = std.json.parseFromSlice(CreateInvoiceResponse, allocator, res.string(), .{
.allocate = .alloc_always,
.ignore_unknown_fields = true,
}) catch |err| {
errdefer std.log.debug("Parse create invoice error: {any}, body:\n{s}", .{ err, res.string() });

return error.WrongJson;
};
defer parsed.deinit();

const payment_request = parsed.value.object.get("payment_request") orelse unreachable;
const payment_hash = parsed.value.object.get("payment_hash") orelse unreachable;

const pr = switch (payment_request) {
.string => |v| try allocator.dupe(u8, v),
else => unreachable,
};
errdefer allocator.free(pr);

const ph = switch (payment_hash) {
.string => |v| try allocator.dupe(u8, v),
else => unreachable,
};
errdefer allocator.free(ph);

return .{
.payment_hash = ph,
.payment_request = pr,
};
return try parsed.value.clone(allocator);
}

/// payInvoice - paying invoice
Expand All @@ -390,8 +328,9 @@ pub const LNBitsClient = struct {
const req_body = try std.json.stringifyAlloc(allocator, &.{ .out = true, .bolt11 = bolt11 }, .{});

const res = try self.post(allocator, "api/v1/payments", req_body);
defer res.deinit();

const parsed = std.json.parseFromSlice(std.json.Value, allocator, res, .{ .allocate = .alloc_always }) catch return error.WrongJson;
const parsed = std.json.parseFromSlice(std.json.Value, allocator, res.string(), .{ .allocate = .alloc_always }) catch return error.WrongJson;

const payment_hash = parsed.value.object.get("payment_hash") orelse unreachable;

Expand Down Expand Up @@ -567,6 +506,15 @@ pub const CreateInvoiceResponse = struct {
/// Payment request (bolt11)
payment_request: []const u8,

pub fn clone(self: CreateInvoiceResponse, alloc: std.mem.Allocator) !CreateInvoiceResponse {
var s: CreateInvoiceResponse = undefined;
errdefer s.deinit(alloc);

s.payment_hash = try alloc.dupe(u8, self.payment_hash);
s.payment_request = try alloc.dupe(u8, self.payment_request);
return s;
}

pub fn deinit(self: CreateInvoiceResponse, alloc: std.mem.Allocator) void {
alloc.free(self.payment_hash);
alloc.free(self.payment_request);
Expand Down
6 changes: 6 additions & 0 deletions src/core/nuts/nut04/nut04.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub const QuoteState = enum {
pub fn jsonStringify(self: *const QuoteState, out: anytype) !void {
try out.write(self.toStr());
}

pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !QuoteState {
const state = try std.json.innerParse([]const u8, allocator, source, options);

return QuoteState.fromStr(state) catch error.UnexpectedToken;
}
};

pub const MintMethodSettings = struct {
Expand Down
4 changes: 2 additions & 2 deletions src/mint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ pub fn main() !void {
break :v try MintDatabase.initFrom(MintMemoryDatabase, gpa.allocator(), db);
},
inline .sqlite => v: {
std.log.warn("hello", .{});
var db = try core.mint_memory.MintSqliteDatabase.initFrom(gpa.allocator(), "./cdk-mintd.sqlite");
// TODO custom path to database?
var db = try core.mint_memory.MintSqliteDatabase.initFrom(gpa.allocator(), "./cocomint_db.sqlite");
errdefer db.deinit();

break :v try MintDatabase.initFrom(core.mint_memory.MintSqliteDatabase, gpa.allocator(), db);
Expand Down
2 changes: 2 additions & 0 deletions src/router/router_handlers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub fn getCheckMintBolt11Quote(
req: *httpz.Request,
res: *httpz.Response,
) !void {
errdefer std.log.debug("{any}", .{@errorReturnTrace()});

const quote_id_hex = req.param("quote_id") orelse return error.ExpectQuoteId;

const quote_id = try zul.UUID.parse(quote_id_hex);
Expand Down

0 comments on commit 81bf2f6

Please sign in to comment.