diff --git a/src/Wallet.sol b/src/Wallet.sol new file mode 100644 index 0000000..e1f2849 --- /dev/null +++ b/src/Wallet.sol @@ -0,0 +1,21 @@ +pragma solidity ^0.8.20; + +contract Wallet { + address payable public owner; + + constructor () payable { + owner = payable(msg.sender); + } + + receive() external payable {} + + function withdraw(uint256 _amount) external { + require(msg.sender == owner, "caller is not owner"); + payable(msg.sender).transfer(_amount); + } + + function setOwner(address _owner) external { + require(msg.sender == owner, "caller is not owner"); + owner = payable(_owner); + } +} diff --git a/test/Auth.t.sol b/test/Auth.t.sol new file mode 100644 index 0000000..d397558 --- /dev/null +++ b/test/Auth.t.sol @@ -0,0 +1,44 @@ +pragma solidity ^0.8.20; + +import "forge-std/Test.sol"; +import {Wallet} from "../src/Wallet.sol"; + +contract AuthTest is Test { + Wallet public wallet; + + function setUp() public { + wallet = new Wallet(); + } + + function testSetOwner() public { + console.log("wallet Owner Before:", wallet.owner()); + wallet.setOwner(address(1)); + console.log("wallet Owner After:", wallet.owner()); + assertEq(wallet.owner(),address(1)); + } + + function testFailNotOwner() public { + console.log("wallet Owner Before:", wallet.owner()); + vm.prank(address(1)); + wallet.setOwner(address(1)); + } + + function testFailSetOwnerAgain() public { + // msg.sender = address(this) + wallet.setOwner(address(1)); + + vm.startPrank(address(1)); + + // msg.sender = address(1) + wallet.setOwner(address(1)); + wallet.setOwner(address(1)); + wallet.setOwner(address(1)); + + vm.stopPrank(); + + // msg.sender = address(this) + + wallet.setOwner(address(1)); + + } +}