From 60c08f0a418df148793870cdbde4b2a2cec73bd0 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Tue, 25 May 2021 15:25:43 +0200 Subject: Rename tagged union fields in znk --- src/nkeys.zig | 16 ++++++++++++++-- src/znk.zig | 32 ++++++++++++++++---------------- 2 files changed, 30 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/nkeys.zig b/src/nkeys.zig index ac5ba1b..42a781f 100644 --- a/src/nkeys.zig +++ b/src/nkeys.zig @@ -26,10 +26,10 @@ pub const KeyTypePrefixByte = enum(u8) { private = 15 << 3, // P fn char(self: Self) u8 { - switch (self) { + return switch (self) { .seed => 'S', .private => 'P', - } + }; } fn fromChar(c: u8) InvalidPrefixByteError!Self { @@ -61,6 +61,16 @@ pub const PublicPrefixByte = enum(u8) { }; } + fn char(self: Self) u8 { + return switch (self) { + .account => 'A', + .cluster => 'C', + .operator => 'O', + .server => 'N', + .user => 'U', + }; + } + fn fromChar(c: u8) InvalidPrefixByteError!Self { return switch (c) { 'A' => .account, @@ -464,6 +474,8 @@ pub fn parseDecoratedUserNkey(contents: []const u8) (NoNkeySeedFoundError || NoN test { testing.refAllDecls(@This()); + testing.refAllDecls(KeyTypePrefixByte); + testing.refAllDecls(PublicPrefixByte); testing.refAllDecls(SeedKeyPair); testing.refAllDecls(PublicKey); testing.refAllDecls(PrivateKey); diff --git a/src/znk.zig b/src/znk.zig index 4ab3077..b088dc4 100644 --- a/src/znk.zig +++ b/src/znk.zig @@ -232,7 +232,7 @@ pub fn cmdSign(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !vo fatal("could not read file to generate signature for", .{}); }; var nkey = readKeyFile(arena, key.?) orelse fatal("could not find a valid key", .{}); - if (nkey == .public_key) fatal("key was provided but is not a seed or private key", .{}); + if (nkey == .PublicKey) fatal("key was provided but is not a seed or private key", .{}); defer nkey.wipe(); const sig = nkey.sign(content) catch fatal("could not generate signature", .{}); @@ -424,15 +424,15 @@ fn toUpper(allocator: *Allocator, slice: []const u8) ![]u8 { pub const Nkey = union(enum) { const Self = @This(); - seed_key_pair: nkeys.SeedKeyPair, - public_key: nkeys.PublicKey, - private_key: nkeys.PrivateKey, + SeedKeyPair: nkeys.SeedKeyPair, + PublicKey: nkeys.PublicKey, + PrivateKey: nkeys.PrivateKey, pub fn wipe(self: *Self) void { switch (self.*) { - .seed_key_pair => |*kp| kp.wipe(), - .public_key => |*pk| pk.wipe(), - .private_key => |*pk| pk.wipe(), + .SeedKeyPair => |*kp| kp.wipe(), + .PublicKey => |*pk| pk.wipe(), + .PrivateKey => |*pk| pk.wipe(), } } @@ -442,9 +442,9 @@ pub const Nkey = union(enum) { sig: [std.crypto.sign.Ed25519.signature_length]u8, ) !void { return switch (self.*) { - .seed_key_pair => |*kp| try kp.verify(msg, sig), - .public_key => |*pk| try pk.verify(msg, sig), - .private_key => |*pk| try pk.verify(msg, sig), + .SeedKeyPair => |*kp| try kp.verify(msg, sig), + .PublicKey => |*pk| try pk.verify(msg, sig), + .PrivateKey => |*pk| try pk.verify(msg, sig), }; } @@ -453,9 +453,9 @@ pub const Nkey = union(enum) { msg: []const u8, ) ![std.crypto.sign.Ed25519.signature_length]u8 { return switch (self.*) { - .seed_key_pair => |*kp| try kp.sign(msg), - .private_key => |*pk| try pk.sign(msg), - .public_key => return error.CantSign, + .SeedKeyPair => |*kp| try kp.sign(msg), + .PrivateKey => |*pk| try pk.sign(msg), + .PublicKey => return error.CantSign, }; } @@ -465,17 +465,17 @@ pub const Nkey = union(enum) { 'S' => { // It's a seed. if (text.len != nkeys.text_seed_len) return error.InvalidSeed; - return Self{ .seed_key_pair = try nkeys.SeedKeyPair.fromTextSeed(text[0..nkeys.text_seed_len]) }; + return Self{ .SeedKeyPair = try nkeys.SeedKeyPair.fromTextSeed(text[0..nkeys.text_seed_len]) }; }, 'P' => { // It's a private key. if (text.len != nkeys.text_private_len) return error.InvalidPrivateKey; - return Self{ .private_key = try nkeys.PrivateKey.fromTextPrivateKey(text[0..nkeys.text_private_len]) }; + return Self{ .PrivateKey = try nkeys.PrivateKey.fromTextPrivateKey(text[0..nkeys.text_private_len]) }; }, else => { // It should be a public key. if (text.len != nkeys.text_public_len) return error.InvalidEncoding; - return Self{ .public_key = try nkeys.PublicKey.fromTextPublicKey(text[0..nkeys.text_public_len]) }; + return Self{ .PublicKey = try nkeys.PublicKey.fromTextPublicKey(text[0..nkeys.text_public_len]) }; }, } } -- cgit v1.2.3