From 3fad00a61187dbef5f21af1e7656aaff620c2f85 Mon Sep 17 00:00:00 2001 From: swrup Date: Sun, 21 Dec 2025 22:13:35 +0100 Subject: [PATCH] wip use DenominationHash directly instead of HashCode --- src/api.ml | 7 ++++-- src/bin_type.ml | 13 ++++++++++ src/http_management.ml | 6 ++--- src/pg.ml | 56 +++++++++++++++++++++++++++++++++++++++--- src/pg_type.ml | 3 +++ tools/offline_sig.ml | 5 ++-- 6 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/api.ml b/src/api.ml index 74f40b79..07355901 100644 --- a/src/api.ml +++ b/src/api.ml @@ -29,6 +29,8 @@ let encode_exn jsont v = Jsont_bytesrw.encode_string jsont v |> Result.get_ok let encode jsont v = Jsont_bytesrw.encode_string jsont v let decode jsont v = Jsont_bytesrw.decode_string jsont v +module DenominationHash = Bin_type.DenominationHash + module Account_operation = struct type t = | Withdraw @@ -70,6 +72,7 @@ module B32 = struct ~decode:B32.decode Caqti_type.string end +(* TODO use DenominationHash directly *) module HashCode : sig type t @@ -442,7 +445,7 @@ end module DenomSignature = struct type t = { - h_denom_pub: HashCode.t; + h_denom_pub: DenominationHash.t; master_sig: DenominationKeyValidity.t; } @@ -451,7 +454,7 @@ module DenomSignature = struct let h_denom_pub v = v.h_denom_pub in let master_sig v = v.master_sig in map ~kind:"DenomSignature" make - |> mem "h_denom_pub" HashCode.jsont ~enc:h_denom_pub + |> mem "h_denom_pub" DenominationHash.jsont ~enc:h_denom_pub |> mem "master_sig" DenominationKeyValidity.jsont ~enc:master_sig |> finish end diff --git a/src/bin_type.ml b/src/bin_type.ml index e2a82283..046e3244 100644 --- a/src/bin_type.ml +++ b/src/bin_type.ml @@ -102,12 +102,18 @@ module Hash_32 = struct let open Bin in map (bytes 32) of_octets to_octets + (* hashes are not b32 encoded in the database *) let caqti : t Caqti_type.t = let open Caqti_type in custom ~encode:(fun v -> Ok (to_octets v)) ~decode:(fun v -> Ok (of_octets v)) octets + + let jsont = + let dec s = Result.map of_octets (B32.decode s) in + let enc v = B32.encode (to_octets v) in + Jsont.of_of_string ~kind:"Hash 32" dec ~enc end module Hash_64 = struct @@ -130,12 +136,18 @@ module Hash_64 = struct let open Bin in map (bytes 64) of_octets to_octets + (* hashes are not b32 encoded in the database *) let caqti : t Caqti_type.t = let open Caqti_type in custom ~encode:(fun v -> Ok (to_octets v)) ~decode:(fun v -> Ok (of_octets v)) octets + + let jsont = + let dec s = Result.map of_octets (B32.decode s) in + let enc v = B32.encode (to_octets v) in + Jsont.of_of_string ~kind:"Hash 64" dec ~enc end (* Hash over string + '\0' *) @@ -160,6 +172,7 @@ module type Hash_S = sig val bin : t Bin.t val caqti : t Caqti_type.t + val jsont : t Jsont.t val hash : string -> t val of_octets : string -> t val to_octets : t -> string diff --git a/src/http_management.ml b/src/http_management.ml index 6df1d656..95c63331 100644 --- a/src/http_management.ml +++ b/src/http_management.ml @@ -117,9 +117,8 @@ end module Keys_post = struct let verify_denom_signature ~sm DenomSignature.{ h_denom_pub; master_sig } = - let denom_hash = HashCode.to_denomination_hash h_denom_pub in let* denom = - match Secmod.find_denom_data sm denom_hash with + match Secmod.find_denom_data sm h_denom_pub with | None -> Fmt.error "404 not found, One of the keys for which a signature was provided \ @@ -138,7 +137,7 @@ module Keys_post = struct fee_withdraw= denom.fee_withdraw; fee_deposit= denom.fee_deposit; fee_refresh= denom.fee_refresh; - denom_hash; + denom_hash= h_denom_pub; } in verify_f ~f:Secmod.verify_with_master_key master_sig r @@ -196,7 +195,6 @@ module Keys_post = struct let* () = denom_sigs |> List.map (fun DenomSignature.{ h_denom_pub; master_sig } -> - let h_denom_pub = HashCode.to_denomination_hash h_denom_pub in (h_denom_pub, master_sig)) |> Secmod.add_denom_master_signatures db_conn sm in diff --git a/src/pg.ml b/src/pg.ml index c57bc8e2..8e9c2ae6 100644 --- a/src/pg.ml +++ b/src/pg.ml @@ -9,7 +9,9 @@ GNU Taler db-events? it seems caqti/pgx does not support it - transaction *) + transaction + + should check validity of signatures got from db, for /management at least *) module type CONN = Caqti_miou.CONNECTION @@ -185,7 +187,7 @@ let insert_auditor_denom_sig = fun (module Conn : CONN) ~auditor_pub ~h_denom_pub ~auditor_sig -> Conn.exec insert_auditor_denom_sig (auditor_pub, h_denom_pub, auditor_sig) -let get_auditors = +let _get_auditors = let get_auditors = Caqti_type.(unit ->* t3 eddsa_pub string string) "SELECT auditor_pub, auditor_url, auditor_name FROM auditors WHERE \ @@ -193,7 +195,7 @@ let get_auditors = in fun (module Conn : CONN) () -> Conn.collect_list get_auditors () -let get_auditor_denoms = +let _get_auditor_denoms = let get_auditor_denoms = let auditor_sig = Bin_sig.ExchangeKeyValidity.caqti in Caqti_type.(unit ->* t3 eddsa_pub denomination_hash auditor_sig) @@ -204,6 +206,54 @@ let get_auditor_denoms = in fun (module Conn : CONN) () -> Conn.collect_list get_auditor_denoms () +(* TODO maybe check that url and name are unique/same for each auditor_pub *) +let get_auditor_keys = + let get_auditor_keys = + let auditor_sig = Bin_sig.ExchangeKeyValidity.caqti in + Caqti_type.( + unit ->* t5 eddsa_pub string string denomination_hash auditor_sig) + "SELECT auditors.auditor_pub, auditors.url, auditors.name, \ + denominations.denom_pub_hash, auditor_denom_sigs.auditor_sig FROM \ + auditor_denom_sigs JOIN auditors USING (auditor_uuid) JOIN \ + denominations USING (denominations_serial) WHERE auditors.is_active" + in + fun (module Conn : CONN) () -> + let open Syntax in + let* l = Conn.collect_list get_auditor_keys () |> unwrap_err_caqti in + let* l = + (* TODO improve DenominationHash/HashCode situation + conversion here is probably bad *) + list_map + (fun (pub, url, name, denom_pub_h, auditor_sig) -> + let denom_pub_h = Bin_type.DenominationHash.to_octets denom_pub_h in + let* denom_pub_h = HashCode.of_b32 denom_pub_h in + Ok (pub, url, name, denom_pub_h, auditor_sig)) + l + in + let ht = Hashtbl.create 0xff in + List.iter + (fun (pub, url, name, denom_pub_h, auditor_sig) -> + let k = (pub, url, name) in + match Hashtbl.find_opt ht k with + | None -> Hashtbl.replace ht k [ (denom_pub_h, auditor_sig) ] + | Some l -> Hashtbl.replace ht k ((denom_pub_h, auditor_sig) :: l)) + l; + let l = Hashtbl.to_seq ht |> List.of_seq in + let l = + List.map + (fun ((auditor_pub, auditor_url, auditor_name), auditor_denoms) -> + let denomination_keys = + List.map + (fun (denom_pub_h, auditor_sig) -> + AuditorDenominationKey.{ denom_pub_h; auditor_sig }) + auditor_denoms + in + AuditorKeys. + { auditor_pub; auditor_url; auditor_name; denomination_keys }) + l + in + Ok l + let insert_wire_fee = let insert_wire_fee = let master_sig = Bin_sig.MasterWireFee.caqti in diff --git a/src/pg_type.ml b/src/pg_type.ml index 4d14d19c..4695f3b4 100644 --- a/src/pg_type.ml +++ b/src/pg_type.ml @@ -4,6 +4,9 @@ open Bin_type open Api open Caqti_type +(* TODO + check that we use Caqti_type.octets for binary data *) + let amount : Amount.t t = let open Amount in custom diff --git a/tools/offline_sig.ml b/tools/offline_sig.ml index 7c442251..eaeda431 100644 --- a/tools/offline_sig.ml +++ b/tools/offline_sig.ml @@ -19,11 +19,10 @@ let denom_signature ~master_key denom_secmod_sig= _; } = let octets = DenominationKey.to_octets denom_pub in - let h_denom_pub = HashCode.hash octets in + let h_denom_pub = DenominationHash.hash octets in let master_sig = let open Bin_sig.DenominationKeyValidity in let master = EddsaPrivateKey.(pub_of_priv master_key) in - let denom_hash = DenominationHash.hash octets in sign_f ~f:(EddsaSignature.sign ~key:master_key) { @@ -36,7 +35,7 @@ let denom_signature ~master_key fee_withdraw; fee_deposit; fee_refresh; - denom_hash; + denom_hash= h_denom_pub; } in DenomSignature.{ h_denom_pub; master_sig }