forked from tonkeeper/w5
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wallet_v5.fc
298 lines (243 loc) · 12.4 KB
/
wallet_v5.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
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
#pragma version =0.4.4;
#include "imports/stdlib.fc";
const int error::signature_disabled = 132;
const int error::invalid_seqno = 133;
const int error::invalid_wallet_id = 134;
const int error::invalid_signature = 135;
const int error::expired = 136;
const int error::external_send_message_must_have_ignore_errors_send_mode = 137;
const int error::invalid_message_operation = 138;
const int error::add_extension = 139;
const int error::remove_extension = 140;
const int error::unsupported_action = 141;
const int error::disable_signature_when_extensions_is_empty = 142;
const int error::this_signature_mode_already_set = 143;
const int error::remove_last_extension_when_signature_disabled = 144;
const int error::extension_wrong_workchain = 145;
const int error::only_extension_can_change_signature_mode = 146;
const int error::invalid_c5 = 147;
const int size::bool = 1;
const int size::seqno = 32;
const int size::wallet_id = 32;
const int size::public_key = 256;
const int size::valid_until = 32;
const int size::message_flags = 4;
const int size::signature = 512;
const int size::message_operation_prefix = 32;
const int size::address_hash_size = 256;
const int size::query_id = 64;
const int prefix::signed_external = 0x7369676E;
const int prefix::signed_internal = 0x73696E74;
const int prefix::extension_action = 0x6578746E;
(slice, int) check_and_remove_add_extension_prefix(slice body) impure asm "x{02} SDBEGINSQ";
(slice, int) check_and_remove_remove_extension_prefix(slice body) impure asm "x{03} SDBEGINSQ";
(slice, int) check_and_remove_set_signature_allowed_prefix(slice body) impure asm "x{04} SDBEGINSQ";
;;; returns the number of trailing zeroes in slice s.
int count_trailing_zeroes(slice s) asm "SDCNTTRAIL0";
;;; returns the last 0 ≤ l ≤ 1023 bits of s.
slice get_last_bits(slice s, int l) asm "SDCUTLAST";
;;; returns all but the last 0 ≤ l ≤ 1023 bits of s.
slice remove_last_bits(slice s, int l) asm "SDSKIPLAST";
;; `action_send_msg` has 0x0ec3c86d prefix
;; https://github.com/ton-blockchain/ton/blob/5c392e0f2d946877bb79a09ed35068f7b0bd333a/crypto/block/block.tlb#L380
slice enforce_and_remove_action_send_msg_prefix(slice body) impure asm "x{0ec3c86d} SDBEGINS";
;;; put raw list of OutActions to C5 register.
;;; OutList TLB-schema - https://github.com/ton-blockchain/ton/blob/5c392e0f2d946877bb79a09ed35068f7b0bd333a/crypto/block/block.tlb#L378
;;; C5 register - https://docs.ton.org/tvm.pdf, page 11
() set_c5_actions(cell action_list) impure asm "c5 POP";
;;; transforms an ordinary or exotic cell into a Slice, as if it were an ordinary cell. A flag is returned indicating whether c is exotic. If that be the case, its type can later be deserialized from the first eight bits of s.
(slice, int) begin_parse_raw(cell c) asm "XCTOS";
cell verify_c5_actions(cell c5, int is_external) inline {
;; XCTOS doesn't automatically load exotic cells (unlike CTOS `begin_parse`).
;; we use it in `verify_c5_actions` because during action phase processing exotic cells in c5 won't be unfolded too.
;; exotic cell starts with 0x02, 0x03 or 0x04 so it will not pass action_send_msg prefix check
(slice cs, _) = c5.begin_parse_raw();
int count = 0;
while (~ cs.slice_empty?()) {
;; only `action_send_msg` is allowed; `action_set_code`, `action_reserve_currency` or `action_change_library` are not.
cs = cs.enforce_and_remove_action_send_msg_prefix();
throw_unless(error::invalid_c5, cs.slice_bits() == 8); ;; send_mode
throw_unless(error::invalid_c5, cs.slice_refs() == 2); ;; next-action-ref and MessageRelaxed ref
;; enforce that send_mode has +2 bit (ignore errors) set for external message.
;; if such send_mode is not set and sending fails at the action phase (for example due to insufficient balance) then the seqno will not be increased and the external message will be processed again and again.
;; action_send_msg#0ec3c86d mode:(## 8) out_msg:^(MessageRelaxed Any) = OutAction;
;; https://github.com/ton-blockchain/ton/blob/5c392e0f2d946877bb79a09ed35068f7b0bd333a/crypto/block/block.tlb#L380
;; load 7 bits and make sure that they end with 1
throw_if(error::external_send_message_must_have_ignore_errors_send_mode, is_external & (count_trailing_zeroes(cs.preload_bits(7)) > 0));
(cs, _) = cs.preload_ref().begin_parse_raw();
count += 1;
}
throw_unless(error::invalid_c5, count <= 255);
throw_unless(error::invalid_c5, cs.slice_refs() == 0);
return c5;
}
() process_actions(slice cs, int is_external, int is_extension) impure inline_ref {
cell c5_actions = cs~load_maybe_ref();
ifnot (cell_null?(c5_actions)) {
;; Simply set the C5 register with all pre-computed actions after verification:
set_c5_actions(c5_actions.verify_c5_actions(is_external));
}
if (cs~load_int(1) == 0) { ;; has_other_actions
return ();
}
;; Loop extended actions until we reach standard actions
while (true) {
int is_add_extension = cs~check_and_remove_add_extension_prefix();
int is_remove_extension = is_add_extension ? 0 : cs~check_and_remove_remove_extension_prefix();
;; Add/remove extensions
if (is_add_extension | is_remove_extension) {
(int address_wc, int address_hash) = parse_std_addr(cs~load_msg_addr());
(int my_address_wc, _) = parse_std_addr(my_address());
throw_unless(error::extension_wrong_workchain, my_address_wc == address_wc);
slice data_slice = get_data().begin_parse();
slice data_slice_before_extensions = data_slice~load_bits(size::bool + size::seqno + size::wallet_id + size::public_key);
cell extensions = data_slice.preload_dict();
;; Add extension
if (is_add_extension) {
(extensions, int is_success) = extensions.udict_add_builder?(size::address_hash_size, address_hash, begin_cell().store_int(-1, 1));
throw_unless( error::add_extension, is_success);
} else { ;; Remove extension
(extensions, int is_success) = extensions.udict_delete?(size::address_hash_size, address_hash);
throw_unless(error::remove_extension, is_success);
int is_signature_allowed = data_slice_before_extensions.preload_int(size::bool);
throw_if(error::remove_last_extension_when_signature_disabled, null?(extensions) & (~ is_signature_allowed));
}
set_data(begin_cell()
.store_slice(data_slice_before_extensions)
.store_dict(extensions)
.end_cell());
} elseif (cs~check_and_remove_set_signature_allowed_prefix()) {
throw_unless(error::only_extension_can_change_signature_mode, is_extension);
int allow_signature = cs~load_int(1);
slice data_slice = get_data().begin_parse();
int is_signature_allowed = data_slice~load_int(size::bool);
throw_if(error::this_signature_mode_already_set, is_signature_allowed == allow_signature);
is_signature_allowed = allow_signature;
slice data_tail = data_slice; ;; seqno, wallet_id, public_key, extensions
ifnot (allow_signature) { ;; disallow
int is_extensions_not_empty = data_slice.skip_bits(size::seqno + size::wallet_id + size::public_key).preload_int(1);
throw_unless(error::disable_signature_when_extensions_is_empty, is_extensions_not_empty);
}
set_data(begin_cell()
.store_int(is_signature_allowed, size::bool)
.store_slice(data_tail) ;; seqno, wallet_id, public_key, extensions
.end_cell());
} else {
throw(error::unsupported_action);
}
ifnot (cs.slice_refs()) {
return ();
}
cs = cs.preload_ref().begin_parse();
}
}
;; ------------------------------------------------------------------------------------------------
() process_signed_request(slice in_msg_body, int is_external) impure inline {
slice signature = in_msg_body.get_last_bits(size::signature);
slice signed_slice = in_msg_body.remove_last_bits(size::signature);
slice cs = signed_slice.skip_bits(size::message_operation_prefix); ;; skip signed_internal or signed_external prefix
(int wallet_id, int valid_until, int seqno) = (cs~load_uint(size::wallet_id), cs~load_uint(size::valid_until), cs~load_uint(size::seqno));
slice data_slice = get_data().begin_parse();
int is_signature_allowed = data_slice~load_int(size::bool);
int stored_seqno = data_slice~load_uint(size::seqno);
slice data_tail = data_slice; ;; wallet_id, public_key, extensions
int stored_wallet_id = data_slice~load_uint(size::wallet_id);
int public_key = data_slice~load_uint(size::public_key);
int is_extensions_not_empty = data_slice.preload_int(1);
int is_signature_valid = check_signature(slice_hash(signed_slice), signature, public_key);
ifnot (is_signature_valid) {
if (is_external) {
throw(error::invalid_signature);
} else {
return ();
}
}
throw_if(error::signature_disabled, (~ is_signature_allowed) & is_extensions_not_empty);
throw_unless(error::invalid_seqno, seqno == stored_seqno);
throw_unless(error::invalid_wallet_id, wallet_id == stored_wallet_id);
throw_if(error::expired, valid_until <= now());
if (is_external) {
accept_message();
}
;; Store and commit the seqno increment to prevent replays even if the subsequent requests fail.
stored_seqno = stored_seqno + 1;
set_data(begin_cell()
.store_int(true, size::bool) ;; is_signature_allowed
.store_uint(stored_seqno, size::seqno)
.store_slice(data_tail) ;; wallet_id, public_key, extensions
.end_cell());
if (is_external) {
commit();
}
process_actions(cs, is_external, false);
}
() recv_external(slice in_msg_body) impure inline {
throw_unless(error::invalid_message_operation, in_msg_body.preload_uint(size::message_operation_prefix) == prefix::signed_external);
process_signed_request(in_msg_body, true);
}
;; ------------------------------------------------------------------------------------------------
() recv_internal(cell in_msg_full, slice in_msg_body) impure inline {
if (in_msg_body.slice_bits() < size::message_operation_prefix) {
return ();
}
int op = in_msg_body.preload_uint(size::message_operation_prefix);
if ((op != prefix::extension_action) & (op != prefix::signed_internal)) {
return ();
}
;; bounded messages has 0xffffff prefix and skipped by op check
if (op == prefix::extension_action) {
in_msg_body~skip_bits(size::message_operation_prefix);
slice in_msg_full_slice = in_msg_full.begin_parse();
in_msg_full_slice~skip_bits(size::message_flags);
;; Authenticate extension by its address.
(int sender_address_wc, int sender_address_hash) = parse_std_addr(in_msg_full_slice~load_msg_addr());
(int my_address_wc, _) = parse_std_addr(my_address());
if (my_address_wc != sender_address_wc) {
return ();
}
cell extensions = get_data().begin_parse()
.skip_bits(size::bool + size::seqno + size::wallet_id + size::public_key)
.preload_dict();
;; Note that some random contract may have deposited funds with this prefix,
;; so we accept the funds silently instead of throwing an error (wallet v4 does the same).
(_, int extension_found) = extensions.udict_get?(size::address_hash_size, sender_address_hash);
ifnot (extension_found) {
return ();
}
in_msg_body~skip_bits(size::query_id); ;; skip query_id
process_actions(in_msg_body, false, true);
return ();
}
;; Additional check to make sure that there are enough bits for reading before signature check
if (in_msg_body.slice_bits() < size::message_operation_prefix + size::wallet_id + size::valid_until + size::seqno + size::signature) {
return ();
}
process_signed_request(in_msg_body, false);
}
;; ------------------------------------------------------------------------------------------------
;; Get methods
int is_signature_allowed() method_id {
return get_data().begin_parse()
.preload_int(size::bool);
}
int seqno() method_id {
return get_data().begin_parse()
.skip_bits(size::bool)
.preload_uint(size::seqno);
}
int get_subwallet_id() method_id {
return get_data().begin_parse()
.skip_bits(size::bool + size::seqno)
.preload_uint(size::wallet_id);
}
int get_public_key() method_id {
return get_data().begin_parse()
.skip_bits(size::bool + size::seqno + size::wallet_id)
.preload_uint(size::public_key);
}
;; Returns raw dictionary (or null if empty) where keys are address hashes. Workchains of extensions are same with wallet smart contract workchain
cell get_extensions() method_id {
return get_data().begin_parse()
.skip_bits(size::bool + size::seqno + size::wallet_id + size::public_key)
.preload_dict();
}