1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
--
-- This file is part of TALER
-- Copyright (C) 2023, 2024 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/>
--
DROP FUNCTION IF EXISTS exchange_do_insert_aml_decision;
CREATE FUNCTION exchange_do_insert_aml_decision(
IN in_payto_uri TEXT, -- can be NULL!
IN in_h_normalized_payto BYTEA,
IN in_h_full_payto BYTEA, -- can be NULL!
IN in_decision_time INT8,
IN in_expiration_time INT8,
IN in_properties TEXT, -- can be NULL
IN in_new_rules TEXT,
IN in_to_investigate BOOLEAN,
IN in_new_measure_name TEXT, -- can be NULL
IN in_jmeasures TEXT, -- can be NULL
IN in_justification TEXT,
IN in_decider_pub BYTEA,
IN in_decider_sig BYTEA,
IN in_notify_s TEXT,
OUT out_invalid_officer BOOLEAN,
OUT out_account_unknown BOOLEAN,
OUT out_last_date INT8,
OUT out_legitimization_measure_serial_id INT8)
LANGUAGE plpgsql
AS $$
DECLARE
my_outcome_serial_id INT8;
my_access_token BYTEA;
BEGIN
out_account_unknown=FALSE;
out_legitimization_measure_serial_id=0;
-- Check officer is eligible to make decisions.
PERFORM
FROM aml_staff
WHERE decider_pub=in_decider_pub
AND is_active
AND NOT read_only;
IF NOT FOUND
THEN
out_invalid_officer=TRUE;
out_last_date=0;
RETURN;
END IF;
out_invalid_officer=FALSE;
-- Check no more recent decision exists.
SELECT decision_time
INTO out_last_date
FROM legitimization_outcomes
WHERE h_payto=in_h_normalized_payto
AND is_active
ORDER BY decision_time DESC;
IF FOUND
THEN
IF out_last_date >= in_decision_time
THEN
-- Refuse to insert older decision.
RETURN;
END IF;
UPDATE legitimization_outcomes
SET is_active=FALSE
WHERE h_payto=in_h_normalized_payto
AND is_active;
ELSE
out_last_date = 0;
END IF;
SELECT access_token
INTO my_access_token
FROM wire_targets
WHERE h_normalized_payto=in_h_normalized_payto;
IF NOT FOUND
THEN
IF in_payto_uri IS NULL
THEN
-- AML decision on an unknown account without payto_uri => fail.
out_account_unknown=TRUE;
RETURN;
END IF;
INSERT INTO wire_targets
(wire_target_h_payto
,h_normalized_payto
,payto_uri)
VALUES
(in_h_full_payto
,in_h_normalized_payto
,in_payto_uri)
RETURNING access_token
INTO my_access_token;
END IF;
-- Did KYC measures get prescribed?
IF in_jmeasures IS NOT NULL
THEN
-- First check if a perfectly equivalent legi measure
-- already exists, to avoid creating tons of duplicates.
SELECT legitimization_measure_serial_id
INTO out_legitimization_measure_serial_id
FROM legitimization_measures
WHERE access_token=my_access_token
AND jmeasures=in_jmeasures
AND NOT is_finished;
IF NOT FOUND
THEN
-- Enable new legitimization measure
INSERT INTO legitimization_measures
(access_token
,start_time
,jmeasures
,display_priority)
VALUES
(my_access_token
,in_decision_time
,in_jmeasures
,1)
RETURNING
legitimization_measure_serial_id
INTO
out_legitimization_measure_serial_id;
END IF;
-- end if for where we had in_jmeasures
END IF;
-- AML decision: mark all other active measures finished!
UPDATE legitimization_measures
SET is_finished=TRUE
WHERE access_token=my_access_token
AND NOT is_finished
AND legitimization_measure_serial_id != out_legitimization_measure_serial_id;
UPDATE legitimization_outcomes
SET is_active=FALSE
WHERE h_payto=in_h_normalized_payto
-- this clause is a minor optimization to avoid
-- updating outcomes that have long expired.
AND expiration_time >= in_decision_time;
INSERT INTO legitimization_outcomes
(h_payto
,decision_time
,expiration_time
,jproperties
,new_measure_name
,to_investigate
,jnew_rules
)
VALUES
(in_h_normalized_payto
,in_decision_time
,in_expiration_time
,in_properties
,in_new_measure_name
,in_to_investigate
,in_new_rules
)
RETURNING
outcome_serial_id
INTO
my_outcome_serial_id;
INSERT INTO aml_history
(h_payto
,outcome_serial_id
,justification
,decider_pub
,decider_sig
) VALUES
(in_h_normalized_payto
,my_outcome_serial_id
,in_justification
,in_decider_pub
,in_decider_sig
);
-- wake up taler-exchange-aggregator
INSERT INTO kyc_alerts
(h_payto
,trigger_type)
VALUES
(in_h_normalized_payto,1)
ON CONFLICT DO NOTHING;
EXECUTE FORMAT (
'NOTIFY %s'
,in_notify_s);
END $$;
COMMENT ON FUNCTION exchange_do_insert_aml_decision(TEXT, BYTEA, BYTEA, INT8, INT8, TEXT, TEXT, BOOLEAN, TEXT, TEXT, TEXT, BYTEA, BYTEA, TEXT)
IS 'Checks whether the AML officer is eligible to make AML decisions and if so inserts the decision into the table';
|