From 5e6f8766fba37fe27d18f671ea3c75de72022fdb Mon Sep 17 00:00:00 2001 From: swrup Date: Sat, 18 Oct 2025 20:28:38 +0200 Subject: [PATCH] quick fix; ok json response ~~ --- src/amount.ml | 26 ++++++++++++-------------- src/binary_formats.ml | 8 +++++--- src/config.ml | 2 +- src/types.ml | 15 ++++++++++----- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/amount.ml b/src/amount.ml index 5401b27e..1fb66513 100644 --- a/src/amount.ml +++ b/src/amount.ml @@ -1,6 +1,7 @@ (* TODO have safe amount arithmetic - make type private *) + make type private + use config currency option *) (* Amounts of currency, serialized as `:.` Fixed-precision numbers with 8 decimal places. - must be at most 11 characters long @@ -18,22 +19,19 @@ type t = { } let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e +let check_not msg = function false -> Ok () | true -> Error msg +let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 +let fraction_upper_bound = Int64.of_int 100_000_000 let make ~sign ~currency ~value ~fraction = - let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 in - let fraction_upper_bound = Int64.of_int 99_999_999 in - let* () = if value < Int64.zero then Error "value is negative" else Ok () in + let* () = check_not "value is negative" (value < Int64.zero) in + let* () = check_not "fraction is negative" (fraction < Int64.zero) in let* () = - if fraction < Int64.zero then Error "fraction is negative" else Ok () + check_not "value is greater than 2^52" (value > value_upper_bound) in let* () = - if value > value_upper_bound then Error "value is greater than 2^52" - else Ok () - in - let* () = - if fraction > fraction_upper_bound then - Error "fraction have more than 8 decimal digits" - else Ok () + check_not "fraction has more than 8 decimal digits" + (fraction >= fraction_upper_bound) in Ok { sign; currency; value; fraction } @@ -46,8 +44,8 @@ let to_string = in let pp_currency ppf = function `Eur -> string ppf "EUR" in fun ppf { sign; currency; value; fraction } -> - pf ppf "%a%a:%Ld.%Ld" (Fmt.option pp_sign) sign pp_currency currency value - fraction + pf ppf "%a%a:%Ld.%02Ld" (Fmt.option pp_sign) sign pp_currency currency + value fraction in Fmt.str "%a" pp diff --git a/src/binary_formats.ml b/src/binary_formats.ml index ad3f594e..4ddcc135 100644 --- a/src/binary_formats.ml +++ b/src/binary_formats.ml @@ -188,7 +188,9 @@ module UTIL = struct let hash s = match String.length s = 32 with - | false -> Fmt.failwith "SHA256 failure: data is not 32 bytes" + | false -> + Fmt.failwith "SHA256 failure: data is not 32 bytes, instead is %d" + (String.length s) | true -> hash s end @@ -218,7 +220,7 @@ module UTIL = struct val bin : t Bin.t val hash : M.src -> t end = struct - include Hash_64 + include HHH_64 let hash src = hash (M.to_octets src) end @@ -233,7 +235,7 @@ module UTIL = struct val bin : t Bin.t val hash : M.src -> t end = struct - include Hash_32 + include HHH_32 let hash src = hash (M.to_octets src) end diff --git a/src/config.ml b/src/config.ml index 5e6a6cc0..0ebe218b 100644 --- a/src/config.ml +++ b/src/config.ml @@ -1,7 +1,7 @@ (* hardcoded config for now *) let amount s = Amount.of_string s |> Result.get_ok -let zero_eur = amount "EUR:0.01" +let zero_eur = amount "EUR:0.00" let dummy_duration = Ptime.Span.of_int_s 99999999 module Exchange = struct diff --git a/src/types.ml b/src/types.ml index 7cf6b6ff..a376a10c 100644 --- a/src/types.ml +++ b/src/types.ml @@ -84,13 +84,18 @@ end = struct (* mirage_crypto: "The result is the concatenation of r and s, as specified in RFC 8032." *) Mirage_crypto_ec.Ed25519.sign ~key s - let of_b32 s = B32.decode s + let check_size t = + match String.length t = 64 with + | false -> Error "EddsaSignature: invalid string length" + | true -> Ok () - let to_b32 t = - let s = B32.encode t in - assert (String.length s = 64); - s + let of_b32 s = + let open Syntax in + let* t = B32.decode s in + let+ () = check_size t in + t + let to_b32 = B32.encode let jsont = Jsont.of_of_string ~kind:"EddsaSignature" of_b32 ~enc:to_b32 end