exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

commit 85020a9204f3fdcc1ab29fa43e18d53c31fb595a
parent 963e84aad2eb6fdd0de17d3176e080b5ae1b415b
Author: Özgür Kesim <oec-taler@kesim.org>
Date:   Fri, 28 Jul 2023 12:36:28 +0200

Merge branch 'master' of ssh://git.taler.net/exchange

Diffstat:
Asrc/exchangedb/0002-age_withdrawl.sql | 156+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/exchangedb/0003-aml_history.sql -> src/exchangedb/0002-aml_history.sql | 0
Rsrc/exchangedb/0003-aml_staff.sql -> src/exchangedb/0002-aml_staff.sql | 0
Rsrc/exchangedb/0003-aml_status.sql -> src/exchangedb/0002-aml_status.sql | 0
Asrc/exchangedb/0002-kyc_attributes.sql | 155+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/exchangedb/0003-purse_actions.sql -> src/exchangedb/0002-purse_actions.sql | 0
Rsrc/exchangedb/0003-purse_deletion.sql -> src/exchangedb/0002-purse_deletion.sql | 0
Msrc/exchangedb/0002-wire_accounts.sql | 11+++++++++++
Dsrc/exchangedb/0003-age_withdraw.sql | 156-------------------------------------------------------------------------------
Dsrc/exchangedb/0003-kyc_attributes.sql | 156-------------------------------------------------------------------------------
Dsrc/exchangedb/0004-kyc_attributes.sql | 44--------------------------------------------
Dsrc/exchangedb/0004-wire_accounts.sql | 26--------------------------
Msrc/exchangedb/Makefile.am | 22++--------------------
Msrc/exchangedb/exchange-0001.sql | 28++++++++--------------------
Msrc/exchangedb/exchange-0002.sql.in | 24++++++++++++++++++++++++
Msrc/exchangedb/exchange-0003.sql.in | 7-------
Dsrc/exchangedb/exchange-0004.sql.in | 25-------------------------
Msrc/exchangedb/exchange_do_age_withdraw.sql | 15+++++++--------
Msrc/exchangedb/exchange_do_reserves_in_insert.sql | 17+----------------
19 files changed, 364 insertions(+), 478 deletions(-)

