Skip to content

Commit b19586d

Browse files
committed
htlc happy path test
1 parent 76df42b commit b19586d

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

tuxedo-core/src/verifier/htlc.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ pub struct HashTimeLockContract {
8686
/// The hash whose preimage must be revealed (along with the recipient's signature) to spend the UTXO.
8787
pub hash_lock: H256,
8888
/// The pubkey that is intended to receive and acknowledge receipt of the funds.
89-
pub recipient_pubkey: H256,
89+
pub recipient_pubkey: Public,
9090
/// The time (as a block height) when the refund path opens up.
9191
pub claim_period_end: u32,
9292
/// 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,
9494
}
9595

9696
///
@@ -112,11 +112,11 @@ impl Verifier for HashTimeLockContract {
112112
match spend_path {
113113
HtlcSpendPath::Claim { secret, signature } => {
114114
// 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
116116
&& sp_io::crypto::sr25519_verify(
117-
&signature,
117+
signature,
118118
simplified_tx,
119-
&Public::from_h256(self.recipient_pubkey),
119+
&self.recipient_pubkey,
120120
)
121121
}
122122
HtlcSpendPath::Refund { signature } => {
@@ -127,9 +127,9 @@ impl Verifier for HashTimeLockContract {
127127

128128
// Check that the refunder has signed properly
129129
sp_io::crypto::sr25519_verify(
130-
&signature,
130+
signature,
131131
simplified_tx,
132-
&Public::from_h256(self.refunder_pubkey),
132+
&self.refunder_pubkey,
133133
)
134134
}
135135
}
@@ -138,6 +138,8 @@ impl Verifier for HashTimeLockContract {
138138

139139
#[cfg(test)]
140140
mod test {
141+
use sp_core::{sr25519::Pair, Pair as _};
142+
141143
use super::*;
142144

143145
#[test]
@@ -181,9 +183,26 @@ mod test {
181183
assert!(!hash_lock.verify(&[], 0, &incorrect.encode()));
182184
}
183185

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+
};
185199

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+
}
187206
// Spend wrong secret
188207
// Spend bogus sig
189208
// Spend but sig is from refunder instead of recipient

0 commit comments

Comments
 (0)