From ea834c773dd2acf41eb5d1142038011d883f846b Mon Sep 17 00:00:00 2001 From: Rebel2169 <190786643+Rebel2169@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:29:01 +0000 Subject: [PATCH] Main Make this contract private for security and positive outcomes --- .../src/TestIncrement.sol | 2 +- .../smart-wallet-retainer/README.md | 58 +++++++++++++++++++ .../contracts/Retainer.sol | 25 ++++++++ .../contracts/SmartWallet.sol | 25 ++++++++ .../migrations/1_initial_migration.js | 4 ++ .../smart-wallet-retainer/package.json | 21 +++++++ .../test/smartWalletTest.js | 31 ++++++++++ .../smart-wallet-retainer/truffle-config.js | 14 +++++ keyManager.ts | 26 +++++++++ 9 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 internal-testnet/smart-wallet-retainer/README.md create mode 100644 internal-testnet/smart-wallet-retainer/contracts/Retainer.sol create mode 100644 internal-testnet/smart-wallet-retainer/contracts/SmartWallet.sol create mode 100644 internal-testnet/smart-wallet-retainer/migrations/1_initial_migration.js create mode 100644 internal-testnet/smart-wallet-retainer/package.json create mode 100644 internal-testnet/smart-wallet-retainer/test/smartWalletTest.js create mode 100644 internal-testnet/smart-wallet-retainer/truffle-config.js create mode 100644 keyManager.ts diff --git a/goerli-alpha/2023-06-08-test-aliased-owner/src/TestIncrement.sol b/goerli-alpha/2023-06-08-test-aliased-owner/src/TestIncrement.sol index bc1b1542..800aeece 100644 --- a/goerli-alpha/2023-06-08-test-aliased-owner/src/TestIncrement.sol +++ b/goerli-alpha/2023-06-08-test-aliased-owner/src/TestIncrement.sol @@ -13,7 +13,7 @@ contract TestIncrement { function increment() external { if (msg.sender != owner) { - revert("Only owner can increment"); + revert("can increment"); } number++; } diff --git a/internal-testnet/smart-wallet-retainer/README.md b/internal-testnet/smart-wallet-retainer/README.md new file mode 100644 index 00000000..5dab16dc --- /dev/null +++ b/internal-testnet/smart-wallet-retainer/README.md @@ -0,0 +1,58 @@ +# Smart Wallet Retainer + +This project implements a smart wallet retainer using Ethereum smart contracts. It consists of two main contracts: `SmartWallet` and `Retainer`, which work together to manage assets securely. + +## Project Structure + +``` +smart-wallet-retainer +├── contracts +│ ├── SmartWallet.sol # SmartWallet contract for managing assets +│ └── Retainer.sol # Retainer contract for asset retention logic +├── migrations +│ └── 1_initial_migration.js # Migration script for deploying contracts +├── test +│ └── smartWalletTest.js # Test cases for SmartWallet contract +├── truffle-config.js # Truffle configuration file +├── package.json # npm configuration file +└── README.md # Project documentation +``` + +## Installation + +1. Clone the repository: + ``` + git clone https://github.com/yourusername/smart-wallet-retainer.git + cd smart-wallet-retainer + ``` + +2. Install the dependencies: + ``` + npm install + ``` + +## Usage + +To deploy the contracts, run the following command: +``` +truffle migrate +``` + +To run the tests for the SmartWallet contract, use: +``` +truffle test +``` + +## Contracts Overview + +### SmartWallet + +The `SmartWallet` contract allows users to deposit and withdraw assets, as well as check their balance. It is designed to provide a secure way to manage digital assets. + +### Retainer + +The `Retainer` contract manages the relationship between the smart wallet and the assets it retains. It includes functions for asset management and retention logic. + +## License + +This project is licensed under the MIT License. See the LICENSE file for details. \ No newline at end of file diff --git a/internal-testnet/smart-wallet-retainer/contracts/Retainer.sol b/internal-testnet/smart-wallet-retainer/contracts/Retainer.sol new file mode 100644 index 00000000..b5e0236a --- /dev/null +++ b/internal-testnet/smart-wallet-retainer/contracts/Retainer.sol @@ -0,0 +1,25 @@ +pragma solidity ^0.8.0; + +import "./SmartWallet.sol"; + +contract Retainer { + SmartWallet private smartWallet; + + constructor(address _smartWalletAddress) { + smartWallet = SmartWallet(_smartWalletAddress); + } + + function retainAsset(address asset, uint256 amount) external { + require(smartWallet.balanceOf(asset) >= amount, "Insufficient balance in SmartWallet"); + smartWallet.transferFrom(msg.sender, address(this), asset, amount); + } + + function releaseAsset(address asset, uint256 amount) external { + require(msg.sender == address(smartWallet), "Only SmartWallet can release assets"); + smartWallet.transfer(asset, msg.sender, amount); + } + + function getRetainedAssets() external view returns (address[] memory) { + // Logic to return retained assets + } +} \ No newline at end of file diff --git a/internal-testnet/smart-wallet-retainer/contracts/SmartWallet.sol b/internal-testnet/smart-wallet-retainer/contracts/SmartWallet.sol new file mode 100644 index 00000000..5005b33c --- /dev/null +++ b/internal-testnet/smart-wallet-retainer/contracts/SmartWallet.sol @@ -0,0 +1,25 @@ +pragma solidity ^0.8.0; + +contract SmartWallet { + mapping(address => uint256) private balances; + + event Deposited(address indexed user, uint256 amount); + event Withdrawn(address indexed user, uint256 amount); + + function deposit() public payable { + require(msg.value > 0, "Deposit amount must be greater than zero"); + balances[msg.sender] += msg.value; + emit Deposited(msg.sender, msg.value); + } + + function withdraw(uint256 amount) public { + require(balances[msg.sender] >= amount, "Insufficient balance"); + balances[msg.sender] -= amount; + payable(msg.sender).transfer(amount); + emit Withdrawn(msg.sender, amount); + } + + function getBalance() public view returns (uint256) { + return balances[msg.sender]; + } +} \ No newline at end of file diff --git a/internal-testnet/smart-wallet-retainer/migrations/1_initial_migration.js b/internal-testnet/smart-wallet-retainer/migrations/1_initial_migration.js new file mode 100644 index 00000000..2b0b4c64 --- /dev/null +++ b/internal-testnet/smart-wallet-retainer/migrations/1_initial_migration.js @@ -0,0 +1,4 @@ +module.exports = function(deployer) { + deployer.deploy(SmartWallet); + deployer.deploy(Retainer); +}; \ No newline at end of file diff --git a/internal-testnet/smart-wallet-retainer/package.json b/internal-testnet/smart-wallet-retainer/package.json new file mode 100644 index 00000000..07aba45e --- /dev/null +++ b/internal-testnet/smart-wallet-retainer/package.json @@ -0,0 +1,21 @@ +{ + "name": "smart-wallet-retainer", + "version": "1.0.0", + "description": "A project for managing smart wallets and asset retention using smart contracts.", + "main": "index.js", + "scripts": { + "test": "truffle test", + "migrate": "truffle migrate", + "dev": "truffle develop" + }, + "dependencies": { + "truffle": "^5.4.0", + "web3": "^1.5.0" + }, + "devDependencies": { + "chai": "^4.3.0", + "mocha": "^8.3.0" + }, + "author": "Your Name", + "license": "MIT" +} \ No newline at end of file diff --git a/internal-testnet/smart-wallet-retainer/test/smartWalletTest.js b/internal-testnet/smart-wallet-retainer/test/smartWalletTest.js new file mode 100644 index 00000000..31b68a78 --- /dev/null +++ b/internal-testnet/smart-wallet-retainer/test/smartWalletTest.js @@ -0,0 +1,31 @@ +const SmartWallet = artifacts.require("SmartWallet"); + +contract("SmartWallet", accounts => { + let smartWallet; + + beforeEach(async () => { + smartWallet = await SmartWallet.new(); + }); + + it("should allow deposits", async () => { + const depositAmount = web3.utils.toWei("1", "ether"); + await smartWallet.deposit({ value: depositAmount }); + const balance = await smartWallet.getBalance(); + assert.equal(balance.toString(), depositAmount, "Balance should be equal to the deposited amount"); + }); + + it("should allow withdrawals", async () => { + const depositAmount = web3.utils.toWei("1", "ether"); + await smartWallet.deposit({ value: depositAmount }); + await smartWallet.withdraw(depositAmount); + const balance = await smartWallet.getBalance(); + assert.equal(balance.toString(), "0", "Balance should be zero after withdrawal"); + }); + + it("should return the correct balance", async () => { + const depositAmount = web3.utils.toWei("1", "ether"); + await smartWallet.deposit({ value: depositAmount }); + const balance = await smartWallet.getBalance(); + assert.equal(balance.toString(), depositAmount, "Balance should be equal to the deposited amount"); + }); +}); \ No newline at end of file diff --git a/internal-testnet/smart-wallet-retainer/truffle-config.js b/internal-testnet/smart-wallet-retainer/truffle-config.js new file mode 100644 index 00000000..15fd53a8 --- /dev/null +++ b/internal-testnet/smart-wallet-retainer/truffle-config.js @@ -0,0 +1,14 @@ +module.exports = { + networks: { + development: { + host: "127.0.0.1", + port: 7545, + network_id: "*" + } + }, + compilers: { + solc: { + version: "0.8.0" + } + } +}; \ No newline at end of file diff --git a/keyManager.ts b/keyManager.ts new file mode 100644 index 00000000..2b28e8f5 --- /dev/null +++ b/keyManager.ts @@ -0,0 +1,26 @@ + +import * as crypto from 'crypto'; + +class KeyManager { + private keys: Map; + + constructor() { + this.keys = new Map(); + } + + generateKey(keyName: string): string { + const key = crypto.randomBytes(32).toString('hex'); + this.keys.set(keyName, key); + return key; + } + + getKey(keyName: string): string | undefined { + return this.keys.get(keyName); + } + + deleteKey(keyName: string): boolean { + return this.keys.delete(keyName); + } +} + +export default KeyManager;