Skip to content
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

Fix json error + schnor sign toStr + expose constants #12

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
StringNick marked this conversation as resolved.
Show resolved Hide resolved
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
Loading