diff --git a/src/amount.ml b/src/amount.ml index c5e06082..26b9ed9d 100644 --- a/src/amount.ml +++ b/src/amount.ml @@ -84,9 +84,8 @@ let pp = in fun ppf { sign; currency; value; fraction } -> (* TODO - depends on the currency's number of fraction digits - assumes value and fraction are in bounds *) - pf ppf "%a%s:%Ld.%02ld" (Fmt.option pp_sign) sign currency value fraction + depends on the currency's number of fraction digits *) + pf ppf "%a%s:%Lu.%02lu" (Fmt.option pp_sign) sign currency value fraction let to_string = Fmt.str "%a" pp diff --git a/src/secmod_rsa.ml b/src/secmod_rsa.ml index 13d75c9c..dd39264c 100644 --- a/src/secmod_rsa.ml +++ b/src/secmod_rsa.ml @@ -55,33 +55,25 @@ type t = { ht: (RsaPublicKey.t, key) Hashtbl.t; } -(* -- util -- *) +let parse_filename = + let scan_filename s = + Scanf.sscanf_opt s "%Lu-%Lu" (fun t1 t2 -> + (Absolute.of_s t1, Absolute.of_s t2)) + in + fun fpath -> scan_filename (Fpath.filename fpath) -let time_abs_of_string s = - int_of_string_opt s |> Option.map (fun n -> Absolute.of_s (Int64.of_int n)) - -let t1_t2_of_fpath fpath = - let fname = Fpath.filename fpath in - match String.split_on_char '-' fname with - | [] -> Fmt.failwith "not possible" - | [ t1; t2 ] -> ( - match (time_abs_of_string t1, time_abs_of_string t2) with - | None, _ | _, None -> None - | Some t1, Some t2 -> Some (t1, t2)) - | _ -> None - -let time_abs_to_string abs = - abs - |> Timestamp.of_absolute - |> Timestamp.to_s - |> Option.get - |> Int64.to_int - |> string_of_int +let pp_filename = + let pp_s ppf abs = + abs |> Timestamp.of_absolute |> Timestamp.to_s |> function + | None -> + (* (= `never`) this should not happen given resonable config value *) + Fmt.failwith "encountered timestamp with value `never`" + | Some i -> Fmt.pf ppf "%Lu" i + in + fun ppf (t1, t2) -> Fmt.pf ppf "%a-%a" pp_s t1 pp_s t2 let key_fpath k = - let t1 = time_abs_to_string k.t1 in - let t2 = time_abs_to_string k.t2 in - let fname = Fmt.str "%s-%s" t1 t2 in + let fname = Fmt.str "%a" pp_filename (k.t1, k.t2) in Fpath.(v Cfg.key_dir / k.section_name / fname) (* -- IO -- *) @@ -128,8 +120,8 @@ let gen_key ~section_name t1 t2 = let bits = Cfg.rsa_keysize ~section_name in let priv, pub = RsaPrivateKey.generate ~bits () in Log.debug (fun m -> - m "generated key (%s-%s):@,`%s`" (time_abs_to_string t1) - (time_abs_to_string t2) (RsaPublicKey.to_b32 pub)); + m "generated key (%a):@,`%s`" pp_filename (t1, t2) + (RsaPublicKey.to_b32 pub)); { section_name; priv; pub; t1; t2 } let sort_keys l = List.sort (fun a b -> Absolute.compare a.t2 b.t2) l @@ -178,7 +170,7 @@ let load_key ~section_name fpath = match Fpath.equal (Fpath.normalize fpath) sm_key_fpath with | true -> Ok None | false -> ( - match t1_t2_of_fpath fpath with + match parse_filename fpath with | None -> Fmt.error "invalid file `%a`" Fpath.pp fpath | Some (t1, t2) -> let* priv = read_rsa fpath in