-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry.fc
223 lines (174 loc) · 6.67 KB
/
registry.fc
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
213
214
215
216
217
218
219
220
221
222
223
;; Verifier registry smart contract
#pragma version >=0.2.0;
#include "imports/stdlib.fc";
global cell storage::verifiers;
global int storage::verifiers_num;
const int const::max_verifiers = 20;
const int op::update_verifier = 0x6002d61a;
const int op::remove_verifier = 0x19fa5637;
const int op::forward_message = 0x75217758;
const slice EXIT_TEXT = "Withdrawal and exit from the verifier registry";
const slice REGISTERED_TEXT = "You were successfully registered as a verifier";
const slice UPDATED_TEXT = "You successfully updated verifier data";
const int EXIT_FEE = 200000000; ;; 0.20 TON
const int STAKE = 10000000000000; ;; 10000 TON
() save_data() impure {
set_data(begin_cell()
.store_dict(storage::verifiers)
.store_uint(storage::verifiers_num,8)
.end_cell());
}
() load_data() impure {
slice data = get_data().begin_parse();
storage::verifiers = data~load_dict();
storage::verifiers_num = data~load_uint(8);
}
() send_msg(int mode, slice to, int amount, slice comment) impure {
var msg = begin_cell()
.store_uint(0x10, 6)
.store_slice(to)
.store_coins(amount)
.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_ref(begin_cell()
.store_uint(0, 32)
.store_slice(comment)
.end_cell());
send_raw_message(msg.end_cell(), mode);
}
() remove_verifier(int id, slice sender_address) impure inline {
(slice cfg, int ok) = storage::verifiers.udict_get?(256, id);
throw_if(404, ok == 0);
slice admin = cfg~load_msg_addr();
;; allow only to admin
throw_unless(401, equal_slices(sender_address, admin));
;; delete from verifiers list
(cell verifiers, _) = storage::verifiers.udict_delete?(256, id);
storage::verifiers = verifiers;
storage::verifiers_num -= 1;
;; withdraw stake + return excess from message
send_msg(64, admin, STAKE - EXIT_FEE, EXIT_TEXT);
save_data();
}
() update_verifier(int id, slice new_settings, slice sender_address, int balance, int coins_sent) impure inline {
slice admin = sender_address;
(slice cfg, int ok) = storage::verifiers.udict_get?(256, id);
if (ok) { ;; exists, check is admin
admin = cfg~load_msg_addr();
;; allow only to admin
throw_unless(401, equal_slices(sender_address, admin));
;; return excess, ignore errors
send_msg(64 + 2, admin, 0, UPDATED_TEXT);
} else {
throw_if(410, coins_sent < STAKE);
throw_if(419, storage::verifiers_num >= const::max_verifiers);
;; return excess
send_msg(1, admin, coins_sent - STAKE, REGISTERED_TEXT);
storage::verifiers_num += 1;
}
slice new_cfg = begin_cell()
.store_slice(admin)
.store_slice(new_settings)
.end_cell().begin_parse();
storage::verifiers = storage::verifiers.udict_set(256, id, new_cfg);
save_data();
}
() forward_message(cell msg, cell signatures, slice sender_address) impure inline {
slice msg_data = msg.begin_parse();
int verifier_id = msg_data~load_uint(256);
int valid_till = msg_data~load_uint(32);
slice source_addr = msg_data~load_msg_addr();
throw_unless(414, equal_slices(sender_address, source_addr));
(slice verifier, int ok) = storage::verifiers.udict_get?(256, verifier_id);
throw_if(404, ok == 0);
verifier~load_msg_addr();
int quorum = verifier~load_uint(8);
cell pub_key_endpoints = verifier.preload_dict();
;; check msg ttl
throw_if(411, valid_till < now());
int valid_signatures = 0;
int msg_hash = msg.cell_hash();
int done = 0;
while (done == 0) {
slice s = signatures.begin_parse();
slice sign = s~load_bits(512);
int key = s~load_uint(256);
;; check key in dict and remove to not allow reuse in current context for more signatures
(cell updated_list, _, int known) = pub_key_endpoints.udict_delete_get?(256, key);
if (known & check_signature(msg_hash, sign, key)) {
valid_signatures += 1;
pub_key_endpoints = updated_list;
}
if ((valid_signatures >= quorum) | (s.slice_refs() == 0)) {
done = 1;
} else {
signatures = s.preload_ref();
}
}
throw_if(413, valid_signatures < quorum);
slice target_addr = msg_data~load_msg_addr();
cell payload_to_forward = msg_data~load_ref();
send_raw_message( begin_cell()
.store_uint(0x10, 6)
.store_slice(target_addr)
.store_coins(0)
.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_ref(payload_to_forward).end_cell(), 64);
save_data();
}
() recv_internal(int balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
if (in_msg_body.slice_empty?()) { ;; ignore empty messages
return ();
}
slice cs = in_msg_full.begin_parse();
int flags = cs~load_uint(4);
if (flags & 1) { ;; ignore all bounced messages
return ();
}
slice sender_address = cs~load_msg_addr();
int op = in_msg_body~load_uint(32);
int query_id = in_msg_body~load_uint(64);
load_data();
if (op == op::forward_message) {
cell msg = in_msg_body~load_ref();
cell signatures = in_msg_body~load_ref();
forward_message(msg, signatures, sender_address);
return ();
}
if (op == op::update_verifier) {
int id = in_msg_body~load_uint(256);
slice new_settings = in_msg_body;
;; validation
in_msg_body~load_uint(8); ;; quorum
in_msg_body~load_dict(); ;; pub_key_endpoints
slice name_ref = in_msg_body~load_ref().begin_parse(); ;; name
throw_unless(420, string_hash(name_ref) == id);
in_msg_body~load_ref(); ;; url ref
in_msg_body.end_parse();
update_verifier(id, new_settings, sender_address, balance, msg_value);
return ();
}
if (op == op::remove_verifier) {
int id = in_msg_body~load_uint(256);
remove_verifier(id, sender_address);
return ();
}
throw(0xffff);
}
(slice, cell, int) get_verifier(int id) method_id {
load_data();
(slice cfg, int ok) = storage::verifiers.udict_get?(256, id);
if (ok == 0) {
return (begin_cell().end_cell().begin_parse(), begin_cell().end_cell(), 0);
}
slice admin = cfg~load_msg_addr();
cell settings = begin_cell().store_slice(cfg).end_cell();
return (admin, settings, -1);
}
int get_verifiers_num() method_id {
load_data();
return storage::verifiers_num;
}
(slice) get_verifiers() method_id {
load_data();
return begin_cell().store_dict(storage::verifiers).end_cell().begin_parse();
}