0002-contracts.sql (2918B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2014--2022 Taler Systems SA 4 -- 5 -- TALER is free software; you can redistribute it and/or modify it under the 6 -- terms of the GNU General Public License as published by the Free Software 7 -- Foundation; either version 3, or (at your option) any later version. 8 -- 9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 -- A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 -- 13 -- You should have received a copy of the GNU General Public License along with 14 -- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 -- 16 17 18 CREATE FUNCTION create_table_contracts( 19 IN partition_suffix TEXT DEFAULT NULL 20 ) 21 RETURNS VOID 22 LANGUAGE plpgsql 23 AS $$ 24 DECLARE 25 table_name TEXT DEFAULT 'contracts'; 26 BEGIN 27 PERFORM create_partitioned_table( 28 'CREATE TABLE %I ' 29 '(contract_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' 30 ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)' 31 ',pub_ckey BYTEA NOT NULL CHECK (LENGTH(pub_ckey)=32)' 32 ',contract_sig BYTEA NOT NULL CHECK (LENGTH(contract_sig)=64)' 33 ',e_contract BYTEA NOT NULL' 34 ',purse_expiration INT8 NOT NULL' 35 ',PRIMARY KEY (purse_pub)' 36 ') %s ;' 37 ,table_name 38 ,'PARTITION BY HASH (purse_pub)' 39 ,partition_suffix 40 ); 41 PERFORM comment_partitioned_table( 42 'encrypted contracts associated with purses' 43 ,table_name 44 ,partition_suffix 45 ); 46 PERFORM comment_partitioned_column( 47 'public key of the purse that the contract is associated with' 48 ,'purse_pub' 49 ,table_name 50 ,partition_suffix 51 ); 52 PERFORM comment_partitioned_column( 53 'signature over the encrypted contract by the purse contract key' 54 ,'contract_sig' 55 ,table_name 56 ,partition_suffix 57 ); 58 PERFORM comment_partitioned_column( 59 'Public ECDH key used to encrypt the contract, to be used with the purse private key for decryption' 60 ,'pub_ckey' 61 ,table_name 62 ,partition_suffix 63 ); 64 PERFORM comment_partitioned_column( 65 'AES-GCM encrypted contract terms (contains gzip compressed JSON after decryption)' 66 ,'e_contract' 67 ,table_name 68 ,partition_suffix 69 ); 70 END 71 $$; 72 73 74 CREATE FUNCTION constrain_table_contracts( 75 IN partition_suffix TEXT 76 ) 77 RETURNS VOID 78 LANGUAGE plpgsql 79 AS $$ 80 DECLARE 81 table_name TEXT DEFAULT 'contracts'; 82 BEGIN 83 table_name = concat_ws('_', table_name, partition_suffix); 84 EXECUTE FORMAT ( 85 'ALTER TABLE ' || table_name || 86 ' ADD CONSTRAINT ' || table_name || '_contract_serial_id_key' 87 ' UNIQUE (contract_serial_id) ' 88 ); 89 END 90 $$; 91 92 93 INSERT INTO exchange_tables 94 (name 95 ,version 96 ,action 97 ,partitioned 98 ,by_range) 99 VALUES 100 ('contracts' 101 ,'exchange-0002' 102 ,'create' 103 ,TRUE 104 ,FALSE), 105 ('contracts' 106 ,'exchange-0002' 107 ,'constrain' 108 ,TRUE 109 ,FALSE);