Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve of 10_Wallet.sol #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 27 additions & 21 deletions solidity/10_Wallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,47 @@ pragma AbiHeader expire;
/// @author Tonlabs
contract Wallet {
/*
Exception codes:
100 - message sender is not a wallet owner.
101 - invalid transfer value.
* Exception codes:
* - 100: the message creator (tx signer, `msg.pubkey`) is not the wallet owner (`tvm.pubkey`)
* - 101: the wallet owner (stored in `tvm.pubkey`, part of state init) is not set up properly
*/

/// @dev Contract constructor.
/// @dev Contract constructor
constructor() {
// check that contract's public key is set
// Check the contract owner (`tvm.pubkey`) is set up properly (is not zero)
require(tvm.pubkey() != 0, 101);
// Check that message has signature (msg.pubkey() is not zero) and message is signed with the owner's private key
require(msg.pubkey() == tvm.pubkey(), 102);

// Check the message has a signature (`msg.pubkey` is not zero) and the message sender
// (tx signer, `msg.pubkey`) is the contract owner (`tvm.pubkey`)
require(msg.pubkey() == tvm.pubkey(), 100);

// Needed for initialization to be possible by an external inbound message
tvm.accept();
}


// Modifier that allows function to accept external call only if it was signed
// with contract owner's public key.
/// @dev Modifier that allows a function to accept an external inbound call only
/// if it was signed with the contract owner's public key
modifier checkOwnerAndAccept {
// Check that inbound message was signed with owner's public key.
// Runtime function that obtains sender's public key.
// Check that the inbound message was signed with the owner's public key
require(msg.pubkey() == tvm.pubkey(), 100);

// Runtime function that allows contract to process inbound messages spending
// its own resources (it's necessary if contract should process all inbound messages,
// not only those that carry value with them).
// Runtime function that allows the contract to process external messages spending
// its own resources (it's necessary if the contract should process external messages,
// not only those that carry value with them)
tvm.accept();

// Function body execution
_;
}

/// @dev Allows to transfer tons to the destination account.
/// @param dest Transfer target address.
/// @param value Nanoevers value to transfer.
/// @param bounce Flag that enables bounce message in case of target contract error.
/// @dev Allows transferring tons to the destination account
/// @param dest Transfer target address
/// @param value Nanoevers value to transfer
/// @param bounce Flag that enables a bounce message in case of the target contract error
function sendTransaction(address dest, uint128 value, bool bounce) public view checkOwnerAndAccept {
// Runtime function that allows to make a transfer with arbitrary settings.
dest.transfer(value, bounce, 0);
// Runtime function that allows making a transfer with arbitrary settings
// Flag is hardcoded to `2` in order to avoid
// [Tx Replay Attack](https://everscale.guide/smart_contracts/replay_protection)
dest.transfer(value, bounce, 2);
}
}