如何通过提取智能合约启用正确的用户识别?

发布于 2025-02-02 18:53:52 字数 1397 浏览 3 评论 0原文

有了这份智能合约,用户可以存入以太并撤回以太。合同应存储每个用户和存款,并随时撤回该金额。在代码中没有错误,但是不幸的是,取消提取的功能无法正常工作,实际上,以太词保留在合同中,也不会退还给所有者。

   contract vaultCoin {
        uint amount; 
        address user;
        uint amountDeposit;
    
        function vaultCoin() public {
            dev = msg.sender;                    
        }
    
        address public dev;                          
        address[] public users;                   
        uint[] public totalDeposited;                
    
        mapping(address => uint) balances;
        
        function sendToken() public payable {
            balances[msg.sender] = amount;
            require(msg.value > 0.001 ether); 
            
            user = msg.sender;               
            amountDeposit = msg.value;      
            
            users.push(user);                     
            totalDeposited.push(amountDeposit);     
        }
    
        function getUsers() public view returns (address[]) {    
            return users;
        }
    
        function getAmount() public view returns (uint[]) {
            return totalDeposited;
        }
    
        function retireMyCoins() public payable {
            require(msg.sender == user);
            require(msg.value == amountDeposit);
            balances[msg.sender] = 0; 
            msg.sender.transfer(balances[msg.sender]); 
        }
    }

With this smart contract, users can deposit ether and withdraw ether. The contract should store each user and the deposit made, allowing the amount to be withdrawn at any time. Within the code there are no errors, but unfortunately the function with which the withdrawal is made does not work properly, in fact the ethers remain within the contract and are not returned to the owner.

   contract vaultCoin {
        uint amount; 
        address user;
        uint amountDeposit;
    
        function vaultCoin() public {
            dev = msg.sender;                    
        }
    
        address public dev;                          
        address[] public users;                   
        uint[] public totalDeposited;                
    
        mapping(address => uint) balances;
        
        function sendToken() public payable {
            balances[msg.sender] = amount;
            require(msg.value > 0.001 ether); 
            
            user = msg.sender;               
            amountDeposit = msg.value;      
            
            users.push(user);                     
            totalDeposited.push(amountDeposit);     
        }
    
        function getUsers() public view returns (address[]) {    
            return users;
        }
    
        function getAmount() public view returns (uint[]) {
            return totalDeposited;
        }
    
        function retireMyCoins() public payable {
            require(msg.sender == user);
            require(msg.value == amountDeposit);
            balances[msg.sender] = 0; 
            msg.sender.transfer(balances[msg.sender]); 
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2025-02-09 18:53:52
balances[msg.sender] = 0;

您的代码首先将余额设置为0

msg.sender.transfer(balances[msg.sender]);

,然后将已经零的值传输给用户。


解决方案:

  1. 记录余额在存储变量中
  2. 记录存储值
  3. 从存储器变量转移
uint256 balance = balances[msg.sender];
balances[msg.sender] = 0; 
msg.sender.transfer(balance); 

值此中间步骤重新输入攻击。

balances[msg.sender] = 0;

Your code sets the balance to 0 at first

msg.sender.transfer(balances[msg.sender]);

And then transfers the already zeroed-out value to the user.


Solution:

  1. Record the balance in a memory variable
  2. Zero out the storage value
  3. Transfer the value from the memory variable
uint256 balance = balances[msg.sender];
balances[msg.sender] = 0; 
msg.sender.transfer(balance); 

This intermediate step prevents reentrancy attack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文