-
Notifications
You must be signed in to change notification settings - Fork 10
/
deadMansSwitch.py
302 lines (235 loc) · 9.72 KB
/
deadMansSwitch.py
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
import smartpy as sp
class DeadMansSwitch(sp.Contract):
"""A dead man's switch contract. A multisig contract will take control of
the funds in case the contract admin is dead or lost access to their keys.
The admin needs to ping the contract regularly to indicate that they are
still alive.
"""
def __init__(self, metadata, administrator, multisig, ping_interval):
"""Initializes the contract.
"""
# Define the contract storage data types for clarity
self.init_type(sp.TRecord(
# The contract metadata
metadata=sp.TBigMap(sp.TString, sp.TBytes),
# The contract administrator
administrator=sp.TAddress,
# The multisig contract address that will become the contract
# administrator when the current one stops pinging the contract
multisig=sp.TAddress,
# The ping interval in days
ping_interval=sp.TNat,
# The last ping timestamp
last_ping=sp.TTimestamp,
# The proposed new administrator address
proposed_administrator=sp.TOption(sp.TAddress)))
# Initialize the contract storage
self.init(
metadata=metadata,
administrator=administrator,
multisig=multisig,
ping_interval=ping_interval,
last_ping=sp.now,
proposed_administrator=sp.none)
def check_is_administrator(self):
"""Checks that the address that called the entry point is the contract
administrator.
"""
sp.verify(sp.sender == self.data.administrator, message="DM_NOT_ADMIN")
def check_is_multisig(self):
"""Checks that the address that called the entry point is the multisig
contract.
"""
sp.verify(sp.sender == self.data.multisig, message="DM_NOT_MULTISIG")
def fa2_transfer(self, fa2, from_, to_, token_id, amount):
"""Transfers a number of editions of a FA2 token to another address.
"""
# Get a handle to the FA2 token transfer entry point
c = sp.contract(
t=sp.TList(sp.TRecord(
from_=sp.TAddress,
txs=sp.TList(sp.TRecord(
to_=sp.TAddress,
token_id=sp.TNat,
amount=sp.TNat).layout(("to_", ("token_id", "amount")))))),
address=fa2,
entry_point="transfer").open_some()
# Transfer the FA2 token editions to the new address
sp.transfer(
arg=sp.list([sp.record(
from_=from_,
txs=sp.list([sp.record(
to_=to_,
token_id=token_id,
amount=amount)]))]),
amount=sp.mutez(0),
destination=c)
def fa12_transfer(self, fa12, from_, to_, value):
"""Transfers a number of editions of a FA1.2 token to another address.
"""
# Get a handle to the FA1.2 token transfer entry point
c = sp.contract(
t=sp.TRecord(
from_=sp.TAddress,
to_=sp.TAddress,
value=sp.TNat).layout(("from_ as from", ("to_ as to", "value"))),
address=fa12,
entry_point="transfer").open_some()
# Transfer the FA1.2 token editions to the new address
sp.transfer(
arg=sp.record(
from_=from_,
to_=to_,
value=value),
amount=sp.mutez(0),
destination=c)
@sp.entry_point
def default(self, unit):
"""Default entrypoint that allows receiving tez transfers in the same
way as one would do with a normal tz wallet.
"""
# Define the input parameter data type
sp.set_type(unit, sp.TUnit)
# Do nothing, just receive tez
pass
@sp.entry_point
def transfer_tez(self, params):
"""Transfers the given tez amounts to the provided destination
addresses.
"""
# Define the input parameter data type
sp.set_type(params, sp.TList(sp.TRecord(
amount=sp.TMutez,
destination=sp.TAddress)))
# Check that the administrator executed the entry point
self.check_is_administrator()
# Transfer the tez
with sp.for_("mutez_transfer", params) as mutez_transfer:
sp.send(mutez_transfer.destination, mutez_transfer.amount)
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
@sp.entry_point
def transfer_tokens(self, params):
"""Transfers a FA2 or FA1.2 token to a given address.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
token_address=sp.TAddress,
token_id=sp.TOption(sp.TNat),
amount=sp.TNat,
destination=sp.TAddress))
# Check that the administrator executed the entry point
self.check_is_administrator()
# Check if we have a FA2 or a FA1.2 token
with sp.if_(params.token_id.is_some()):
self.fa2_transfer(
fa2=params.token_address,
from_=sp.self_address,
to_=params.destination,
token_id=params.token_id.open_some(),
amount=params.amount)
with sp.else_():
self.fa12_transfer(
fa12=params.token_address,
from_=sp.self_address,
to_=params.destination,
value=params.amount)
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
@sp.entry_point
def ping(self, unit):
"""Pings the contract.
"""
# Define the input parameter data type
sp.set_type(unit, sp.TUnit)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
@sp.entry_point
def take_control(self, unit):
"""The multisig tries to take control of the contract.
"""
# Define the input parameter data type
sp.set_type(unit, sp.TUnit)
# Check that the multisig executed the entry point
self.check_is_multisig()
# Check that the last ping is older than the ping interval time
ping_deadline = self.data.last_ping.add_days(
sp.to_int(self.data.ping_interval))
sp.verify(sp.now > ping_deadline, message="DM_NOT_DEAD")
# Set the multisig as the new contract administrator
self.data.administrator = sp.sender
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
@sp.entry_point
def transfer_administrator(self, proposed_administrator):
"""Proposes to transfer the contract administrator to another address.
"""
# Define the input parameter data type
sp.set_type(proposed_administrator, sp.TAddress)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Set the new proposed administrator address
self.data.proposed_administrator = sp.some(proposed_administrator)
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
@sp.entry_point
def accept_administrator(self):
"""The proposed administrator accepts the contract administrator
responsibilities.
"""
# Check that the proposed administrator executed the entry point
sp.verify(sp.sender == self.data.proposed_administrator.open_some(
message="DM_NO_NEW_ADMIN"),
message="DM_NOT_PROPOSED_ADMIN")
# Set the new administrator address
self.data.administrator = sp.sender
# Reset the proposed administrator value
self.data.proposed_administrator = sp.none
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
@sp.entry_point
def set_multisig(self, multisig):
"""Sets the multisig contract address.
"""
# Define the input parameter data type
sp.set_type(multisig, sp.TAddress)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Set the new multisig contract address
self.data.multisig = multisig
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
@sp.entry_point
def set_ping_interval(self, ping_interval):
"""Sets the ping interval in days.
"""
# Define the input parameter data type
sp.set_type(ping_interval, sp.TNat)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Check that the ping interval is larger than 1 day
sp.verify(ping_interval > 0, message="DM_INVALID_PING_INTERVAL")
# Set the new ping interval
self.data.ping_interval = ping_interval
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
@sp.entry_point
def set_delegate(self, new_baker):
"""Delegates the contract stored tez to the given baker address.
"""
# Define the input parameter data type
sp.set_type(new_baker, sp.TOption(sp.TKeyHash))
# Check that the administrator executed the entry point
self.check_is_administrator()
# Update the baker address
sp.set_delegate(new_baker)
# Update the last ping timestamp parameter
self.data.last_ping = sp.now
# Add a compilation target
sp.add_compilation_target("deadMansSwitch", DeadMansSwitch(
metadata=sp.utils.metadata_of_url("ipfs://aaaa"),
administrator=sp.address("tz111"),
multisig=sp.address("KT111"),
ping_interval=90))