-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmultisig_wallet.scilla
937 lines (866 loc) · 36.4 KB
/
multisig_wallet.scilla
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
scilla_version 0
import ListUtils BoolUtils
(***************************************************)
(* Associated library *)
(***************************************************)
library WalletLib
(* Event for communicating a new transaction id *)
let mk_transaction_added_event =
fun (tc : Uint32) =>
{ _eventname : "Transaction created" ;
transactionId : tc }
(* Event for communicating that a transaction was signed *)
let mk_signed_transaction_event =
fun (tc : Uint32) =>
{ _eventname : "Transaction signed"; transactionId : tc }
(* Event for communicating that a signature was revoked *)
let mk_signature_revoked_event =
fun (tc : Uint32) =>
{ _eventname : "Signature revoked"; transactionId : tc }
type Error =
| NonOwnerCannotSign
| UnknownTransactionId
| InsufficientFunds
| NoSignatureListFound
| AlreadySigned
| NotAlreadySigned
| InvalidContract
| InvalidAmount
| NotEnoughSignatures
| SenderMayNotExecute
| NonOwnerCannotSubmit
| IncorrectSignatureCount
(* Error events *)
let mk_error_event =
fun (err : Error) =>
let err_code =
match err with
| NonOwnerCannotSign => Int32 -1
| UnknownTransactionId => Int32 -2
| InsufficientFunds => Int32 -3
| NoSignatureListFound => Int32 -4
| AlreadySigned => Int32 -5
| NotAlreadySigned => Int32 -6
| InvalidContract => Int32 -7
| InvalidAmount => Int32 -8
| NotEnoughSignatures => Int32 -9
| SenderMayNotExecute => Int32 -10
| NonOwnerCannotSubmit => Int32 -11
| IncorrectSignatureCount => Int32 -12
end in
{ _eventname : "WalletError" ; err_code : err_code }
let t = True
let f = False
let zero = Uint32 0
let zeroUint128 = Uint128 0
let one = Uint32 1
let transaction_inc = one
type SSNCycleInfo =
| SSNCycleInfo of Uint128 Uint128
type SsnRewardShare =
| SsnRewardShare of ByStr20 Uint128
(* Type of Proxy transactions. *)
(* All calls are made to the proxy contract *)
type CalleeTransaction =
(***************************************************)
(* Proxy Transition *)
(***************************************************)
(* UpgradeTo(newImplementation: ByStr20) *)
| UpgradeTo of ByStr20
(* ChangeProxyAdmin(newAdmin: ByStr20) *)
| ChangeProxyAdmin of ByStr20
(* ClaimProxyAdmin() *)
| ClaimProxyAdmin
(***************************************************)
(* gZIL Transitions *)
(***************************************************)
(* ChangeMinter(new_minter: ByStr20) *)
| ChangeMinter of ByStr20
(***************************************************)
(* SSNlist Transitions *)
(***************************************************)
(***************************************************)
(* House keeping transition *)
(***************************************************)
(* Pause() *)
| Pause
(* UnPause() *)
| UnPause
(* UpdateAdmin(new_admin: ByStr20) *)
| UpdateAdmin of ByStr20
(* ClaimAdmin() *)
| ClaimAdmin
(* UpdateVerifier(verif: ByStr20) *)
| UpdateVerifier of ByStr20
(* UpdateVerifierRewardAddr(addr: ByStr20) *)
| UpdateVerifierRewardAddr of ByStr20
(* UpdateStakingParameters(min_stake: Uint128, min_deleg_stake: Uint128, max_comm_change_rate: Uint128) *)
| UpdateStakingParameters of Uint128 Uint128 Uint128
(* ChangeBNumReq(input_bnum_req: Uint128) *)
| ChangeBNumReq of Uint128
(* UpdateGzilAddr(gzil_addr: ByStr20) *)
| UpdateGzilAddr of ByStr20
(* AddSSN(ssnaddr: ByStr20, name: String, urlraw: String, urlapi: String, comm: Uint128) *)
| AddSSN of ByStr20 String String String Uint128
(* UpdateSSN(ssnaddr: ByStr20, new_name: String, new_urlraw: String, new_urlapi: String) *)
| UpdateSSN of ByStr20 String String String
(***************************************************)
(* Generic transition *)
(***************************************************)
(* AddSSNAfterUpgrade(ssnaddr: ByStr20, stake_amt: Uint128, rewards: Uint128, name: String, urlraw: String, urlapi: String, buff_deposit: Uint128, comm: Uint128, comm_rewards: Uint128, rec_addr: ByStr20) *)
| AddSSNAfterUpgrade of ByStr20 Uint128 Uint128 String String String Uint128 Uint128 Uint128 ByStr20
(***************************************************)
(* Contract upgrade transition *)
(***************************************************)
(* UpdateDeleg(ssnaddr: ByStr20, deleg: ByStr20, stake_amt: Uint128) *)
| UpdateDeleg of ByStr20 ByStr20 Uint128
(* PopulateStakeSSNPerCycle(ssn_addr: ByStr20, cycle: Uint32, totalAmt: Uint128, rewards: Uint128) *)
| PopulateStakeSSNPerCycle of ByStr20 Uint32 Uint128 Uint128
(* PopulateLastWithdrawCycleForDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32) *)
| PopulateLastWithdrawCycleForDeleg of ByStr20 ByStr20 Uint32
(* PopulateLastBufDepositCycleDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32) *)
| PopulateLastBufDepositCycleDeleg of ByStr20 ByStr20 Uint32
(* PopulateBuffDeposit(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128) *)
| PopulateBuffDeposit of ByStr20 ByStr20 Uint32 Uint128
(* PopulateDirectDeposit(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128) *)
| PopulateDirectDeposit of ByStr20 ByStr20 Uint32 Uint128
(* PopulateDepositAmtDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, amt: Uint128) *)
| PopulateDepositAmtDeleg of ByStr20 ByStr20 Uint128
(* PopulateDelegStakePerCycle(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128) *)
| PopulateDelegStakePerCycle of ByStr20 ByStr20 Uint32 Uint128
(* PopulateLastRewardCycle(cycle: Uint32) *)
| PopulateLastRewardCycle of Uint32
(* PopulateCommForSSN(ssn_addr: ByStr20, cycle: Uint32, comm: Uint128) *)
| PopulateCommForSSN of ByStr20 Uint32 Uint128
(* PopulateTotalStakeAmt(amt: Uint128) *)
| PopulateTotalStakeAmt of Uint128
(* PopulatePendingWithdrawal(ssn_addr: ByStr20, block_number: BNum, stake: Uint128) *)
| PopulatePendingWithdrawal of ByStr20 BNum Uint128
(* DrainContractBalance(amt: Uint128) *)
| DrainContractBalance of Uint128
(* Type of (outstanding) transactions *)
type Transaction =
(* Transfer of native tokens *)
| NativeTransaction of ByStr20 Uint128 String
(* Custom token transactions *)
| CustomTransaction of ByStr20 CalleeTransaction
(* Make map of owners *)
let mk_owners_map =
fun (owners : List ByStr20) =>
let init = Emp ByStr20 Bool in
let iter =
fun (acc : Map ByStr20 Bool) =>
fun (cur_owner : ByStr20) =>
(* Add owner unconditionally. We check for duplicates later *)
builtin put acc cur_owner t
in
let folder = @list_foldl ByStr20 (Map ByStr20 Bool) in
folder iter init owners
(* Wrap single message into singleton list *)
let one_msg =
fun (msg : Message) =>
let nil_msg = Nil {Message} in
Cons {Message} msg nil_msg
(* Create native transaction message as singleton list *)
let native_transaction_msg_as_list =
fun (recipient : ByStr20) =>
fun (amount : Uint128) =>
fun (tag : String) =>
let msg = {_tag : tag; _recipient : recipient; _amount : amount } in
one_msg msg
(* Create custom transaction message as singleton list *)
let custom_transaction_msg_as_list =
fun (calleeContract : ByStr20) =>
fun (calleeTransaction : CalleeTransaction) =>
let msg =
match calleeTransaction with
(* UpgradeTo(newImplementation: ByStr20) *)
| UpgradeTo newImplementation =>
{_recipient: calleeContract ;
_tag: "UpgradeTo";
_amount: Uint128 0;
newImplementation : newImplementation}
(* ChangeProxyAdmin(newAdmin: ByStr20) *)
| ChangeProxyAdmin newAdmin =>
{_recipient: calleeContract;
_tag: "ChangeProxyAdmin";
_amount: Uint128 0;
newAdmin: newAdmin}
(* ClaimProxyAdmin() *)
| ClaimProxyAdmin =>
{_recipient: calleeContract;
_tag: "ClaimProxyAdmin";
_amount: Uint128 0}
(* ChangeMinter(new_minter: ByStr20) *)
| ChangeMinter new_minter =>
{_recipient: calleeContract;
_tag: "ChangeMinter";
_amount: Uint128 0;
new_minter: new_minter}
(* Pause() *)
| Pause =>
{_recipient: calleeContract;
_tag: "Pause";
_amount: Uint128 0}
(* UnPause() *)
| UnPause =>
{_recipient: calleeContract ;
_tag: "UnPause";
_amount: Uint128 0}
(* UpdateAdmin(new_admin: ByStr20) *)
| UpdateAdmin new_admin =>
{_recipient: calleeContract;
_tag: "UpdateAdmin";
_amount: Uint128 0;
new_admin: new_admin}
(* ClaimAdmin() *)
| ClaimAdmin =>
{_recipient: calleeContract;
_tag: "ClaimAdmin";
_amount: Uint128 0}
(* UpdateVerifier(verif: ByStr20) *)
| UpdateVerifier verif =>
{_recipient: calleeContract;
_tag: "UpdateVerifier";
_amount: Uint128 0;
verif: verif}
(* UpdateVerifierRewardAddr(addr: ByStr20) *)
| UpdateVerifierRewardAddr addr =>
{_recipient : calleeContract;
_tag: "UpdateVerifierRewardAddr";
_amount: Uint128 0;
addr: addr}
(* UpdateStakingParameters(min_stake: Uint128, min_deleg_stake: Uint128, max_comm_change_rate: Uint128) *)
| UpdateStakingParameters min_stake min_deleg_stake max_comm_change_rate =>
{_recipient : calleeContract;
_tag: "UpdateStakingParameters";
_amount: Uint128 0;
min_stake: min_stake;
min_deleg_stake: min_deleg_stake;
max_comm_change_rate: max_comm_change_rate}
(* ChangeBNumReq(input_bnum_req: Uint128) *)
| ChangeBNumReq input_bnum_req =>
{_recipient : calleeContract;
_tag: "ChangeBNumReq";
_amount: Uint128 0;
input_bnum_req: input_bnum_req}
(* UpdateGzilAddr(gzil_addr: ByStr20) *)
| UpdateGzilAddr gzil_addr =>
{_recipient : calleeContract;
_tag: "UpdateGzilAddr";
_amount: Uint128 0;
gzil_addr: gzil_addr}
(* AddSSN(ssnaddr: ByStr20, name: String, urlraw: String, urlapi: String, comm: Uint128) *)
| AddSSN ssnaddr name urlraw urlapi comm =>
{_recipient : calleeContract;
_tag: "AddSSN";
_amount: Uint128 0;
ssnaddr: ssnaddr;
name: name;
urlraw: urlraw;
urlapi: urlapi;
comm: comm}
(* UpdateSSN(ssnaddr: ByStr20, new_name: String, new_urlraw: String, new_urlapi: String) *)
| UpdateSSN ssnaddr new_name new_urlraw new_urlapi =>
{_recipient : calleeContract;
_tag: "UpdateSSN";
_amount: Uint128 0;
ssnaddr: ssnaddr;
new_name: new_name;
new_urlraw: new_urlraw;
new_urlapi: new_urlapi}
(* AddSSNAfterUpgrade(ssnaddr: ByStr20, stake_amt: Uint128, rewards: Uint128, name: String, urlraw: String, urlapi: String, buff_deposit: Uint128, comm: Uint128, comm_rewards: Uint128, rec_addr: ByStr20) *)
| AddSSNAfterUpgrade ssnaddr stake_amt rewards name urlraw urlapi buff_deposit comm comm_rewards rec_addr =>
{_recipient : calleeContract;
_tag: "AddSSNAfterUpgrade";
_amount: Uint128 0;
ssnaddr: ssnaddr;
stake_amt: stake_amt;
rewards: rewards;
name: name;
urlraw: urlraw;
urlapi: urlapi;
buff_deposit: buff_deposit;
comm: comm;
comm_rewards: comm_rewards;
rec_addr: rec_addr}
(* UpdateDeleg(ssnaddr: ByStr20, deleg: ByStr20, stake_amt: Uint128) *)
| UpdateDeleg ssnaddr deleg stake_amt =>
{_recipient : calleeContract;
_tag: "UpdateDeleg";
_amount: Uint128 0;
ssnaddr: ssnaddr;
deleg: deleg;
stake_amt: stake_amt}
(* PopulateStakeSSNPerCycle(ssn_addr: ByStr20, cycle: Uint32, totalAmt: Uint128, rewards: Uint128) *)
| PopulateStakeSSNPerCycle ssn_addr cycle totalAmt rewards =>
{_recipient: calleeContract;
_tag: "PopulateStakeSSNPerCycle";
_amount : Uint128 0;
ssn_addr: ssn_addr;
cycle: cycle;
totalAmt: totalAmt;
rewards: rewards}
(* PopulateLastWithdrawCycleForDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32) *)
| PopulateLastWithdrawCycleForDeleg deleg_addr ssn_addr cycle =>
{_recipient: calleeContract;
_tag: "PopulateLastWithdrawCycleForDeleg";
_amount: Uint128 0;
deleg_addr: deleg_addr;
ssn_addr: ssn_addr;
cycle: cycle}
(* PopulateLastBufDepositCycleDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32) *)
| PopulateLastBufDepositCycleDeleg deleg_addr ssn_addr cycle =>
{_recipient: calleeContract;
_tag: "PopulateLastBufDepositCycleDeleg";
_amount: Uint128 0;
deleg_addr: deleg_addr;
ssn_addr: ssn_addr;
cycle: cycle}
(* PopulateBuffDeposit(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128) *)
| PopulateBuffDeposit deleg_addr ssn_addr cycle amt =>
{_recipient: calleeContract;
_tag: "PopulateBuffDeposit";
_amount: Uint128 0;
deleg_addr: deleg_addr;
ssn_addr: ssn_addr;
cycle: cycle;
amt: amt}
(* PopulateDirectDeposit(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128) *)
| PopulateDirectDeposit deleg_addr ssn_addr cycle amt =>
{_recipient: calleeContract;
_tag: "PopulateDirectDeposit";
_amount: Uint128 0;
deleg_addr: deleg_addr;
ssn_addr: ssn_addr;
cycle: cycle;
amt: amt}
(* PopulateDepositAmtDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, amt: Uint128) *)
| PopulateDepositAmtDeleg deleg_addr ssn_addr amt =>
{_recipient: calleeContract;
_tag: "PopulateDepositAmtDeleg";
_amount: Uint128 0;
deleg_addr: deleg_addr;
ssn_addr: ssn_addr;
amt: amt}
(* PopulateDelegStakePerCycle(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128) *)
| PopulateDelegStakePerCycle deleg_addr ssn_addr cycle amt =>
{_recipient: calleeContract;
_tag: "PopulateDelegStakePerCycle";
_amount: Uint128 0;
deleg_addr: deleg_addr;
ssn_addr: ssn_addr;
cycle: cycle;
amt: amt}
(* PopulateLastRewardCycle(cycle: Uint32) *)
| PopulateLastRewardCycle cycle =>
{_recipient: calleeContract;
_tag: "PopulateLastRewardCycle";
_amount: Uint128 0;
cycle: cycle}
(* PopulateCommForSSN(ssn_addr: ByStr20, cycle: Uint32, comm: Uint128) *)
| PopulateCommForSSN ssn_addr cycle comm =>
{_recipient : calleeContract;
_tag: "PopulateCommForSSN";
_amount: Uint128 0;
ssn_addr: ssn_addr;
cycle: cycle;
comm: comm}
(* PopulateTotalStakeAmt(amt: Uint128) *)
| PopulateTotalStakeAmt amt =>
{_recipient: calleeContract;
_tag: "PopulateTotalStakeAmt";
_amount: Uint128 0;
amt: amt}
(* PopulatePendingWithdrawal(ssn_addr: ByStr20, block_number: BNum, stake: Uint128) *)
| PopulatePendingWithdrawal ssn_addr block_number stake =>
{_recipient: calleeContract;
_tag: "PopulatePendingWithdrawal";
_amount: Uint128 0;
ssn_addr: ssn_addr;
block_number: block_number;
stake: stake}
(* DrainContractBalance(amt: Uint128) *)
| DrainContractBalance amt =>
{_recipient : calleeContract;
_tag : "DrainContractBalance";
_amount : Uint128 0;
amt: amt}
end
in
one_msg msg
(***************************************************)
(* The contract definition *)
(* *)
(* This contract holds funds that can be paid out *)
(* to arbitrary users, provided that enough people *)
(* in the collection of owners sign off on the *)
(* payout. *)
(* *)
(* The transaction must be added to the contract *)
(* before signatures can be collected. Once enough *)
(* signatures are collected, the recipient can ask *)
(* for the transaction to be executed and the *)
(* money paid out. *)
(* *)
(* If an owner changes his mind about a *)
(* transaction, the signature can be revoked until *)
(* the transaction is executed. *)
(* *)
(* This wallet does not allow adding or removing *)
(* owners, or changing the number of required *)
(* signatures. To do any of those things, perform *)
(* the following steps: *)
(* *)
(* 1. Deploy a new wallet with owners and *)
(* required_signatures set to the new values. *)
(* MAKE SURE THAT THE NEW WALLET HAS BEEN *)
(* SUCCESFULLY DEPLOYED WITH THE CORRECT *)
(* PARAMETERS BEFORE CONTINUING! *)
(* 2. Invoke the SubmitTransaction transition on *)
(* the old wallet with the following *)
(* parameters: *)
(* recipient : The address of the new wallet *)
(* amount : The _balance of the old wallet *)
(* tag : "AddFunds" *)
(* 3. Have (a sufficient number of) the owners of *)
(* the old contract invoke the SignTransaction *)
(* transition on the old wallet. The parameter *)
(* transactionId should be set to the Id of the *)
(* transaction created in step 2. *)
(* 4. Have one of the owners of the old contract *)
(* invoke the ExecuteTransaction transition on *)
(* the old contract. This will cause the entire *)
(* balance of the old contract to be *)
(* transferred to the new wallet. Note that no *)
(* un-executed transactions will be transferred *)
(* to the new wallet along with the funds. *)
(* *)
(* WARNING: If a sufficient number of owners lose *)
(* their private keys, or for any other reason are *)
(* unable or unwilling to sign for new *)
(* transactions, the funds in the wallet will be *)
(* locked forever. It is therefore a good idea to *)
(* set required_signatures to a value strictly *)
(* less than the number of owners, so that the *)
(* remaining owners can retrieve the funds should *)
(* such a scenario occur. *)
(* *)
(* If an owner loses his private key, the *)
(* remaining owners should move the funds to a new *)
(* wallet (using the workflow described above) to *)
(* ensure that funds are not locked if another *)
(* owner loses his private key. The owner who *)
(* originally lost his private key can generate a *)
(* new key, and the corresponding address be added *)
(* to the new wallet, so that the same set of *)
(* persons own the new wallet. *)
(* *)
(***************************************************)
contract Wallet
(
owners_list : List ByStr20,
required_signatures : Uint32
)
with
let len = @list_length ByStr20 in
let no_of_owners = len owners_list in
let owners_ok = builtin lt zero no_of_owners in
let required_sigs_not_too_low = builtin lt zero required_signatures in
let required_sigs_too_high = builtin lt no_of_owners required_signatures in
let required_sigs_not_too_high = negb required_sigs_too_high in
let required_sigs_ok = andb required_sigs_not_too_high required_sigs_not_too_low in
let all_ok = andb required_sigs_ok owners_ok in
(* Building the owners map is expensive, so avoid checking the owners map until *)
(* everything else has been checked *)
match all_ok with
| True =>
let owners_map = mk_owners_map owners_list in
let size_of_owners_map = builtin size owners_map in
builtin eq size_of_owners_map no_of_owners
| False =>
False
end
=>
(* adr -> True indicates an owner *)
(* adr not in map indicates non-owner *)
(* adr -> False is not used *)
field owners : Map ByStr20 Bool = mk_owners_map owners_list
field transactionCount : Uint32 = Uint32 0
(* Collected signatures for transactions *)
field signatures : Map Uint32 (Map ByStr20 Bool) =
Emp Uint32 (Map ByStr20 Bool)
(* Running count of collected signatures for transactions *)
field signature_counts : Map Uint32 Uint32 =
Emp Uint32 Uint32
(* Transactions *)
field transactions : Map Uint32 Transaction =
Emp Uint32 Transaction
procedure MakeError (err : Error)
e = mk_error_event err;
event e
end
(* Add signature to signature list *)
procedure AddSignature (transactionId : Uint32, signee : ByStr20)
sig <- exists signatures[transactionId][signee];
match sig with
| False =>
count <- signature_counts[transactionId];
match count with
| None =>
(* 0 signatures *)
signature_counts[transactionId] := one
| Some c =>
new_c = builtin add c one;
signature_counts[transactionId] := new_c
end;
signatures[transactionId][signee] := t;
e = mk_signed_transaction_event transactionId;
event e
| True =>
(* Already signed *)
err = AlreadySigned;
MakeError err
end
end
(* Common procedure for all new transactions. *)
(* Check that the sender is owner. Store and sign the transaction. *)
procedure SubmitTransaction (transaction : Transaction)
sender_is_owner <- exists owners[_sender];
match sender_is_owner with
| False =>
err = NonOwnerCannotSubmit;
MakeError err
| True =>
tc <- transactionCount;
transactions[tc] := transaction;
(* Sender implicitly signs *)
AddSignature tc _sender;
(* Increment transaction counter *)
tc_new = builtin add tc transaction_inc;
(* Update transaction count *)
transactionCount := tc_new;
(* Create event with transaction Id *)
e = mk_transaction_added_event tc;
event e
end
end
(* Submit a transaction of native tokens for future signoff *)
transition SubmitNativeTransaction (recipient : ByStr20, amount : Uint128, tag : String)
zero = Uint128 0;
amount_is_zero = builtin eq amount zero;
match amount_is_zero with
| True =>
(* Illegal transaction *)
err = InvalidAmount;
MakeError err
| False =>
transaction = NativeTransaction recipient amount tag;
SubmitTransaction transaction
end
end
(* Common submit procedure for custom transactions *)
procedure SubmitCustomTransaction (calleeContract : ByStr20, calleeTransaction : CalleeTransaction)
transaction = CustomTransaction calleeContract calleeTransaction;
SubmitTransaction transaction
end
(***************************************************)
(* Proxy Transition *)
(***************************************************)
(* Submit a new UpgradeTo transaction for future signoff *)
transition SubmitCustomUpgradeToTransaction (calleeContract : ByStr20, newImplementation : ByStr20)
transaction = UpgradeTo newImplementation;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new ChangeProxyAdmin transaction for future signoff *)
transition SubmitCustomChangeProxyAdminTransaction (calleeContract : ByStr20, newAdmin : ByStr20)
transaction = ChangeProxyAdmin newAdmin;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new ClaimProxyAdmin transaction for future signoff *)
transition SubmitCustomClaimProxyAdminTransaction (calleeContract : ByStr20)
transaction = ClaimProxyAdmin;
SubmitCustomTransaction calleeContract transaction
end
(***************************************************)
(* gZIL Transitions *)
(***************************************************)
transition SubmitCustomChangeMinterTransaction (calleeContract : ByStr20, new_minter : ByStr20)
transaction = ChangeMinter new_minter;
SubmitCustomTransaction calleeContract transaction
end
(***************************************************)
(* SSNlist Transitions *)
(***************************************************)
(***************************************************)
(* House keeping transition *)
(***************************************************)
(* Submit a new pause transaction for future signoff *)
transition SubmitCustomPauseTransaction(calleeContract: ByStr20)
transaction = Pause;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new unpause transaction for future signoff *)
transition SubmitCustomUnpauseTransaction(calleeContract: ByStr20)
transaction = UnPause;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new UpdateAdmin transaction for future signoff *)
transition SubmitCustomUpdateAdminTransaction(calleeContract: ByStr20, new_admin: ByStr20)
transaction = UpdateAdmin new_admin;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new ClaimAdmin transaction for future signoff *)
transition SubmitCustomClaimAdminTransaction(calleeContract: ByStr20)
transaction = ClaimAdmin;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new UpdateVerifier transaction for future signoff *)
transition SubmitCustomUpdateVerifierTransaction(calleeContract: ByStr20, verif: ByStr20)
transaction = UpdateVerifier verif;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new UpdateVerifierRewardAddr transaction for future signoff *)
transition SubmitCustomUpdateVerifierRewardAddrTransaction(calleeContract: ByStr20, addr: ByStr20)
transaction = UpdateVerifierRewardAddr addr;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new ContractStakingParameter transaction for future signoff *)
transition SubmitCustomUpdateStakingParametersTransaction(calleeContract: ByStr20, min_stake: Uint128, min_deleg_stake: Uint128, max_comm_change_rate: Uint128)
transaction = UpdateStakingParameters min_stake min_deleg_stake max_comm_change_rate;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new ChangeBNumReq transaction for future signoff *)
transition SubmitCustomChangeBNumReqTransaction(calleeContract: ByStr20, input_bnum_req: Uint128)
transaction = ChangeBNumReq input_bnum_req;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new UpdateGzilAddr transaction for future signoff *)
transition SubmitCustomUpdateGzilAddrTransaction(calleeContract: ByStr20, gzil_addr: ByStr20)
transaction = UpdateGzilAddr gzil_addr;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new AddSSN transaction for future signoff *)
transition SubmitCustomAddSSNTransaction(calleeContract: ByStr20, ssnaddr: ByStr20, name: String, urlraw: String, urlapi: String, comm: Uint128)
transaction = AddSSN ssnaddr name urlraw urlapi comm;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new UpdateSSN transaction for future signoff *)
transition SubmitCustomUpdateSSNTransaction(calleeContract: ByStr20, ssnaddr: ByStr20, new_name: String, new_urlraw: String, new_urlapi: String)
transaction = UpdateSSN ssnaddr new_name new_urlraw new_urlapi;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new AddSSNAfterUpgrade transaction for future signoff *)
transition SubmitCustomAddSSNAfterUpgradeTransaction(calleeContract: ByStr20, ssnaddr: ByStr20, stake_amt: Uint128, rewards: Uint128, name: String, urlraw: String, urlapi: String, buff_deposit: Uint128, comm: Uint128, comm_rewards: Uint128, rec_addr: ByStr20)
transaction = AddSSNAfterUpgrade ssnaddr stake_amt rewards name urlraw urlapi buff_deposit comm comm_rewards rec_addr;
SubmitCustomTransaction calleeContract transaction
end
(***************************************************)
(* Contract upgrade transition *)
(***************************************************)
(* Submit a new UpdateDeleg transaction for future signoff *)
transition SubmitUpdateDelegTransaction(calleeContract: ByStr20, ssnaddr: ByStr20, deleg: ByStr20, stake_amt: Uint128)
transaction = UpdateDeleg ssnaddr deleg stake_amt;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateStakeSSNPerCycle transaction for future signoff *)
transition SubmitPopulateStakeSSNPerCycleTransaction(calleeContract: ByStr20, ssn_addr: ByStr20, cycle: Uint32, totalAmt: Uint128, rewards: Uint128)
transaction = PopulateStakeSSNPerCycle ssn_addr cycle totalAmt rewards;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateLastWithdrawCycleForDeleg transaction for future signoff *)
transition SubmitPopulateLastWithdrawCycleForDelegTransaction(calleeContract: ByStr20, deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32)
transaction = PopulateLastWithdrawCycleForDeleg deleg_addr ssn_addr cycle;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateLastBufDepositCycleDeleg transaction for future signoff *)
transition SubmitPopulateLastBufDepositCycleDelegTransaction(calleeContract: ByStr20, deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32)
transaction = PopulateLastBufDepositCycleDeleg deleg_addr ssn_addr cycle;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateBuffDeposit transaction for future signoff *)
transition SubmitPopulateBuffDepositTransaction(calleeContract: ByStr20, deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128)
transaction = PopulateBuffDeposit deleg_addr ssn_addr cycle amt;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateDirectDeposit transaction for future signoff *)
transition SubmitPopulateDirectDepositTransaction(calleeContract: ByStr20, deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128)
transaction = PopulateDirectDeposit deleg_addr ssn_addr cycle amt;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateDepositAmtDeleg transaction for future signoff *)
transition SubmitPopulateDepositAmtDelegTransaction(calleeContract: ByStr20, deleg_addr: ByStr20, ssn_addr: ByStr20, amt: Uint128)
transaction = PopulateDepositAmtDeleg deleg_addr ssn_addr amt;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateDelegStakePerCycle transaction for future signoff *)
transition SubmitPopulateDelegStakePerCycleTransaction(calleeContract: ByStr20, deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128)
transaction = PopulateDelegStakePerCycle deleg_addr ssn_addr cycle amt;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateLastRewardCycle transaction for future signoff *)
transition SubmitPopulateLastRewardCycleTransaction(calleeContract: ByStr20, cycle: Uint32)
transaction = PopulateLastRewardCycle cycle;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateCommForSSN transaction for future signoff *)
transition SubmitPopulateCommForSSNTransaction(calleeContract: ByStr20, ssn_addr: ByStr20, cycle: Uint32, comm: Uint128)
transaction = PopulateCommForSSN ssn_addr cycle comm;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulateTotalStakeAmt transaction for future signoff *)
transition SubmitPopulateTotalStakeAmtTransaction(calleeContract: ByStr20, amt: Uint128)
transaction = PopulateTotalStakeAmt amt;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new PopulatePendingWithdrawal transaction for future signoff *)
transition SubmitPopulatePendingWithdrawalTransaction(calleeContract: ByStr20, ssn_addr: ByStr20, block_number: BNum, stake: Uint128)
transaction = PopulatePendingWithdrawal ssn_addr block_number stake;
SubmitCustomTransaction calleeContract transaction
end
(* Submit a new DrainContractBalance transaction for future signoff *)
transition SubmitCustomDrainContractBalanceTransaction(calleeContract : ByStr20, amt: Uint128)
transaction = DrainContractBalance amt;
SubmitCustomTransaction calleeContract transaction
end
(* Sign off on an existing transaction *)
transition SignTransaction (transactionId : Uint32)
(* Only owners are allowed to sign off transactions *)
sender_is_owner <- exists owners[_sender];
match sender_is_owner with
| False =>
err = NonOwnerCannotSign;
MakeError err
| True =>
(* Transaction must have been submitted *)
transaction <- transactions[transactionId];
match transaction with
| None =>
err = UnknownTransactionId;
MakeError err
| Some _ =>
(* Remaining error cases handled by AddSignature *)
AddSignature transactionId _sender
end
end
end
(* Revoke signature of existing transaction, if it has not yet been executed. *)
transition RevokeSignature (transactionId : Uint32)
sig <- exists signatures[transactionId][_sender];
match sig with
| False =>
err = NotAlreadySigned;
MakeError err
| True =>
count <- signature_counts[transactionId];
match count with
| None =>
err = IncorrectSignatureCount;
MakeError err
| Some c =>
c_is_zero = builtin eq c zero;
match c_is_zero with
| True =>
err = IncorrectSignatureCount;
MakeError err
| False =>
new_c = builtin sub c one;
signature_counts[transactionId] := new_c;
delete signatures[transactionId][_sender];
e = mk_signature_revoked_event transactionId;
event e
end
end
end
end
(* Delete transaction and signatures *)
procedure DeleteTransaction (transactionId : Uint32)
delete transactions[transactionId];
delete signatures[transactionId];
delete signature_counts[transactionId]
end
(* Execute native token transaction. *)
(* Checks permission to execute, and checks for sufficient balance. *)
(* Assumes the transaction has been signed off by enough owners. *)
procedure ExecuteNativeTransaction (recipient : ByStr20, amount : Uint128, tag : String)
(* Only the recipient or an owner can execute the transaction *)
recipient_is_sender = builtin eq recipient _sender;
sender_is_owner <- exists owners[_sender];
sender_may_execute = orb recipient_is_sender sender_is_owner;
match sender_may_execute with
| False =>
err = SenderMayNotExecute;
MakeError err
| True =>
(* Check for sufficient funds *)
bal <- _balance;
not_enough_money = builtin lt bal amount;
match not_enough_money with
| True =>
err = InsufficientFunds;
MakeError err
| False =>
(* Transaction approved, and enough money available. *)
(* Execute transaction *)
msgs = native_transaction_msg_as_list recipient amount tag;
send msgs
end
end
end
(* Execute custom transaction. *)
(* Checks permission to execute. *)
(* Assumes the transaction has been signed off by enough owners. *)
procedure ExecuteCustomTransaction (calleeContract : ByStr20, calleeTransaction : CalleeTransaction)
(* Only owners may execute *)
sender_is_owner <- exists owners[_sender];
match sender_is_owner with
| False =>
err = SenderMayNotExecute;
MakeError err
| True =>
as_msg = custom_transaction_msg_as_list calleeContract calleeTransaction;
send as_msg
end
end
(* Execute signed-off transaction *)
transition ExecuteTransaction (transactionId : Uint32)
transaction_opt <- transactions[transactionId];
match transaction_opt with
| None =>
(* Transaction was not found. *)
err = UnknownTransactionId;
MakeError err
| Some transaction =>
sig_count_opt <- signature_counts[transactionId];
match sig_count_opt with
| None =>
(* Signature count not found, even though the transaction exists.*)
err = NoSignatureListFound;
MakeError err
| Some sig_count =>
not_enough_signatures = builtin lt sig_count required_signatures;
match not_enough_signatures with
| True =>
err = NotEnoughSignatures;
MakeError err
| False =>
match transaction with
| NativeTransaction recipient amount tag =>
ExecuteNativeTransaction recipient amount tag
| CustomTransaction calleeContract calleeTransaction =>
ExecuteCustomTransaction calleeContract calleeTransaction
end;
(* Remove transaction and signatures. *)
(* Note: The transaction may have failed, but without a callback *)
(* we have no way of detecting whether it did *)
DeleteTransaction transactionId
end
end
end
end
(* Add native funds to wallet *)
transition AddFunds ()
accept;
e = { _eventname: "FundsAdded"; sender: _sender; amount : _amount};
event e
end