wip: more sql

This commit is contained in:
swrup 2025-11-23 15:37:05 +01:00
parent 056c111d88
commit 92a4f9a469
4 changed files with 120 additions and 47 deletions

View file

@ -108,6 +108,13 @@ module Hash_32 = struct
let bin = let bin =
let open Bin in let open Bin in
map (bytes 32) of_octets to_octets map (bytes 32) of_octets to_octets
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
end end
module Hash_64 = struct module Hash_64 = struct
@ -125,51 +132,37 @@ module Hash_64 = struct
let bin = let bin =
let open Bin in let open Bin in
map (bytes 64) of_octets to_octets map (bytes 64) of_octets to_octets
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
end end
(* Hash over string + '\0' *) (* Hash over string + '\0' *)
module Hash_32_cstr = struct module Hash_32_cstr = struct
type t = Digestif.SHA256.t include Hash_32
let hash s = let hash s =
let s = s ^ "\x00" in let s = s ^ "\x00" in
Digestif.SHA256.(digest_string s) Digestif.SHA256.(digest_string s)
let of_octets s =
match String.length s = 32 with
| false -> Fmt.failwith "Hash.of_octets failure: data is not 32 bytes"
| true -> Digestif.SHA256.of_raw_string s
let to_octets = Digestif.SHA256.to_raw_string
let bin =
let open Bin in
map (bytes 32) of_octets to_octets
end end
module Hash_64_cstr = struct module Hash_64_cstr = struct
type t = Digestif.SHA512.t include Hash_64
let hash s = let hash s =
let s = s ^ "\x00" in let s = s ^ "\x00" in
Digestif.SHA512.(digest_string s) Digestif.SHA512.(digest_string s)
let of_octets s =
match String.length s = 64 with
| false -> Fmt.failwith "Hash.of_octets failure: data is not 64 bytes"
| true -> Digestif.SHA512.of_raw_string s
let to_octets = Digestif.SHA512.to_raw_string
let bin =
let open Bin in
map (bytes 64) of_octets to_octets
end end
module type Hash_S = sig module type Hash_S = sig
type t type t
val bin : t Bin.t val bin : t Bin.t
val caqti : t Caqti_type.t
val hash : string -> t val hash : string -> t
val of_octets : string -> t val of_octets : string -> t
val to_octets : t -> string val to_octets : t -> string

View file

@ -12,6 +12,7 @@ type t = {
fee_deposit: Amount.t; fee_deposit: Amount.t;
fee_refresh: Amount.t; fee_refresh: Amount.t;
fee_refund: Amount.t; fee_refund: Amount.t;
age_mask: int;
h_pub: Bin_type.DenominationHash.t; h_pub: Bin_type.DenominationHash.t;
sign: string -> RsaSignature.t; sign: string -> RsaSignature.t;
master_sig: EddsaSignature.t option; master_sig: EddsaSignature.t option;
@ -62,6 +63,7 @@ let make
fee_deposit; fee_deposit;
fee_refresh; fee_refresh;
fee_refund; fee_refund;
age_mask= 0;
h_pub; h_pub;
sign; sign;
master_sig; master_sig;

View file

@ -14,13 +14,14 @@ let mk_future_denom denom_key_signf
fee_deposit; fee_deposit;
fee_refresh; fee_refresh;
fee_refund; fee_refund;
age_mask;
h_pub; h_pub;
sign= _; sign= _;
master_sig= _; master_sig= _;
} : } :
Denomination.t) = Denomination.t) =
let denom_pub = let denom_pub =
DenominationKey.Rsa RsaDenominationKey.{ age_mask= 0; rsa_pub= pub } DenominationKey.Rsa RsaDenominationKey.{ age_mask; rsa_pub= pub }
in in
let denom_secmod_sig = let denom_secmod_sig =
let open Bin_type in let open Bin_type in

121
src/pg.ml
View file

@ -20,6 +20,15 @@ module Caqti_type = struct
Amount.make ~sign:None ~currency:Config.currency ~value ~fraction) Amount.make ~sign:None ~currency:Config.currency ~value ~fraction)
(t2 int64 int32) (t2 int64 int32)
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
let eddsa_public : EddsaPublicKey.t t = let eddsa_public : EddsaPublicKey.t t =
let open EddsaPublicKey in let open EddsaPublicKey in
custom custom
@ -34,9 +43,30 @@ module Caqti_type = struct
~decode:(fun s -> Ok (of_octets s)) ~decode:(fun s -> Ok (of_octets s))
octets octets
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
include Caqti_request.Infix include Caqti_request.Infix
end end
module type CONN = Caqti_miou.CONNECTION
open Bin_type
let preflight = let preflight =
let l = let l =
List.map List.map
@ -50,13 +80,22 @@ let preflight =
fun (module Conn : Caqti_miou.CONNECTION) -> fun (module Conn : Caqti_miou.CONNECTION) ->
Syntax.list_iter (fun p -> Conn.exec p ()) l Syntax.list_iter (fun p -> Conn.exec p ()) l
let lookup_signing_key =
let lookup_signing_key =
Caqti_type.(eddsa_public ->? t3 ptime ptime ptime)
"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
let activate_signing_key = let activate_signing_key =
let insert_signkey = let insert_signkey =
Caqti_type.(t5 eddsa_public ptime ptime ptime eddsa_signature ->. unit) 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
fun (module Conn : Caqti_miou.CONNECTION) exchange_signkey -> fun (module Conn : CONN) exchange_signkey ->
let Signkey. let Signkey.
{ pub; stamp_start; stamp_expire; stamp_end; master_sig= _; sign= _ } { pub; stamp_start; stamp_expire; stamp_end; master_sig= _; sign= _ }
= =
@ -67,24 +106,62 @@ let activate_signing_key =
Conn.exec insert_signkey Conn.exec insert_signkey
(pub, stamp_start, stamp_expire, stamp_end, master_sig) (pub, stamp_start, stamp_expire, stamp_end, master_sig)
(* WIP: minimum db functions for basic /management *) let lookup_denomination_key =
let lookup_denomination_key =
Caqti_type.(
denomination_hash
->? t10 ptime ptime ptime ptime amount amount amount amount amount
age_mask)
"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 =
let pg_todo () = assert false Caqti_type.(
let lookup_signing_key = pg_todo t12 denomination_hash rsa_public eddsa_signature ptime ptime ptime ptime
let iterate_active_signkeys = pg_todo amount amount amount amount (t2 amount age_mask)
let insert_signkey_revocation = pg_todo ->. unit)
let lookup_signkey_revocation = pg_todo "INSERT INTO denominations (denom_pub_hash, denom_pub, master_sig, \
valid_from, expire_withdraw, expire_deposit, expire_legal, coin, \
(* denominations *) fee_withdraw, fee_deposit, fee_refresh, fee_refund, age_mask) VALUES \
let insert_denomination_info = pg_todo ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)"
let get_denomination_info = pg_todo in
let get_denomination_by_serial = pg_todo fun (module Conn : CONN) denom ->
let iterate_denomination_info = pg_todo let Denomination.
let iterate_denominations = pg_todo {
let get_coin_denomination = pg_todo pub;
let insert_denomination_revocation = pg_todo section_name= _;
let get_denomination_revocation = pg_todo value;
let lookup_denomination_key = pg_todo stamp_start;
let add_denomination_key = pg_todo stamp_expire_withdraw;
stamp_expire_deposit;
stamp_expire_legal;
fee_withdraw;
fee_deposit;
fee_refresh;
fee_refund;
age_mask;
h_pub;
sign= _;
master_sig;
} =
denom
in
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) )