Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
r0wdy1 committed Dec 12, 2023
1 parent 0c2fa8c commit 9c1be99
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 29 deletions.
17 changes: 13 additions & 4 deletions src/zkbob/manager/MPCOperatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "../../utils/Ownable.sol";
import "../utils/CustomABIDecoder.sol";
import "../../../lib/@openzeppelin/contracts/contracts/utils/cryptography/ECDSA.sol";

import "forge-std/console2.sol";

/**
* @title MPCOperatorManager
* @dev Implements a specialized policy which utilizes offchain verifiers' signatures.
Expand All @@ -25,20 +27,27 @@ contract MPCOperatorManager is
// new MutableOperatorManager(_operator, _feeReceiver, _operatorURI);
// }

constructor(address _operator, address _feeReceiver, string memory _operatorURI) Ownable() {
new MutableOperatorManager(_operator, _feeReceiver, _operatorURI);
}
constructor(
address _operator,
address _feeReceiver,
string memory _operatorURI
)
MutableOperatorManager(_operator, _feeReceiver, _operatorURI)
{}

function setSigners(address[] calldata _signers ) external onlyOwner {
signers = _signers;
}

function isOperator(address _addr) external view override returns (bool) {
function isOperator(address _addr) public view override returns (bool) {
if (!super.isOperator(_addr)) {
return false;
}
(uint8 count, bytes calldata signatures) = _mpc_signatures();
uint256 _signersCount = signers.length;
console2.log("_signersCount", _signersCount);
console2.log("count", count);

require(count == _signersCount, "MPCOperatorManager: bad quorum");
uint256 offset = 0;
for (uint256 index = 0; index < _signersCount; index++) {
Expand Down
2 changes: 1 addition & 1 deletion src/zkbob/manager/MutableOperatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract MutableOperatorManager is IOperatorManager, Ownable {
emit UpdateOperator(_operator, _feeReceiver, _operatorURI);
}

function isOperator(address _addr) external view override returns (bool) {
function isOperator(address _addr) public view virtual returns (bool) {
return operator == _addr || operator == address(0);
}

Expand Down
3 changes: 2 additions & 1 deletion src/zkbob/utils/CustomABIDecoder.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.15;

import "forge-std/console2.sol";
contract CustomABIDecoder {
uint256 constant transfer_nullifier_pos = 4;
uint256 constant transfer_nullifier_size = 32;
Expand Down Expand Up @@ -212,6 +212,7 @@ contract CustomABIDecoder {

function _mpc_signatures() internal pure returns (uint8 count, bytes calldata signatures) {
uint256 countPos = _mpc_signatures_pos();
console2.log("mpc_signatures_pos", mpc_signatures_pos());
count = uint8(_loaduint256(countPos+8-uint256_size));
uint256 length = count * sign_r_vs_size;
uint256 offset = countPos + 8;
Expand Down
70 changes: 47 additions & 23 deletions test/zkbob/manager/MPCOperatorManager.t.sol
Original file line number Diff line number Diff line change
@@ -1,39 +1,63 @@
pragma solidity ^0.8.15;
import "forge-std/Test.sol";
import "../../../src/zkbob/manager/MPCOperatorManager.sol";
import "../ZkBobPool.t.sol";
import "../../shared/Env.t.sol";

import "../../shared/ForkTests.t.sol";

contract MPCOperatorManagerTest is
AbstractZkBobPoolTest,
AbstractPolygonForkTest
{
address[] signers;

constructor() {
D = 1;
token = address(0xB0B195aEFA3650A6908f15CdaC7D92F8a5791B0B);
weth = address(0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270);
tempToken = address(0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174);
poolType = PoolType.BOB;
autoApproveQueue = false;
permitType = PermitType.BOBPermit;
denominator = 1_000_000_000;
precision = 1_000_000_000;
}

contract MPCOperatorManagerTest is Test {

// MPCOperatorManager _manager;


address bar = makeAddr("bar");
address signer1Addr;
uint256 signer1Key;

address[] signers ;
function testIsOperator() public {
address operator = makeAddr("operator");

function setUp() public {

(signer1Addr, signer1Key) = makeAddrAndKey("signer1");
// (address signer2Addr, uint256 signer2Key) = makeAddrAndKey("signer2");
// (address signer3Addr, uint256 signer3Key) = makeAddrAndKey("signer3");

MPCOperatorManager _manager = new MPCOperatorManager(
operator,
operator,
"_"
);
_manager.transferOwnership(operator);

(address signer1Addr, uint256 signer1Key) = makeAddrAndKey("signer1");
(address signer2Addr, uint256 signer2Key) = makeAddrAndKey("signer1");
signers.push(signer1Addr);
// signers.push(signer2Addr);
// signers.push(signer3Addr);
}
signers.push(signer2Addr);
vm.prank(operator);
_manager.setSigners(signers);

function testInit() public {
bytes memory data = _encodeDeposit(1 ether, 0.1 ether);
data = abi.encodePacked(data, uint8(2));
data = appendSignature(data, signer1Key);
data = appendSignature(data, signer2Key);

MPCOperatorManager _manager = new MPCOperatorManager(signer1Addr, signer1Addr, "");
assert(_manager.isOperator(operator));

// _manager.setSigners(_signers);
//
}

function testIsOperator() public {
// vm.prank(foo);

function appendSignature(bytes memory data, uint256 key ) internal returns ( bytes memory updated ) {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(
key,
keccak256(data)
);
updated = abi.encodePacked(data, r, uint256(s) + (v == 28 ? (1 << 255) : 0));
}
}

0 comments on commit 9c1be99

Please sign in to comment.