Skip to content
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
53 changes: 49 additions & 4 deletions contracts/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ impl AncoreAccount {
.unwrap_or(0))
}

/// Execute a transaction
/// Execute a transaction: validate nonce, perform cross-contract call, increment nonce.
///
/// # Security
/// - Must verify caller is owner or valid session key
/// - Must check and increment nonce
/// - Must validate signature
/// - Caller must be owner (session key auth not yet wired)
/// - `expected_nonce` must match current nonce (replay protection)
/// - Nonce is incremented only after a successful invocation
pub fn execute(
env: Env,
_to: Address,
Expand Down Expand Up @@ -202,4 +202,49 @@ mod test {
client.initialize(&owner);
client.initialize(&owner); // Should panic with contract error #1
}

#[test]
#[should_panic(expected = "Invalid nonce")]
fn test_execute_rejects_invalid_nonce() {
let env = Env::default();
let contract_id = env.register_contract(None, AncoreAccount);
let client = AncoreAccountClient::new(&env, &contract_id);

let owner = Address::generate(&env);
client.initialize(&owner);

env.mock_all_auths();

let to = Address::generate(&env);
let function = soroban_sdk::symbol_short!("transfer");
let args = Vec::new(&env);

// Current nonce is 0; passing expected_nonce = 1 should panic Invalid nonce
client.execute(&to, &function, &args, &1u64);
}

#[test]
fn test_execute_validates_nonce_then_increments() {
let env = Env::default();
let contract_id = env.register_contract(None, AncoreAccount);
let client = AncoreAccountClient::new(&env, &contract_id);

let owner = Address::generate(&env);
client.initialize(&owner);

assert_eq!(client.get_nonce(), 0);

env.mock_all_auths();

// Deploy a trivial contract that returns a Val so we can invoke it
let callee_id = env.register_contract(None, AncoreAccount);
let to = callee_id;
let function = soroban_sdk::symbol_short!("get_nonce");
let args = Vec::new(&env);

// Execute with expected_nonce = 0 (matches current); invokes get_nonce on callee
let _result = client.execute(&to, &function, &args, &0u64);

assert_eq!(client.get_nonce(), 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
{
"generators": {
"address": 3,
"nonce": 0
},
"auth": [
[],
[]
],
"ledger": {
"protocol_version": 21,
"sequence_number": 0,
"timestamp": 0,
"network_id": "0000000000000000000000000000000000000000000000000000000000000000",
"base_reserve": 0,
"min_persistent_entry_ttl": 4096,
"min_temp_entry_ttl": 16,
"max_entry_ttl": 6312000,
"ledger_entries": [
[
{
"contract_data": {
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": "ledger_key_contract_instance",
"durability": "persistent"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": "ledger_key_contract_instance",
"durability": "persistent",
"val": {
"contract_instance": {
"executable": {
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"storage": [
{
"key": {
"vec": [
{
"symbol": "Nonce"
}
]
},
"val": {
"u64": 0
}
},
{
"key": {
"vec": [
{
"symbol": "Owner"
}
]
},
"val": {
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
}
}
]
}
}
}
},
"ext": "v0"
},
4095
]
],
[
{
"contract_code": {
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_code": {
"ext": "v0",
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"code": ""
}
},
"ext": "v0"
},
4095
]
]
]
},
"events": [
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000001"
},
{
"symbol": "initialize"
}
],
"data": {
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "initialize"
}
],
"data": "void"
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000001"
},
{
"symbol": "execute"
}
],
"data": {
"vec": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M"
},
{
"symbol": "transfer"
},
{
"vec": []
},
{
"u64": 1
}
]
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "log"
}
],
"data": {
"vec": [
{
"string": "caught panic 'Invalid nonce' from contract function 'Symbol(execute)'"
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M"
},
{
"symbol": "transfer"
},
{
"vec": []
},
{
"u64": 1
}
]
}
}
}
},
"failed_call": true
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "error"
},
{
"error": {
"wasm_vm": "invalid_action"
}
}
],
"data": {
"string": "caught error from function"
}
}
}
},
"failed_call": true
},
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "error"
},
{
"error": {
"wasm_vm": "invalid_action"
}
}
],
"data": {
"vec": [
{
"string": "contract call failed"
},
{
"symbol": "execute"
},
{
"vec": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M"
},
{
"symbol": "transfer"
},
{
"vec": []
},
{
"u64": 1
}
]
}
]
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "error"
},
{
"error": {
"wasm_vm": "invalid_action"
}
}
],
"data": {
"string": "escalating error to panic"
}
}
}
},
"failed_call": false
}
]
}
Loading
Loading