From 210483ff380859caf3d3701ae66e01501224e100 Mon Sep 17 00:00:00 2001 From: StringNick Date: Wed, 11 Sep 2024 12:59:21 +0200 Subject: [PATCH] fix json error + schnor sign toStr + expose constants --- src/schnorr.zig | 4 ++++ src/secp256k1.zig | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/schnorr.zig b/src/schnorr.zig index b5662ed..90a632c 100644 --- a/src/schnorr.zig +++ b/src/schnorr.zig @@ -27,6 +27,10 @@ pub const Signature = struct { return .{ .inner = res }; } + + pub inline fn toStr(self: *const Signature) [constants.schnorr_signature_size * 2]u8 { + return std.fmt.bytesToHex(self.inner, .lower); + } }; pub const Secp = struct { diff --git a/src/secp256k1.zig b/src/secp256k1.zig index e6fdb17..db5f9e9 100644 --- a/src/secp256k1.zig +++ b/src/secp256k1.zig @@ -1,10 +1,11 @@ const std = @import("std"); const crypto = std.crypto; -const constants = @import("constants.zig"); const ecdsa_lib = @import("ecdsa/ecdsa.zig"); const recovery_lib = @import("ecdsa/recovery.zig"); const schnorr_lib = @import("schnorr.zig"); +pub const constants = @import("constants.zig"); + /// The main error type for this library. pub const Error = error{ /// Signature failed verification. @@ -251,11 +252,11 @@ pub const PublicKey = struct { } // json serializing func - pub fn jsonStringify(self: PublicKey, out: anytype) (Error || std.json.Error)!void { + pub fn jsonStringify(self: PublicKey, out: anytype) !void { try out.write(std.fmt.bytesToHex(&self.serialize(), .lower)); } - pub fn jsonParse(_: std.mem.Allocator, source: anytype, _: std.json.ParseOptions) std.json.Error!@This() { + pub fn jsonParse(_: std.mem.Allocator, source: anytype, _: std.json.ParseOptions) !@This() { switch (try source.next()) { .string => |s| { var hex_buffer: [60]u8 = undefined; @@ -516,11 +517,11 @@ pub const SecretKey = struct { } } - pub fn jsonStringify(self: *const SecretKey, out: anytype) (Error || std.json.Error)!void { + pub fn jsonStringify(self: *const SecretKey, out: anytype) !void { try out.write(self.toString()); } - pub fn jsonParse(_: std.mem.Allocator, source: anytype, _: std.json.ParseOptions) std.json.ParseError(source)!@This() { + pub fn jsonParse(_: std.mem.Allocator, source: anytype, _: std.json.ParseOptions) !@This() { return switch (try source.next()) { .string, .allocated_string => |hex_sec| SecretKey.fromString(hex_sec) catch return error.UnexpectedToken, else => return error.UnexpectedToken,