We have already learned about some of the capabilities of smart contracts in the Ethereum blockchain. In this post, I will describe in more detail one of their applications which is a wallet, whose task is to store and manage Ether. A wallet is a contract that can, for example, keep an eye on the daily limit of funds transfer, have several owners and require transactions to be approved by a certain number of them. We will create two types of wallets, both with one and multiple owners.
Keeping an eye on spending
One of the popular wallets is Ethereum Wallet. You can get acquainted with the contract code by browsing the Wallet.sol
file from the repository at this link. Its instance will be created using the Mist client, which I have used in previous posts. After launching the node and the digging process in the private blockchain, we can enable the Mist client application and create accounts for Agnieszka, Gregory and Peter, which will be used to observe the wallet's performance.
After running the digging process and the Ether obtained thanks to it, we can fund Agnieszka's account. We will use it to create a wallet with a single owner. In the Wallet contracts section, we select Add wallet contract. A window will appear where we can define the name of the wallet to be created, its main owner (that is, the account from which the contract will be created) and the type of wallet. We choose an account with one owner and create the contract.
When the contract is created on the screen we will see the generated address with a message to keep it in a safe place. Losing the address is, in a sense, losing the wallet in the maze of other contracts, so it is worth following the advice displayed.
The created wallet is visible in the Wallet contracts section. Viewing its details allows you to see information such as the current balance or the wallet owner's address.
Let's fund Agnieszka's wallet from her private account by transferring, for example, 50 Ether to the address of the contract. From now on, we will be able to transfer Ether from the contract, while the execution fee will be charged to the account from which we order the transaction. This allows us to clearly separate the cost of transactions from their value. By clicking on the Show contrcat info button on the wallet tab, we can see the features the contract provides. One of them is Set Daily Limit. It allows you to set a daily spending limit. The amount is specified in wei. So, setting the limit at 10 Ether, we need to enter 10000000000000000000 (10 and eighteen zeros).
Other functions of this contract allow:
- ChangeOwner - change one of the owners to another,
- Remove Owner - removing one of the owners of the portfolio,
- Is Owner - check if the address is an owner,
- Reset Spent Today - reset the counter of Ether spent,
- Add Owner - add a co-owner,
- Confirm - approve the transaction (if it requires approval by several owners, the argument will be the address of the ordered transaction),
- Execute - order the transaction,
- Revoke - remove transaction confirmation (the argument will be the confirmation transaction ID),
- Change Requirement - change the number of required transaction confirmations,
- Kill - delete the wallet, Ethery will be transferred to one of the owners.
Most of the functions used for a wallet with multiple owners will require the approval of a fixed minimum number of them.
Having set the limit at 10 Ether, let's try to make a transfer from the contract account. We can perform it in two ways: by calling the Execute function and by Transfer Ethers and Tokens. The second method is a graphical overlay for the Execute method and is always executed from the contract owner's account. After ordering a transfer of 6 Ether from Agnes' wallet to Peter's account address in the wallet tab, we will see information about the remaining withdrawal limit. It is worth trying to exceed it. This limit will be reset the next day.
Shared wallet
When creating a shared wallet, we specify the number of owners, the daily withdrawal limit not requiring confirmations, and the minimum number of owners to approve transactions above the set limit. Orders up to the limit can be ordered by any wallet owner. Orders add up, if the limit is exceeded approval by at least one (in our example) of the other owners will be required.
Displaying the information about the Fund created, we can see the current daily limit, the number of required confirmations and information about the portfolio owners.
When ordering a transaction with a value that exceeds the limit, right after it is kicked off, a notification will appear directing you to the Confirm or Revoke function, approving or revoking the transaction approval, respectively.
I encourage you to experiment with different wallet contract methods on both its single and multi-owner instances. In a private blockchain, we can order transactions at will without worrying about buying Ether.
Summary
The discussed contract is not the only wallet implementation in Ethereum. Anyone can implement their own, other popular wallets are also available. One of them, embedded in the Parity application, became the target of an attack in July. I will write about what its causes and consequences were in the next episode.