Skip to content

Commit

Permalink
generate addr
Browse files Browse the repository at this point in the history
  • Loading branch information
iskyd committed Oct 5, 2024
1 parent 612aafc commit b3a5dff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/db/db.zig
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub fn getDescriptors(allocator: std.mem.Allocator, db: *sqlite.Db) ![]Descripto
}

pub fn getDescriptor(allocator: std.mem.Allocator, db: *sqlite.Db, path: []u8, private: bool) !?Descriptor {
const sql = "SELECT extended_key, path, private FROM descriptor WHERE path=? AND private=? LIMIT 1;";
const sql = "SELECT extended_key, path, private FROM descriptors WHERE path=? AND private=? LIMIT 1;";
var stmt = try db.prepare(sql);
defer stmt.deinit();
const row = try stmt.oneAlloc(struct { extended_key: [111]u8, path: []const u8, private: bool }, allocator, .{}, .{ .path = path, .private = private });
Expand Down Expand Up @@ -243,3 +243,26 @@ pub fn getUsedKeyPaths(allocator: std.mem.Allocator, db: *sqlite.Db) ![]KeyPath(
}
return keypaths;
}

fn sqliteKeypathLastIndex(str: []const u8) u32 {
const k = KeyPath(5).fromStr(str) catch return 0;
return k.path[4];
}

pub fn getLastUsedIndexFromOutputs(db: *sqlite.Db) !?u32 {
const sql_count = "SELECT COUNT(*) as total from outputs;";
var stmt_count = try db.prepare(sql_count);
defer stmt_count.deinit();
const row_count = try stmt_count.one(struct { total: usize }, .{}, .{});
if (row_count.?.total == 0) {
return null;
}

try db.createScalarFunction("KEYPATH_LAST_INDEX", sqliteKeypathLastIndex, .{});
const sql = "SELECT MAX(KEYPATH_LAST_INDEX(path)) AS last FROM outputs;";
var stmt = try db.prepare(sql);
defer stmt.deinit();
const row = try stmt.one(struct { last: u32 }, .{}, .{});
assert(row != null); // a row must exists since the count is > 0
return row.?.last;
}
31 changes: 31 additions & 0 deletions src/walle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const bip32 = @import("bip32.zig");
const bip44 = @import("bip44.zig");
const Network = @import("const.zig").Network;
const utils = @import("utils.zig");
const address = @import("address.zig");
const script = @import("script.zig");

fn showHelp() void {
std.debug.print("Valid commands: walletcreate, walletimport\nFor more information use walle <cmd> help", .{});
Expand All @@ -16,6 +18,7 @@ pub fn main() !void {

const Commands = enum {
walletcreate,
addrnew,
};

const args = std.process.argsAlloc(allocator) catch {
Expand Down Expand Up @@ -88,5 +91,33 @@ pub fn main() !void {

std.debug.print("Wallet initialized\n", .{});
},
.addrnew => {
if (args.len < 3 or std.mem.eql(u8, args[2], "help")) {
std.debug.print("Create new wallet\nwalle addrnew <mainnet/testnet>\n", .{});
return;
}

const network: Network = if (std.mem.eql(u8, args[2], "mainnet")) .mainnet else .testnet;
const res = try db.getLastUsedIndexFromOutputs(&database);
var next_index: u32 = 0;
if (res != null) {
next_index = res.?;
}

var descriptor_path = "84'/1'/0'".*;
const descriptor = try db.getDescriptor(allocator, &database, &descriptor_path, false);
if (descriptor == null) {
std.debug.print("A wallet do not exists. Please create it using walletcreate\n", .{});
return;
}
// Generate index with change = 1 for external address
const account_pubkey = try bip32.ExtendedPublicKey.fromAddress(descriptor.?.extended_key);
const pubkey = try bip44.generatePublicFromAccountPublicKey(account_pubkey, bip44.change_external_chain, next_index);
const pubkey_hash = try pubkey.toHashHex();
const s = try script.p2wpkh(allocator, &pubkey_hash);
const addr = try address.deriveP2WPKHAddress(allocator, s, network);
defer addr.deinit();
std.debug.print("addr {s}\n", .{addr.val});
},
}
}

0 comments on commit b3a5dff

Please sign in to comment.