-
Notifications
You must be signed in to change notification settings - Fork 65
/
ArgentAccount.cairo
593 lines (506 loc) · 15.3 KB
/
ArgentAccount.cairo
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
%lang starknet
%builtins pedersen range_check ecdsa
from starkware.cairo.common.hash import hash2
from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin
from starkware.cairo.common.signature import verify_ecdsa_signature
from starkware.cairo.common.registers import get_fp_and_pc
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.math import assert_not_zero, assert_le, assert_nn
from starkware.starknet.common.syscalls import call_contract, get_tx_signature
####################
# CONSTANTS
####################
const CHANGE_SIGNER_SELECTOR = 1540130945889430637313403138889853410180247761946478946165786566748520529557
const CHANGE_GUARDIAN_SELECTOR = 1374386526556551464817815908276843861478960435557596145330240747921847320237
const CHANGE_L1_ADDRESS_SELECTOR = 279169963369459328778917024654659648799474594494056791695540097993562699432
const TRIGGER_ESCAPE_SELECTOR = 654787765132774538659281525944449989569480594447680779882263455595827967108
const CANCEL_ESCAPE_SELECTOR = 992575500541331354489361836180456905167517944319528538469723604173440834912
const ESCAPE_GUARDIAN_SELECTOR = 1662889347576632967292303062205906116436469425870979472602094601074614456040
const ESCAPE_SIGNER_SELECTOR = 578307412324655990419134484880427622068887477430675222732446709420063579565
const IS_VALID_SIGNATURE_SELECTOR = 1138073982574099226972715907883430523600275391887289231447128254784345409857
const ESCAPE_SECURITY_PERIOD = 500 # set to e.g. 7 days in prod
####################
# STRUCTS
####################
struct Escape:
member active_at: felt
member caller: felt
end
####################
# STORAGE VARIABLES
####################
@storage_var
func _current_nonce() -> (res: felt):
end
@storage_var
func _signer() -> (res: felt):
end
@storage_var
func _guardian() -> (res: felt):
end
@storage_var
func _escape() -> (res: Escape):
end
@storage_var
func _L1_address() -> (res: felt):
end
####################
# EXTERNAL FUNCTIONS
####################
@constructor
func constructor{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
} (
signer: felt,
guardian: felt,
L1_address: felt
):
# check that the signer is not zero
assert_not_zero(signer)
# initialize the contract
_signer.write(signer)
_guardian.write(guardian)
_L1_address.write(L1_address)
return ()
end
@external
func execute{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
to: felt,
selector: felt,
calldata_len: felt,
calldata: felt*,
nonce: felt
) -> (response : felt):
alloc_locals
# get the signatures
let (local sig_len : felt, local sig : felt*) = get_tx_signature()
# validate and bump nonce
validate_and_bump_nonce(nonce)
# validate signatures
let (local message_hash) = get_message_hash(to, selector, calldata_len, calldata, nonce)
validate_signer_signature(message_hash, sig, sig_len)
validate_guardian_signature(message_hash, sig + 2, sig_len - 2)
# execute call
let response = call_contract(
contract_address=to,
function_selector=selector,
calldata_size=calldata_len,
calldata=calldata
)
return (response=response.retdata_size)
end
@external
func change_signer{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
new_signer: felt,
nonce: felt
):
alloc_locals
# get the signatures
let (local sig_len : felt, local sig : felt*) = get_tx_signature()
# validate and bump nonce
validate_and_bump_nonce(nonce)
# validate signatures
let (to) = _self_address.read()
let calldata: felt* = alloc()
assert calldata[0] = new_signer
let (local message_hash) = get_message_hash(to, CHANGE_SIGNER_SELECTOR, 1, calldata, nonce)
validate_signer_signature(message_hash, sig, sig_len)
validate_guardian_signature(message_hash, sig + 2, sig_len - 2)
# change signer
assert_not_zero(new_signer)
_signer.write(new_signer)
return()
end
@external
func change_guardian{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
new_guardian: felt,
nonce: felt
):
alloc_locals
# get the signatures
let (local sig_len : felt, local sig : felt*) = get_tx_signature()
# validate and bump nonce
validate_and_bump_nonce(nonce)
# validate signatures
let (to) = _self_address.read()
let calldata: felt* = alloc()
assert calldata[0] = new_guardian
let (local message_hash) = get_message_hash(to, CHANGE_GUARDIAN_SELECTOR, 1, calldata, nonce)
validate_signer_signature(message_hash, sig, sig_len)
validate_guardian_signature(message_hash, sig + 2, sig_len - 2)
# change guardian
assert_not_zero(new_guardian)
_guardian.write(new_guardian)
return()
end
@external
func change_L1_address{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
new_L1_address: felt,
nonce: felt
):
alloc_locals
# get the signatures
let (local sig_len : felt, local sig : felt*) = get_tx_signature()
# validate and bump nonce
validate_and_bump_nonce(nonce)
# validate signatures
let (to) = _self_address.read()
let calldata: felt* = alloc()
assert calldata[0] = new_L1_address
let (local message_hash) = get_message_hash(to, CHANGE_L1_ADDRESS_SELECTOR, 1, calldata, nonce)
validate_signer_signature(message_hash, sig, sig_len)
validate_guardian_signature(message_hash, sig + 2, sig_len - 2)
# change guardian
_L1_address.write(new_L1_address)
return()
end
@external
func trigger_escape{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
escapor: felt,
nonce: felt
):
alloc_locals
# get the signatures
let (local sig_len : felt, local sig : felt*) = get_tx_signature()
# validate and bump nonce
validate_and_bump_nonce(nonce)
# no escape when the guardian is not set
let (local guardian) = _guardian.read()
assert_not_zero(guardian)
# check if there is already an escape
let (local current_escape) = _escape.read()
let (local signer) = _signer.read()
if current_escape.active_at != 0:
assert current_escape.caller = guardian
assert escapor = signer
end
# validate signature
let (to) = _self_address.read()
let calldata: felt* = alloc()
assert calldata[0] = escapor
let (local message_hash) = get_message_hash(to, TRIGGER_ESCAPE_SELECTOR, 1, calldata, nonce)
if escapor == signer:
validate_signer_signature(message_hash, sig, sig_len)
else:
assert escapor = guardian
validate_guardian_signature(message_hash, sig, sig_len)
end
# rebinding ptrs
local ecdsa_ptr: SignatureBuiltin* = ecdsa_ptr
# store new escape
let (block_timestamp) = _block_timestamp.read()
local new_escape: Escape = Escape(block_timestamp + ESCAPE_SECURITY_PERIOD, escapor)
_escape.write(new_escape)
return()
end
@external
func cancel_escape{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
nonce: felt
):
alloc_locals
# get the signatures
let (local sig_len : felt, local sig : felt*) = get_tx_signature()
# validate and bump nonce
validate_and_bump_nonce(nonce)
# validate there is an active escape
let (local current_escape) = _escape.read()
assert_not_zero(current_escape.active_at)
# validate signatures
let (to) = _self_address.read()
let calldata: felt* = alloc()
let (local message_hash) = get_message_hash(to, CANCEL_ESCAPE_SELECTOR, 0, calldata, nonce)
validate_signer_signature(message_hash, sig, sig_len)
validate_guardian_signature(message_hash, sig + 2, sig_len - 2)
# clear escape
local new_escape: Escape = Escape(0, 0)
_escape.write(new_escape)
return()
end
@external
func escape_guardian{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
new_guardian: felt,
nonce: felt
):
alloc_locals
# get the signatures
let (local sig_len : felt, local sig : felt*) = get_tx_signature()
# validate and bump nonce
validate_and_bump_nonce(nonce)
# validate there is an active escape
let (local block_timestamp) = _block_timestamp.read()
let (local current_escape) = _escape.read()
assert_le(current_escape.active_at, block_timestamp)
# validate signer signatures
let (to) = _self_address.read()
let calldata: felt* = alloc()
assert calldata[0] = new_guardian
let (local message_hash) = get_message_hash(to, ESCAPE_GUARDIAN_SELECTOR, 1, calldata, nonce)
validate_signer_signature(message_hash, sig, sig_len)
# clear escape
local new_escape: Escape = Escape(0, 0)
_escape.write(new_escape)
# change guardian
assert_not_zero(new_guardian)
_guardian.write(new_guardian)
return()
end
@external
func escape_signer{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
new_signer: felt,
nonce: felt
):
alloc_locals
# get the signatures
let (local sig_len : felt, local sig : felt*) = get_tx_signature()
# validate and bump nonce
validate_and_bump_nonce(nonce)
# validate there is an active escape
let (local block_timestamp) = _block_timestamp.read()
let (local current_escape) = _escape.read()
assert_le(current_escape.active_at, block_timestamp)
# validate signer signatures
let (to) = _self_address.read()
let calldata: felt* = alloc()
assert calldata[0] = new_signer
let (local message_hash) = get_message_hash(to, ESCAPE_SIGNER_SELECTOR, 1, calldata, nonce)
validate_guardian_signature(message_hash, sig, sig_len)
# clear escape
local new_escape: Escape = Escape(0, 0)
_escape.write(new_escape)
# change signer
assert_not_zero(new_signer)
_signer.write(new_signer)
return()
end
####################
# VIEW FUNCTIONS
####################
@view
func is_valid_signature{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
hash: felt,
sig_len: felt,
sig: felt*
) -> (magic_value: felt):
alloc_locals
validate_signer_signature(hash, sig, sig_len)
validate_guardian_signature(hash, sig + 2, sig_len - 2)
return (magic_value = IS_VALID_SIGNATURE_SELECTOR)
end
@view
func get_current_nonce{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (nonce: felt):
let (res) = _current_nonce.read()
return (nonce=res)
end
@view
func get_signer{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (signer: felt):
let (res) = _signer.read()
return (signer=res)
end
@view
func get_guardian{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (guardian: felt):
let (res) = _guardian.read()
return (guardian=res)
end
@view
func get_escape{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (active_at: felt, caller: felt):
let (res) = _escape.read()
return (active_at=res.active_at, caller=res.caller)
end
@view
func get_L1_address{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (L1_address: felt):
let (res) = _L1_address.read()
return (L1_address=res)
end
####################
# INTERNAL FUNCTIONS
####################
func validate_and_bump_nonce{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
} (
message_nonce: felt
) -> ():
let (current_nonce) = _current_nonce.read()
assert current_nonce = message_nonce
_current_nonce.write(current_nonce + 1)
return()
end
func validate_signer_signature{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
message: felt,
signatures: felt*,
signatures_len: felt
) -> ():
assert_nn(signatures_len - 2)
let (signer) = _signer.read()
verify_ecdsa_signature(
message=message,
public_key=signer,
signature_r=signatures[0],
signature_s=signatures[1])
return()
end
func validate_guardian_signature{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr : SignatureBuiltin*,
range_check_ptr
} (
message: felt,
signatures: felt*,
signatures_len: felt
) -> ():
let (guardian) = _guardian.read()
if guardian == 0:
return()
else:
assert_nn(signatures_len - 2)
verify_ecdsa_signature(
message=message,
public_key=guardian,
signature_r=signatures[0],
signature_s=signatures[1])
return()
end
end
func get_message_hash{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
ecdsa_ptr: SignatureBuiltin*,
range_check_ptr
} (
to: felt,
selector: felt,
calldata_len: felt,
calldata: felt*,
nonce: felt
) -> (res: felt):
alloc_locals
let (account) = _self_address.read()
let (res) = hash2{hash_ptr=pedersen_ptr}(account, to)
let (res) = hash2{hash_ptr=pedersen_ptr}(res, selector)
# we need to make `res` local
# to prevent the reference from being revoked
local syscall_ptr: felt* = syscall_ptr
local range_check_ptr = range_check_ptr
local res = res
let (res_calldata) = hash_calldata(calldata, calldata_len)
let (res) = hash2{hash_ptr=pedersen_ptr}(res, res_calldata)
let (res) = hash2{hash_ptr=pedersen_ptr}(res, nonce)
return (res=res)
end
func hash_calldata{pedersen_ptr: HashBuiltin*}(
calldata: felt*,
calldata_size: felt
) -> (res: felt):
if calldata_size == 0:
return (res=0)
end
if calldata_size == 1:
return (res=[calldata])
end
let _calldata = [calldata]
let (res) = hash_calldata(calldata + 1, calldata_size - 1)
let (res) = hash2{hash_ptr=pedersen_ptr}(res, _calldata)
return (res=res)
end
####################
# TMP HACK
####################
@storage_var
func _self_address() -> (res: felt):
end
@external
func set_self_address{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr}(new_address: felt):
_self_address.write(new_address)
return ()
end
@storage_var
func _block_timestamp() -> (res: felt):
end
@view
func get_block_timestamp{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (block_timestamp: felt):
let (res) = _block_timestamp.read()
return (block_timestamp=res)
end
@external
func set_block_timestamp{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr}(new_block_timestamp: felt):
_block_timestamp.write(new_block_timestamp)
return ()
end