-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
116 lines (95 loc) · 3.55 KB
/
Copy pathlib.rs
File metadata and controls
116 lines (95 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! SECURE: Self-Transfer Guard
//!
//! A secure mirror of the VulnerableToken contract. Identical API (`mint`,
//! `balance`, `transfer`) but `transfer` asserts `from != to` as its very
//! first operation — before `require_auth` and before any storage access —
//! preventing the storage-slot collision that inflates balances in the
//! vulnerable version.
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, Address, Env};
#[contracttype]
pub enum DataKey {
Balance(Address),
}
fn get_balance(env: &Env, account: &Address) -> i128 {
env.storage()
.persistent()
.get(&DataKey::Balance(account.clone()))
.unwrap_or(0)
}
fn set_balance(env: &Env, account: &Address, amount: i128) {
env.storage()
.persistent()
.set(&DataKey::Balance(account.clone()), &amount);
}
#[contract]
pub struct SecureToken;
#[contractimpl]
impl SecureToken {
pub fn mint(env: Env, to: Address, amount: i128) {
let current = get_balance(&env, &to);
set_balance(&env, &to, current.checked_add(amount).expect("mint overflow"));
}
pub fn balance(env: Env, account: Address) -> i128 {
get_balance(&env, &account)
}
pub fn transfer(env: Env, from: Address, to: Address, amount: i128) {
// ✅ Guard fires before require_auth and any storage access
assert!(from != to, "self-transfer not allowed");
from.require_auth();
let from_balance = get_balance(&env, &from);
let to_balance = get_balance(&env, &to);
set_balance(&env, &from, from_balance.checked_sub(amount).expect("transfer underflow"));
set_balance(&env, &to, to_balance.checked_add(amount).expect("transfer overflow"));
}
}
#[cfg(test)]
mod tests {
use super::*;
use soroban_sdk::{testutils::Address as _, Address, Env};
#[test]
fn test_normal_transfer() {
let env = Env::default();
let contract_id = env.register_contract(None, SecureToken);
let client = SecureTokenClient::new(&env, &contract_id);
let alice = Address::generate(&env);
let bob = Address::generate(&env);
env.mock_all_auths();
client.mint(&alice, &500);
client.mint(&bob, &100);
client.transfer(&alice, &bob, &200);
assert_eq!(client.balance(&alice), 300);
assert_eq!(client.balance(&bob), 300);
}
#[test]
#[should_panic(expected = "self-transfer not allowed")]
fn test_self_transfer_rejected() {
let env = Env::default();
let contract_id = env.register_contract(None, SecureToken);
let client = SecureTokenClient::new(&env, &contract_id);
let alice = Address::generate(&env);
env.mock_all_auths();
client.mint(&alice, &500);
client.transfer(&alice, &alice, &100);
}
#[test]
#[should_panic]
fn test_transfer_requires_auth() {
let env = Env::default();
let contract_id = env.register_contract(None, SecureToken);
let client = SecureTokenClient::new(&env, &contract_id);
let alice = Address::generate(&env);
let bob = Address::generate(&env);
client.mint(&alice, &500);
// No mock_all_auths — should panic on require_auth
client.transfer(&alice, &bob, &100);
}
#[test]
fn test_balance_defaults_to_zero() {
let env = Env::default();
let contract_id = env.register_contract(None, SecureToken);
let client = SecureTokenClient::new(&env, &contract_id);
let fresh = Address::generate(&env);
assert_eq!(client.balance(&fresh), 0);
}
}