-
Notifications
You must be signed in to change notification settings - Fork 3
/
SimpleGAMultiSig.aes
259 lines (226 loc) · 11.1 KB
/
SimpleGAMultiSig.aes
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
// ISC License
//
// Copyright (c) 2022, aeternity developers
//
// Credit to Michel Meier & Marco Walz (kryptokrauts.com)
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// THIS IS NOT SECURITY AUDITED
// DO NEVER USE THIS WITHOUT SECURITY AUDIT FIRST
@compiler >= 6
include "List.aes"
include "Option.aes"
include "Set.aes"
include "String.aes"
// This contract provides multi signature based on generalized accounts.
// A transaction can be proposed by one of the signers which are registered during contract deployment.
// The transaction must be confirmed by a certain amount of signers. This is also defined during contract deployment.
// Proposing a transaction automatically counts as confirmation.
// When proposing a tx, the signer can define a TTL until the proposed tx needs to be confirmed and authorized.
// If the amount of confirmations is reached, the special function "authorize" can be called by anybody.
// Only one transaction can be handled at the same time.
// Revocation of proposed tx is also possible but requires enough refusals in case it's not triggered by the proposer of the tx.
contract SimpleGAMultiSig =
datatype event
= TxProposed(hash, address, int)
| TxConfirmed(hash, address)
| TxRefused(hash, address)
| TxConsensusReached(hash)
| TxConsensusLost(hash)
| TxRevoked(hash, address)
| TxAuthorized(hash)
| FeeProtectionDisabled(address)
record transaction =
{ tx_hash : hash,
expires_at : int,
proposed_by : address,
confirmed_by : Set.set(address),
refused_by : Set.set(address) }
record consensus_info =
{ tx_hash : option(hash),
confirmations_required : int,
confirmed_by : list(address),
refused_by : list(address),
has_consensus : bool,
expiration_height : int,
expired : bool,
proposed_by : option(address) }
record fee_protection =
{ max_fee : int,
max_gasprice : int }
record state =
{ signers : Set.set(address),
confirmations_required : int,
current_tx : option(transaction),
// fee protection currently needs to be set on contract level until it's enforced by the procotol itself
fee_protection : option(fee_protection),
// if fee protection is enforced by protocol after a future hardfork this can be disabled on contract level
fee_protection_enabled : bool,
nonce : int,
// for applications to know about the correct interface in case this changes in future versions (e.g. fee protection is likely to be removed completely)
version : string }
stateful entrypoint init(confirmations_required : int, signers : Set.set(address)) =
require(!Set.member(Call.caller, signers), "ERROR_ACCOUNT_OF_GA_MUST_NOT_BE_SIGNER")
require(Set.size(signers) >= 2, "ERROR_MIN_2_SIGNERS")
require(Set.size(signers) >= confirmations_required, "ERROR_CONFIRMATIONS_EXCEED_AMOUNT_OF_SIGNERS")
{ signers = signers,
confirmations_required = confirmations_required,
current_tx = None,
// reasonable default values (consulted with Hans)
fee_protection = Some({ max_fee = 2_000_000_000_000_000, max_gasprice = 10_000_000_000 }),
fee_protection_enabled = true,
nonce = 1,
version = "2.0.0" }
// GA authorize function
stateful entrypoint authorize(nonce : int) : bool =
let tx = Option.force_msg(state.current_tx, "ERROR_NO_TX_PROPOSED")
require(nonce >= state.nonce, "ERROR_NONCE_TOO_LOW")
require(nonce =< state.nonce, "ERROR_NONCE_TOO_HIGH")
require(!expired(tx), "ERROR_TX_EXPIRED")
if(state.fee_protection_enabled)
verify_tx_fee()
switch(Auth.tx_hash)
None => abort("ERROR_NO_AUTH_CONTEXT")
Some(tx_hash) =>
require(has_consensus(tx), "ERROR_NO_CONSENSUS")
require(tx.tx_hash == tx_hash, "ERROR_UNEQUAL_HASHES")
Chain.event(TxAuthorized(tx_hash))
put(state{ nonce = nonce + 1, current_tx = None })
true
// propose a new tx valid for the given ttl
stateful entrypoint propose(tx_hash : hash, ttl : Chain.ttl) =
require(Set.member(Call.caller, state.signers), "ERROR_NOT_AUTHORIZED")
switch(state.current_tx)
None => set_current_tx(tx_hash, ttl)
Some(tx) =>
require(expired(tx), "ERROR_EXISTING_PROPOSED_TX_NOT_EXPIRED")
set_current_tx(tx_hash, ttl)
// signer confirms the current tx
stateful entrypoint confirm(tx_hash : hash) =
let tx = Option.force_msg(state.current_tx, "ERROR_NOTHING_TO_CONFIRM")
require_valid_signer_and_tx_hash(Call.caller, tx_hash, tx)
require(!Set.member(Call.caller, tx.confirmed_by), "ERROR_ALREADY_CONFIRMED")
require(!expired(tx), "ERROR_TX_EXPIRED")
switch(Set.member(Call.caller, tx.refused_by))
true =>
let updated_tx = tx{ refused_by = Set.delete(Call.caller, tx.refused_by),
confirmed_by = Set.insert(Call.caller, tx.confirmed_by) }
confirm_and_update_tx(updated_tx, tx)
false =>
let updated_tx = tx{ confirmed_by = Set.insert(Call.caller, tx.confirmed_by) }
confirm_and_update_tx(updated_tx, tx)
// refuse the current tx
stateful entrypoint refuse(tx_hash: hash) =
let tx = Option.force_msg(state.current_tx, "ERROR_NOTHING_TO_REFUSE")
require_valid_signer_and_tx_hash(Call.caller, tx_hash, tx)
require(!Set.member(Call.caller, tx.refused_by), "ERROR_ALREADY_REFUSED")
switch(Set.member(Call.caller, tx.confirmed_by))
true =>
let updated_tx = tx{ confirmed_by = Set.delete(Call.caller, tx.confirmed_by),
refused_by = Set.insert(Call.caller, tx.refused_by) }
if(state.confirmations_required - 1 == Set.size(updated_tx.confirmed_by))
Chain.event(TxConsensusLost(tx.tx_hash))
refuse_and_revoke(updated_tx)
false =>
let updated_tx = tx{ refused_by = Set.insert(Call.caller, tx.refused_by) }
refuse_and_revoke(updated_tx)
// revoke the current tx and clean state
stateful entrypoint revoke(tx_hash : hash) =
let tx = Option.force_msg(state.current_tx, "ERROR_NOTHING_TO_REVOKE")
require_valid_signer_and_tx_hash(Call.caller, tx_hash, tx)
require(Call.caller == tx.proposed_by, "ERROR_CALLER_NOT_PROPOSER")
internal_revoke(tx.tx_hash)
stateful entrypoint update_fee_protection(fee_protection : fee_protection) =
require(state.fee_protection_enabled, "ERROR_FEE_PROTECTION_ALREADY_DISABLED")
require(Set.member(Call.caller, state.signers), "ERROR_NOT_A_VALID_SIGNER")
put(state{ fee_protection = Some(fee_protection) })
stateful entrypoint disable_fee_protection() =
require(state.fee_protection_enabled, "ERROR_FEE_PROTECTION_ALREADY_DISABLED")
require(Set.member(Call.caller, state.signers), "ERROR_NOT_A_VALID_SIGNER")
put(state{ fee_protection = None, fee_protection_enabled = false })
Chain.event(FeeProtectionDisabled(Call.caller))
// return the contract version
entrypoint get_version() : string =
state.version
// return if fee protection is enabled
entrypoint is_fee_protection_enabled() : bool =
state.fee_protection_enabled
// return the fee protection values
entrypoint get_fee_protection() : option(fee_protection) =
state.fee_protection
// return the current nonce
entrypoint get_nonce() : int =
state.nonce
// return all signers
entrypoint get_signers() : list(address) =
Set.to_list(state.signers)
// return the consensus info
entrypoint get_consensus_info() : consensus_info =
switch(state.current_tx)
None => { tx_hash = None,
confirmations_required = state.confirmations_required,
confirmed_by = [],
refused_by = [],
has_consensus = false,
expiration_height = 0,
expired = false,
proposed_by = None }
Some(tx) => { tx_hash = Some(tx.tx_hash),
confirmations_required = state.confirmations_required,
confirmed_by = Set.to_list(tx.confirmed_by),
refused_by = Set.to_list(tx.refused_by),
has_consensus = has_consensus(tx),
expiration_height = tx.expires_at,
expired = expired(tx),
proposed_by = Some(tx.proposed_by) }
stateful function refuse_and_revoke(tx: transaction) =
put(state{ current_tx = Some(tx) })
Chain.event(TxRefused(tx.tx_hash, Call.caller))
if(state.confirmations_required == Set.size(tx.refused_by))
internal_revoke(tx.tx_hash)
stateful function confirm_and_update_tx(updated_tx, tx: transaction) =
put(state{ current_tx = Some(updated_tx) })
Chain.event(TxConfirmed(tx.tx_hash, Call.caller))
if(state.confirmations_required == Set.size(updated_tx.confirmed_by))
Chain.event(TxConsensusReached(tx.tx_hash))
stateful function internal_revoke(tx_hash: hash) =
put(state{ current_tx = None })
Chain.event(TxRevoked(tx_hash, Call.caller))
// verify tx fee (fee protection which is required right now)
function verify_tx_fee() =
let fee_protection = Option.force(state.fee_protection)
require(Call.fee =< fee_protection.max_fee, "ERROR_MAX_FEE_EXCEEDED")
require(Call.gas_price =< fee_protection.max_gasprice, "ERROR_MAX_GASPRICE_EXCEEDED")
// validate if enough signers have confirmed
function has_consensus(tx: transaction) : bool =
Set.size(tx.confirmed_by) >= state.confirmations_required
// require that given address is within the list of signers
function require_valid_signer_and_tx_hash(addr : address, tx_hash_to_verify : hash, current_tx : transaction) =
require(Set.member(addr, state.signers) && current_tx.tx_hash == tx_hash_to_verify, "ERROR_NOT_AUTHORIZED")
// check if current_tx is expired
function expired(tx: transaction) : bool =
Chain.block_height > tx.expires_at
// set current tx to state and emit new proposed event
stateful function set_current_tx(tx_hash : hash, ttl : Chain.ttl) =
let new_tx = { tx_hash = tx_hash,
expires_at = expiration_height(ttl),
proposed_by = Call.caller,
confirmed_by = Set.insert(Call.caller, Set.new()),
refused_by = Set.new() }
put(state{ current_tx = Some(new_tx) })
Chain.event(TxProposed(new_tx.tx_hash, new_tx.proposed_by, new_tx.expires_at))
// compute the expiration height depending on the ttl type
function expiration_height(ttl : Chain.ttl) : int =
switch(ttl)
FixedTTL(height) => height
RelativeTTL(relative_ttl) => Chain.block_height + relative_ttl