Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): add tests for invoice check signature #225

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fiber/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use crate::ckb::{CkbChainMessage, FundingRequest, FundingTx, TraceTxRequest, Tra
use crate::fiber::channel::{
AddTlcCommand, AddTlcResponse, TxCollaborationCommand, TxUpdateCommand,
};
use crate::fiber::graph::{ChannelInfo, NodeInfo, PaymentSession};
use crate::fiber::graph::{ChannelInfo, PaymentSession};
use crate::fiber::types::{
secp256k1_instance, FiberChannelMessage, PaymentOnionPacket, PeeledPaymentOnionPacket,
TxSignatures,
Expand Down
4 changes: 2 additions & 2 deletions src/invoice/invoice_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ impl CkbInvoice {
hash
}

/// Checks if the signature is valid for the included payee public key or if none exists if it's
/// valid for the recovered signature (which should always be true?).
/// Checks if the signature is valid for the included payee public key
/// and also check the invoice data is consistent with the signature
fn validate_signature(&self) -> bool {
if self.signature.is_none() {
return true;
Expand Down
57 changes: 57 additions & 0 deletions src/invoice/tests/invoice_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,63 @@ fn test_invoice_builder() {
assert_eq!(invoice.amount, Some(1280));
assert_eq!(invoice.payment_hash(), &gen_payment_hash);
assert_eq!(invoice.data.attrs.len(), 7);
assert_eq!(invoice.check_signature().is_ok(), true);
}

#[test]
fn test_invoice_check_signature() {
let gen_payment_hash = rand_sha256_hash();
let (public_key, private_key) = gen_rand_keypair();

let invoice = InvoiceBuilder::new(Currency::Fibb)
.amount(Some(1280))
.payment_hash(gen_payment_hash)
.fallback_address("address".to_string())
.expiry_time(Duration::from_secs(1024))
.payee_pub_key(public_key)
.add_attr(Attribute::FinalHtlcTimeout(5))
.add_attr(Attribute::FinalHtlcMinimumCltvExpiry(12))
.add_attr(Attribute::Description("description".to_string()))
.add_attr(Attribute::UdtScript(CkbScript(Script::default())))
.build_with_sign(|hash| Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))
.unwrap();

assert_eq!(invoice.check_signature(), Ok(()));
let payee_pubkey = invoice.payee_pub_key();
assert_eq!(payee_pubkey, Some(&public_key));

// modify the some element then check signature will fail
let mut invoice_clone = invoice.clone();
invoice_clone.data.attrs[0] = Attribute::FinalHtlcTimeout(6);
assert_eq!(
invoice_clone.check_signature(),
Err(InvoiceError::InvalidSignature)
);

let mut invoice_clone = invoice.clone();
invoice_clone.amount = Some(1281);
assert_eq!(
invoice_clone.check_signature(),
Err(InvoiceError::InvalidSignature)
);

// if the invoice is not signed, check_signature will skipped
let invoice = InvoiceBuilder::new(Currency::Fibb)
.amount(Some(1280))
.payment_hash(gen_payment_hash)
.fallback_address("address".to_string())
.expiry_time(Duration::from_secs(1024))
.payee_pub_key(public_key)
.add_attr(Attribute::FinalHtlcTimeout(5))
.add_attr(Attribute::FinalHtlcMinimumCltvExpiry(12))
.build()
.unwrap();

assert_eq!(invoice.check_signature(), Ok(()));
// modify the some element then check signature will also skip
let mut invoice_clone = invoice.clone();
invoice_clone.amount = Some(1281);
assert_eq!(invoice_clone.check_signature(), Ok(()));
}

#[test]
Expand Down
Loading