mfat path: keep it simple

This commit is contained in:
swrup 2026-03-21 07:38:29 +01:00 committed by Swrup
parent cbae1c773a
commit ec4e166173
4 changed files with 46 additions and 47 deletions

View file

@ -5,8 +5,6 @@ module Log = (val Logs.src_log src : Logs.LOG)
(* - *)
open Syntax
open Time
module Sfn = Mfat.Sfn
module Spath = Mfat.Spath
module DenominationHash = Hash.DenominationHash
module Coin_config = struct
@ -27,12 +25,8 @@ end
module Cfg = struct
include Config.Exchange_secmod_rsa
let to_spath s =
let open Fat.Spath in
match of_string s with Error (`Msg e) -> Fmt.failwith "%s" e | Ok v -> v
let key_dir = to_spath "/RSA"
let sm_key = to_spath "/SM_RSA"
let key_dir = "/RSA"
let sm_key = "/SM_RSA"
let coin_config_list = List.map Coin_config.of_coin Config.Coin.all_coins
let get_coin_config ~section_name =
@ -74,39 +68,35 @@ let key_bin =
|> sealr
let key_spath k =
let sfn_res =
String.sub (DenominationHash.to_b32 k.h_pub) 0 8 |> Sfn.of_string
in
match sfn_res with
| Error _ -> failwith "not possible"
| Ok sfn -> Spath.(Cfg.key_dir / sfn)
let sfn = String.sub (DenominationHash.to_b32 k.h_pub) 0 8 in
Fat.Path.add Cfg.key_dir sfn
let read_eddsa fs spath =
Log.debug (fun m -> m "reading key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "reading key file `%s`" spath);
let* data = Fat.read fs spath |> unwrap_msg in
let* priv = Eddsa.priv_of_octets data in
let pub = Eddsa.pub_of_priv priv in
Ok (priv, pub)
let write_eddsa fs spath priv =
Log.debug (fun m -> m "writing key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "writing key file `%s`" spath);
let data = Eddsa.priv_to_octets priv in
Fat.write fs spath data |> unwrap_msg
let read_key fs spath =
Log.debug (fun m -> m "reading key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "reading key file `%s`" spath);
let* data = Fat.read fs spath |> unwrap_msg in
let k = Bin.decode key_bin data (ref 0) in
Ok k
let write_key fs k =
let spath = key_spath k in
Log.debug (fun m -> m "writing key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "writing key file `%s`" spath);
let data = Bin.to_string key_bin k in
Fat.write fs spath data |> unwrap_msg
let delete_file fs spath =
Log.debug (fun m -> m "delete key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "delete key file `%s`" spath);
let+ () = Fat.remove fs spath |> unwrap_msg in
()
@ -156,8 +146,8 @@ let load fs =
else Fat.mkdir fs Cfg.key_dir |> unwrap_msg
in
let* l = Fat.ls fs Cfg.key_dir |> unwrap_msg in
let l = List.map (fun entry -> Spath.add Cfg.key_dir entry.Fat.name) l in
let l = List.filter (fun spath -> not @@ Spath.equal spath Cfg.sm_key) l in
let l = List.map (fun entry -> Fat.Path.add Cfg.key_dir entry.Fat.name) l in
let l = List.filter (fun spath -> not @@ String.equal Cfg.sm_key spath) l in
let* keys = list_map (read_key fs) l in
match keys with
| [] -> Ok None