Skip to content
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
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions helix-contract/contracts/wallet/MultiSigWallet.sol
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(
Copy link
Author

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 check
meanwhile, storing less data can save us some gas gee

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);
}
}
Loading