mte/src/pg.ml

170 lines
5.1 KiB
OCaml
Raw Normal View History

2025-11-22 21:40:24 +01:00
open Crypto
2025-11-16 05:33:06 +01:00
module Caqti_type = struct
include Caqti_type
2025-11-22 21:40:24 +01:00
(* TODO add a dune stanza like for prelude:
"(flags (:standard -open Prelude))" *)
2025-11-16 05:33:06 +01:00
(* we want to use int64 timestamps,
not postgresql built-in timestamp type *)
let ptime : Ptime.t option t = Timestamp.caqti
let time = Timestamp.caqti
2025-11-16 05:33:06 +01:00
2025-11-22 21:40:24 +01:00
let amount : Amount.t t =
let open Amount in
2025-12-07 01:51:07 +01:00
let intro value fraction =
match make ~sign:None ~currency:Config.currency ~value ~fraction with
| Error e -> raise (Caqti_type.Reject e)
| Ok v -> v
in
let p1 t = t.value in
let p2 t = t.fraction in
product intro @@ proj int64 p1 @@ proj int32 p2 @@ proj_end
2025-11-21 19:29:04 +01:00
2025-11-23 15:37:05 +01:00
let age_mask : int t = Caqti_type.int
let rsa_public : RsaPublicKey.t t =
let open RsaPublicKey in
custom
~encode:(fun v -> Ok (to_octets v))
~decode:(fun v -> Ok (of_octets v))
octets
2025-11-22 21:40:24 +01:00
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
2025-11-16 05:33:06 +01:00
2025-11-22 21:40:24 +01:00
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
2025-11-21 19:29:04 +01:00
2025-11-23 15:37:05 +01:00
include struct
(* alias for hash *)
open Bin_type
let fullpayto_hash = FullPaytoHash.caqti
let nomalizaedpayto_hash = NormalizedPaytoHash.caqti
let denomination_hash = DenominationHash.caqti
let privatecontract_hash = PrivateContractHash.caqti
let extensionspolicy_hash = ExtensionsPolicyHash.caqti
let merchantwire_hash = MerchantWireHash.caqti
let agecommitment_hash = AgeCommitmentHash.caqti
let blindedcoin_hash = BlindedCoinHash.caqti
let coinpub_hash = CoinPubHash.caqti
let outputcommitment_hash = OutputCommitmentHash.caqti
let planchets_hash = HashPlanchetsP.caqti
end
2025-11-22 21:40:24 +01:00
include Caqti_request.Infix
end
2025-11-21 19:29:04 +01:00
2025-11-23 15:37:05 +01:00
module type CONN = Caqti_miou.CONNECTION
open Bin_type
2025-11-22 21:40:24 +01:00
let preflight =
let l =
List.map
Caqti_type.(unit ->. unit)
[
"SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL \
2025-11-29 19:36:35 +01:00
SERIALIZABLE;";
"SET enable_sort=OFF;";
"SET enable_seqscan=OFF;";
"SET enable_mergejoin=OFF;";
"SET search_path TO exchange;";
2025-11-22 21:40:24 +01:00
]
2025-11-21 19:29:04 +01:00
in
2025-11-22 21:40:24 +01:00
fun (module Conn : Caqti_miou.CONNECTION) ->
Syntax.list_iter (fun p -> Conn.exec p ()) l
2025-11-23 15:37:05 +01:00
let lookup_signing_key =
let lookup_signing_key =
Caqti_type.(eddsa_public ->? t3 time time time)
2025-11-23 15:37:05 +01:00
"SELECT valid_from, expire_sign, expire_legal FROM exchange_sign_keys \
WHERE exchange_pub=$1"
in
fun (module Conn : CONN) (exchange_pub : EddsaPublicKey.t) ->
Conn.find_opt lookup_signing_key exchange_pub
2025-11-22 21:40:24 +01:00
let activate_signing_key =
let insert_signkey =
2025-12-07 01:02:54 +01:00
let master_sig = Api_sig.ExchangeSigningKeyValidityPS.caqti in
Caqti_type.(t5 eddsa_public time time time master_sig ->. unit)
2025-11-21 19:29:04 +01:00
"INSERT INTO exchange_sign_keys (exchange_pub, valid_from, expire_sign, \
2025-11-23 15:37:05 +01:00
expire_legal, master_sig) VALUES ($1, $2, $3, $4, $5)"
2025-11-21 19:29:04 +01:00
in
2025-11-23 15:37:05 +01:00
fun (module Conn : CONN)
2025-11-28 07:47:51 +01:00
Signkey.{ pub; priv= _; stamp_start; stamp_expire; stamp_end; master_sig }
2025-11-23 15:37:05 +01:00
->
(* TODO master_sig *)
let master_sig = master_sig |> Option.get in
2025-11-22 21:40:24 +01:00
Conn.exec insert_signkey
(pub, stamp_start, stamp_expire, stamp_end, master_sig)
2025-11-16 15:55:32 +01:00
2025-11-23 15:37:05 +01:00
let lookup_denomination_key =
let lookup_denomination_key =
Caqti_type.(
denomination_hash
->? t10 time time time time amount amount amount amount amount age_mask)
2025-11-23 15:37:05 +01:00
"SELECT valid_from, expire_withdraw, expire_deposit, expire_legal, coin, \
fee_withdraw, fee_deposit, fee_refresh, fee_refund, age_mask FROM \
denominations WHERE denom_pub_hash=$1"
in
fun (module Conn : CONN) (h_denom_pub : DenominationHash.t) ->
Conn.find_opt lookup_denomination_key h_denom_pub
let add_denomination_key =
let denomination_insert =
2025-12-07 01:02:54 +01:00
let master_sig = Api_sig.DenominationKeyValidityPS.caqti in
2025-11-23 15:37:05 +01:00
Caqti_type.(
2025-12-07 01:02:54 +01:00
t12 denomination_hash rsa_public master_sig time time time time amount
amount amount amount (t2 amount age_mask)
2025-11-23 15:37:05 +01:00
->. unit)
"INSERT INTO denominations (denom_pub_hash, denom_pub, master_sig, \
valid_from, expire_withdraw, expire_deposit, expire_legal, coin, \
fee_withdraw, fee_deposit, fee_refresh, fee_refund, age_mask) VALUES \
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)"
in
fun (module Conn : CONN)
Denomination.
{
pub;
2025-11-29 14:03:37 +01:00
priv= _;
2025-11-23 15:37:05 +01:00
section_name= _;
value;
stamp_start;
stamp_expire_withdraw;
stamp_expire_deposit;
stamp_expire_legal;
fee_withdraw;
fee_deposit;
fee_refresh;
fee_refund;
age_mask;
h_pub;
master_sig;
}
->
(* TODO master_sig *)
let master_sig = master_sig |> Option.get in
Conn.exec denomination_insert
( h_pub,
pub,
master_sig,
stamp_start,
stamp_expire_withdraw,
stamp_expire_deposit,
stamp_expire_legal,
value,
fee_withdraw,
fee_deposit,
fee_refresh,
(fee_refund, age_mask) )