Skip to content

Commit

Permalink
Merge pull request #69 from ryzen-xp/Transaction-Signing
Browse files Browse the repository at this point in the history
Verify transaction signing
  • Loading branch information
aguilar1x authored Feb 27, 2025
2 parents 178fd90 + 15e7659 commit dcd8ebc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion StarShopContracts/implement-payment-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use soroban_sdk::{contract, contracterror, contractimpl, symbol_short, Address,

mod dispute;
mod refund;
mod transaction;
#[cfg(test)]
mod test;
mod transaction;

pub use dispute::{DisputeContract, DisputeContractClient, DisputeDecision, DisputeError};
pub use refund::{RefundContract, RefundContractClient, RefundError};
Expand Down
78 changes: 70 additions & 8 deletions StarShopContracts/implement-payment-contract/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use soroban_sdk::token::{StellarAssetClient as TokenAdmin, TokenClient};
use soroban_sdk::{
symbol_short,
testutils::{Address as _, AuthorizedFunction, AuthorizedInvocation, Events},
Address, Env, IntoVal, Symbol, vec, String,
vec, Address, Env, IntoVal, String, Symbol,
};

mod new_contract {
Expand Down Expand Up @@ -125,11 +125,11 @@ fn test_successful_upgrade() {

let contract_id = env.register(PaymentContract, ());
let client = PaymentContractClient::new(&env, &contract_id);

let admin = Address::generate(&env);

let new_wasm_hash = install_new_wasm(&env);

// Initialize first
client.initialize(&admin);
assert_eq!(client.get_admin(), admin);
Expand Down Expand Up @@ -192,20 +192,20 @@ fn test_succesful_transfer_admin() {

let contract_id = env.register(PaymentContract, ());
let client = PaymentContractClient::new(&env, &contract_id);

let admin = Address::generate(&env);
let new_admin = Address::generate(&env);

// Initialize contract
client.initialize(&admin);

// Test admin transfer
client.transfer_admin(&new_admin);

// Verify authorization
let auths = env.auths();
assert_eq!(auths.len(), 2);

// Verify new admin is set
assert_eq!(client.get_admin(), new_admin);
}
Expand Down Expand Up @@ -495,6 +495,37 @@ fn test_resolve_dispute_refund_buyer() {
&DisputeDecision::RefundBuyer,
);

// Verify signed transactions
assert_eq!(
env.auths(),
std::vec![(
arbitrator.clone(),
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
contract_id.clone(),
Symbol::new(&env, "resolve_dispute"),
(
token_contract.address().clone(),
arbitrator.clone(),
buyer.clone(),
seller.clone(),
refund_amount,
DisputeDecision::RefundBuyer as u32
)
.into_val(&env),
)),
sub_invocations: std::vec![AuthorizedInvocation {
function: AuthorizedFunction::Contract((
token_contract.address().clone(),
symbol_short!("transfer"),
(arbitrator.clone(), buyer.clone(), refund_amount).into_val(&env),
)),
sub_invocations: std::vec![],
}]
}
)]
);

// Check the balance after the transfer
let arbitrator_balance_after = token_client.balance(&arbitrator);
assert_eq!(arbitrator_balance_after, 0);
Expand Down Expand Up @@ -541,7 +572,7 @@ fn test_resolve_dispute_pay_seller() {
let seller_balance_before = token_client.balance(&seller);
assert_eq!(seller_balance_before, 0);

// Simulate resolving a dispute in favor of the buyer
// Simulate resolving a dispute in favor of the seller
client.resolve_dispute(
&token_address,
&arbitrator,
Expand All @@ -551,6 +582,37 @@ fn test_resolve_dispute_pay_seller() {
&DisputeDecision::PaySeller,
);

// Verify signed transactions
assert_eq!(
env.auths(),
std::vec![(
arbitrator.clone(),
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
contract_id.clone(),
Symbol::new(&env, "resolve_dispute"),
(
token_contract.address().clone(),
arbitrator.clone(),
buyer.clone(),
seller.clone(),
refund_amount,
DisputeDecision::PaySeller as u32
)
.into_val(&env),
)),
sub_invocations: std::vec![AuthorizedInvocation {
function: AuthorizedFunction::Contract((
token_contract.address().clone(),
symbol_short!("transfer"),
(arbitrator.clone(), seller.clone(), refund_amount).into_val(&env),
)),
sub_invocations: std::vec![],
}]
}
)]
);

// Check the balance after the transfer
let arbitrator_balance_after = token_client.balance(&arbitrator);
assert_eq!(arbitrator_balance_after, 0);
Expand Down

0 comments on commit dcd8ebc

Please sign in to comment.