From 4893fdbe202e31dbd7422a4a7f0adead7a0e38ee Mon Sep 17 00:00:00 2001 From: swrup Date: Sat, 29 Nov 2025 15:52:13 +0100 Subject: [PATCH] --- src/config.ml | 11 ----------- src/devices.ml | 32 ++++++++++++++++++-------------- src/syntax.ml | 8 ++++++++ 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/config.ml b/src/config.ml index c91ccfdb..136c8995 100644 --- a/src/config.ml +++ b/src/config.ml @@ -175,17 +175,6 @@ module Coin = struct } let all_coins = List.map parse_coin coin_sections - - let get_coin name = - match List.find_opt (fun v -> v.section_name = name) all_coins with - | None -> - fail_with - (Fmt.str "section `[coin_%s]` not found, coin `%s` is not defined" - name name) - | Some v -> v - - let kudo_1 = get_coin "kudo_1" - let kudo_2 = get_coin "kudo_2" end module Exchange_secmod_rsa = struct diff --git a/src/devices.ml b/src/devices.ml index 97ee42ee..fc821823 100644 --- a/src/devices.ml +++ b/src/devices.ml @@ -19,6 +19,10 @@ let db_connection : (env, Caqti_miou.connection) Vif.Device.device = | Ok () -> conn) module Secmod_signkey = struct + (* TODO + - key rotation + - how many signkey to use? + we just use 1 for now *) type t = { sm_key: Signkey.t; keys: Signkey.t list; @@ -39,15 +43,18 @@ module Secmod_signkey = struct Fmt.error_msg "secmod_signkey load error: invalid store state." in let* sm_key = Data_file.load_signkey conn Fpath.(dir / "sm_key") in - let* key_0 = - let fname = Fpath.(dir / string_of_int 0) in - Data_file.load_signkey conn fname + let* keys = + let l = List.init 1 (fun i -> Fpath.(dir / string_of_int i)) in + let* l = + Syntax.list_map (fun fname -> Data_file.load_signkey conn fname) l + in + match Syntax.opt_list l with + | Error () -> error_invalid_state + | Ok opt -> Ok opt in - match (sm_key, key_0) with + match (sm_key, keys) with | None, None -> Ok None - | Some sm_key, Some key_0 -> - let keys = [ key_0 ] in - Ok (Some { sm_key; keys }) + | Some sm_key, Some keys -> Ok (Some { sm_key; keys }) | _, _ -> error_invalid_state let generate_fresh_secmod_data () = @@ -94,7 +101,7 @@ module Secmod_denom = struct let* keys = let* l = let open Config.Coin in - list_map + Syntax.list_map (fun coin -> let section_name = coin.section_name in let fname = Fpath.(dir / section_name) in @@ -102,12 +109,9 @@ module Secmod_denom = struct Data_file.load_denom conn ~section_name fname) all_coins in - match (List.for_all Option.is_none l, List.for_all Option.is_some l) with - | _, true -> - let l = List.map Option.get l in - Ok (Some l) - | true, _ -> Ok None - | _, _ -> error_invalid_state + match Syntax.opt_list l with + | Error () -> error_invalid_state + | Ok opt -> Ok opt in match (sm_key, keys) with | None, None -> Ok None diff --git a/src/syntax.ml b/src/syntax.ml index da6c9b01..f354ca6c 100644 --- a/src/syntax.ml +++ b/src/syntax.ml @@ -38,3 +38,11 @@ let list_fold_left f acc l = let* acc = acc in f acc v) (Ok acc) l + +let opt_list l = + match (List.for_all Option.is_none l, List.for_all Option.is_some l) with + | _, true -> + let l = List.map Option.get l in + Ok (Some l) + | true, _ -> Ok None + | _, _ -> Error ()