forked from zig-bitcoin/btczee
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from zig-bitcoin/main
Misc: Vanity address generator (zig-bitcoin#68)
- Loading branch information
Showing
10 changed files
with
478 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.