Skip to content

Commit

Permalink
address comments, add clone on MintInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
lana-shanghai committed Oct 2, 2024
1 parent 26cb207 commit 039141a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/core/database/wallet_memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ pub const WalletMemoryDatabase = struct {
errdefer _melt_quotes.deinit();

for (melt_quotes) |q| {
try _melt_quotes.put(q.id, q);
try _melt_quotes.put(q.id, try q.clone(allocator));
}

var _mint_keys = std.AutoHashMap(nuts.Id, nuts.nut01.Keys).init(allocator);
errdefer _mint_keys.deinit();
errdefer {
var it = _mint_keys.valueIterator();
while (it.next()) |k| {
k.deinit(allocator);
}
}

for (mint_keys) |k| {
try _mint_keys.put(try nuts.Id.fromKeys(allocator, k.inner), k);
Expand Down Expand Up @@ -92,7 +97,7 @@ pub const WalletMemoryDatabase = struct {
self.lock.lock();
defer self.lock.unlock();

try self.mints.put(mint_url, mint_info);
try self.mints.put(mint_url, mint_info.?);
}

pub fn removeMint(
Expand All @@ -102,10 +107,11 @@ pub const WalletMemoryDatabase = struct {
self.lock.lock();
defer self.lock.unlock();

_ = self.mints.fetchRemove(mint_url) orelse return;
const kv = self.mints.fetchRemove(mint_url) orelse return;
kv.value.?.deinit(self.allocator);
}

pub fn getMint(self: *Self, mint_url: []u8) !?MintInfo {
pub fn getMint(self: *Self, mint_url: []u8, allocator: std.mem.Allocator) !?MintInfo {
self.lock.lockShared();
defer self.lock.unlockShared();

Expand All @@ -114,7 +120,8 @@ pub const WalletMemoryDatabase = struct {
if (mint_info == null) {
return null;
}
return mint_info.?;

return if (self.mints.get(mint_url)) |m| try m.?.clone(allocator) else null;
}

pub fn getMints(self: *Self, allocator: std.mem.Allocator) !std.StringHashMap(?MintInfo) {
Expand All @@ -125,7 +132,8 @@ pub const WalletMemoryDatabase = struct {

var it = self.mints.iterator();
while (it.next()) |entry| {
try mints_copy.put(entry.key_ptr.*, entry.value_ptr.*);
const key_copy = try allocator.dupe(u8, entry.key_ptr.*);
try mints_copy.put(key_copy, entry.value_ptr.*);
}

return mints_copy;
Expand Down
28 changes: 28 additions & 0 deletions src/core/nuts/nut06/nut06.zig
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,34 @@ pub const MintInfo = struct {
mint_icon_url: ?[]const u8 = null,
/// message of the day that the wallet must display to the user
motd: ?[]const u8 = null,

pub fn deinit(self: MintInfo, allocator: std.mem.Allocator) void {
allocator.free(self.name.?);
// TODO check all fields that were allocated
}

pub fn clone(self: MintInfo, allocator: std.mem.Allocator) !MintInfo {
var cloned = self;

const name = try allocator.dupe(u8, self.name.?);
errdefer allocator.free(name);

const description = try allocator.dupe(u8, self.description.?);
errdefer allocator.free(description);

const description_long = try allocator.dupe(u8, self.description_long.?);
errdefer allocator.free(description_long);

const motd = try allocator.dupe(u8, self.motd.?);
errdefer allocator.free(motd);

cloned.name = name;
cloned.description = description;
cloned.description_long = description_long;
cloned.motd = motd;

return cloned;
}
};

/// Check state Settings
Expand Down

0 comments on commit 039141a

Please sign in to comment.