-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtlc-contract.ak
250 lines (236 loc) · 6.66 KB
/
htlc-contract.ak
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
use aiken/dict
use aiken/hash.{Blake2b_224, Hash, sha2_256}
use aiken/interval.{Finite, Interval, IntervalBound}
use aiken/list.{any}
use aiken/time.{PosixTime}
use aiken/transaction.{
NoDatum, Output, OutputReference, ScriptContext, Spend, Transaction,
TransactionId,
}
use aiken/transaction/credential.{VerificationKey, from_verification_key}
use aiken/transaction/value.{from_lovelace, lovelace_of, to_minted_value, zero}
type HTLCDatum {
secret_hash: ByteArray,
timeout: PosixTime,
owner: Hash<Blake2b_224, VerificationKey>,
}
type HTLCRedeemer {
Claim(ByteArray)
Refund
}
// Function to create fee value
fn fee_value() -> Int {
1000000
}
// Check if the required fee has been paid
fn fee_paid(
ctx: ScriptContext,
team_address: Hash<Blake2b_224, VerificationKey>,
required_fee: Int,
) -> Bool {
any(
ctx.transaction.outputs,
fn(output: Output) {
from_verification_key(team_address) == output.address && lovelace_of(
output.value,
) >= required_fee
},
)
}
// Check if the transaction is signed by the owner
fn signed_by_owner(
ctx: ScriptContext,
owner: Hash<Blake2b_224, VerificationKey>,
) -> Bool {
any(
ctx.transaction.extra_signatories,
fn(sig: Hash<Blake2b_224, VerificationKey>) { sig == owner },
)
}
// Check if the deadline has passed
fn deadline_passed(timeout: PosixTime, current_time: PosixTime) -> Bool {
current_time > timeout
}
// Get the current time from the ScriptContext
fn get_current_time(ctx: ScriptContext) -> PosixTime {
let validity_range = ctx.transaction.validity_range
when validity_range.lower_bound is {
IntervalBound { bound_type: Finite(lower), is_inclusive: _ } -> lower
_ -> 0
}
}
// Validator
validator(team_address: Hash<Blake2b_224, VerificationKey>) {
fn spend(datum: HTLCDatum, redeemer: HTLCRedeemer, ctx: ScriptContext) -> Bool {
let current_time = get_current_time(ctx)
when redeemer is {
Claim(secret) ->
sha2_256(secret) == datum.secret_hash && !deadline_passed(
datum.timeout,
current_time,
) && fee_paid(ctx, team_address, fee_value())
Refund ->
signed_by_owner(ctx, datum.owner) && deadline_passed(
datum.timeout,
current_time,
) && fee_paid(ctx, team_address, fee_value())
}
}
}
// Tests
test deadline_passed_test() {
let timeout = 1000
let current_time = 1500
deadline_passed(timeout, current_time)
}
test deadline_not_passed_test() {
let timeout = 2000
let current_time = 1500
!deadline_passed(timeout, current_time)
}
test fee_paid_test() {
let team_address = #"00000000000000000000000000000000000000000000000000000000"
let fee = fee_value()
let outputs =
[
Output {
address: from_verification_key(team_address),
value: from_lovelace(fee),
datum: NoDatum,
reference_script: None,
},
]
let ctx =
ScriptContext {
transaction: Transaction {
inputs: [],
reference_inputs: [],
outputs,
fee: from_lovelace(0),
mint: to_minted_value(zero()),
certificates: [],
withdrawals: dict.new(),
validity_range: interval.everything(),
extra_signatories: [],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
},
purpose: Spend(
OutputReference {
transaction_id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
output_index: 0,
},
),
}
fee_paid(ctx, team_address, fee)
}
test fee_not_paid_test() {
let team_address = #"00000000000000000000000000000000000000000000000000000000"
let fee = fee_value() - 1
let outputs =
[
Output {
address: from_verification_key(team_address),
value: from_lovelace(fee),
datum: NoDatum,
reference_script: None,
},
]
let ctx =
ScriptContext {
transaction: Transaction {
inputs: [],
reference_inputs: [],
outputs,
fee: from_lovelace(0),
mint: to_minted_value(zero()),
certificates: [],
withdrawals: dict.new(),
validity_range: interval.everything(),
extra_signatories: [],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
},
purpose: Spend(
OutputReference {
transaction_id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
output_index: 0,
},
),
}
!fee_paid(ctx, team_address, fee_value())
}
test signed_by_owner_test() {
let owner = #"00000000000000000000000000000000000000000000000000000000"
let ctx =
ScriptContext {
transaction: Transaction {
inputs: [],
reference_inputs: [],
outputs: [],
fee: from_lovelace(0),
mint: to_minted_value(zero()),
certificates: [],
withdrawals: dict.new(),
validity_range: interval.everything(),
extra_signatories: [owner],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
},
purpose: Spend(
OutputReference {
transaction_id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
output_index: 0,
},
),
}
signed_by_owner(ctx, owner)
}
test not_signed_by_owner_test() {
let owner = #"00000000000000000000000000000000000000000000000000000000"
let other_signer =
#"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
let ctx =
ScriptContext {
transaction: Transaction {
inputs: [],
reference_inputs: [],
outputs: [],
fee: from_lovelace(0),
mint: to_minted_value(zero()),
certificates: [],
withdrawals: dict.new(),
validity_range: interval.everything(),
extra_signatories: [other_signer],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
},
purpose: Spend(
OutputReference {
transaction_id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
output_index: 0,
},
),
}
!signed_by_owner(ctx, owner)
}