Skip to content

Commit

Permalink
Merge pull request #11 from zig-bitcoin/main
Browse files Browse the repository at this point in the history
Misc: Vanity address generator (zig-bitcoin#68)
  • Loading branch information
KhairallahA authored Sep 23, 2024
2 parents 77276c9 + 48a5db6 commit dec6bd5
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.idea
profile.json
**/.DS_Store

.zig-cache
zig-out

.bitcoin
.vscode
.vscode
45 changes: 45 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub fn build(b: *std.Build) !void {
const deps = build_helpers.generateModuleDependencies(
b,
&external_dependencies,
.{
.optimize = optimize,
.target = target,
},
) catch unreachable;

// **************************************************************
Expand Down Expand Up @@ -73,6 +77,40 @@ pub fn build(b: *std.Build) !void {
// running `zig build`).
b.installArtifact(lib);

// **************************************************************
// * VANITYGEN AS AN EXECUTABLE *
// **************************************************************
{
const exe = b.addExecutable(.{
.name = "vanitygen",
.root_source_file = b.path("src/vanitygen.zig"),
.target = target,
.optimize = optimize,
.single_threaded = false,
.omit_frame_pointer = true,
.strip = false,
});
// Add dependency modules to the executable.
for (deps) |mod| exe.root_module.addImport(
mod.name,
mod.module,
);

exe.root_module.addImport("btczee", &lib.root_module);

b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());

if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step("vanitygen-run", "Run the app");
run_step.dependOn(&run_cmd.step);
}

// **************************************************************
// * BTCZEE AS AN EXECUTABLE *
// **************************************************************
Expand Down Expand Up @@ -118,9 +156,16 @@ pub fn build(b: *std.Build) !void {
mod.name,
mod.module,
);

const check_test = b.addTest(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
});

// This step is used to check if btczee compiles, it helps to provide a faster feedback loop when developing.
const check = b.step("check", "Check if btczee compiles");
check.dependOn(&exe_check.step);
check.dependOn(&check_test.step);
}

