@@ -139,9 +139,15 @@ impl Verifier for HashTimeLockContract {
139
139
140
140
#[ cfg( test) ]
141
141
mod test {
142
+ use super :: * ;
142
143
use sp_core:: { sr25519:: Pair , Pair as _} ;
143
144
144
- use super :: * ;
145
+ fn bad_sig ( ) -> Signature {
146
+ Signature :: from_slice (
147
+ b"bogus_signature_bogus_signature_bogus_signature_bogus_signature!" . as_slice ( ) ,
148
+ )
149
+ . expect ( "Should be able to create a bogus signature." )
150
+ }
145
151
146
152
#[ test]
147
153
fn time_lock_too_soon ( ) {
@@ -189,7 +195,7 @@ mod test {
189
195
const THRESHOLD : u32 = 100 ;
190
196
let secret = "htlc ftw" . encode ( ) ;
191
197
let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
192
- let refunder_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
198
+ let refunder_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
193
199
194
200
let htlc = HashTimeLockContract {
195
201
hash_lock : BlakeTwo256 :: hash ( & secret) ,
@@ -207,11 +213,168 @@ mod test {
207
213
208
214
assert ! ( htlc. verify( & simplified_tx, 0 , & redeemer) ) ;
209
215
}
210
- // Spend wrong secret
211
- // Spend bogus sig
212
- // Spend but sig is from refunder instead of recipient
213
- // Refund Success
214
- // Refund too early
215
- // Refund bogus sig
216
- // Refund but sig is from recipient instead of refunder
216
+
217
+ #[ test]
218
+ fn htlc_claim_wrong_secret ( ) {
219
+ const THRESHOLD : u32 = 100 ;
220
+ let secret = "htlc ftw" . encode ( ) ;
221
+ let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
222
+ let refunder_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
223
+
224
+ let htlc = HashTimeLockContract {
225
+ hash_lock : BlakeTwo256 :: hash ( & secret) ,
226
+ recipient_pubkey : recipient_pair. public ( ) ,
227
+ claim_period_end : THRESHOLD ,
228
+ refunder_pubkey : refunder_pair. public ( ) ,
229
+ } ;
230
+
231
+ let incorrect_secret = "there is no second best" . encode ( ) ;
232
+
233
+ let simplified_tx = b"hello world" . as_slice ( ) ;
234
+ let recipient_sig = recipient_pair. sign ( simplified_tx) ;
235
+ let redeemer = HtlcSpendPath :: Claim {
236
+ secret : incorrect_secret,
237
+ signature : recipient_sig,
238
+ } ;
239
+
240
+ assert ! ( !htlc. verify( & simplified_tx, 0 , & redeemer) ) ;
241
+ }
242
+
243
+ #[ test]
244
+ fn htlc_claim_bogus_signature ( ) {
245
+ const THRESHOLD : u32 = 100 ;
246
+ let secret = "htlc ftw" . encode ( ) ;
247
+ let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
248
+ let refunder_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
249
+
250
+ let htlc = HashTimeLockContract {
251
+ hash_lock : BlakeTwo256 :: hash ( & secret) ,
252
+ recipient_pubkey : recipient_pair. public ( ) ,
253
+ claim_period_end : THRESHOLD ,
254
+ refunder_pubkey : refunder_pair. public ( ) ,
255
+ } ;
256
+
257
+ let simplified_tx = b"hello world" . as_slice ( ) ;
258
+ let redeemer = HtlcSpendPath :: Claim {
259
+ secret,
260
+ signature : bad_sig ( ) ,
261
+ } ;
262
+
263
+ assert ! ( !htlc. verify( & simplified_tx, 0 , & redeemer) ) ;
264
+ }
265
+
266
+ #[ test]
267
+ fn htlc_claim_fails_when_signature_is_from_refunder ( ) {
268
+ const THRESHOLD : u32 = 100 ;
269
+ let secret = "htlc ftw" . encode ( ) ;
270
+ let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
271
+ let refunder_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
272
+
273
+ let htlc = HashTimeLockContract {
274
+ hash_lock : BlakeTwo256 :: hash ( & secret) ,
275
+ recipient_pubkey : recipient_pair. public ( ) ,
276
+ claim_period_end : THRESHOLD ,
277
+ refunder_pubkey : refunder_pair. public ( ) ,
278
+ } ;
279
+
280
+ let simplified_tx = b"hello world" . as_slice ( ) ;
281
+ let refunder_sig = refunder_pair. sign ( simplified_tx) ;
282
+ let redeemer = HtlcSpendPath :: Claim {
283
+ secret,
284
+ signature : refunder_sig,
285
+ } ;
286
+
287
+ assert ! ( !htlc. verify( & simplified_tx, 0 , & redeemer) ) ;
288
+ }
289
+
290
+ #[ test]
291
+ fn htlc_refund_success ( ) {
292
+ const THRESHOLD : u32 = 100 ;
293
+ let secret = "htlc ftw" . encode ( ) ;
294
+ let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
295
+ let refunder_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
296
+
297
+ let htlc = HashTimeLockContract {
298
+ hash_lock : BlakeTwo256 :: hash ( & secret) ,
299
+ recipient_pubkey : recipient_pair. public ( ) ,
300
+ claim_period_end : THRESHOLD ,
301
+ refunder_pubkey : refunder_pair. public ( ) ,
302
+ } ;
303
+
304
+ let simplified_tx = b"hello world" . as_slice ( ) ;
305
+ let refunder_sig = refunder_pair. sign ( simplified_tx) ;
306
+ let redeemer = HtlcSpendPath :: Refund {
307
+ signature : refunder_sig,
308
+ } ;
309
+
310
+ assert ! ( htlc. verify( & simplified_tx, 2 * THRESHOLD , & redeemer) ) ;
311
+ }
312
+
313
+ #[ test]
314
+ fn htlc_refund_too_early ( ) {
315
+ const THRESHOLD : u32 = 100 ;
316
+ let secret = "htlc ftw" . encode ( ) ;
317
+ let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
318
+ let refunder_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
319
+
320
+ let htlc = HashTimeLockContract {
321
+ hash_lock : BlakeTwo256 :: hash ( & secret) ,
322
+ recipient_pubkey : recipient_pair. public ( ) ,
323
+ claim_period_end : THRESHOLD ,
324
+ refunder_pubkey : refunder_pair. public ( ) ,
325
+ } ;
326
+
327
+ let simplified_tx = b"hello world" . as_slice ( ) ;
328
+ let refunder_sig = refunder_pair. sign ( simplified_tx) ;
329
+ let redeemer = HtlcSpendPath :: Refund {
330
+ signature : refunder_sig,
331
+ } ;
332
+
333
+ assert ! ( !htlc. verify( & simplified_tx, 0 , & redeemer) ) ;
334
+ }
335
+
336
+ #[ test]
337
+ fn htlc_refund_bogus_sig ( ) {
338
+ const THRESHOLD : u32 = 100 ;
339
+ let secret = "htlc ftw" . encode ( ) ;
340
+ let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
341
+ let refunder_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
342
+
343
+ let htlc = HashTimeLockContract {
344
+ hash_lock : BlakeTwo256 :: hash ( & secret) ,
345
+ recipient_pubkey : recipient_pair. public ( ) ,
346
+ claim_period_end : THRESHOLD ,
347
+ refunder_pubkey : refunder_pair. public ( ) ,
348
+ } ;
349
+
350
+ let simplified_tx = b"hello world" . as_slice ( ) ;
351
+ let redeemer = HtlcSpendPath :: Refund {
352
+ signature : bad_sig ( ) ,
353
+ } ;
354
+
355
+ assert ! ( !htlc. verify( & simplified_tx, 2 * THRESHOLD , & redeemer) ) ;
356
+ }
357
+
358
+ #[ test]
359
+ fn htlc_refund_fails_when_signature_is_from_recipient ( ) {
360
+ const THRESHOLD : u32 = 100 ;
361
+ let secret = "htlc ftw" . encode ( ) ;
362
+ let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
363
+ let refunder_pair = Pair :: from_seed ( & [ 1u8 ; 32 ] ) ;
364
+
365
+ let htlc = HashTimeLockContract {
366
+ hash_lock : BlakeTwo256 :: hash ( & secret) ,
367
+ recipient_pubkey : recipient_pair. public ( ) ,
368
+ claim_period_end : THRESHOLD ,
369
+ refunder_pubkey : refunder_pair. public ( ) ,
370
+ } ;
371
+
372
+ let simplified_tx = b"hello world" . as_slice ( ) ;
373
+ let recipient_sig = recipient_pair. sign ( simplified_tx) ;
374
+ let redeemer = HtlcSpendPath :: Refund {
375
+ signature : recipient_sig,
376
+ } ;
377
+
378
+ assert ! ( !htlc. verify( & simplified_tx, 2 * THRESHOLD , & redeemer) ) ;
379
+ }
217
380
}
0 commit comments