quick fix; ok json response ~~

This commit is contained in:
swrup 2025-10-18 20:28:38 +02:00
parent 1674e338ac
commit 90c404468b
4 changed files with 22 additions and 14 deletions

View file

@ -1,6 +1,7 @@
(* TODO (* TODO
have safe amount arithmetic have safe amount arithmetic
make type private *) make type private
use config currency option *)
(* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>` (* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>`
Fixed-precision numbers with 8 decimal places. Fixed-precision numbers with 8 decimal places.
- <Currency> must be at most 11 characters long - <Currency> must be at most 11 characters long
@ -21,7 +22,7 @@ let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e
let make ~sign ~currency ~value ~fraction = let make ~sign ~currency ~value ~fraction =
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 in 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 fraction_upper_bound = Int64.of_int 100_000_000 in
let* () = if value < Int64.zero then Error "value is negative" else Ok () in let* () = if value < Int64.zero then Error "value is negative" else Ok () in
let* () = let* () =
if fraction < Int64.zero then Error "fraction is negative" else Ok () if fraction < Int64.zero then Error "fraction is negative" else Ok ()
@ -31,7 +32,7 @@ let make ~sign ~currency ~value ~fraction =
else Ok () else Ok ()
in in
let* () = let* () =
if fraction > fraction_upper_bound then if fraction >= fraction_upper_bound then
Error "fraction have more than 8 decimal digits" Error "fraction have more than 8 decimal digits"
else Ok () else Ok ()
in in
@ -46,8 +47,8 @@ let to_string =
in in
let pp_currency ppf = function `Eur -> string ppf "EUR" in let pp_currency ppf = function `Eur -> string ppf "EUR" in
fun ppf { sign; currency; value; fraction } -> fun ppf { sign; currency; value; fraction } ->
pf ppf "%a%a:%Ld.%Ld" (Fmt.option pp_sign) sign pp_currency currency value pf ppf "%a%a:%Ld.%02Ld" (Fmt.option pp_sign) sign pp_currency currency
fraction value fraction
in in
Fmt.str "%a" pp Fmt.str "%a" pp

View file

@ -188,7 +188,9 @@ module UTIL = struct
let hash s = let hash s =
match String.length s = 32 with 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 | true -> hash s
end end
@ -218,7 +220,7 @@ module UTIL = struct
val bin : t Bin.t val bin : t Bin.t
val hash : M.src -> t val hash : M.src -> t
end = struct end = struct
include Hash_64 include HHH_64
let hash src = hash (M.to_octets src) let hash src = hash (M.to_octets src)
end end
@ -233,7 +235,7 @@ module UTIL = struct
val bin : t Bin.t val bin : t Bin.t
val hash : M.src -> t val hash : M.src -> t
end = struct end = struct
include Hash_32 include HHH_32
let hash src = hash (M.to_octets src) let hash src = hash (M.to_octets src)
end end

View file

@ -1,7 +1,7 @@
(* hardcoded config for now *) (* hardcoded config for now *)
let amount s = Amount.of_string s |> Result.get_ok 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 let dummy_duration = Ptime.Span.of_int_s 99999999
module Exchange = struct module Exchange = struct

View file

@ -84,13 +84,18 @@ end = struct
(* mirage_crypto: "The result is the concatenation of r and s, as specified in RFC 8032." *) (* mirage_crypto: "The result is the concatenation of r and s, as specified in RFC 8032." *)
Mirage_crypto_ec.Ed25519.sign ~key s 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 of_b32 s =
let s = B32.encode t in let open Syntax in
assert (String.length s = 64); let* t = B32.decode s in
s let+ () = check_size t in
t
let to_b32 = B32.encode
let jsont = Jsont.of_of_string ~kind:"EddsaSignature" of_b32 ~enc:to_b32 let jsont = Jsont.of_of_string ~kind:"EddsaSignature" of_b32 ~enc:to_b32
end end