// **************************************************************
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
.hash = "1220f9e1eb744c8dc2750c1e6e1ceb1c2d521bedb161ddead1a6bb772032e576d74a",
},
.@"bitcoin-primitives" = .{
.url = "git+https://github.com/zig-bitcoin/bitcoin-primitives#3743701d398b35af80826b729b6eb12d3e8e8df9",
.hash = "12204e7aa2c42049440faf891e80cd7bc85f64b4aacdcda75e891ca52787f267342c",
.url = "git+https://github.com/zig-bitcoin/bitcoin-primitives#6595846b34c8c157175c52380f5c7cc6fc9ca108",
.hash = "12208a138853cd57db1c5e3348d60a74aa54d5c0a63393b6367098f1c150a0c31438",
},
},
.paths = .{
Expand Down
3 changes: 2 additions & 1 deletion build_helpers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub const Dependency = struct {
pub fn generateModuleDependencies(
b: *std.Build,
external_dependencies: []const Dependency,
dependencies_opts: anytype,
) ![]std.Build.Module.Import {
var dependency_modules = std.ArrayList(*std.Build.Module).init(b.allocator);
defer _ = dependency_modules.deinit();
Expand All @@ -24,7 +25,7 @@ pub fn generateModuleDependencies(
for (external_dependencies) |dep| {
const module = b.dependency(
dep.name,
.{},
dependencies_opts,
).module(dep.module_name);
_ = dependency_modules.append(module) catch unreachable;
}
Expand Down
105 changes: 105 additions & 0 deletions src/address/address.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const std = @import("std");
const bitcoin_primitives = @import("bitcoin-primitives");

const NetworkKind = @import("../network/network.zig").NetworkKind;
const Sha256 = std.crypto.hash.sha2.Sha256;
const Hash160 = bitcoin_primitives.hashes.Hash160;

/// The different types of addresses.
pub const AddressType = enum {
/// Pay to pubkey hash.
p2pkh,
/// Pay to script hash.
p2sh,
/// Pay to witness pubkey hash.
p2wpkh,
/// Pay to witness script hash.
p2wsh,
/// Pay to taproot.
p2tr,
};

// TODO move to crypto
/// A hash of a public key.
pub const PubkeyHash = struct {
// hash160
inner: [Hash160.digest_length]u8,
};

/// SegWit version of a public key hash.
pub const WpubkeyHash = struct {
// hash160
inner: [Hash160.digest_length]u8,
};

/// A hash of Bitcoin Script bytecode.
pub const ScriptHash = struct {
// hash160
inner: [Hash160.digest_length]u8,
};
/// SegWit version of a Bitcoin Script bytecode hash.
pub const WScriptHash = struct {
// sha256 hash
inner: [Sha256.digest_length]u8,
};

/// Known bech32 human-readable parts.
///
/// This is the human-readable part before the separator (`1`) in a bech32 encoded address e.g.,
/// the "bc" in "bc1p2wsldez5mud2yam29q22wgfh9439spgduvct83k3pm50fcxa5dps59h4z5".
pub const KnownHrp = enum {
/// The main Bitcoin network.
mainnet,
/// The test networks, testnet and signet.
testnets,
/// The regtest network.
regtest,
};

// TODO move blockdata constants
/// Mainnet (bitcoin) pubkey address prefix.
pub const pubkey_address_prefix_main: u8 = 0; // 0x00
/// Test (tesnet, signet, regtest) pubkey address prefix.
pub const pubkey_address_prefix_test: u8 = 111; // 0x6f

pub const Address = union(enum) {
p2pkh: struct { hash: PubkeyHash, network: NetworkKind },
p2sh: struct { hash: ScriptHash, network: NetworkKind },
// TODO WitnessProgram
// segwit: struct { program: WitnessProgram, hrp: KnownHrp },

/// inint p2pkh address
pub fn initP2pkh(hash: PubkeyHash, network: NetworkKind) Address {
return .{
.p2pkh = .{
.hash = hash,
.network = network,
},
};
}

// TODO make other types of address
/// Encoding address to string
/// caller responsible to free data
pub fn toString(self: Address) !std.BoundedArray(u8, 50) {
var buf: [50]u8 = undefined;
switch (self) {
.p2pkh => |addr| {
const prefixed: [21]u8 = [1]u8{switch (addr.network) {
.main => pubkey_address_prefix_main,
.@"test" => pubkey_address_prefix_test,
}} ++ addr.hash.inner;

var encoder = bitcoin_primitives.base58.Encoder{};

var res = try std.BoundedArray(u8, 50).init(0);

res.resize(encoder.encodeCheck(&res.buffer, &buf, &prefixed)) catch unreachable;

return res;
},
// TODO: implement another types of address
else => unreachable,
}
}
};
2 changes: 2 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ pub const config = @import("config/config.zig");
pub const mempool = @import("core/mempool.zig");
pub const p2p = @import("network/p2p.zig");
pub const rpc = @import("network/rpc.zig");
pub const network = @import("network/network.zig");
pub const storage = @import("storage/storage.zig");
pub const wallet = @import("wallet/wallet.zig");
pub const miner = @import("miner/miner.zig");
pub const node = @import("node/node.zig");
pub const script = @import("script/lib.zig");
pub const address = @import("address/address.zig");
pub const wire = @import("network/wire/lib.zig");

test {
Expand Down
32 changes: 32 additions & 0 deletions src/network/network.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// What kind of network we are on.
pub const NetworkKind = enum {
/// The Bitcoin mainnet network.
main,
/// Some kind of testnet network.
@"test",

pub fn fromNetwork(n: Network) NetworkKind {
return n.toKind();
}
};

/// The cryptocurrency network to act on.
pub const Network = enum {
/// Mainnet Bitcoin.
bitcoin,
/// Bitcoin's testnet network.
testnet,
/// Bitcoin's signet network.
signet,
/// Bitcoin's regtest network.
regtest,

pub fn toKind(self: Network) NetworkKind {
return switch (self) {
.bitcoin => .main,
.testnet, .signet, .regtest => .@"test",
};
}

// TODO: fromMagic and etc
};
4 changes: 2 additions & 2 deletions src/util/sync/mpmc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn UnboundedChannel(comptime T: type) type {
new_stack |= (stack & ~PTR_MASK);

// Push to the stack with a release barrier for the consumer to see the proper list links.
stack = self.stack.tryCompareAndSwap(
stack = self.stack.cmpxchgStrong(
stack,
new_stack,
.release,
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn UnboundedChannel(comptime T: type) type {

// Acquire barrier on getting the consumer to see cache/Node updates done by previous consumers
// and to ensure our cache/Node updates in pop() happen after that of previous consumers.
stack = self.stack.tryCompareAndSwap(
stack = self.stack.cmpxchgStrong(
stack,
new_stack,
.acquire,
Expand Down
2 changes: 1 addition & 1 deletion src/util/sync/ref.zig
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ pub fn Arc(comptime T: type) type {
}

inline fn innerPtr(self: *const Self) *Inner {
return @fieldParentPtr("value", self.value);
return @alignCast(@fieldParentPtr("value", self.value));
}

/// A multi-threaded, weak reference to a reference-counted value.
Expand Down
Loading

0 comments on commit dec6bd5

Please sign in to comment.