-
Notifications
You must be signed in to change notification settings - Fork 8
add signet support #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
futurepaul
wants to merge
2
commits into
leishman:master
Choose a base branch
from
futurepaul:signet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,159 @@ | ||
| // Network.zig - Network configuration (mainnet/signet) | ||
| // | ||
| // Usage: yam --signet <challenge_hex> [command] | ||
| // | ||
| // These are "set once at startup" static variables - never modified after init. | ||
|
|
||
| const std = @import("std"); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Static network configuration (set once at startup, never modified after) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Public signet challenge (Bitcoin Core default) | ||
| pub const default_signet_challenge_hex = | ||
| "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae"; | ||
|
|
||
| /// Public signet DNS seeds (Bitcoin Core default) | ||
| pub const signet_dns_seeds = [_][]const u8{ | ||
| "seed.signet.bitcoin.sprovoost.nl", | ||
| "seed.signet.achownodes.xyz", | ||
| }; | ||
|
|
||
| /// Network magic bytes (mainnet: 0xD9B4BEF9, signet: computed from challenge) | ||
| pub var magic: u32 = 0xD9B4BEF9; | ||
|
|
||
| /// Default P2P port (mainnet: 8333, signet: 38333) | ||
| pub var default_port: u16 = 8333; | ||
|
|
||
| /// Whether we're running in signet mode | ||
| pub var is_signet: bool = false; | ||
|
|
||
| /// Whether to use DNS seeds for signet discovery | ||
| pub var has_signet_seeds: bool = false; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Runtime initialization (call from main before anything else) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Initialize signet mode at runtime from --signet flag. | ||
| /// Must be called before any network operations. | ||
| pub fn initSignet(challenge_hex: []const u8) !void { | ||
| magic = try computeSignetMagic(challenge_hex); | ||
| default_port = 38333; | ||
| is_signet = true; | ||
| has_signet_seeds = false; | ||
| } | ||
|
|
||
| /// Initialize default (public) signet. | ||
| pub fn initSignetDefault() !void { | ||
| try initSignet(default_signet_challenge_hex); | ||
| has_signet_seeds = true; | ||
| } | ||
|
|
||
| pub const SignetParseResult = struct { | ||
| cmd_arg: ?[]const u8, | ||
| enabled: bool, | ||
| }; | ||
|
|
||
| /// Parse leading --signet flag and initialize network settings. | ||
| /// Returns the command argument to use (if any). | ||
| pub fn parseSignetArgs(args_iter: anytype) !SignetParseResult { | ||
| const first_arg = args_iter.next(); | ||
| if (first_arg) |arg| { | ||
| if (std.mem.eql(u8, arg, "--signet")) { | ||
| const maybe_next = args_iter.next(); | ||
| if (maybe_next) |next| { | ||
| if (isCommandArg(next)) { | ||
| try initSignetDefault(); | ||
| return .{ .cmd_arg = next, .enabled = true }; | ||
| } | ||
| try initSignet(next); | ||
| return .{ .cmd_arg = args_iter.next(), .enabled = true }; | ||
| } | ||
| try initSignetDefault(); | ||
| return .{ .cmd_arg = args_iter.next(), .enabled = true }; | ||
| } | ||
| } | ||
|
|
||
| return .{ .cmd_arg = first_arg, .enabled = false }; | ||
| } | ||
|
|
||
| /// Compute signet magic from challenge hex. | ||
| /// Algorithm: first 4 bytes of SHA256d(CompactSize(len) ++ challenge_bytes) | ||
| fn computeSignetMagic(challenge_hex: []const u8) !u32 { | ||
| if (challenge_hex.len % 2 != 0) return error.InvalidHexLength; | ||
| const challenge_len: u64 = challenge_hex.len / 2; | ||
|
|
||
| // Encode CompactSize length prefix (varint) | ||
| var prefix: [9]u8 = undefined; | ||
| const prefix_len = encodeCompactSize(&prefix, challenge_len); | ||
|
|
||
| // SHA256d (double SHA256) with streaming input | ||
| var sha = std.crypto.hash.sha2.Sha256.init(.{}); | ||
| sha.update(prefix[0..prefix_len]); | ||
|
|
||
| var i: usize = 0; | ||
| var one: [1]u8 = undefined; | ||
| while (i < challenge_hex.len) : (i += 2) { | ||
| one[0] = std.fmt.parseInt(u8, challenge_hex[i..][0..2], 16) catch | ||
| return error.InvalidHexChar; | ||
| sha.update(one[0..1]); | ||
| } | ||
|
|
||
| var h1: [32]u8 = undefined; | ||
| sha.final(&h1); | ||
|
|
||
| var h2: [32]u8 = undefined; | ||
| std.crypto.hash.sha2.Sha256.hash(&h1, &h2, .{}); | ||
|
|
||
| return std.mem.readInt(u32, h2[0..4], .little); | ||
| } | ||
|
|
||
| fn encodeCompactSize(buf: *[9]u8, value: u64) usize { | ||
| if (value < 0xfd) { | ||
| buf[0] = @intCast(value); | ||
| return 1; | ||
| } else if (value <= 0xffff) { | ||
| buf[0] = 0xfd; | ||
| const v: u16 = @intCast(value); | ||
| buf[1] = @intCast(v & 0xff); | ||
| buf[2] = @intCast((v >> 8) & 0xff); | ||
| return 3; | ||
| } else if (value <= 0xffffffff) { | ||
| buf[0] = 0xfe; | ||
| const v: u32 = @intCast(value); | ||
| buf[1] = @intCast(v & 0xff); | ||
| buf[2] = @intCast((v >> 8) & 0xff); | ||
| buf[3] = @intCast((v >> 16) & 0xff); | ||
| buf[4] = @intCast((v >> 24) & 0xff); | ||
| return 5; | ||
| } else { | ||
| buf[0] = 0xff; | ||
| const v: u64 = value; | ||
| buf[1] = @intCast(v & 0xff); | ||
| buf[2] = @intCast((v >> 8) & 0xff); | ||
| buf[3] = @intCast((v >> 16) & 0xff); | ||
| buf[4] = @intCast((v >> 24) & 0xff); | ||
| buf[5] = @intCast((v >> 32) & 0xff); | ||
| buf[6] = @intCast((v >> 40) & 0xff); | ||
| buf[7] = @intCast((v >> 48) & 0xff); | ||
| buf[8] = @intCast((v >> 56) & 0xff); | ||
| return 9; | ||
| } | ||
| } | ||
|
|
||
| fn isCommandArg(arg: []const u8) bool { | ||
| return std.mem.eql(u8, arg, "broadcast") or | ||
| std.mem.eql(u8, arg, "explore") or | ||
| std.mem.eql(u8, arg, "help") or | ||
| std.mem.eql(u8, arg, "--help") or | ||
| std.mem.eql(u8, arg, "-h"); | ||
| } | ||
|
|
||
| test "compute signet magic from BIP-325 example" { | ||
| const challenge = | ||
| "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be43051ae"; | ||
| const computed_magic = try computeSignetMagic(challenge); | ||
| try std.testing.expectEqual(@as(u32, 0xA553C67E), computed_magic); | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
discovercommand. Runningyam --signet discoverwill incorrectly treat "discover" as a custom challenge hex, fail to parse it, and return an error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch thank you. This also demonstrated how invasive adding signet as a flag is. I changed it to an environment variable so it's less disruptive to overall logic / usage.