-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmultisig.fc
More file actions
313 lines (262 loc) · 8.81 KB
/
multisig.fc
File metadata and controls
313 lines (262 loc) · 8.81 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
;; Multisig Draft
;; MUST BE WORKED OUT!
#pragma version >=0.4.0;
#include "imports/stdlib.fc";
#include "utils/constants.fc";
#include "utils/flags.fc";
#include "utils/op-codes.fc";
#include "utils/params.fc";
_ unpack_state() inline_ref {
var ds = begin_parse(get_data());
var res = (ds~load_uint(8), ds~load_uint(8), ds~load_uint(8), ds~load_uint(64), ds~load_dict(), ds~load_dict());
ds.end_parse();
return res;
}
_ pack_state(cell pending_queries, cell public_keys, int last_cleaned, int k, int n, int min_k) inline_ref {
return begin_cell()
.store_uint(n, 8)
.store_uint(k, 8)
.store_uint(min_k, 8)
.store_uint(last_cleaned, 64)
.store_dict(public_keys)
.store_dict(pending_queries)
.end_cell();
}
(int, int) check_signatures(cell public_keys, cell signatures, int hash, int cnt_bits, int k_required) inline_ref {
int cnt = 0;
int accepted = 0;
do {
slice cs = signatures.begin_parse();
slice signature = cs~load_bits(512);
int i = cs~load_uint(8);
signatures = cs~load_dict();
(slice public_key, var found?) = public_keys.udict_get?(8, i);
throw_unless(37, found?);
dump_stack();
throw_unless(38, check_signature(hash, signature, public_key.preload_uint(256)));
int mask = (1 << i);
int old_cnt_bits = cnt_bits;
cnt_bits |= mask;
int should_check = cnt_bits != old_cnt_bits;
cnt -= should_check;
;; ACCEPT MESSAGE IF NUMBER OF SIGNATURES IS ENOUGH TO ACCEPT MESSAGE
if( (k_required != 0) & (cnt == k_required) & (accepted == 0)){
accept_message();
accepted = -1;
}
} until (cell_null?(signatures));
return (cnt, cnt_bits);
}
() recv_internal(slice in_msg) impure {
;; do nothing for internal messages
}
(int, int, slice) unpack_query_data(slice in_msg, int n, slice query, var found?) inline_ref {
if (found?) {
throw_unless(35, query~load_int(1));
(int cnt, int cnt_bits, slice msg) = (query~load_uint(8), query~load_uint(n), query);
throw_unless(36, slice_hash(msg) == slice_hash(in_msg));
return (cnt, cnt_bits, msg);
}
return (0, 0, in_msg);
}
() try_init() impure inline_ref {
;; first query without signatures is always accepted
(int n, int k, int min_k, int last_cleaned, cell public_keys, cell pending_queries) = unpack_state();
throw_if(37, last_cleaned);
accept_message();
set_data(pack_state(pending_queries, public_keys, 1, k, n, min_k));
}
cell update_pending_queries(cell pending_queries, slice msg, int query_id, int cnt, int cnt_bits, int n, int k) impure inline_ref {
if (cnt >= k) {
while (msg.slice_refs()) {
var mode = msg~load_uint(8);
send_raw_message(msg~load_ref(), mode);
}
pending_queries~udict_set_builder(64, query_id, begin_cell().store_int(0, 1));
} else {
pending_queries~udict_set_builder(64, query_id, begin_cell()
.store_uint(1, 1)
.store_uint(cnt, 8)
.store_uint(cnt_bits, n)
.store_slice(msg));
}
return pending_queries;
}
() recv_external(slice in_msg) impure {
;; empty message triggers init
if (slice_empty?(in_msg)) {
return try_init();
}
;; Check root signature
slice root_signature = in_msg~load_bits(512);
int root_hash = slice_hash(in_msg);
int root_i = in_msg~load_uint(8);
(int n, int k, int min_k, int last_cleaned, cell public_keys, cell pending_queries) = unpack_state();
last_cleaned -= last_cleaned == 0;
(slice public_key, var found?) = public_keys.udict_get?(8, root_i);
throw_unless(31, found?);
throw_unless(32, check_signature(root_hash, root_signature, public_key.preload_uint(256)));
cell signatures = in_msg~load_dict();
var hash = slice_hash(in_msg);
int query_id = in_msg~load_uint(64);
var bound = (now() << 32);
throw_if(33, query_id < bound);
(slice query, var found?) = pending_queries.udict_get?(64, query_id);
(int cnt, int cnt_bits, slice msg) = unpack_query_data(in_msg, n, query, found?);
int mask = 1 << root_i;
throw_if(34, cnt_bits & mask);
cnt_bits |= mask;
cnt += 1;
;; DO NOT ACCEPT MESSAGE WITH ROOT SIGNATURE ONLY
;; TODO: reserve some gas or FAIL
;; accept_message();
;; pending_queries = update_pending_queries(pending_queries, msg, query_id, cnt, cnt_bits, n, k);
;; set_data(pack_state(pending_queries, public_keys, last_cleaned, k, n));
;; commit();
int need_save = 0;
;; DO NOT ACCEPT NEW REQUEST WITH ONE SIGNATURE
throw_if( 40, (found? == 0) & cell_null?(signatures) );
throw_if( 41, cnt >= k );
var k_required = min_k - 1;
if( found? ){
pending_queries = update_pending_queries(pending_queries, msg, query_id, cnt, cnt_bits, n, k);
set_data(pack_state(pending_queries, public_keys, last_cleaned, k, n, min_k));
accept_message();
commit();
k_required = 0;
}
ifnot (cell_null?(signatures) | (cnt >= k)) {
(int new_cnt, cnt_bits) = check_signatures(public_keys, signatures, hash, cnt_bits, k_required);
;; DO NOT ACCEPT NEW REQUEST UNTIL MIN_K OF SIGNATURES ARE CONFIRMED
throw_unless(42, new_cnt >= k_required);
cnt += new_cnt;
;; COMMIT IF NEW REQUEST PASSED THE TEST
if( found? == 0){
pending_queries = update_pending_queries(pending_queries, msg, query_id, cnt, cnt_bits, n, k);
set_data(pack_state(pending_queries, public_keys, last_cleaned, k, n, min_k));
commit();
}
need_save = -1;
}
bound -= (64 << 32); ;; clean up records expired more than 64 seconds ago
int old_last_cleaned = last_cleaned;
do {
var (pending_queries', i, _, f) = pending_queries.udict_delete_get_min(64);
f~touch();
if (f) {
f = (i < bound);
}
if (f) {
pending_queries = pending_queries';
last_cleaned = i;
need_save = -1;
}
} until (~ f);
if (need_save) {
set_data(pack_state(pending_queries, public_keys, last_cleaned, k, n, min_k));
}
}
;; Get methods
;; returns -1 for processed queries, 0 for unprocessed, 1 for unknown (forgotten)
(int, int) get_query_state(int query_id) method_id {
(int n, _, _, int last_cleaned, _, cell pending_queries) = unpack_state();
(slice cs, var found) = pending_queries.udict_get?(64, query_id);
if (found) {
if (cs~load_int(1)) {
cs~load_uint(8);
return (0, cs~load_uint(n));
} else {
return (-1, 0);
}
} else {
return (-(query_id <= last_cleaned), 0);
}
}
int processed?(int query_id) method_id {
(int x, _) = get_query_state(query_id);
return x;
}
cell create_init_state(int n, int k, int min_k, cell public_keys) method_id {
return pack_state(new_dict(), public_keys, 0, k, n, min_k);
}
cell merge_list(cell a, cell b) {
if (cell_null?(a)) {
return b;
}
if (cell_null?(b)) {
return a;
}
slice as = a.begin_parse();
if (as.slice_refs() != 0) {
cell tail = merge_list(as~load_ref(), b);
return begin_cell().store_slice(as).store_ref(tail).end_cell();
}
as~skip_last_bits(1);
return begin_cell().store_slice(as).store_dict(b).end_cell();
}
cell get_public_keys() method_id {
(_, _, _, _, cell public_keys, _) = unpack_state();
return public_keys;
}
(int, int) check_query_signatures(cell query) method_id {
slice cs = query.begin_parse();
slice root_signature = cs~load_bits(512);
int root_hash = slice_hash(cs);
int root_i = cs~load_uint(8);
cs~dump();
cell public_keys = get_public_keys();
(slice public_key, var found?) = public_keys.udict_get?(8, root_i);
throw_unless(31, found?);
dump_stack();
throw_unless(32, check_signature(root_hash, root_signature, public_key.preload_uint(256)));
int mask = 1 << root_i;
cell signatures = cs~load_dict();
if (cell_null?(signatures)) {
return (1, mask);
}
dump_stack();
(int cnt, mask) = check_signatures(public_keys, signatures, slice_hash(cs), mask, 0);
return (cnt + 1, mask);
}
cell messages_by_mask(int mask) method_id {
(int n, _, _,_ , _, cell pending_queries) = unpack_state();
int i = -1;
cell a = new_dict();
do {
(i, var cs, var f) = pending_queries.udict_get_next?(64, i);
if (f) {
if (cs~load_int(1)) {
int cnt_bits = cs.skip_bits(8).preload_uint(n);
if (cnt_bits & mask) {
a~udict_set_builder(64, i, begin_cell().store_slice(cs));
}
}
}
} until (~ f);
return a;
}
cell get_messages_unsigned_by_id(int id) method_id {
return messages_by_mask(1 << id);
}
cell get_messages_unsigned() method_id {
return messages_by_mask(~ 0);
}
(int, int) get_n_k() method_id {
(int n, int k, _, _, _, _) = unpack_state();
return (n, k);
}
int get_min_k() method_id {
(_, _, int min_k, _, _, _) = unpack_state();
return min_k;
}
cell merge_inner_queries(cell a, cell b) method_id {
slice ca = a.begin_parse();
slice cb = b.begin_parse();
cell list_a = ca~load_dict();
cell list_b = cb~load_dict();
throw_unless(31, slice_hash(ca) == slice_hash(cb));
return begin_cell()
.store_dict(merge_list(list_a, list_b))
.store_slice(ca)
.end_cell();
}