working database~~

This commit is contained in:
swrup 2025-11-22 21:40:24 +01:00
parent 22f8a1d26e
commit 25d4694da4
5 changed files with 96 additions and 92 deletions

View file

@ -11,10 +11,7 @@ module EddsaPublicKey = struct
let to_octets t = pub_to_octets t let to_octets t = pub_to_octets t
let of_octets t = pub_of_octets t |> Result.get_ok let of_octets t = pub_of_octets t |> Result.get_ok
let bin = Bin.map (Bin.bytes 32) of_octets to_octets
let bin =
let open Bin in
map (bytes 32) of_octets to_octets
let of_b32 s = let of_b32 s =
let open Syntax in let open Syntax in
@ -37,10 +34,7 @@ module EddsaPrivateKey = struct
let to_octets t = priv_to_octets t let to_octets t = priv_to_octets t
let of_octets t = priv_of_octets t |> Result.get_ok let of_octets t = priv_of_octets t |> Result.get_ok
let bin = Bin.map (Bin.bytes 32) of_octets to_octets
let bin =
let open Bin in
map (bytes 32) of_octets to_octets
let of_b32 s = let of_b32 s =
let open Syntax in let open Syntax in
@ -80,9 +74,7 @@ end = struct
Fmt.failwith "EddsaSignature.of_octets failure: data is not 64 bytes." Fmt.failwith "EddsaSignature.of_octets failure: data is not 64 bytes."
| true -> v | true -> v
let bin = let bin = Bin.map (Bin.bytes 64) of_octets to_octets
let open Bin in
map (bytes 64) of_octets to_octets
let sign ~key s = let sign ~key s =
(* 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." *)

View file

@ -1,17 +1,16 @@
(* TODO (* TODO
- pg.ml
- preflight
- GNU Taler db-events? - GNU Taler db-events?
https://git.gnunet.org/gana.git/tree/gnu-taler-db-events/registry.rec *) it seems caqti/pgx does not support it *)
let test req server _ = let test req server _ =
let pool = Vif.Server.device Devices.caqti server in let (module Conn : Caqti_miou.CONNECTION) =
Vif.Server.device Devices.db_connection server
in
let query = let query =
let open Caqti_request.Infix in let open Caqti_request.Infix in
Caqti_type.(unit ->! int) "SELECT 53 - 11" Caqti_type.(unit ->! int) "SELECT 53 - 11"
in in
let fn (module Conn : Caqti_miou.CONNECTION) = Conn.find query () in match Conn.find query () with
match Caqti_miou_unix.Pool.use fn pool with
| Ok n -> | Ok n ->
let open Vif.Response.Syntax in let open Vif.Response.Syntax in
let* () = let* () =
@ -26,4 +25,24 @@ let test req server _ =
let* () = Vif.Response.with_string req str in let* () = Vif.Response.with_string req str in
Vif.Response.respond `Internal_server_error Vif.Response.respond `Internal_server_error
let init = test let test_activate req server _ =
(*let (module Conn : Caqti_miou.CONNECTION) =*)
let db_conn = Vif.Server.device Devices.db_connection server in
let secmod_signkey = Vif.Server.device Devices.secmod_signkey server in
let exchange_public_key = secmod_signkey.sm_key in
match Pg.activate_signing_key db_conn exchange_public_key with
| Ok () ->
let open Vif.Response.Syntax in
let* () =
Vif.Response.add ~field:"content-type" "text/plain; charset= utf-8"
in
let* () =
Vif.Response.with_string req (Fmt.str "activate_signing_key done~~@.")
in
Vif.Response.respond `OK
| Error err ->
(* TODO don't leak private data in error messages *)
let open Vif.Response.Syntax in
let str = Fmt.str "Database error: %a." Caqti_error.pp err in
let* () = Vif.Response.with_string req str in
Vif.Response.respond `Internal_server_error

View file

@ -36,13 +36,15 @@ end
let secmod_signkey = Secmod_signkey.v let secmod_signkey = Secmod_signkey.v
let secmod_denom = Secmod_denom.v let secmod_denom = Secmod_denom.v
let caqti : let db_connection : (env, Caqti_miou.connection) Vif.Device.device =
( env, let finally (module Conn : Caqti_miou.CONNECTION) = Conn.disconnect () in
(Caqti_miou.connection, Caqti_error.t) Caqti_miou_unix.Pool.t ) Vif.Device.v ~name:"db_connection" ~finally []
Vif.Device.device = @@ fun { caqti_switch; db_uri } ->
let finally pool = Caqti_miou_unix.Pool.drain pool in match Caqti_miou_unix.connect ~sw:caqti_switch db_uri with
Vif.Device.v ~name:"caqti" ~finally [] @@ fun { caqti_switch; db_uri } ->
match Caqti_miou_unix.connect_pool ~sw:caqti_switch db_uri with
| Error err -> | Error err ->
Fmt.failwith "Database connection failure: %a." Caqti_error.pp err Fmt.failwith "Database connection failure: %a." Caqti_error.pp err
| Ok pool -> pool | Ok conn -> (
match Pg.preflight conn with
| Error err ->
Fmt.failwith "Database preflight failure: %a." Caqti_error.pp err
| Ok () -> conn)

View file

@ -113,7 +113,7 @@ let routes =
get (rel / "privacy" /?? nil) --> Static.privacy; get (rel / "privacy" /?? nil) --> Static.privacy;
get (rel / "management" / "keys" /?? nil) --> Management.keys; get (rel / "management" / "keys" /?? nil) --> Management.keys;
get (rel / "db_test" /?? nil) --> Database.test; get (rel / "db_test" /?? nil) --> Database.test;
get (rel / "db_init" /?? nil) --> Database.init; get (rel / "db_activate" /?? nil) --> Database.test_activate;
] ]
let () = let () =
@ -128,8 +128,8 @@ let () =
{ caqti_switch; db_uri= Config.Exchangedb_postgres.config } { caqti_switch; db_uri= Config.Exchangedb_postgres.config }
in in
let devices = let devices =
(* todo: rm unneeded env parameter from secmod devices *) Vif.Devices.
Vif.Devices.[ Devices.secmod_signkey; Devices.secmod_denom; Devices.caqti ] [ Devices.secmod_signkey; Devices.secmod_denom; Devices.db_connection ]
in in
let middlewares = Vif.Middlewares.[] in let middlewares = Vif.Middlewares.[] in
Vif.run ~cfg ~devices ~middlewares routes env Vif.run ~cfg ~devices ~middlewares routes env

117
src/pg.ml
View file

@ -1,83 +1,74 @@
(* TODO be sure to not use postgres ptime open Crypto
add a dune stanza like for prelude:
"(flags (:standard -open Prelude))" *)
module Caqti_type = struct module Caqti_type = struct
include Caqti_type include Caqti_type
(* we want to use int64 timestamps, (* we want to use int64 timestamps,
not postgresql built-in timestamp type *) not postgresql built-in timestamp type *)
let ptime : Ptime.t Caqti_type.t = let ptime : Ptime.t t =
let encode i = Ok (Util.ptime_to_int64 i) in let encode i = Ok (Util.ptime_to_int64 i) in
let decode = Util.ptime_of_int64 in let decode = Util.ptime_of_int64 in
Caqti_type.custom ~encode ~decode Caqti_type.(int64) custom ~encode ~decode int64
let amount : Amount.t t =
let open Amount in
custom
~encode:(fun amount -> Ok (amount.value, amount.fraction))
~decode:(fun (value, fraction) ->
Amount.make ~sign:None ~currency:Config.currency ~value ~fraction)
(t2 int64 int32)
let eddsa_public : EddsaPublicKey.t t =
let open EddsaPublicKey in
custom
~encode:(fun v -> Ok (to_octets v))
~decode:(fun v -> Ok (of_octets v))
octets
let eddsa_signature : EddsaSignature.t t =
let open EddsaSignature in
custom
~encode:(fun v -> Ok (to_octets v))
~decode:(fun s -> Ok (of_octets s))
octets
include Caqti_request.Infix
end end
open Crypto let preflight =
(*open Bin_type*) let l =
List.map
let amount_t : Amount.t Caqti_type.t = Caqti_type.(unit ->. unit)
let open Amount in [
Caqti_type.custom "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL \
~encode:(fun amount -> Ok (amount.value, amount.fraction)) SERIALIZABLE;"; "SET enable_sort=OFF;"; "SET enable_seqscan=OFF;";
~decode:(fun (value, fraction) -> "SET enable_mergejoin=OFF;"; "SET search_path TO exchange;";
Amount.make ~sign:None ~currency:Config.currency ~value ~fraction) ]
Caqti_type.(t2 int64 int32)
let eddsa_signature_t : EddsaSignature.t Caqti_type.t =
let open EddsaSignature in
Caqti_type.custom
~encode:(fun master_sig -> Ok (to_octets master_sig))
~decode:(fun s -> Ok (of_octets s))
Caqti_type.octets
(* WIP: minimum db functions for basic /management *)
let pg_todo () = assert false
open Caqti_request.Infix
(* signkey *)
(* todo:
- what type to use for TALER_ExchangePublicKeyP + TALER_EXCHANGEDB_SignkeyMetaData
-> I think it could be something = to FutureSignKey.t
- master signature: over smthing? *)
let activate_signing_key _cls (exchange_public_key : Signkey.t) =
(*let open Bin_type in*)
let _exchange_pub = EddsaPublicKey.(to_octets exchange_public_key.pub) in
let _master_sig =
exchange_public_key.master_sig |> Option.get
(*|> EddsaSignature.to_octets*)
in in
let _insert_signkey = fun (module Conn : Caqti_miou.CONNECTION) ->
Caqti_type.(t5 string ptime ptime ptime string ->! int) Syntax.list_iter (fun p -> Conn.exec p ()) l
let activate_signing_key =
let insert_signkey =
Caqti_type.(t5 eddsa_public ptime ptime ptime eddsa_signature ->. unit)
"INSERT INTO exchange_sign_keys (exchange_pub, valid_from, expire_sign, \ "INSERT INTO exchange_sign_keys (exchange_pub, valid_from, expire_sign, \
expire_legal, master_sig) VALUES ($1, $2, $3, $4, $5);" expire_legal, master_sig) VALUES ($1, $2, $3, $4, $5);"
in in
pg_todo () fun (module Conn : Caqti_miou.CONNECTION) (exchange_public_key : Signkey.t) ->
let exchange_pub = exchange_public_key.pub in
let valid_from = exchange_public_key.stamp_start in
let expire_sign = exchange_public_key.stamp_expire in
let expire_legal = exchange_public_key.stamp_end in
(*let master_sig = exchange_public_key.master_sig |> Option.get in*)
let master_sig = EddsaSignature.of_octets (String.make 64 'x') in
Conn.exec insert_signkey
(exchange_pub, valid_from, expire_sign, expire_legal, master_sig)
(* (* WIP: minimum db functions for basic /management *)
enum GNUNET_DB_QueryStatus
TEH_PG_activate_signing_key (
void *cls,
const struct TALER_ExchangePublicKeyP *exchange_pub,
const struct TALER_EXCHANGEDB_SignkeyMetaData *meta,
const struct TALER_MasterSignatureP *master_sig)
{
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam iparams[] = {
GNUNET_PQ_query_param_auto_from_type (exchange_pub),
GNUNET_PQ_query_param_timestamp (&meta->start),
GNUNET_PQ_query_param_timestamp (&meta->expire_sign),
GNUNET_PQ_query_param_timestamp (&meta->expire_legal),
GNUNET_PQ_query_param_auto_from_type (master_sig),
GNUNET_PQ_query_param_end
};
}
*)
(* ----- *) (* ----- *)
let pg_todo () = assert false
let lookup_signing_key = pg_todo let lookup_signing_key = pg_todo
let iterate_active_signkeys = pg_todo let iterate_active_signkeys = pg_todo
let insert_signkey_revocation = pg_todo let insert_signkey_revocation = pg_todo