2 | | - | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | | - | {-# CONTENT_TYPE DAPP #-} |
---|
4 | | - | func getAddressFromEntry (key) = { |
---|
5 | | - | let address = match getString(this, key) { |
---|
6 | | - | case str: String => |
---|
7 | | - | str |
---|
8 | | - | case _ => |
---|
9 | | - | throw("First address not set!") |
---|
10 | | - | } |
---|
11 | | - | address |
---|
12 | | - | } |
---|
13 | | - | |
---|
14 | | - | |
---|
15 | | - | func getAddressFromEntryOrEmpty (key) = { |
---|
16 | | - | let address = match getString(this, key) { |
---|
17 | | - | case str: String => |
---|
18 | | - | str |
---|
19 | | - | case _ => |
---|
20 | | - | "" |
---|
21 | | - | } |
---|
22 | | - | address |
---|
23 | | - | } |
---|
24 | | - | |
---|
25 | | - | |
---|
26 | | - | func getIntFromKey (key) = { |
---|
27 | | - | let firstAddressAmount = match getInteger(this, key) { |
---|
28 | | - | case n: Int => |
---|
29 | | - | n |
---|
30 | | - | case _ => |
---|
31 | | - | 0 |
---|
32 | | - | } |
---|
33 | | - | firstAddressAmount |
---|
34 | | - | } |
---|
35 | | - | |
---|
36 | | - | |
---|
37 | | - | func checkIfMethodCallComesFromValidAccount (sender) = { |
---|
38 | | - | let firstAddress = getAddressFromEntry("firstAddress") |
---|
39 | | - | let secondAddress = getAddressFromEntry("secondAddress") |
---|
40 | | - | if (if ((firstAddress != sender)) |
---|
41 | | - | then (secondAddress != sender) |
---|
42 | | - | else false) |
---|
43 | | - | then throw(("Method call from invalid account: " + sender)) |
---|
44 | | - | else true |
---|
45 | | - | } |
---|
46 | | - | |
---|
47 | | - | |
---|
48 | | - | @Callable(i) |
---|
49 | | - | func claimCheating (recipientA,amountRecipientA,recipientB,amountRecipientB,timestamp,signature) = { |
---|
50 | | - | let sender = toBase58String(i.caller.bytes) |
---|
51 | | - | let validSender = checkIfMethodCallComesFromValidAccount(sender) |
---|
52 | | - | let originalString = ((((recipientA + toString(amountRecipientA)) + recipientB) + toString(amountRecipientB)) + toString(timestamp)) |
---|
53 | | - | let counterPart = getAddressFromEntry((sender + "_counterpart")) |
---|
54 | | - | let signedByCounterpart = sigVerify(toBytes(originalString), fromBase58String(signature), fromBase58String(counterPart)) |
---|
55 | | - | let oldTimestamp = getIntFromKey("timestamp") |
---|
56 | | - | if (if (if (validSender) |
---|
57 | | - | then signedByCounterpart |
---|
58 | | - | else false) |
---|
59 | | - | then (timestamp > oldTimestamp) |
---|
60 | | - | else false) |
---|
61 | | - | then TransferSet([ScriptTransfer(i.caller, (amountRecipientA + amountRecipientB), unit)]) |
---|
62 | | - | else throw("Wrong signature or timestamp not newer!") |
---|
63 | | - | } |
---|
64 | | - | |
---|
65 | | - | |
---|
66 | | - | |
---|
67 | | - | @Callable(i) |
---|
68 | | - | func confirmClose () = { |
---|
69 | | - | let sender = toBase58String(i.caller.bytes) |
---|
70 | | - | let validSender = checkIfMethodCallComesFromValidAccount(sender) |
---|
71 | | - | let initiator = getString(this, "closing_initiated_by") |
---|
72 | | - | if (if ((sender != initiator)) |
---|
73 | | - | then validSender |
---|
74 | | - | else false) |
---|
75 | | - | then { |
---|
76 | | - | let firstAddress = getAddressFromEntry("firstAddress") |
---|
77 | | - | let secondAddress = getAddressFromEntry("secondAddress") |
---|
78 | | - | let firstAddressAmount = getIntFromKey(firstAddress) |
---|
79 | | - | let secondAddressAmount = getIntFromKey(secondAddress) |
---|
80 | | - | TransferSet([ScriptTransfer(Address(fromBase58String(firstAddress)), firstAddressAmount, unit), ScriptTransfer(Address(fromBase58String(secondAddress)), secondAddressAmount, unit)]) |
---|
81 | | - | } |
---|
82 | | - | else throw("Initiator can not confirm closing of the channel!") |
---|
83 | | - | } |
---|
84 | | - | |
---|
85 | | - | |
---|
86 | | - | |
---|
87 | | - | @Callable(i) |
---|
88 | | - | func closeAfterTimelock () = { |
---|
89 | | - | let sender = toBase58String(i.caller.bytes) |
---|
90 | | - | let validSender = checkIfMethodCallComesFromValidAccount(sender) |
---|
91 | | - | let timelock = getIntFromKey("timelock") |
---|
92 | | - | if (if ((height > timelock)) |
---|
93 | | - | then validSender |
---|
94 | | - | else false) |
---|
95 | | - | then { |
---|
96 | | - | let firstAddress = getAddressFromEntry("firstAddress") |
---|
97 | | - | let secondAddress = getAddressFromEntry("secondAddress") |
---|
98 | | - | let firstAddressAmount = getIntFromKey(firstAddress) |
---|
99 | | - | let secondAddressAmount = getIntFromKey(secondAddress) |
---|
100 | | - | TransferSet([ScriptTransfer(Address(fromBase58String(firstAddress)), firstAddressAmount, unit), ScriptTransfer(Address(fromBase58String(secondAddress)), secondAddressAmount, unit)]) |
---|
101 | | - | } |
---|
102 | | - | else throw("Timelock still valid!") |
---|
103 | | - | } |
---|
104 | | - | |
---|
105 | | - | |
---|
106 | | - | |
---|
107 | | - | @Callable(i) |
---|
108 | | - | func initialClosing (recipientA,amountRecipientA,recipientB,amountRecipientB,timestamp,signature) = { |
---|
109 | | - | let sender = toBase58String(i.caller.bytes) |
---|
110 | | - | let validSender = checkIfMethodCallComesFromValidAccount(sender) |
---|
111 | | - | let originalString = ((((recipientA + toString(amountRecipientA)) + recipientB) + toString(amountRecipientB)) + toString(timestamp)) |
---|
112 | | - | let counterPart = getAddressFromEntry((sender + "_counterpart")) |
---|
113 | | - | let signedByCounterpart = sigVerify(toBytes(originalString), fromBase58String(signature), fromBase58String(counterPart)) |
---|
114 | | - | let timelockPeriod = getIntFromKey("timelockPeriod") |
---|
115 | | - | if (if (signedByCounterpart) |
---|
116 | | - | then validSender |
---|
117 | | - | else false) |
---|
118 | | - | then WriteSet([DataEntry(recipientA, amountRecipientA), DataEntry(recipientB, amountRecipientB), DataEntry("closing_initiated_by", sender), DataEntry("timestamp", timestamp), DataEntry("timelock", (height + timelockPeriod))]) |
---|
119 | | - | else throw("Wrong signature!") |
---|
120 | | - | } |
---|
121 | | - | |
---|
122 | | - | |
---|
123 | | - | |
---|
124 | | - | @Callable(i) |
---|
125 | | - | func fund () = { |
---|
126 | | - | let sender = toBase58String(i.caller.bytes) |
---|
127 | | - | let validSender = checkIfMethodCallComesFromValidAccount(sender) |
---|
128 | | - | let payment = extract(i.payment) |
---|
129 | | - | let amountKey = (sender + "_amount") |
---|
130 | | - | let currentAmount = getIntFromKey(amountKey) |
---|
131 | | - | if (validSender) |
---|
132 | | - | then if ((currentAmount != 0)) |
---|
133 | | - | then throw("User has already funded payment channel!") |
---|
134 | | - | else WriteSet([DataEntry(amountKey, (currentAmount + payment.amount))]) |
---|
135 | | - | else throw("Wrong account tries to fund channel!") |
---|
136 | | - | } |
---|
137 | | - | |
---|
138 | | - | |
---|
139 | | - | |
---|
140 | | - | @Callable(i) |
---|
141 | | - | func init (firstAddress,firstPublicKey,secondAddress,secondPublicKey,timelockPeriod) = { |
---|
142 | | - | let currentFirstAddress = getAddressFromEntryOrEmpty((firstAddress + "_counterpart")) |
---|
143 | | - | let currentSecondAddress = getAddressFromEntryOrEmpty((secondAddress + "_counterpart")) |
---|
144 | | - | if (if ((currentFirstAddress != "")) |
---|
145 | | - | then true |
---|
146 | | - | else (currentSecondAddress != "")) |
---|
147 | | - | then throw("Contract already initiated!") |
---|
148 | | - | else WriteSet([DataEntry("firstAddress", firstAddress), DataEntry("secondAddress", secondAddress), DataEntry((firstAddress + "_counterpart"), secondPublicKey), DataEntry((secondAddress + "_counterpart"), firstPublicKey), DataEntry("timelockPeriod", timelockPeriod)]) |
---|
149 | | - | } |
---|
150 | | - | |
---|
151 | | - | |
---|
152 | | - | @Verifier(tx) |
---|
153 | | - | func verify () = match tx { |
---|
154 | | - | case _ => |
---|
155 | | - | true |
---|
156 | | - | } |
---|
157 | | - | |
---|
| 2 | + | {-# CONTENT_TYPE EXPRESSION #-} |
---|
| 3 | + | let PubKey1 = base58'854p8BYzrj6yBPRPmfQur3oF1Rjc1AJ548qRp5FT5kDa' |
---|
| 4 | + | let PubKey2 = base58'DKGFPozLrsiR8NM4NJzqQaBYC8NyGYjuw2hDYicQVjco' |
---|
| 5 | + | let sig1 = if (sigVerify(tx.bodyBytes, tx.proofs[0], PubKey1)) |
---|
| 6 | + | then 1 |
---|
| 7 | + | else 0 |
---|
| 8 | + | let sig2 = if (sigVerify(tx.bodyBytes, tx.proofs[1], PubKey2)) |
---|
| 9 | + | then 1 |
---|
| 10 | + | else 0 |
---|
| 11 | + | ((sig1 + sig2) > 0) |
---|