-
Notifications
You must be signed in to change notification settings - Fork 4
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
[WIP]Create MultiSigWallet.sol #76
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.17; | ||
|
||
import {ECDSA} from "@openzeppelin/[email protected]/utils/cryptography/ECDSA.sol"; | ||
|
||
contract MultiSigWallet { | ||
event ExecutionResult(bytes32 indexed hash, bool result); | ||
|
||
address[] public owners; | ||
uint64 public threshold; | ||
mapping(address => bool) public isOwner; | ||
mapping(bytes32 => bool) public doneOf; | ||
mapping(bytes32 => address[]) public transactionSigners; | ||
|
||
receive() external payable {} | ||
|
||
constructor(address[] memory signers, uint64 _threshold) { | ||
require(signers.length > 0, "owners required"); | ||
require( | ||
_threshold > 0 && _threshold <= signers.length, | ||
"invalid threshold" | ||
); | ||
|
||
for (uint256 i = 0; i < signers.length; i++) { | ||
address owner = signers[i]; | ||
require(owner != address(0), "invalid owner"); | ||
require(!isOwner[owner], "owner not unique"); | ||
|
||
isOwner[owner] = true; | ||
owners.push(owner); | ||
} | ||
|
||
threshold = _threshold; | ||
} | ||
|
||
function proposeOrApproveTransaction( | ||
address to, | ||
uint256 value, | ||
uint256 expiration, | ||
bytes memory data, | ||
bytes calldata signature | ||
) external { | ||
require(isOwner[msg.sender], "not an owner"); | ||
bytes memory txData = abi.encode( | ||
block.chainid, | ||
address(this), | ||
to, | ||
value, | ||
expiration, | ||
data | ||
); | ||
bytes32 hash = keccak256(txData); | ||
|
||
require( | ||
transactionSigners[hash].length < threshold, | ||
"threshold reached" | ||
); | ||
require( | ||
ECDSA.recover(ECDSA.toEthSignedMessageHash(hash), signature) == | ||
msg.sender, | ||
"invalid signature" | ||
); | ||
transactionSigners[hash].push(msg.sender); | ||
|
||
if (transactionSigners[hash].length == threshold) { | ||
bytes memory signatures = new bytes(threshold * 65); | ||
for (uint256 i = 0; i < threshold; i++) { | ||
bytes memory sig = abi.encodePacked( | ||
transactionSigners[hash][i] | ||
); | ||
for (uint256 j = 0; j < 65; j++) { | ||
signatures[i * 65 + j] = sig[j]; | ||
} | ||
} | ||
|
||
exec(to, value, expiration, data); | ||
} | ||
} | ||
|
||
function exec( | ||
address to, | ||
uint256 value, | ||
uint256 expiration, | ||
bytes memory data | ||
) internal returns (bool success) { | ||
bytes memory txData = abi.encode( | ||
block.chainid, | ||
address(this), | ||
to, | ||
value, | ||
expiration, | ||
data | ||
); | ||
bytes32 hash = keccak256(txData); | ||
(success, ) = to.call{value: value}(data); | ||
doneOf[hash] = true; | ||
emit ExecutionResult(hash, success); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think of let's directly validate signature here without storing signatures anywhere?
it looks safe to me, only signer with private keys can successfully pass this
require
checkmeanwhile, storing less data can save us some gas gee