Skip to content
Open
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
10 changes: 10 additions & 0 deletions contracts/account/src/tests/test_invoice_signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn test_create_invoice_signed_by_manager_success() {
&description,
&amount,
&token,
&None,
&nonce,
&signature,
);
Expand Down Expand Up @@ -152,6 +153,7 @@ fn test_create_invoice_signed_by_admin_success() {
&description,
&amount,
&token,
&None,
&nonce,
&signature,
);
Expand Down Expand Up @@ -189,6 +191,7 @@ fn test_create_invoice_signed_nonce_replay_fails() {
&description,
&amount,
&token,
&None,
&nonce,
&signature,
);
Expand All @@ -200,6 +203,7 @@ fn test_create_invoice_signed_nonce_replay_fails() {
&description,
&amount,
&token,
&None,
&nonce,
&signature,
);
Expand All @@ -225,6 +229,7 @@ fn test_create_invoice_signed_different_nonces_succeed() {
&description,
&amount,
&token,
&None,
&generate_nonce(&env),
&generate_signature(&env),
);
Expand All @@ -235,6 +240,7 @@ fn test_create_invoice_signed_different_nonces_succeed() {
&description,
&amount,
&token,
&None,
&generate_nonce_2(&env),
&generate_signature(&env),
);
Expand All @@ -261,6 +267,7 @@ fn test_create_invoice_signed_unauthorized_caller_fails() {
&description,
&500,
&token,
&None,
&generate_nonce(&env),
&generate_signature(&env),
);
Expand All @@ -286,6 +293,7 @@ fn test_create_invoice_signed_unregistered_merchant_fails() {
&description,
&500,
&token,
&None,
&generate_nonce(&env),
&generate_signature(&env),
);
Expand All @@ -311,6 +319,7 @@ fn test_create_invoice_signed_invalid_amount_fails() {
&description,
&0,
&token,
&None,
&generate_nonce(&env),
&generate_signature(&env),
);
Expand Down Expand Up @@ -338,6 +347,7 @@ fn test_create_invoice_signed_when_paused_fails() {
&description,
&500,
&token,
&None,
&generate_nonce(&env),
&generate_signature(&env),
);
Expand Down
11 changes: 11 additions & 0 deletions contracts/shade/src/components/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn create_invoice(
description: &String,
amount: i128,
token: &Address,
expires_at: &Option<u64>,
) -> u64 {
merchant_address.require_auth();
if amount <= 0 {
Expand Down Expand Up @@ -46,6 +47,7 @@ pub fn create_invoice(
payer: None,
date_created: env.ledger().timestamp(),
date_paid: None,
expires_at: *expires_at,
amount_refunded: 0,
};
env.storage()
Expand All @@ -72,6 +74,7 @@ pub fn create_invoice_signed(
description: &String,
amount: i128,
token: &Address,
expires_at: &Option<u64>,
nonce: &BytesN<32>,
signature: &BytesN<64>,
) -> u64 {
Expand Down Expand Up @@ -100,6 +103,7 @@ pub fn create_invoice_signed(
description,
amount,
token,
expires_at,
nonce,
signature,
);
Expand Down Expand Up @@ -132,6 +136,7 @@ pub fn create_invoice_signed(
payer: None,
date_created: env.ledger().timestamp(),
date_paid: None,
expires_at: *expires_at,
amount_refunded: 0,
};

Expand Down Expand Up @@ -315,6 +320,12 @@ pub fn pay_invoice(env: &Env, payer: &Address, invoice_id: u64) -> i128 {
panic_with_error!(env, ContractError::InvalidInvoiceStatus);
}

if let Some(expires_at) = invoice.expires_at {
if env.ledger().timestamp() > expires_at {
panic_with_error!(env, ContractError::InvoiceExpired);
}
}

// Validate that the invoice token is accepted
if !admin::is_accepted_token(env, &invoice.token) {
panic_with_error!(env, ContractError::TokenNotAccepted);
Expand Down
5 changes: 4 additions & 1 deletion contracts/shade/src/components/signature_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn build_message(
description: &String,
amount: i128,
token: &Address,
expires_at: &Option<u64>,
nonce: &BytesN<32>,
) -> Bytes {
let mut msg = Bytes::new(env);
Expand All @@ -22,6 +23,7 @@ fn build_message(
msg.append(&Bytes::from_slice(env, &amount.to_be_bytes()));
msg.append(&token.clone().to_xdr(env));
msg.append(&description.clone().to_xdr(env));
msg.append(&Bytes::from_slice(env, &expires_at.unwrap_or(0).to_be_bytes()));
msg
}

Expand All @@ -36,6 +38,7 @@ pub fn verify_invoice_signature(
description: &String,
amount: i128,
token: &Address,
expires_at: &Option<u64>,
nonce: &BytesN<32>,
signature: &BytesN<64>,
) {
Expand All @@ -45,7 +48,7 @@ pub fn verify_invoice_signature(
.get(&DataKey::MerchantKey(merchant.clone()))
.unwrap_or_else(|| panic_with_error!(env, ContractError::MerchantKeyNotFound));

let message = build_message(env, merchant, description, amount, token, nonce);
let message = build_message(env, merchant, description, amount, token, expires_at, nonce);

env.crypto().ed25519_verify(&key, &message, signature);
}
Expand Down
1 change: 1 addition & 0 deletions contracts/shade/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ pub enum ContractError {
WasmHashNotSet = 18,
InvoiceAlreadyPaid = 19,
MerchantAccountNotSet = 20,
InvoiceExpired = 21,
}
2 changes: 2 additions & 0 deletions contracts/shade/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub trait ShadeTrait {
description: String,
amount: i128,
token: Address,
expires_at: Option<u64>,
) -> u64;
#[allow(clippy::too_many_arguments)]
fn create_invoice_signed(
Expand All @@ -35,6 +36,7 @@ pub trait ShadeTrait {
description: String,
amount: i128,
token: Address,
expires_at: Option<u64>,
nonce: BytesN<32>,
signature: BytesN<64>,
) -> u64;
Expand Down
5 changes: 4 additions & 1 deletion contracts/shade/src/shade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ impl ShadeTrait for Shade {
description: String,
amount: i128,
token: Address,
expires_at: Option<u64>,
) -> u64 {
pausable_component::assert_not_paused(&env);
invoice_component::create_invoice(&env, &merchant, &description, amount, &token)
invoice_component::create_invoice(&env, &merchant, &description, amount, &token, &expires_at)
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -117,6 +118,7 @@ impl ShadeTrait for Shade {
description: String,
amount: i128,
token: Address,
expires_at: Option<u64>,
nonce: BytesN<32>,
signature: BytesN<64>,
) -> u64 {
Expand All @@ -128,6 +130,7 @@ impl ShadeTrait for Shade {
&description,
amount,
&token,
&expires_at,
&nonce,
&signature,
)
Expand Down
1 change: 1 addition & 0 deletions contracts/shade/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod test_access_control;
pub mod test_admin_payment;
pub mod test_fees;
pub mod test_invoice;
pub mod test_invoice_expiry;
pub mod test_invoice_signed;
pub mod test_invoice_void;
pub mod test_merchant;
Expand Down
5 changes: 5 additions & 0 deletions contracts/shade/src/tests/test_admin_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn test_invoice_state_validation() {
&String::from_str(&env, "Test Invoice"),
&1000,
&token,
&None,
);

// Verify initial state
Expand All @@ -77,12 +78,14 @@ fn test_multiple_invoices_independent() {
&String::from_str(&env, "Invoice 1"),
&1000,
&token,
&None,
);
let id_2 = client.create_invoice(
&merchant,
&String::from_str(&env, "Invoice 2"),
&2000,
&token,
&None,
);

// Set second to Paid via storage manipulation
Expand Down Expand Up @@ -117,6 +120,7 @@ fn test_fee_preservation() {
&String::from_str(&env, "Test Invoice"),
&1000,
&token,
&None,
);

// Verify fee and invoice data
Expand Down Expand Up @@ -159,6 +163,7 @@ fn test_contract_pause_and_unpause() {
&String::from_str(&env, "Post-unpause invoice"),
&500,
&token,
&None,
);
assert!(invoice_id > 0);
}
Loading
Loading