-
我写了个coin练习,测试也通过了。突然恍惚了,当我向其他人地址存钱的时候,难道不需要对方的签名就能够操作对方的balance么? fun deposit(addr: address, check: Coin) acquires Balance{ |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
如果是向某一个地址转coin,转出方必须要签名才行,所以你可以看到 coin::tranfer 函数的调用必须要 &signer: /// Transfers `amount` of coins `CoinType` from `from` to `to`.
public entry fun transfer<CoinType>(
from: &signer,
to: address,
amount: u64,
) acquires CoinStore {
// 省略
} 对于接收方,不用接收方签名,有地址就可以,但是需要接受方提前注册过 coin 才行,所以你可以看到 deposit 的函数签名是用的是address,并且判断是否对coin进行了注册: /// Deposit the coin balance into the recipient's account and emit an event.
public fun deposit<CoinType>(account_addr: address, coin: Coin<CoinType>) acquires CoinStore {
assert!(
is_account_registered<CoinType>(account_addr),
error::not_found(ECOIN_STORE_NOT_PUBLISHED),
); |
Beta Was this translation helpful? Give feedback.
-
Aptos上的 public fun register<CoinType>(account: &signer) {
let account_addr = signer::address_of(account);
// Short-circuit and do nothing if account is already registered for CoinType.
if (is_account_registered<CoinType>(account_addr)) {
return
};
account::register_coin<CoinType>(account_addr);
let coin_store = CoinStore<CoinType> {
coin: Coin { value: 0 },
frozen: false,
deposit_events: account::new_event_handle<DepositEvent>(account),
withdraw_events: account::new_event_handle<WithdrawEvent>(account),
};
move_to(account, coin_store);
}
/// Transfers `amount` of coins `CoinType` from `from` to `to`.
public entry fun transfer<CoinType>(
from: &signer,
to: address,
amount: u64,
) acquires CoinStore {
let coin = withdraw<CoinType>(from, amount);
deposit(to, coin);
}
/// Withdraw specifed `amount` of coin `CoinType` from the signing account.
public fun withdraw<CoinType>(
account: &signer,
amount: u64,
): Coin<CoinType> acquires CoinStore {
let account_addr = signer::address_of(account);
assert!(
is_account_registered<CoinType>(account_addr),
error::not_found(ECOIN_STORE_NOT_PUBLISHED),
);
let coin_store = borrow_global_mut<CoinStore<CoinType>>(account_addr);
assert!(
!coin_store.frozen,
error::permission_denied(EFROZEN),
);
event::emit_event<WithdrawEvent>(
&mut coin_store.withdraw_events,
WithdrawEvent { amount },
);
extract(&mut coin_store.coin, amount)
}
/// Deposit the coin balance into the recipient's account and emit an event.
public fun deposit<CoinType>(account_addr: address, coin: Coin<CoinType>) acquires CoinStore {
assert!(
is_account_registered<CoinType>(account_addr),
error::not_found(ECOIN_STORE_NOT_PUBLISHED),
);
let coin_store = borrow_global_mut<CoinStore<CoinType>>(account_addr);
assert!(
!coin_store.frozen,
error::permission_denied(EFROZEN),
);
event::emit_event<DepositEvent>(
&mut coin_store.deposit_events,
DepositEvent { amount: coin.value },
);
merge(&mut coin_store.coin, coin);
} |
Beta Was this translation helpful? Give feedback.
Aptos上的
coin::transfer
都必须在接收者地址调用了coin::register
之后,才能被转入。所以不是“不需要对方的签名就能够操作对方的balance”,而是接收方事先注册过了当前转账的Coin,所以可以接收他人的转入,
但转出是必须需要签名的。
既遵守了"资源"的概念,也避免了被空投一些山寨币或广告币。
代码如下