@@ -86,11 +86,11 @@ pub struct HashTimeLockContract {
86
86
/// The hash whose preimage must be revealed (along with the recipient's signature) to spend the UTXO.
87
87
pub hash_lock : H256 ,
88
88
/// The pubkey that is intended to receive and acknowledge receipt of the funds.
89
- pub recipient_pubkey : H256 ,
89
+ pub recipient_pubkey : Public ,
90
90
/// The time (as a block height) when the refund path opens up.
91
91
pub claim_period_end : u32 ,
92
92
/// The address who can spend the coins without revealing the preimage after the claim period has ended.
93
- pub refunder_pubkey : H256 ,
93
+ pub refunder_pubkey : Public ,
94
94
}
95
95
96
96
///
@@ -112,11 +112,11 @@ impl Verifier for HashTimeLockContract {
112
112
match spend_path {
113
113
HtlcSpendPath :: Claim { secret, signature } => {
114
114
// Claims are valid as long as the secret is correct and the receiver signature is correct.
115
- BlakeTwo256 :: hash ( & secret) == self . hash_lock
115
+ BlakeTwo256 :: hash ( secret) == self . hash_lock
116
116
&& sp_io:: crypto:: sr25519_verify (
117
- & signature,
117
+ signature,
118
118
simplified_tx,
119
- & Public :: from_h256 ( self . recipient_pubkey ) ,
119
+ & self . recipient_pubkey ,
120
120
)
121
121
}
122
122
HtlcSpendPath :: Refund { signature } => {
@@ -127,9 +127,9 @@ impl Verifier for HashTimeLockContract {
127
127
128
128
// Check that the refunder has signed properly
129
129
sp_io:: crypto:: sr25519_verify (
130
- & signature,
130
+ signature,
131
131
simplified_tx,
132
- & Public :: from_h256 ( self . refunder_pubkey ) ,
132
+ & self . refunder_pubkey ,
133
133
)
134
134
}
135
135
}
@@ -138,6 +138,8 @@ impl Verifier for HashTimeLockContract {
138
138
139
139
#[ cfg( test) ]
140
140
mod test {
141
+ use sp_core:: { sr25519:: Pair , Pair as _} ;
142
+
141
143
use super :: * ;
142
144
143
145
#[ test]
@@ -181,9 +183,26 @@ mod test {
181
183
assert ! ( !hash_lock. verify( & [ ] , 0 , & incorrect. encode( ) ) ) ;
182
184
}
183
185
184
- //TODO HTLC Tests
186
+ #[ test]
187
+ fn htlc_claim_success ( ) {
188
+ const THRESHOLD : u32 = 100 ;
189
+ let secret = "htlc ftw" . encode ( ) ;
190
+ let recipient_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
191
+ let refunder_pair = Pair :: from_seed ( & [ 0u8 ; 32 ] ) ;
192
+
193
+ let htlc = HashTimeLockContract {
194
+ hash_lock : BlakeTwo256 :: hash ( & secret) ,
195
+ recipient_pubkey : recipient_pair. public ( ) ,
196
+ claim_period_end : THRESHOLD ,
197
+ refunder_pubkey : refunder_pair. public ( ) ,
198
+ } ;
185
199
186
- // Spend Success
200
+ let simplified_tx = b"hello world" . as_slice ( ) ;
201
+ let recipient_sig = recipient_pair. sign ( simplified_tx) ;
202
+ let redeemer = HtlcSpendPath :: Claim { secret, signature : recipient_sig } ;
203
+
204
+ assert ! ( htlc. verify( & simplified_tx, 0 , & redeemer) ) ;
205
+ }
187
206
// Spend wrong secret
188
207
// Spend bogus sig
189
208
// Spend but sig is from refunder instead of recipient
0 commit comments