diff --git a/lib_crypto/ed25519.ml b/lib_crypto/ed25519.ml index 33bfd410b..6877b58af 100644 --- a/lib_crypto/ed25519.ml +++ b/lib_crypto/ed25519.ml @@ -62,7 +62,24 @@ module Public_key = struct let of_hex_exn s = of_string_exn (Hex.to_string s) let to_hex s = Hex.of_string (to_string s) - let of_bytes s = Sodium.Sign.Bytes.to_public_key s + let of_bytes_opt s = + match Sodium.Sign.Bigbytes.to_public_key s with + | exception _ -> None + | pk -> Some pk + + let of_bytes s = + match of_bytes_opt s with + | None -> + generic_error "Could not deserialize Ed25519 public key (invalid format)" + | Some pk -> ok pk + + let of_bytes_exn s = + match of_bytes_opt s with + | None -> + Pervasives.invalid_arg "Ed25519.Public_key.of_bytes_exn: argument is not a serialized public key" + | Some pk -> pk + + let to_bytes = Sodium.Sign.Bigbytes.of_public_key let param ?(name="ed25519-public") ?(desc="Ed25519 public key (b58check-encoded)") t = Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t) @@ -126,7 +143,25 @@ module Secret_key = struct | None -> generic_error "Unexpected hash (ed25519 secret key)" let to_b58check s = Base58.simple_encode b58check_encoding s - let of_bytes s = Sodium.Sign.Bytes.to_secret_key s + let of_bytes_opt s = + match Sodium.Sign.Bigbytes.to_seed s with + | exception _ -> None + | seed -> Some (seed |> Sodium.Sign.seed_keypair |> fst) + + let of_bytes s = + match of_bytes_opt s with + | None -> + generic_error "Could not deserialize Ed25519 seed (invalid format)" + | Some sk -> ok sk + + let of_bytes_exn s = + match of_bytes_opt s with + | None -> + Pervasives.invalid_arg "Ed25519.Secret_key.of_bytes_exn: argument is not a serialized seed" + | Some sk -> sk + + let to_bytes sk = + Sodium.Sign.(sk |> secret_key_to_seed |> Bigbytes.of_seed) let param ?(name="ed25519-secret") ?(desc="Ed25519 secret key (b58check-encoded)") t = Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t) @@ -185,7 +220,24 @@ module Signature = struct | None -> generic_error "Unexpected hash (ed25519 signature)" let to_b58check s = Base58.simple_encode b58check_encoding s - let of_bytes s = MBytes.of_string (Bytes.to_string s) + let of_bytes_opt s = + match Sodium.Sign.Bigbytes.to_signature s with + | exception _ -> None + | _signature -> Some s + + let of_bytes s = + match of_bytes_opt s with + | None -> + generic_error "Could not deserialize Ed25519 signature (invalid format)" + | Some signature -> ok signature + + let of_bytes_exn s = + match of_bytes_opt s with + | None -> + Pervasives.invalid_arg "Ed25519.Signature.of_bytes_exn: argument is not a serialized signature" + | Some signature -> signature + + let to_bytes x = x let param ?(name="signature") ?(desc="Signature (b58check-encoded)") t = Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t) diff --git a/lib_crypto/ed25519.mli b/lib_crypto/ed25519.mli index 82b0ad2f7..d6d56f6c7 100644 --- a/lib_crypto/ed25519.mli +++ b/lib_crypto/ed25519.mli @@ -40,7 +40,10 @@ module Public_key : sig val of_hex: Hex.t -> t option val of_hex_exn: Hex.t -> t - val of_bytes: Bytes.t -> t + val of_bytes: MBytes.t -> t tzresult + val of_bytes_exn: MBytes.t -> t + val of_bytes_opt: MBytes.t -> t option + val to_bytes: t -> MBytes.t end @@ -65,7 +68,10 @@ module Secret_key : sig val of_b58check_opt: string -> t option val to_b58check: t -> string - val of_bytes: Bytes.t -> t + val of_bytes: MBytes.t -> t tzresult + val of_bytes_exn: MBytes.t -> t + val of_bytes_opt: MBytes.t -> t option + val to_bytes: t -> MBytes.t end @@ -88,7 +94,10 @@ module Signature : sig val of_b58check_opt: string -> t option val to_b58check: t -> string - val of_bytes: Bytes.t -> t + val of_bytes: MBytes.t -> t tzresult + val of_bytes_exn: MBytes.t -> t + val of_bytes_opt: MBytes.t -> t option + val to_bytes: t -> MBytes.t (** Checks a signature *) val check: Public_key.t -> t -> MBytes.t -> bool diff --git a/lib_protocol_environment_sigs/v1/ed25519.mli b/lib_protocol_environment_sigs/v1/ed25519.mli index 36faed593..b15cbe1b0 100644 --- a/lib_protocol_environment_sigs/v1/ed25519.mli +++ b/lib_protocol_environment_sigs/v1/ed25519.mli @@ -30,7 +30,9 @@ module Public_key : sig val of_b58check_opt: string -> t option val to_b58check: t -> string - val of_bytes: Bytes.t -> t + val of_bytes_exn: MBytes.t -> t + val of_bytes_opt: MBytes.t -> t option + val to_bytes: t -> MBytes.t val to_hex: t -> [ `Hex of string ] val of_hex: [ `Hex of string ] -> t option @@ -50,7 +52,9 @@ module Secret_key : sig val of_b58check_opt: string -> t option val to_b58check: t -> string - val of_bytes: Bytes.t -> t + val of_bytes_exn: MBytes.t -> t + val of_bytes_opt: MBytes.t -> t option + val to_bytes: t -> MBytes.t end @@ -66,7 +70,9 @@ module Signature : sig val of_b58check_opt: string -> t option val to_b58check: t -> string - val of_bytes: Bytes.t -> t + val of_bytes_exn: MBytes.t -> t + val of_bytes_opt: MBytes.t -> t option + val to_bytes: t -> MBytes.t (** Checks a signature *) val check: Public_key.t -> t -> MBytes.t -> bool