From 2cc7bbd5fcf0680fe138c2a3e370a1794c77b067 Mon Sep 17 00:00:00 2001 From: swrup Date: Wed, 25 Feb 2026 22:42:33 +0100 Subject: [PATCH] + drain --- src/api.ml | 2 +- src/http_management.ml | 14 ++++++++++++-- src/pg.ml | 11 ++++++++++- src/pg_type.ml | 3 +-- test/offline_management.sh | 15 ++++++++++++--- tools/offline.ml | 15 ++++----------- tools/offline_impl.ml | 5 +++++ 7 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/api.ml b/src/api.ml index 4d462dd6..f6b87cc3 100644 --- a/src/api.ml +++ b/src/api.ml @@ -647,7 +647,7 @@ end module DrainProfitsMessage = struct type t = { - wtid: B32.t; + wtid: Bytes32.t; debit_account_section: string; credit_payto_uri: string; date: Timestamp.t; diff --git a/src/http_management.ml b/src/http_management.ml index 16b92e65..47b4a139 100644 --- a/src/http_management.ml +++ b/src/http_management.ml @@ -537,8 +537,18 @@ module Drain = struct } let do_ ~db_conn v = - let+ () = Pg.insert_drain_profit db_conn v |> unwrap_err_caqti in - () + let* opt = + Pg.find_drain_profit db_conn v.DrainProfitsMessage.wtid + |> unwrap_err_caqti + in + match opt with + | Some _ -> + Logs.info (fun m -> m "drain profit message already added to database"); + Ok () + | None -> + let+ () = Pg.insert_drain_profit db_conn v |> unwrap_err_caqti in + Logs.info (fun m -> m "added drain profit message to database"); + () let jsont = DrainProfitsMessage.jsont diff --git a/src/pg.ml b/src/pg.ml index 784e6104..749d9d46 100644 --- a/src/pg.ml +++ b/src/pg.ml @@ -288,11 +288,20 @@ let get_wire_accounts = in fun (module Conn : CONN) () -> Conn.collect_list req () +let find_drain_profit = + let req = + Caqti_type.(octets ->? drain_profit_message) + "SELECT wtid, account_section, payto_uri, trigger_date, (amount).*, \ + master_sig FROM profit_drains WHERE wtid=$1" + in + fun (module Conn : CONN) wtid -> Conn.find_opt req wtid + let insert_drain_profit = let req = Caqti_type.(drain_profit_message ->. unit) "INSERT INTO profit_drains (wtid, account_section, payto_uri, \ - trigger_date, amount, master_sig) VALUES ($1, $2, $3, $4, ($5,$6), $7)" + trigger_date, amount, master_sig) VALUES ($1::BYTEA, $2, $3, $4, \ + ($5,$6), $7)" in fun (module Conn : CONN) v -> Conn.exec req v diff --git a/src/pg_type.ml b/src/pg_type.ml index a78f274a..57865968 100644 --- a/src/pg_type.ml +++ b/src/pg_type.ml @@ -24,7 +24,6 @@ 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 = B32.caqti include struct (* alias for hash *) @@ -266,7 +265,7 @@ let drain_profit_message = amount; master_sig; }) - Caqti_type.(t6 b32 string string time amount master_sig) + Caqti_type.(t6 octets string string time amount master_sig) let aml_officer_setup = let master_sig = Signatures.MasterAmlOfficerStatus.caqti in diff --git a/test/offline_management.sh b/test/offline_management.sh index 0b2702b3..63877239 100755 --- a/test/offline_management.sh +++ b/test/offline_management.sh @@ -98,6 +98,15 @@ offline_tool disable-wire \ offline_tool upload --input $b --url $url"/management/wire/disable" echo "[OK] /management/wire/disable" -# todo test drain -# todo test aml-officer -# todo test partners +# wtid (after base32 decode) must be 32bytes +wtid="000G40R40M30E209185GR38E1W8124GK2GAHC5RR34D1P70X3RFG" +offline_tool drain \ +--master_key $master_key \ +--output $b \ +--debit_account_section "" \ +--credit_payto_uri "" \ +--wtid $wtid \ +--date 0 \ +--amount $zero_euro +offline_tool upload --input $b --url $url"/management/drain" +echo "[OK] /management/drain" diff --git a/tools/offline.ml b/tools/offline.ml index 306f4dd8..595bcc9c 100644 --- a/tools/offline.ml +++ b/tools/offline.ml @@ -1,13 +1,6 @@ -(* TODO - all management operations: - /management/wire - /management/wire/disable - - /management/aml-officers - -> /aml - /management/partners - -> /wads *) - +(* not done: + /management/aml-officers (for /aml) + /management/partners (for /wads) *) open Cmdliner open Cmdliner.Term.Syntax open Offline_impl @@ -365,9 +358,9 @@ let cli = disable_auditor_cmd; wire_fee_cmd; global_fees_cmd; - drain_cmd; enable_wire_cmd; disable_wire_cmd; + drain_cmd; (* - *) test_hash64_cmd; ] diff --git a/tools/offline_impl.ml b/tools/offline_impl.ml index d8452579..cc681153 100644 --- a/tools/offline_impl.ml +++ b/tools/offline_impl.ml @@ -411,6 +411,11 @@ let disable_wire ~output ~master_key ~payto_uri = let drain ~output ~master_key ~debit_account_section ~credit_payto_uri ~wtid ~date ~amount = let* key = read_master_key_file master_key in + let* () = + match String.length wtid = 32 with + | false -> Error "invalid wtid: must be 32 bytes" + | true -> Ok () + in let master_sig = let open Signatures.MasterDrainProfit in signf (EddsaSignature.sign ~key)