Skip to content

Commit

Permalink
fix json error + schnor sign toStr + expose constants
Browse files Browse the repository at this point in the history
  • Loading branch information
StringNick committed Sep 11, 2024
1 parent 03d1ec9 commit 210483f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/schnorr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions src/secp256k1.zig
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 210483f

Please sign in to comment.