From 8f433fcb28af928201f7ab7d2dff98250e9279a2 Mon Sep 17 00:00:00 2001 From: swrup Date: Sun, 21 Dec 2025 19:32:54 +0100 Subject: [PATCH] add pg_type.ml --- src/pg.ml | 381 +------------------------------------------------ src/pg_type.ml | 367 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 371 insertions(+), 377 deletions(-) create mode 100644 src/pg_type.ml diff --git a/src/pg.ml b/src/pg.ml index 6d711389..a90846b3 100644 --- a/src/pg.ml +++ b/src/pg.ml @@ -13,388 +13,15 @@ module type CONN = Caqti_miou.CONNECTION -open Crypto -open Bin_type -open Api - module Caqti_type = struct include Caqti_type - - 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) - - (* we want to use int64 timestamps, - not postgresql built-in timestamp type *) - let ptime : Ptime.t option t = Timestamp.caqti - let time = Timestamp.caqti - let time_span = Timestamp.Span.caqti - let age_mask : int t = Caqti_type.int - let rsa_pub = RsaPublicKey.caqti - let eddsa_pub = EddsaPublicKey.caqti - let eddsa_sig = EddsaSignature.caqti - - (* todo: enum type for wire_method? *) - let wire_method = Caqti_type.string - let payto_uri = Caqti_type.string - let b32 = Api.B32.caqti - - include struct - (* alias for hash *) - - 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 - - let signkey_data = - let master_sig = Bin_sig.ExchangeSigningKeyValidity.caqti in - custom - ~encode:(fun - Signkey_data.{ pub; stamp_start; stamp_expire; stamp_end; master_sig } - -> - match master_sig with - | None -> Error "signkey_data master_sig is none" - | Some master_sig -> - Ok (pub, stamp_start, stamp_expire, stamp_end, master_sig)) - ~decode:(fun (pub, stamp_start, stamp_expire, stamp_end, master_sig) -> - Ok - { - pub; - stamp_start; - stamp_expire; - stamp_end; - master_sig= Some master_sig; - }) - (t5 eddsa_pub time time time master_sig) - - let denom_data = - let master_sig = Bin_sig.DenominationKeyValidity.caqti in - custom - ~encode:(fun - Denom_data. - { - pub; - 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; - } - -> - match master_sig with - | None -> Error "denom_data master_sig is none" - | Some master_sig -> - Ok - ( pub, - 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) )) - ~decode:(fun - ( pub, - 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) ) - -> - Ok - { - pub; - 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= Some master_sig; - }) - (t12 rsa_pub amount time time time time amount amount amount amount int - (t2 denomination_hash master_sig)) - - let account_restrictions = - custom - ~encode:(fun l -> Api.encode (Jsont.list AccountRestriction.jsont) l) - ~decode:(fun s -> Api.decode (Jsont.list AccountRestriction.jsont) s) - string - - let global_fee = - let master_sig = Bin_sig.GlobalFees.caqti in - custom - ~encode:(fun - GlobalFees. - { - start_date; - end_date; - history_fee; - account_fee; - purse_fee; - history_expiration; - purse_account_limit; - purse_timeout; - master_sig; - } - -> - Ok - ( start_date, - end_date, - history_fee, - account_fee, - purse_fee, - history_expiration, - purse_account_limit, - purse_timeout, - master_sig )) - ~decode:(fun - ( start_date, - end_date, - history_fee, - account_fee, - purse_fee, - history_expiration, - purse_account_limit, - purse_timeout, - master_sig ) - -> - Ok - { - start_date; - end_date; - history_fee; - account_fee; - purse_fee; - history_expiration; - purse_account_limit; - purse_timeout; - master_sig; - }) - Caqti_type.( - t9 time time amount amount amount time_span int32 time_span master_sig) - - let aggregate_transfer_fee = - let master_sig = Bin_sig.MasterWireFee.caqti in - Caqti_type.custom - ~encode:(fun - AggregateTransferFee. - { wire_fee; closing_fee; start_date; end_date; sig_ } - -> Ok (wire_fee, closing_fee, start_date, end_date, sig_)) - ~decode:(fun (wire_fee, closing_fee, start_date, end_date, sig_) -> - Ok - AggregateTransferFee. - { wire_fee; closing_fee; start_date; end_date; sig_ }) - Caqti_type.(t5 amount amount time time master_sig) - - let exchange_wire_account = - let master_sig = Bin_sig.MasterWireDetails.caqti in - Caqti_type.custom - ~encode:(fun - ExchangeWireAccount. - { - payto_uri; - conversion_url; - debit_restrictions; - credit_restrictions; - master_sig; - bank_label; - priority; - } - -> - Ok - ( payto_uri, - conversion_url, - debit_restrictions, - credit_restrictions, - master_sig, - bank_label, - priority )) - ~decode:(fun - ( payto_uri, - conversion_url, - debit_restrictions, - credit_restrictions, - master_sig, - bank_label, - priority ) - -> - Ok - { - payto_uri; - conversion_url; - debit_restrictions; - credit_restrictions; - master_sig; - bank_label; - priority; - }) - Caqti_type.( - t7 payto_uri (option string) account_restrictions account_restrictions - master_sig (option string) (option int)) - - let drain_profit_message = - let master_sig = Bin_sig.MasterDrainProfit.caqti in - Caqti_type.custom - ~encode:(fun - DrainProfitsMessage. - { - wtid; - debit_account_section; - credit_payto_uri; - date; - amount; - master_sig; - } - -> - Ok - ( wtid, - debit_account_section, - credit_payto_uri, - date, - amount, - master_sig )) - ~decode:(fun - ( wtid, - debit_account_section, - credit_payto_uri, - date, - amount, - master_sig ) - -> - Ok - { - wtid; - debit_account_section; - credit_payto_uri; - date; - amount; - master_sig; - }) - Caqti_type.(t6 b32 string string time amount master_sig) - - let aml_officer_setup = - let master_sig = Bin_sig.MasterAmlOfficerStatus.caqti in - Caqti_type.custom - ~encode:(fun - AmlOfficerSetup. - { - officer_pub; - master_sig; - officer_name; - is_active; - read_only; - change_date; - } - -> - Ok - ( officer_pub, - master_sig, - officer_name, - is_active, - read_only, - change_date )) - ~decode:(fun - ( officer_pub, - master_sig, - officer_name, - is_active, - read_only, - change_date ) - -> - Ok - { - officer_pub; - master_sig; - officer_name; - is_active; - read_only; - change_date; - }) - Caqti_type.(t6 eddsa_pub master_sig string bool bool time) - - let exchange_partner_setup = - let master_sig = Bin_sig.PartnerConfiguration.caqti in - Caqti_type.custom - ~encode:(fun - ExchangePartnerSetupRequest. - { - partner_pub; - start_date; - end_date; - wad_frequency; - wad_fee; - master_sig; - partner_base_url; - } - -> - Ok - ( partner_pub, - start_date, - end_date, - wad_frequency, - wad_fee, - master_sig, - partner_base_url )) - ~decode:(fun - ( partner_pub, - start_date, - end_date, - wad_frequency, - wad_fee, - master_sig, - partner_base_url ) - -> - Ok - { - partner_base_url; - partner_pub; - wad_frequency; - master_sig; - start_date; - end_date; - wad_fee; - }) - Caqti_type.(t7 eddsa_pub time time time_span amount master_sig string) - + include Pg_type include Caqti_request.Infix end +open Crypto +open Api + let preflight = let l = List.map diff --git a/src/pg_type.ml b/src/pg_type.ml new file mode 100644 index 00000000..dd0b70f0 --- /dev/null +++ b/src/pg_type.ml @@ -0,0 +1,367 @@ +(* this module defines caqti encoding/decodings *) +open Crypto +open Bin_type +open Api +open Caqti_type + +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) + +(* we want to use int64 timestamps, + not postgresql built-in timestamp type *) +let ptime : Ptime.t option t = Timestamp.caqti +let time = Timestamp.caqti +let time_span = Timestamp.Span.caqti +let age_mask : int t = Caqti_type.int +let rsa_pub = RsaPublicKey.caqti +let eddsa_pub = EddsaPublicKey.caqti +let eddsa_sig = EddsaSignature.caqti + +(* todo: enum type for wire_method? *) +let wire_method = Caqti_type.string +let payto_uri = Caqti_type.string +let b32 = Api.B32.caqti + +include struct + (* alias for hash *) + + 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 + +let signkey_data = + let master_sig = Bin_sig.ExchangeSigningKeyValidity.caqti in + custom + ~encode:(fun + Signkey_data.{ pub; stamp_start; stamp_expire; stamp_end; master_sig } + -> + match master_sig with + | None -> Error "signkey_data master_sig is none" + | Some master_sig -> + Ok (pub, stamp_start, stamp_expire, stamp_end, master_sig)) + ~decode:(fun (pub, stamp_start, stamp_expire, stamp_end, master_sig) -> + Ok + { + pub; + stamp_start; + stamp_expire; + stamp_end; + master_sig= Some master_sig; + }) + (t5 eddsa_pub time time time master_sig) + +let denom_data = + let master_sig = Bin_sig.DenominationKeyValidity.caqti in + custom + ~encode:(fun + Denom_data. + { + pub; + 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; + } + -> + match master_sig with + | None -> Error "denom_data master_sig is none" + | Some master_sig -> + Ok + ( pub, + 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) )) + ~decode:(fun + ( pub, + 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) ) + -> + Ok + { + pub; + 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= Some master_sig; + }) + (t12 rsa_pub amount time time time time amount amount amount amount int + (t2 denomination_hash master_sig)) + +let account_restrictions = + custom + ~encode:(fun l -> Api.encode (Jsont.list AccountRestriction.jsont) l) + ~decode:(fun s -> Api.decode (Jsont.list AccountRestriction.jsont) s) + string + +let global_fee = + let master_sig = Bin_sig.GlobalFees.caqti in + custom + ~encode:(fun + GlobalFees. + { + start_date; + end_date; + history_fee; + account_fee; + purse_fee; + history_expiration; + purse_account_limit; + purse_timeout; + master_sig; + } + -> + Ok + ( start_date, + end_date, + history_fee, + account_fee, + purse_fee, + history_expiration, + purse_account_limit, + purse_timeout, + master_sig )) + ~decode:(fun + ( start_date, + end_date, + history_fee, + account_fee, + purse_fee, + history_expiration, + purse_account_limit, + purse_timeout, + master_sig ) + -> + Ok + { + start_date; + end_date; + history_fee; + account_fee; + purse_fee; + history_expiration; + purse_account_limit; + purse_timeout; + master_sig; + }) + Caqti_type.( + t9 time time amount amount amount time_span int32 time_span master_sig) + +let aggregate_transfer_fee = + let master_sig = Bin_sig.MasterWireFee.caqti in + Caqti_type.custom + ~encode:(fun + AggregateTransferFee. + { wire_fee; closing_fee; start_date; end_date; sig_ } + -> Ok (wire_fee, closing_fee, start_date, end_date, sig_)) + ~decode:(fun (wire_fee, closing_fee, start_date, end_date, sig_) -> + Ok + AggregateTransferFee. + { wire_fee; closing_fee; start_date; end_date; sig_ }) + Caqti_type.(t5 amount amount time time master_sig) + +let exchange_wire_account = + let master_sig = Bin_sig.MasterWireDetails.caqti in + Caqti_type.custom + ~encode:(fun + ExchangeWireAccount. + { + payto_uri; + conversion_url; + debit_restrictions; + credit_restrictions; + master_sig; + bank_label; + priority; + } + -> + Ok + ( payto_uri, + conversion_url, + debit_restrictions, + credit_restrictions, + master_sig, + bank_label, + priority )) + ~decode:(fun + ( payto_uri, + conversion_url, + debit_restrictions, + credit_restrictions, + master_sig, + bank_label, + priority ) + -> + Ok + { + payto_uri; + conversion_url; + debit_restrictions; + credit_restrictions; + master_sig; + bank_label; + priority; + }) + Caqti_type.( + t7 payto_uri (option string) account_restrictions account_restrictions + master_sig (option string) (option int)) + +let drain_profit_message = + let master_sig = Bin_sig.MasterDrainProfit.caqti in + Caqti_type.custom + ~encode:(fun + DrainProfitsMessage. + { + wtid; + debit_account_section; + credit_payto_uri; + date; + amount; + master_sig; + } + -> + Ok + (wtid, debit_account_section, credit_payto_uri, date, amount, master_sig)) + ~decode:(fun + (wtid, debit_account_section, credit_payto_uri, date, amount, master_sig) + -> + Ok + { + wtid; + debit_account_section; + credit_payto_uri; + date; + amount; + master_sig; + }) + Caqti_type.(t6 b32 string string time amount master_sig) + +let aml_officer_setup = + let master_sig = Bin_sig.MasterAmlOfficerStatus.caqti in + Caqti_type.custom + ~encode:(fun + AmlOfficerSetup. + { + officer_pub; + master_sig; + officer_name; + is_active; + read_only; + change_date; + } + -> + Ok + ( officer_pub, + master_sig, + officer_name, + is_active, + read_only, + change_date )) + ~decode:(fun + ( officer_pub, + master_sig, + officer_name, + is_active, + read_only, + change_date ) + -> + Ok + { + officer_pub; + master_sig; + officer_name; + is_active; + read_only; + change_date; + }) + Caqti_type.(t6 eddsa_pub master_sig string bool bool time) + +let exchange_partner_setup = + let master_sig = Bin_sig.PartnerConfiguration.caqti in + Caqti_type.custom + ~encode:(fun + ExchangePartnerSetupRequest. + { + partner_pub; + start_date; + end_date; + wad_frequency; + wad_fee; + master_sig; + partner_base_url; + } + -> + Ok + ( partner_pub, + start_date, + end_date, + wad_frequency, + wad_fee, + master_sig, + partner_base_url )) + ~decode:(fun + ( partner_pub, + start_date, + end_date, + wad_frequency, + wad_fee, + master_sig, + partner_base_url ) + -> + Ok + { + partner_base_url; + partner_pub; + wad_frequency; + master_sig; + start_date; + end_date; + wad_fee; + }) + Caqti_type.(t7 eddsa_pub time time time_span amount master_sig string)