diff --git a/src/exchangedb/0002-age_withdrawl.sql b/src/exchangedb/0002-age_withdrawl.sql @@ -0,0 +1,156 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2023 Taler Systems SA +-- +-- TALER is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +-- +-- @author Özgür Kesim + +CREATE FUNCTION create_table_age_withdraw( + IN partition_suffix VARCHAR DEFAULT NULL +) +RETURNS VOID +LANGUAGE plpgsql +AS $$ +DECLARE + table_name VARCHAR DEFAULT 'age_withdraw'; +BEGIN + PERFORM create_partitioned_table( + 'CREATE TABLE %I' + '(age_withdraw_id BIGINT GENERATED BY DEFAULT AS IDENTITY' + ',h_commitment BYTEA NOT NULL CONSTRAINT h_commitment_length CHECK(LENGTH(h_commitment)=64)' + ',max_age SMALLINT NOT NULL CONSTRAINT max_age_positive CHECK(max_age>=0)' + ',amount_with_fee taler_amount NOT NULL' + ',reserve_pub BYTEA NOT NULL CONSTRAINT reserve_pub_length CHECK(LENGTH(reserve_pub)=32)' + ',reserve_sig BYTEA NOT NULL CONSTRAINT reserve_sig_length CHECK(LENGTH(reserve_sig)=64)' + ',noreveal_index SMALLINT NOT NULL CONSTRAINT noreveal_index_positive CHECK(noreveal_index>=0)' + ',h_blind_evs BYTEA[] NOT NULL CONSTRAINT h_blind_evs_length CHECK(cardinality(h_blind_evs)=cardinality(denom_serials))' + ',denom_serials INT8[] NOT NULL CONSTRAINT denom_serials_array_length CHECK(cardinality(denom_serials)=cardinality(denom_sigs))' + ',denom_sigs BYTEA[] NOT NULL CONSTRAINT denom_sigs_array_length CHECK(cardinality(denom_sigs)=cardinality(denom_serials))' + ') %s ;' + ,table_name + ,'PARTITION BY HASH (reserve_pub)' + ,partition_suffix + ); + PERFORM comment_partitioned_table( + 'Commitments made when withdrawing coins with age restriction and the gamma value chosen by the exchange. ' + 'It also contains the blindly signed coins, their signatures and denominations.' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'The gamma value chosen by the exchange in the cut-and-choose protocol' + ,'noreveal_index' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'The maximum age (in years) that the client commits to with this request' + ,'max_age' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'Commitment made by the client, hash over the various client inputs in the cut-and-choose protocol' + ,'h_commitment' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'Reference to the public key of the reserve from which the coins are going to be withdrawn' + ,'reserve_pub' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'Signature of the reserve''s private key over the age-withdraw request' + ,'reserve_sig' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'Array of references to the denominations' + ,'denom_serials' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'Array of the blinded envelopes of the chosen fresh coins, with value as given by the denomination in the corresponding slot in denom_serials' + ,'h_blind_evs' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'Array of signatures over each blinded envelope' + ,'denom_sigs' + ,table_name + ,partition_suffix + ); +END +$$; + + +CREATE FUNCTION constrain_table_age_withdraw( + IN partition_suffix VARCHAR +) +RETURNS void +LANGUAGE plpgsql +AS $$ +DECLARE + table_name VARCHAR DEFAULT 'age_withdraw'; +BEGIN + table_name = concat_ws('_', table_name, partition_suffix); + EXECUTE FORMAT ( + 'ALTER TABLE ' || table_name || + ' ADD PRIMARY KEY (h_commitment);' + ); + EXECUTE FORMAT ( + 'ALTER TABLE ' || table_name || + ' ADD CONSTRAINT ' || table_name || '_h_commitment_reserve_pub_key' + ' UNIQUE (h_commitment, reserve_pub);' + ); + EXECUTE FORMAT ( + 'ALTER TABLE ' || table_name || + ' ADD CONSTRAINT ' || table_name || '_age_withdraw_id_key' + ' UNIQUE (age_withdraw_id);' + ); +END +$$; + + +CREATE FUNCTION foreign_table_age_withdraw() +RETURNS void +LANGUAGE plpgsql +AS $$ +DECLARE + table_name VARCHAR DEFAULT 'age_withdraw'; +BEGIN + EXECUTE FORMAT ( + 'ALTER TABLE ' || table_name || + ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub' + ' FOREIGN KEY (reserve_pub)' + ' REFERENCES reserves(reserve_pub);' -- ON DELETE CASCADE;' + ); +END +$$; + + +INSERT INTO exchange_tables + (name + ,version + ,action + ,partitioned + ,by_range) +VALUES + ('age_withdraw', 'exchange-0003', 'create', TRUE ,FALSE), + ('age_withdraw', 'exchange-0003', 'constrain',TRUE ,FALSE), + ('age_withdraw', 'exchange-0003', 'foreign', TRUE ,FALSE); diff --git a/src/exchangedb/0003-aml_history.sql b/src/exchangedb/0002-aml_history.sql diff --git a/src/exchangedb/0003-aml_staff.sql b/src/exchangedb/0002-aml_staff.sql diff --git a/src/exchangedb/0003-aml_status.sql b/src/exchangedb/0002-aml_status.sql diff --git a/src/exchangedb/0002-kyc_attributes.sql b/src/exchangedb/0002-kyc_attributes.sql @@ -0,0 +1,155 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2014--2022 Taler Systems SA +-- +-- TALER is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +-- + +CREATE OR REPLACE FUNCTION create_table_kyc_attributes( + IN partition_suffix VARCHAR DEFAULT NULL +) +RETURNS VOID +LANGUAGE plpgsql +AS $$ +DECLARE + table_name VARCHAR DEFAULT 'kyc_attributes'; +BEGIN + PERFORM create_partitioned_table( + 'CREATE TABLE IF NOT EXISTS %I' + '(kyc_attributes_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' + ',h_payto BYTEA PRIMARY KEY CHECK (LENGTH(h_payto)=32)' + ',kyc_prox BYTEA NOT NULL CHECK (LENGTH(kyc_prox)=32)' + ',provider VARCHAR NOT NULL' + ',collection_time INT8 NOT NULL' + ',expiration_time INT8 NOT NULL' + ',encrypted_attributes BYTEA NOT NULL' + ',legitimization_serial INT8 NOT NULL' + ') %s ;' + ,table_name + ,'PARTITION BY HASH (h_payto)' + ,partition_suffix + ); + PERFORM comment_partitioned_table( + 'KYC data about particular payment addresses' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'hash of payto://-URI the attributes are about' + ,'h_payto' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'short hash of normalized full name and birthdate; used to efficiently find likely duplicate users' + ,'kyc_prox' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'birth date of the user, in format YYYY-MM-DD where a value of 0 is used to indicate unknown (in official documents); NULL if the birth date was not collected by the provider; used for KYC-driven age restrictions' + ,'birthdate' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'time when the attributes were collected by the provider' + ,'collection_time' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'time when the attributes should no longer be considered validated' + ,'expiration_time' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'configuration section name of the provider that affirmed the attributes' + ,'provider' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + '(encrypted) JSON object (as string) with the attributes' + ,'encrypted_attributes' + ,table_name + ,partition_suffix + ); + PERFORM comment_partitioned_column( + 'Reference the legitimization process for which theses attributes are gathered for.' + ,'legitimization_serial' + ,table_name + ,partition_suffix + ); +END $$; + +COMMENT ON FUNCTION create_table_kyc_attributes + IS 'Creates the kyc_attributes table'; + + +CREATE OR REPLACE FUNCTION constrain_table_kyc_attributes( + IN partition_suffix VARCHAR +) +RETURNS void +LANGUAGE plpgsql +AS $$ +DECLARE + table_name VARCHAR DEFAULT 'kyc_attributes'; +BEGIN + table_name = concat_ws('_', table_name, partition_suffix); + EXECUTE FORMAT ( + 'ALTER TABLE ' || table_name || + ' ADD CONSTRAINT ' || table_name || '_serial_key ' + 'UNIQUE (kyc_attributes_serial_id)' + ); + -- The legitimization_serial is a foreign key. + -- TODO: due to partitioning by h_payto, we can not simply reference + -- the serial id of the legitimization_processes + -- EXECUTE FORMAT ( + -- 'ALTER TABLE ' || table_name || + -- ' ADD CONSTRAINT ' || table_name || '_foreign_legitimization_processes' + -- ' FOREIGN KEY (legitimization_serial) ' + -- ' REFERENCES legitimization_processes (legitimization_process_serial_id)' -- ON DELETE CASCADE + -- ); + -- To search similar users (e.g. during AML checks) + EXECUTE FORMAT ( + 'CREATE INDEX ' || table_name || '_similarity_index ' + 'ON ' || table_name || ' ' + '(kyc_prox);' + ); + -- For garbage collection + EXECUTE FORMAT ( + 'CREATE INDEX ' || table_name || '_expiration_time ' + 'ON ' || table_name || ' ' + '(expiration_time ASC);' + ); +END $$; + + +INSERT INTO exchange_tables + (name + ,version + ,action + ,partitioned + ,by_range) + VALUES + ('kyc_attributes' + ,'exchange-0003' + ,'create' + ,TRUE + ,FALSE), + ('kyc_attributes' + ,'exchange-0003' + ,'constrain' + ,TRUE + ,FALSE); diff --git a/src/exchangedb/0003-purse_actions.sql b/src/exchangedb/0002-purse_actions.sql diff --git a/src/exchangedb/0003-purse_deletion.sql b/src/exchangedb/0002-purse_deletion.sql diff --git a/src/exchangedb/0002-wire_accounts.sql b/src/exchangedb/0002-wire_accounts.sql @@ -19,6 +19,9 @@ CREATE TABLE wire_accounts ,master_sig BYTEA CHECK (LENGTH(master_sig)=64) ,is_active BOOLEAN NOT NULL ,last_change INT8 NOT NULL + ,conversion_url VARCHAR DEFAULT (NULL) + ,debit_restrictions VARCHAR DEFAULT (NULL) + ,credit_restrictions VARCHAR DEFAULT (NULL) ); COMMENT ON TABLE wire_accounts IS 'Table with current and historic bank accounts of the exchange. Entries never expire as we need to remember the last_change column indefinitely.'; @@ -30,5 +33,13 @@ COMMENT ON COLUMN wire_accounts.is_active IS 'true if we are currently supporting the use of this account.'; COMMENT ON COLUMN wire_accounts.last_change IS 'Latest time when active status changed. Used to detect replays of old messages.'; +COMMENT ON COLUMN wire_accounts.conversion_url + IS 'URL of a currency conversion service if conversion is needed when this account is used; NULL if there is no conversion.'; +COMMENT ON COLUMN wire_accounts.debit_restrictions + IS 'JSON array describing restrictions imposed when debiting this account. Empty for no restrictions, NULL if account was migrated from previous database revision or account is disabled.'; +COMMENT ON COLUMN wire_accounts.credit_restrictions + IS 'JSON array describing restrictions imposed when crediting this account. Empty for no restrictions, NULL if account was migrated from previous database revision or account is disabled.'; + + -- "wire_accounts" has no sequence because it is a 'mutable' table -- and is of no concern to the auditor diff --git a/src/exchangedb/0003-age_withdraw.sql b/src/exchangedb/0003-age_withdraw.sql @@ -1,156 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2023 Taler Systems SA --- --- TALER is free software; you can redistribute it and/or modify it under the --- terms of the GNU General Public License as published by the Free Software --- Foundation; either version 3, or (at your option) any later version. --- --- TALER is distributed in the hope that it will be useful, but WITHOUT ANY --- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR --- A PARTICULAR PURPOSE. See the GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> --- --- @author Özgür Kesim - -CREATE FUNCTION create_table_age_withdraw( - IN partition_suffix VARCHAR DEFAULT NULL -) -RETURNS VOID -LANGUAGE plpgsql -AS $$ -DECLARE - table_name VARCHAR DEFAULT 'age_withdraw'; -BEGIN - PERFORM create_partitioned_table( - 'CREATE TABLE %I' - '(age_withdraw_id BIGINT GENERATED BY DEFAULT AS IDENTITY' - ',h_commitment BYTEA NOT NULL CONSTRAINT h_commitment_length CHECK(LENGTH(h_commitment)=64)' - ',max_age SMALLINT NOT NULL CONSTRAINT max_age_positive CHECK(max_age>=0)' - ',amount_with_fee TALER_AMOUNT NOT NULL' - ',reserve_pub BYTEA NOT NULL CONSTRAINT reserve_pub_length CHECK(LENGTH(reserve_pub)=32)' - ',reserve_sig BYTEA NOT NULL CONSTRAINT reserve_sig_length CHECK(LENGTH(reserve_sig)=64)' - ',noreveal_index SMALLINT NOT NULL CONSTRAINT noreveal_index_positive CHECK(noreveal_index>=0)' - ',h_blind_evs BYTEA[] NOT NULL CONSTRAINT h_blind_evs_length CHECK(cardinality(h_blind_evs)=cardinality(denom_serials))' - ',denom_serials INT8[] NOT NULL CONSTRAINT denom_serials_array_length CHECK(cardinality(denom_serials)=cardinality(denom_sigs))' - ',denom_sigs BYTEA[] NOT NULL CONSTRAINT denom_sigs_array_length CHECK(cardinality(denom_sigs)=cardinality(denom_serials))' - ') %s ;' - ,table_name - ,'PARTITION BY HASH (reserve_pub)' - ,partition_suffix - ); - PERFORM comment_partitioned_table( - 'Commitments made when withdrawing coins with age restriction and the gamma value chosen by the exchange. ' - 'It also contains the blindly signed coins, their signatures and denominations.' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'The gamma value chosen by the exchange in the cut-and-choose protocol' - ,'noreveal_index' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'The maximum age (in years) that the client commits to with this request' - ,'max_age' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'Commitment made by the client, hash over the various client inputs in the cut-and-choose protocol' - ,'h_commitment' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'Reference to the public key of the reserve from which the coins are going to be withdrawn' - ,'reserve_pub' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'Signature of the reserve''s private key over the age-withdraw request' - ,'reserve_sig' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'Array of references to the denominations' - ,'denom_serials' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'Array of the blinded envelopes of the chosen fresh coins, with value as given by the denomination in the corresponding slot in denom_serials' - ,'h_blind_evs' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'Array of signatures over each blinded envelope' - ,'denom_sigs' - ,table_name - ,partition_suffix - ); -END -$$; - - -CREATE FUNCTION constrain_table_age_withdraw( - IN partition_suffix VARCHAR -) -RETURNS void -LANGUAGE plpgsql -AS $$ -DECLARE - table_name VARCHAR DEFAULT 'age_withdraw'; -BEGIN - table_name = concat_ws('_', table_name, partition_suffix); - EXECUTE FORMAT ( - 'ALTER TABLE ' || table_name || - ' ADD PRIMARY KEY (h_commitment);' - ); - EXECUTE FORMAT ( - 'ALTER TABLE ' || table_name || - ' ADD CONSTRAINT ' || table_name || '_h_commitment_reserve_pub_key' - ' UNIQUE (h_commitment, reserve_pub);' - ); - EXECUTE FORMAT ( - 'ALTER TABLE ' || table_name || - ' ADD CONSTRAINT ' || table_name || '_age_withdraw_id_key' - ' UNIQUE (age_withdraw_id);' - ); -END -$$; - - -CREATE FUNCTION foreign_table_age_withdraw() -RETURNS void -LANGUAGE plpgsql -AS $$ -DECLARE - table_name VARCHAR DEFAULT 'age_withdraw'; -BEGIN - EXECUTE FORMAT ( - 'ALTER TABLE ' || table_name || - ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub' - ' FOREIGN KEY (reserve_pub)' - ' REFERENCES reserves(reserve_pub);' -- ON DELETE CASCADE;' - ); -END -$$; - - -INSERT INTO exchange_tables - (name - ,version - ,action - ,partitioned - ,by_range) -VALUES - ('age_withdraw', 'exchange-0003', 'create', TRUE ,FALSE), - ('age_withdraw', 'exchange-0003', 'constrain',TRUE ,FALSE), - ('age_withdraw', 'exchange-0003', 'foreign', TRUE ,FALSE); diff --git a/src/exchangedb/0003-kyc_attributes.sql b/src/exchangedb/0003-kyc_attributes.sql @@ -1,156 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2014--2022 Taler Systems SA --- --- TALER is free software; you can redistribute it and/or modify it under the --- terms of the GNU General Public License as published by the Free Software --- Foundation; either version 3, or (at your option) any later version. --- --- TALER is distributed in the hope that it will be useful, but WITHOUT ANY --- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR --- A PARTICULAR PURPOSE. See the GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> --- - -CREATE OR REPLACE FUNCTION create_table_kyc_attributes( - IN partition_suffix VARCHAR DEFAULT NULL -) -RETURNS VOID -LANGUAGE plpgsql -AS $$ -DECLARE - table_name VARCHAR DEFAULT 'kyc_attributes'; -BEGIN - PERFORM create_partitioned_table( - 'CREATE TABLE IF NOT EXISTS %I' - '(kyc_attributes_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' - ',h_payto BYTEA PRIMARY KEY CHECK (LENGTH(h_payto)=32)' - ',kyc_prox BYTEA NOT NULL CHECK (LENGTH(kyc_prox)=32)' - ',provider VARCHAR NOT NULL' - ',birthdate VARCHAR' - ',collection_time INT8 NOT NULL' - ',expiration_time INT8 NOT NULL' - ',encrypted_attributes BYTEA NOT NULL' - ',legitimization_serial INT8 NOT NULL' - ') %s ;' - ,table_name - ,'PARTITION BY HASH (h_payto)' - ,partition_suffix - ); - PERFORM comment_partitioned_table( - 'KYC data about particular payment addresses' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'hash of payto://-URI the attributes are about' - ,'h_payto' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'short hash of normalized full name and birthdate; used to efficiently find likely duplicate users' - ,'kyc_prox' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'birth date of the user, in format YYYY-MM-DD where a value of 0 is used to indicate unknown (in official documents); NULL if the birth date was not collected by the provider; used for KYC-driven age restrictions' - ,'birthdate' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'time when the attributes were collected by the provider' - ,'collection_time' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'time when the attributes should no longer be considered validated' - ,'expiration_time' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'configuration section name of the provider that affirmed the attributes' - ,'provider' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - '(encrypted) JSON object (as string) with the attributes' - ,'encrypted_attributes' - ,table_name - ,partition_suffix - ); - PERFORM comment_partitioned_column( - 'Reference the legitimization process for which theses attributes are gathered for.' - ,'legitimization_serial' - ,table_name - ,partition_suffix - ); -END $$; - -COMMENT ON FUNCTION create_table_kyc_attributes - IS 'Creates the kyc_attributes table'; - - -CREATE OR REPLACE FUNCTION constrain_table_kyc_attributes( - IN partition_suffix VARCHAR -) -RETURNS void -LANGUAGE plpgsql -AS $$ -DECLARE - table_name VARCHAR DEFAULT 'kyc_attributes'; -BEGIN - table_name = concat_ws('_', table_name, partition_suffix); - EXECUTE FORMAT ( - 'ALTER TABLE ' || table_name || - ' ADD CONSTRAINT ' || table_name || '_serial_key ' - 'UNIQUE (kyc_attributes_serial_id)' - ); - -- The legitimization_serial is a foreign key. - -- TODO: due to partitioning by h_payto, we can not simply reference - -- the serial id of the legitimization_processes - -- EXECUTE FORMAT ( - -- 'ALTER TABLE ' || table_name || - -- ' ADD CONSTRAINT ' || table_name || '_foreign_legitimization_processes' - -- ' FOREIGN KEY (legitimization_serial) ' - -- ' REFERENCES legitimization_processes (legitimization_process_serial_id)' -- ON DELETE CASCADE - -- ); - -- To search similar users (e.g. during AML checks) - EXECUTE FORMAT ( - 'CREATE INDEX ' || table_name || '_similarity_index ' - 'ON ' || table_name || ' ' - '(kyc_prox);' - ); - -- For garbage collection - EXECUTE FORMAT ( - 'CREATE INDEX ' || table_name || '_expiration_time ' - 'ON ' || table_name || ' ' - '(expiration_time ASC);' - ); -END $$; - - -INSERT INTO exchange_tables - (name - ,version - ,action - ,partitioned - ,by_range) - VALUES - ('kyc_attributes' - ,'exchange-0003' - ,'create' - ,TRUE - ,FALSE), - ('kyc_attributes' - ,'exchange-0003' - ,'constrain' - ,TRUE - ,FALSE); diff --git a/src/exchangedb/0004-kyc_attributes.sql b/src/exchangedb/0004-kyc_attributes.sql @@ -1,44 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2023 Taler Systems SA --- --- TALER is free software; you can redistribute it and/or modify it under the --- terms of the GNU General Public License as published by the Free Software --- Foundation; either version 3, or (at your option) any later version. --- --- TALER is distributed in the hope that it will be useful, but WITHOUT ANY --- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR --- A PARTICULAR PURPOSE. See the GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> --- - -CREATE OR REPLACE FUNCTION master_table_kyc_attributes_V2() -RETURNS VOID -LANGUAGE plpgsql -AS $$ -DECLARE - table_name VARCHAR DEFAULT 'kyc_attributes'; -BEGIN - EXECUTE FORMAT ( - 'ALTER TABLE ' || table_name || - ' DROP COLUMN birthdate;' - ); -END $$; - -COMMENT ON FUNCTION master_table_kyc_attributes_V2 - IS 'Removes birthdate column from the kyc_attributes table'; - -INSERT INTO exchange_tables - (name - ,version - ,action - ,partitioned - ,by_range) - VALUES - ('kyc_attributes_V2' - ,'exchange-0004' - ,'master' - ,TRUE - ,FALSE); diff --git a/src/exchangedb/0004-wire_accounts.sql b/src/exchangedb/0004-wire_accounts.sql @@ -1,26 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2023 Taler Systems SA --- --- TALER is free software; you can redistribute it and/or modify it under the --- terms of the GNU General Public License as published by the Free Software --- Foundation; either version 3, or (at your option) any later version. --- --- TALER is distributed in the hope that it will be useful, but WITHOUT ANY --- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR --- A PARTICULAR PURPOSE. See the GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> --- - -ALTER TABLE wire_accounts - ADD COLUMN conversion_url VARCHAR DEFAULT (NULL), - ADD COLUMN debit_restrictions VARCHAR DEFAULT (NULL), - ADD COLUMN credit_restrictions VARCHAR DEFAULT (NULL); -COMMENT ON COLUMN wire_accounts.conversion_url - IS 'URL of a currency conversion service if conversion is needed when this account is used; NULL if there is no conversion.'; -COMMENT ON COLUMN wire_accounts.debit_restrictions - IS 'JSON array describing restrictions imposed when debiting this account. Empty for no restrictions, NULL if account was migrated from previous database revision or account is disabled.'; -COMMENT ON COLUMN wire_accounts.credit_restrictions - IS 'JSON array describing restrictions imposed when crediting this account. Empty for no restrictions, NULL if account was migrated from previous database revision or account is disabled.'; diff --git a/src/exchangedb/Makefile.am b/src/exchangedb/Makefile.am @@ -18,19 +18,13 @@ sqlinputs = \ exchange_do_*.sql \ procedures.sql.in \ 0002-*.sql \ - exchange-0002.sql.in \ - 0003-*.sql \ - exchange-0003.sql.in \ - 0004-*.sql \ - exchange-0004.sql.in + exchange-0002.sql.in sql_DATA = \ benchmark-0001.sql \ versioning.sql \ exchange-0001.sql \ exchange-0002.sql \ - exchange-0003.sql \ - exchange-0004.sql \ drop.sql \ procedures.sql @@ -41,9 +35,7 @@ BUILT_SOURCES = \ procedures.sql CLEANFILES = \ - exchange-0002.sql \ - exchange-0003.sql \ - exchange-0004.sql + exchange-0002.sql procedures.sql: procedures.sql.in exchange_do_*.sql chmod +w $@ || true @@ -55,16 +47,6 @@ exchange-0002.sql: exchange-0002.sql.in 0002-*.sql gcc -E -P -undef - < exchange-0002.sql.in 2>/dev/null | sed -e "s/--.*//" | awk 'NF' - >$@ chmod ugo-w $@ -exchange-0003.sql: exchange-0003.sql.in 0003-*.sql - chmod +w $@ || true - gcc -E -P -undef - < exchange-0003.sql.in 2>/dev/null | sed -e "s/--.*//" | awk 'NF' - >$@ - chmod ugo-w $@ - -exchange-0004.sql: exchange-0004.sql.in 0004-*.sql - chmod +w $@ || true - gcc -E -P -undef - < exchange-0004.sql.in 2>/dev/null | sed -e "s/--.*//" | awk 'NF' - >$@ - chmod ugo-w $@ - check_SCRIPTS = \ test_idempotency.sh diff --git a/src/exchangedb/exchange-0001.sql b/src/exchangedb/exchange-0001.sql @@ -141,22 +141,10 @@ COMMENT ON FUNCTION comment_partitioned_column -- Taler amounts and helper functiosn ------------------------------------------------------------- -DO $$ -BEGIN - CREATE TYPE TALER_AMOUNT - AS (val INT8 - ,frac INT4); - - COMMENT ON TYPE TALER_AMOUNT - IS 'Type to store a TALER-amount as (val, frac) pair.'; -EXCEPTION - WHEN duplicate_object THEN null; -END -$$; CREATE PROCEDURE amount_normalize( - IN amount TALER_AMOUNT - ,OUT normalized TALER_AMOUNT + IN amount taler_amount + ,OUT normalized taler_amount ) LANGUAGE plpgsql AS $$ @@ -169,9 +157,9 @@ COMMENT ON PROCEDURE amount_normalize IS 'Returns the normalized amount by adding to the .val the value of (.frac / 100000000) and removing the modulus 100000000 from .frac.'; CREATE PROCEDURE amount_add( - IN a TALER_AMOUNT - ,IN b TALER_AMOUNT - ,OUT sum TALER_AMOUNT + IN a taler_amount + ,IN b taler_amount + ,OUT sum taler_amount ) LANGUAGE plpgsql AS $$ @@ -189,9 +177,9 @@ COMMENT ON PROCEDURE amount_add IS 'Returns the normalized sum of two amounts. It raises an exception when the resulting .val is larger than 2^52'; CREATE FUNCTION amount_left_minus_right( - IN l TALER_AMOUNT - ,IN r TALER_AMOUNT - ,OUT diff TALER_AMOUNT + IN l taler_amount + ,IN r taler_amount + ,OUT diff taler_amount ,OUT ok BOOLEAN ) LANGUAGE plpgsql diff --git a/src/exchangedb/exchange-0002.sql.in b/src/exchangedb/exchange-0002.sql.in @@ -19,6 +19,21 @@ BEGIN; SELECT _v.register_patch('exchange-0002', NULL, NULL); SET search_path TO exchange; +CREATE TYPE taler_amount + AS + (val INT8 + ,frac INT4); +COMMENT ON TYPE taler_amount + IS 'Stores an amount, fraction is in units of 1/100000000 of the base value'; + +CREATE TYPE exchange_do_array_reserve_insert_return_type + AS + (transaction_duplicate BOOLEAN + ,ruuid INT8 + ); +COMMENT ON TYPE exchange_do_array_reserve_insert_return_type + IS 'Return type for exchange_do_array_reserves_insert() stored procedure'; + #include "0002-denominations.sql" #include "0002-denomination_revocations.sql" #include "0002-wire_targets.sql" @@ -71,5 +86,14 @@ SET search_path TO exchange; #include "0002-revolving_work_shards.sql" #include "0002-partners.sql" #include "0002-partner_accounts.sql" +#include "0002-purse_actions.sql" +#include "0002-purse_deletion.sql" +#include "0002-kyc_attributes.sql" +#include "0002-aml_status.sql" +#include "0002-aml_staff.sql" +#include "0002-aml_history.sql" +#include "0002-age_withdraw.sql" + + COMMIT; diff --git a/src/exchangedb/exchange-0003.sql.in b/src/exchangedb/exchange-0003.sql.in @@ -19,13 +19,6 @@ BEGIN; SELECT _v.register_patch('exchange-0003', NULL, NULL); SET search_path TO exchange; -#include "0003-purse_actions.sql" -#include "0003-purse_deletion.sql" -#include "0003-kyc_attributes.sql" -#include "0003-aml_status.sql" -#include "0003-aml_staff.sql" -#include "0003-aml_history.sql" -#include "0003-age_withdraw.sql" COMMIT; diff --git a/src/exchangedb/exchange-0004.sql.in b/src/exchangedb/exchange-0004.sql.in @@ -1,25 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2023 Taler Systems SA --- --- TALER is free software; you can redistribute it and/or modify it under the --- terms of the GNU General Public License as published by the Free Software --- Foundation; either version 3, or (at your option) any later version. --- --- TALER is distributed in the hope that it will be useful, but WITHOUT ANY --- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR --- A PARTICULAR PURPOSE. See the GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License along with --- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> --- - -BEGIN; - -SELECT _v.register_patch('exchange-0004', NULL, NULL); -SET search_path TO exchange; - -#include "0004-kyc_attributes.sql" -#include "0004-wire_accounts.sql" - -COMMIT; diff --git a/src/exchangedb/exchange_do_age_withdraw.sql b/src/exchangedb/exchange_do_age_withdraw.sql @@ -16,7 +16,7 @@ -- @author Özgür Kesim CREATE OR REPLACE FUNCTION exchange_do_age_withdraw( - IN amount_with_fee TALER_AMOUNT, + IN amount_with_fee taler_amount, IN rpub BYTEA, IN rsig BYTEA, IN now INT8, @@ -38,8 +38,8 @@ AS $$ DECLARE reserve_gc INT8; difference RECORD; - balance TALER_AMOUNT; - new_balance TALER_AMOUNT; + balance taler_amount; + new_balance taler_amount; not_before date; earliest_date date; BEGIN @@ -104,9 +104,9 @@ required_age=0; -- Check reserve balance is sufficient. SELECT * -INTO +INTO difference -FROM +FROM amount_left_minus_right( balance ,amount_with_fee); @@ -114,7 +114,7 @@ FROM balance_ok = difference.ok; IF NOT balance_ok -THEN +THEN RETURN; END IF; @@ -166,6 +166,5 @@ END IF; END $$; -COMMENT ON FUNCTION exchange_do_age_withdraw(TALER_AMOUNT, BYTEA, BYTEA, INT8, INT8, BYTEA, INT2, INT2, BYTEA[], INT8[], BYTEA[]) +COMMENT ON FUNCTION exchange_do_age_withdraw(taler_amount, BYTEA, BYTEA, INT8, INT8, BYTEA, INT2, INT2, BYTEA[], INT8[], BYTEA[]) IS 'Checks whether the reserve has sufficient balance for an age-withdraw operation (or the request is repeated and was previously approved) and that age requirements are met. If so updates the database with the result. Includes storing the blinded planchets and denomination signatures, or signaling conflict'; - diff --git a/src/exchangedb/exchange_do_reserves_in_insert.sql b/src/exchangedb/exchange_do_reserves_in_insert.sql @@ -966,21 +966,6 @@ END $$; - - - - -DO $$ -BEGIN - CREATE TYPE exchange_do_array_reserve_insert_return_type - AS - (transaction_duplicate BOOLEAN - ,ruuid INT8); -EXCEPTION - WHEN duplicate_object THEN null; -END -$$; - CREATE OR REPLACE FUNCTION exchange_do_array_reserves_insert( IN in_gc_date INT8, IN in_reserve_expiration INT8, @@ -1019,7 +1004,7 @@ BEGIN ,UNNEST (ina_payto_uri) AS payto_uri ON CONFLICT DO NOTHING; - FOR i IN + FOR i IN SELECT reserve_pub ,wire_ref