forked from starknet-edu/starknet-accounts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultisig.cairo
297 lines (245 loc) · 7.26 KB
/
multisig.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
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin
from starkware.starknet.common.syscalls import call_contract, get_caller_address, get_tx_info
from starkware.cairo.common.math import assert_not_zero, assert_le
from starkware.cairo.common.signature import verify_ecdsa_signature
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.bool import TRUE, FALSE
####################
# STRUCTS
####################
struct Transaction:
member contract_address : felt
member function_selector : felt
member calldata_len : felt
end
struct TestSignature:
member hash : felt
member pub : felt
member sig_r : felt
member sig_s : felt
end
####################
# INTERFACES
####################
@contract_interface
namespace IAccount:
func get_signer() -> (res : felt):
end
func is_valid_signature(hash : felt, signature_len : felt, signature : felt*):
end
end
####################
# STORAGE VARIABLES
####################
@storage_var
func owners(index : felt) -> (owner : felt):
end
@storage_var
func owners_map(owner : felt) -> (index : felt):
end
@storage_var
func num_owners() -> (res : felt):
end
@storage_var
func curr_tx_index() -> (res : felt):
end
@storage_var
func tx_confirms(tx_index : felt) -> (num_confirms : felt):
end
@storage_var
func tx_is_executed(tx_index : felt) -> (is_executed : felt):
end
@storage_var
func has_confirmed(tx_index : felt, owner : felt) -> (confirmed : felt):
end
@storage_var
func transactions(tx_index : felt) -> (tx : Transaction):
end
@storage_var
func transaction_calldata(tx_index : felt, calldata_index : felt) -> (value : felt):
end
@storage_var
func test_signature() -> (value : TestSignature):
end
####################
# EVENTS
####################
@event
func submit(owner : felt, tx_index : felt):
end
@event
func confirm(owner : felt, tx_index : felt):
end
@event
func execute(tx_index : felt):
end
####################
# CONSTRUCTOR
####################
@constructor
func constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
owners_len : felt, owners : felt*
):
assert_le(3, owners_len)
num_owners.write(owners_len)
_set_owners(owners_len=owners_len, new_owners=owners)
return ()
end
####################
# GETTERS
####################
@view
func get_confirmations{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
tx_index : felt
) -> (res : felt):
let (res) = tx_confirms.read(tx_index)
return (res)
end
@view
func get_owner_confirmed{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
tx_index : felt, owner : felt
) -> (res : felt):
let (res) = has_confirmed.read(tx_index, owner)
return (res)
end
@view
func get_num_owners{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
res : felt
):
let (res) = num_owners.read()
return (res)
end
@view
func get_test_sig{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
value : TestSignature
):
let (value) = test_signature.read()
return (value)
end
@view
func get_owners{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (
signers_len : felt, signers : felt*
):
alloc_locals
let (signers_len) = num_owners.read()
let (signers) = alloc()
_get_signers(0, signers_len, signers)
return (signers_len=signers_len, signers=signers)
end
####################
# INTERNAL FUNCTIONS
####################
func _require_owner{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
let (caller) = get_caller_address()
let (index) = owners_map.read(owner=caller)
with_attr error_message("not owner"):
assert_not_zero(index)
end
return ()
end
func _set_owners{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
owners_len : felt, new_owners : felt*
):
if owners_len == 0:
return ()
end
owners.write(index=owners_len, value=[new_owners])
owners_map.write(owner=[new_owners], value=owners_len)
_set_owners(owners_len=owners_len - 1, new_owners=new_owners + 1)
return ()
end
func _spread_calldata{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
tx_index : felt, calldata_index : felt, calldata_len : felt, calldata : felt*
):
if calldata_index == calldata_len:
return ()
end
transaction_calldata.write(tx_index, calldata_index, calldata[calldata_index])
_spread_calldata(tx_index, calldata_index + 1, calldata_len, calldata)
return ()
end
func _pack_calldata{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
tx_index : felt, calldata_index : felt, calldata_len : felt, calldata : felt*
):
if calldata_index == calldata_len:
return ()
end
let (calldata_val) = transaction_calldata.read(tx_index, calldata_index)
assert calldata[calldata_index] = calldata_val
_pack_calldata(tx_index, calldata_index + 1, calldata_len, calldata)
return ()
end
func _get_signers{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
signer_idx : felt, signers_len : felt, signers : felt*
):
if signer_idx == signers_len:
return ()
end
let (owner) = owners.read(signer_idx + 1)
assert_not_zero(owner)
assert signers[signer_idx] = owner
_get_signers(signer_idx + 1, signers_len, signers)
return ()
end
####################
# EXTERNAL FUNCTIONS
####################
#
# ACTION ITEM 1: implement the ability for a whitelisted signer to submit a tx to the multisig
#
@external
func submit_tx{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr, ecdsa_ptr : SignatureBuiltin*
}(contract_address : felt, function_selector : felt, calldata_len : felt, calldata : felt*):
alloc_locals
_require_owner()
#
# <CODE>
#
submit.emit(caller, tx_index)
return ()
end
#
# ACTION ITEM 2: implement the ability for a multisig member to confirm a submitted transaction
#
@external
func confirm_tx{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr, ecdsa_ptr : SignatureBuiltin*
}(tx_index : felt):
alloc_locals
_require_owner()
#
# <CODE>
#
confirm.emit(caller, tx_index)
return ()
end
@external
func __execute__{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
tx_index : felt
) -> (retdata_len : felt, retdata : felt*):
alloc_locals
_require_owner()
let (tx : Transaction) = transactions.read(tx_index)
assert_not_zero(tx.contract_address)
let (num_confirmations) = tx_confirms.read(tx_index)
let (executed) = tx_is_executed.read(tx_index)
assert executed = FALSE
let (owners_len) = num_owners.read()
with_attr error_message("need more confirmations"):
assert_le(owners_len - 1, num_confirmations)
end
tx_is_executed.write(tx_index, TRUE)
let (calldata) = alloc()
_pack_calldata(tx_index, 0, tx.calldata_len, calldata)
assert calldata[tx.calldata_len] = tx_index
execute.emit(tx_index)
let (retdata_len : felt, retdata : felt*) = call_contract(
contract_address=tx.contract_address,
function_selector=tx.function_selector,
calldata_size=tx.calldata_len + 1,
calldata=calldata,
)
return (retdata_len=retdata_len, retdata=retdata)
end