From d8b44283a2247858c84fc2c319634c3118091a71 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sun, 22 Jul 2018 18:15:35 -0500 Subject: Move example contracts into new examples dir --- packages/contracts/compiler.json | 4 +- packages/contracts/package.json | 12 +- .../examples/ExchangeWrapper/ExchangeWrapper.sol | 100 +++++++++++++++ .../src/2.0.0/examples/Validator/Validator.sol | 56 +++++++++ .../contracts/src/2.0.0/examples/Wallet/Wallet.sol | 65 ++++++++++ .../src/2.0.0/examples/Whitelist/Whitelist.sol | 136 +++++++++++++++++++++ .../2.0.0/test/DummyERC20Token/DummyERC20Token.sol | 5 +- .../2.0.0/test/ExchangeWrapper/ExchangeWrapper.sol | 100 --------------- .../TestAssetProxyDispatcher.sol | 4 +- .../src/2.0.0/test/TestValidator/TestValidator.sol | 56 --------- .../src/2.0.0/test/TestWallet/TestWallet.sol | 65 ---------- .../src/2.0.0/test/Whitelist/Whitelist.sol | 136 --------------------- .../contracts/test/exchange/signature_validator.ts | 16 +-- packages/contracts/test/utils/artifacts.ts | 8 +- 14 files changed, 386 insertions(+), 377 deletions(-) create mode 100644 packages/contracts/src/2.0.0/examples/ExchangeWrapper/ExchangeWrapper.sol create mode 100644 packages/contracts/src/2.0.0/examples/Validator/Validator.sol create mode 100644 packages/contracts/src/2.0.0/examples/Wallet/Wallet.sol create mode 100644 packages/contracts/src/2.0.0/examples/Whitelist/Whitelist.sol delete mode 100644 packages/contracts/src/2.0.0/test/ExchangeWrapper/ExchangeWrapper.sol delete mode 100644 packages/contracts/src/2.0.0/test/TestValidator/TestValidator.sol delete mode 100644 packages/contracts/src/2.0.0/test/TestWallet/TestWallet.sol delete mode 100644 packages/contracts/src/2.0.0/test/Whitelist/Whitelist.sol (limited to 'packages/contracts') diff --git a/packages/contracts/compiler.json b/packages/contracts/compiler.json index 2a7f8bbb9..047a75b07 100644 --- a/packages/contracts/compiler.json +++ b/packages/contracts/compiler.json @@ -42,9 +42,9 @@ "TestLibBytes", "TestLibs", "TestSignatureValidator", - "TestValidator", - "TestWallet", "TokenRegistry", + "Validator", + "Wallet", "Whitelist", "WETH9", "ZRXToken" diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 066f9b41a..a6a7bdf6c 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -20,11 +20,14 @@ "test:coverage": "SOLIDITY_COVERAGE=true run-s build run_mocha coverage:report:text coverage:report:lcov", "test:profiler": "SOLIDITY_PROFILER=true run-s build run_mocha profiler:report:html", "test:trace": "SOLIDITY_REVERT_TRACE=true run-s build run_mocha", - "run_mocha": "mocha --require source-map-support/register --require make-promises-safe 'lib/test/**/*.js' --timeout 100000 --bail --exit", + "run_mocha": + "mocha --require source-map-support/register --require make-promises-safe 'lib/test/**/*.js' --timeout 100000 --bail --exit", "compile": "sol-compiler --contracts-dir src", "clean": "shx rm -rf lib generated_contract_wrappers", - "generate_contract_wrappers": "abi-gen --abis ${npm_package_config_abis} --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output generated_contract_wrappers --backend ethers", - "lint": "tslint --project . --exclude **/src/generated_contract_wrappers/**/* --exclude **/lib/**/* && yarn lint-contracts", + "generate_contract_wrappers": + "abi-gen --abis ${npm_package_config_abis} --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output generated_contract_wrappers --backend ethers", + "lint": + "tslint --project . --exclude **/src/generated_contract_wrappers/**/* --exclude **/lib/**/* && yarn lint-contracts", "coverage:report:text": "istanbul report text", "coverage:report:html": "istanbul report html && open coverage/index.html", "profiler:report:html": "istanbul report html && open coverage/index.html", @@ -33,7 +36,8 @@ "lint-contracts": "solhint src/2.0.0/**/**/**/**/*.sol" }, "config": { - "abis": "../migrations/artifacts/2.0.0/@(AssetProxyOwner|DummyERC20Token|DummyERC721Receiver|DummyERC721Token|ERC20Proxy|ERC721Proxy|Forwarder|Exchange|ExchangeWrapper|IAssetData|IAssetProxy|MixinAuthorizable|MultiSigWallet|MultiSigWalletWithTimeLock|TestAssetProxyOwner|TestAssetProxyDispatcher|TestLibBytes|TestLibs|TestSignatureValidator|TestValidator|TestWallet|TokenRegistry|Whitelist|WETH9|ZRXToken).json" + "abis": + "../migrations/artifacts/2.0.0/@(AssetProxyOwner|DummyERC20Token|DummyERC721Receiver|DummyERC721Token|ERC20Proxy|ERC721Proxy|Forwarder|Exchange|ExchangeWrapper|IAssetData|IAssetProxy|MixinAuthorizable|MultiSigWallet|MultiSigWalletWithTimeLock|TestAssetProxyOwner|TestAssetProxyDispatcher|TestLibBytes|TestLibs|TestSignatureValidator|Validator|Wallet|TokenRegistry|Whitelist|WETH9|ZRXToken).json" }, "repository": { "type": "git", diff --git a/packages/contracts/src/2.0.0/examples/ExchangeWrapper/ExchangeWrapper.sol b/packages/contracts/src/2.0.0/examples/ExchangeWrapper/ExchangeWrapper.sol new file mode 100644 index 000000000..2fa0e3c5e --- /dev/null +++ b/packages/contracts/src/2.0.0/examples/ExchangeWrapper/ExchangeWrapper.sol @@ -0,0 +1,100 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "../../protocol/Exchange/interfaces/IExchange.sol"; +import "../../protocol/Exchange/libs/LibOrder.sol"; + + +contract ExchangeWrapper { + + // Exchange contract. + // solhint-disable-next-line var-name-mixedcase + IExchange internal EXCHANGE; + + constructor (address _exchange) + public + { + EXCHANGE = IExchange(_exchange); + } + + /// @dev Cancels all orders created by sender with a salt less than or equal to the targetOrderEpoch + /// and senderAddress equal to this contract. + /// @param targetOrderEpoch Orders created with a salt less or equal to this value will be cancelled. + /// @param salt Arbitrary value to gaurantee uniqueness of 0x transaction hash. + /// @param makerSignature Proof that maker wishes to call this function with given params. + function cancelOrdersUpTo( + uint256 targetOrderEpoch, + uint256 salt, + bytes makerSignature + ) + external + { + address makerAddress = msg.sender; + + // Encode arguments into byte array. + bytes memory data = abi.encodeWithSelector( + EXCHANGE.cancelOrdersUpTo.selector, + targetOrderEpoch + ); + + // Call `cancelOrdersUpTo` via `executeTransaction`. + EXCHANGE.executeTransaction( + salt, + makerAddress, + data, + makerSignature + ); + } + + /// @dev Fills an order using `msg.sender` as the taker. + /// @param order Order struct containing order specifications. + /// @param takerAssetFillAmount Desired amount of takerAsset to sell. + /// @param salt Arbitrary value to gaurantee uniqueness of 0x transaction hash. + /// @param orderSignature Proof that order has been created by maker. + /// @param takerSignature Proof that taker wishes to call this function with given params. + function fillOrder( + LibOrder.Order memory order, + uint256 takerAssetFillAmount, + uint256 salt, + bytes memory orderSignature, + bytes memory takerSignature + ) + public + { + address takerAddress = msg.sender; + + // Encode arguments into byte array. + bytes memory data = abi.encodeWithSelector( + EXCHANGE.fillOrder.selector, + order, + takerAssetFillAmount, + orderSignature + ); + + // Call `fillOrder` via `executeTransaction`. + EXCHANGE.executeTransaction( + salt, + takerAddress, + data, + takerSignature + ); + } +} diff --git a/packages/contracts/src/2.0.0/examples/Validator/Validator.sol b/packages/contracts/src/2.0.0/examples/Validator/Validator.sol new file mode 100644 index 000000000..72ed528ba --- /dev/null +++ b/packages/contracts/src/2.0.0/examples/Validator/Validator.sol @@ -0,0 +1,56 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "../../protocol/Exchange/interfaces/IValidator.sol"; + + +contract Validator is + IValidator +{ + + // The single valid signer for this wallet. + // solhint-disable-next-line var-name-mixedcase + address internal VALID_SIGNER; + + /// @dev constructs a new `Validator` with a single valid signer. + /// @param validSigner The sole, valid signer. + constructor (address validSigner) public { + VALID_SIGNER = validSigner; + } + + /// @dev Verifies that a signature is valid. `signer` must match `VALID_SIGNER`. + /// @param hash Message hash that is signed. + /// @param signerAddress Address that should have signed the given hash. + /// @param signature Proof of signing. + /// @return Validity of signature. + // solhint-disable no-unused-vars + function isValidSignature( + bytes32 hash, + address signerAddress, + bytes signature + ) + external + view + returns (bool isValid) + { + return (signerAddress == VALID_SIGNER); + } + // solhint-enable no-unused-vars +} diff --git a/packages/contracts/src/2.0.0/examples/Wallet/Wallet.sol b/packages/contracts/src/2.0.0/examples/Wallet/Wallet.sol new file mode 100644 index 000000000..b75021a31 --- /dev/null +++ b/packages/contracts/src/2.0.0/examples/Wallet/Wallet.sol @@ -0,0 +1,65 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "../../protocol/Exchange/interfaces/IWallet.sol"; +import "../../utils/LibBytes/LibBytes.sol"; + + +contract Wallet is + IWallet +{ + using LibBytes for bytes; + + // The owner of this wallet. + // solhint-disable-next-line var-name-mixedcase + address internal WALLET_OWNER; + + /// @dev constructs a new `Wallet` with a single owner. + /// @param walletOwner The owner of this wallet. + constructor (address walletOwner) public { + WALLET_OWNER = walletOwner; + } + + /// @dev Validates an EIP712 signature. + /// The signer must match the owner of this wallet. + /// @param hash Message hash that is signed. + /// @param eip712Signature Proof of signing. + /// @return Validity of signature. + function isValidSignature( + bytes32 hash, + bytes eip712Signature + ) + external + view + returns (bool isValid) + { + require( + eip712Signature.length == 65, + "LENGTH_65_REQUIRED" + ); + + uint8 v = uint8(eip712Signature[0]); + bytes32 r = eip712Signature.readBytes32(1); + bytes32 s = eip712Signature.readBytes32(33); + address recoveredAddress = ecrecover(hash, v, r, s); + isValid = WALLET_OWNER == recoveredAddress; + return isValid; + } +} diff --git a/packages/contracts/src/2.0.0/examples/Whitelist/Whitelist.sol b/packages/contracts/src/2.0.0/examples/Whitelist/Whitelist.sol new file mode 100644 index 000000000..60cac26ea --- /dev/null +++ b/packages/contracts/src/2.0.0/examples/Whitelist/Whitelist.sol @@ -0,0 +1,136 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "../../protocol/Exchange/interfaces/IExchange.sol"; +import "../../protocol/Exchange/libs/LibOrder.sol"; +import "../../utils/Ownable/Ownable.sol"; + + +contract Whitelist is + Ownable +{ + + // Mapping of address => whitelist status. + mapping (address => bool) public isWhitelisted; + + // Exchange contract. + // solhint-disable var-name-mixedcase + IExchange internal EXCHANGE; + bytes internal TX_ORIGIN_SIGNATURE; + // solhint-enable var-name-mixedcase + + byte constant internal VALIDATOR_SIGNATURE_BYTE = "\x06"; + + constructor (address _exchange) + public + { + EXCHANGE = IExchange(_exchange); + TX_ORIGIN_SIGNATURE = abi.encodePacked(address(this), VALIDATOR_SIGNATURE_BYTE); + } + + /// @dev Adds or removes an address from the whitelist. + /// @param target Address to add or remove from whitelist. + /// @param isApproved Whitelist status to assign to address. + function updateWhitelistStatus( + address target, + bool isApproved + ) + external + onlyOwner + { + isWhitelisted[target] = isApproved; + } + + /// @dev Verifies signer is same as signer of current Ethereum transaction. + /// NOTE: This function can currently be used to validate signatures coming from outside of this contract. + /// Extra safety checks can be added for a production contract. + /// @param signerAddress Address that should have signed the given hash. + /// @param signature Proof of signing. + /// @return Validity of order signature. + // solhint-disable no-unused-vars + function isValidSignature( + bytes32 hash, + address signerAddress, + bytes signature + ) + external + view + returns (bool isValid) + { + // solhint-disable-next-line avoid-tx-origin + return signerAddress == tx.origin; + } + // solhint-enable no-unused-vars + + /// @dev Fills an order using `msg.sender` as the taker. + /// The transaction will revert if both the maker and taker are not whitelisted. + /// Orders should specify this contract as the `senderAddress` in order to gaurantee + /// that both maker and taker have been whitelisted. + /// @param order Order struct containing order specifications. + /// @param takerAssetFillAmount Desired amount of takerAsset to sell. + /// @param salt Arbitrary value to gaurantee uniqueness of 0x transaction hash. + /// @param orderSignature Proof that order has been created by maker. + function fillOrderIfWhitelisted( + LibOrder.Order memory order, + uint256 takerAssetFillAmount, + uint256 salt, + bytes memory orderSignature + ) + public + { + address takerAddress = msg.sender; + + // This contract must be the entry point for the transaction. + require( + // solhint-disable-next-line avoid-tx-origin + takerAddress == tx.origin, + "INVALID_SENDER" + ); + + // Check if maker is on the whitelist. + require( + isWhitelisted[order.makerAddress], + "MAKER_NOT_WHITELISTED" + ); + + // Check if taker is on the whitelist. + require( + isWhitelisted[takerAddress], + "TAKER_NOT_WHITELISTED" + ); + + // Encode arguments into byte array. + bytes memory data = abi.encodeWithSelector( + EXCHANGE.fillOrder.selector, + order, + takerAssetFillAmount, + orderSignature + ); + + // Call `fillOrder` via `executeTransaction`. + EXCHANGE.executeTransaction( + salt, + takerAddress, + data, + TX_ORIGIN_SIGNATURE + ); + } +} diff --git a/packages/contracts/src/2.0.0/test/DummyERC20Token/DummyERC20Token.sol b/packages/contracts/src/2.0.0/test/DummyERC20Token/DummyERC20Token.sol index 97801166a..9272b18a8 100644 --- a/packages/contracts/src/2.0.0/test/DummyERC20Token/DummyERC20Token.sol +++ b/packages/contracts/src/2.0.0/test/DummyERC20Token/DummyERC20Token.sol @@ -22,7 +22,10 @@ import "../Mintable/Mintable.sol"; import "../../utils/Ownable/Ownable.sol"; -contract DummyERC20Token is Mintable, Ownable { +contract DummyERC20Token is + Mintable, + Ownable +{ string public name; string public symbol; uint256 public decimals; diff --git a/packages/contracts/src/2.0.0/test/ExchangeWrapper/ExchangeWrapper.sol b/packages/contracts/src/2.0.0/test/ExchangeWrapper/ExchangeWrapper.sol deleted file mode 100644 index 2fa0e3c5e..000000000 --- a/packages/contracts/src/2.0.0/test/ExchangeWrapper/ExchangeWrapper.sol +++ /dev/null @@ -1,100 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; -pragma experimental ABIEncoderV2; - -import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; - - -contract ExchangeWrapper { - - // Exchange contract. - // solhint-disable-next-line var-name-mixedcase - IExchange internal EXCHANGE; - - constructor (address _exchange) - public - { - EXCHANGE = IExchange(_exchange); - } - - /// @dev Cancels all orders created by sender with a salt less than or equal to the targetOrderEpoch - /// and senderAddress equal to this contract. - /// @param targetOrderEpoch Orders created with a salt less or equal to this value will be cancelled. - /// @param salt Arbitrary value to gaurantee uniqueness of 0x transaction hash. - /// @param makerSignature Proof that maker wishes to call this function with given params. - function cancelOrdersUpTo( - uint256 targetOrderEpoch, - uint256 salt, - bytes makerSignature - ) - external - { - address makerAddress = msg.sender; - - // Encode arguments into byte array. - bytes memory data = abi.encodeWithSelector( - EXCHANGE.cancelOrdersUpTo.selector, - targetOrderEpoch - ); - - // Call `cancelOrdersUpTo` via `executeTransaction`. - EXCHANGE.executeTransaction( - salt, - makerAddress, - data, - makerSignature - ); - } - - /// @dev Fills an order using `msg.sender` as the taker. - /// @param order Order struct containing order specifications. - /// @param takerAssetFillAmount Desired amount of takerAsset to sell. - /// @param salt Arbitrary value to gaurantee uniqueness of 0x transaction hash. - /// @param orderSignature Proof that order has been created by maker. - /// @param takerSignature Proof that taker wishes to call this function with given params. - function fillOrder( - LibOrder.Order memory order, - uint256 takerAssetFillAmount, - uint256 salt, - bytes memory orderSignature, - bytes memory takerSignature - ) - public - { - address takerAddress = msg.sender; - - // Encode arguments into byte array. - bytes memory data = abi.encodeWithSelector( - EXCHANGE.fillOrder.selector, - order, - takerAssetFillAmount, - orderSignature - ); - - // Call `fillOrder` via `executeTransaction`. - EXCHANGE.executeTransaction( - salt, - takerAddress, - data, - takerSignature - ); - } -} diff --git a/packages/contracts/src/2.0.0/test/TestAssetProxyDispatcher/TestAssetProxyDispatcher.sol b/packages/contracts/src/2.0.0/test/TestAssetProxyDispatcher/TestAssetProxyDispatcher.sol index 07986f4bb..ad71fc9a1 100644 --- a/packages/contracts/src/2.0.0/test/TestAssetProxyDispatcher/TestAssetProxyDispatcher.sol +++ b/packages/contracts/src/2.0.0/test/TestAssetProxyDispatcher/TestAssetProxyDispatcher.sol @@ -21,7 +21,9 @@ pragma solidity 0.4.24; import "../../protocol/Exchange/MixinAssetProxyDispatcher.sol"; -contract TestAssetProxyDispatcher is MixinAssetProxyDispatcher { +contract TestAssetProxyDispatcher is + MixinAssetProxyDispatcher +{ function publicDispatchTransferFrom( bytes memory assetData, address from, diff --git a/packages/contracts/src/2.0.0/test/TestValidator/TestValidator.sol b/packages/contracts/src/2.0.0/test/TestValidator/TestValidator.sol deleted file mode 100644 index 6278aede0..000000000 --- a/packages/contracts/src/2.0.0/test/TestValidator/TestValidator.sol +++ /dev/null @@ -1,56 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - -import "../../protocol/Exchange/interfaces/IValidator.sol"; - - -contract TestValidator is - IValidator -{ - - // The single valid signer for this wallet. - // solhint-disable-next-line var-name-mixedcase - address internal VALID_SIGNER; - - /// @dev constructs a new `TestValidator` with a single valid signer. - /// @param validSigner The sole, valid signer. - constructor (address validSigner) public { - VALID_SIGNER = validSigner; - } - - /// @dev Verifies that a signature is valid. `signer` must match `VALID_SIGNER`. - /// @param hash Message hash that is signed. - /// @param signerAddress Address that should have signed the given hash. - /// @param signature Proof of signing. - /// @return Validity of signature. - // solhint-disable no-unused-vars - function isValidSignature( - bytes32 hash, - address signerAddress, - bytes signature - ) - external - view - returns (bool isValid) - { - return (signerAddress == VALID_SIGNER); - } - // solhint-enable no-unused-vars -} diff --git a/packages/contracts/src/2.0.0/test/TestWallet/TestWallet.sol b/packages/contracts/src/2.0.0/test/TestWallet/TestWallet.sol deleted file mode 100644 index 0415823e3..000000000 --- a/packages/contracts/src/2.0.0/test/TestWallet/TestWallet.sol +++ /dev/null @@ -1,65 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - -import "../../protocol/Exchange/interfaces/IWallet.sol"; -import "../../utils/LibBytes/LibBytes.sol"; - - -contract TestWallet is - IWallet -{ - using LibBytes for bytes; - - // The owner of this wallet. - // solhint-disable-next-line var-name-mixedcase - address internal WALLET_OWNER; - - /// @dev constructs a new `TestWallet` with a single owner. - /// @param walletOwner The owner of this wallet. - constructor (address walletOwner) public { - WALLET_OWNER = walletOwner; - } - - /// @dev Validates an EIP712 signature. - /// The signer must match the owner of this wallet. - /// @param hash Message hash that is signed. - /// @param eip712Signature Proof of signing. - /// @return Validity of signature. - function isValidSignature( - bytes32 hash, - bytes eip712Signature - ) - external - view - returns (bool isValid) - { - require( - eip712Signature.length == 65, - "LENGTH_65_REQUIRED" - ); - - uint8 v = uint8(eip712Signature[0]); - bytes32 r = eip712Signature.readBytes32(1); - bytes32 s = eip712Signature.readBytes32(33); - address recoveredAddress = ecrecover(hash, v, r, s); - isValid = WALLET_OWNER == recoveredAddress; - return isValid; - } -} diff --git a/packages/contracts/src/2.0.0/test/Whitelist/Whitelist.sol b/packages/contracts/src/2.0.0/test/Whitelist/Whitelist.sol deleted file mode 100644 index 60cac26ea..000000000 --- a/packages/contracts/src/2.0.0/test/Whitelist/Whitelist.sol +++ /dev/null @@ -1,136 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; -pragma experimental ABIEncoderV2; - -import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; -import "../../utils/Ownable/Ownable.sol"; - - -contract Whitelist is - Ownable -{ - - // Mapping of address => whitelist status. - mapping (address => bool) public isWhitelisted; - - // Exchange contract. - // solhint-disable var-name-mixedcase - IExchange internal EXCHANGE; - bytes internal TX_ORIGIN_SIGNATURE; - // solhint-enable var-name-mixedcase - - byte constant internal VALIDATOR_SIGNATURE_BYTE = "\x06"; - - constructor (address _exchange) - public - { - EXCHANGE = IExchange(_exchange); - TX_ORIGIN_SIGNATURE = abi.encodePacked(address(this), VALIDATOR_SIGNATURE_BYTE); - } - - /// @dev Adds or removes an address from the whitelist. - /// @param target Address to add or remove from whitelist. - /// @param isApproved Whitelist status to assign to address. - function updateWhitelistStatus( - address target, - bool isApproved - ) - external - onlyOwner - { - isWhitelisted[target] = isApproved; - } - - /// @dev Verifies signer is same as signer of current Ethereum transaction. - /// NOTE: This function can currently be used to validate signatures coming from outside of this contract. - /// Extra safety checks can be added for a production contract. - /// @param signerAddress Address that should have signed the given hash. - /// @param signature Proof of signing. - /// @return Validity of order signature. - // solhint-disable no-unused-vars - function isValidSignature( - bytes32 hash, - address signerAddress, - bytes signature - ) - external - view - returns (bool isValid) - { - // solhint-disable-next-line avoid-tx-origin - return signerAddress == tx.origin; - } - // solhint-enable no-unused-vars - - /// @dev Fills an order using `msg.sender` as the taker. - /// The transaction will revert if both the maker and taker are not whitelisted. - /// Orders should specify this contract as the `senderAddress` in order to gaurantee - /// that both maker and taker have been whitelisted. - /// @param order Order struct containing order specifications. - /// @param takerAssetFillAmount Desired amount of takerAsset to sell. - /// @param salt Arbitrary value to gaurantee uniqueness of 0x transaction hash. - /// @param orderSignature Proof that order has been created by maker. - function fillOrderIfWhitelisted( - LibOrder.Order memory order, - uint256 takerAssetFillAmount, - uint256 salt, - bytes memory orderSignature - ) - public - { - address takerAddress = msg.sender; - - // This contract must be the entry point for the transaction. - require( - // solhint-disable-next-line avoid-tx-origin - takerAddress == tx.origin, - "INVALID_SENDER" - ); - - // Check if maker is on the whitelist. - require( - isWhitelisted[order.makerAddress], - "MAKER_NOT_WHITELISTED" - ); - - // Check if taker is on the whitelist. - require( - isWhitelisted[takerAddress], - "TAKER_NOT_WHITELISTED" - ); - - // Encode arguments into byte array. - bytes memory data = abi.encodeWithSelector( - EXCHANGE.fillOrder.selector, - order, - takerAssetFillAmount, - orderSignature - ); - - // Call `fillOrder` via `executeTransaction`. - EXCHANGE.executeTransaction( - salt, - takerAddress, - data, - TX_ORIGIN_SIGNATURE - ); - } -} diff --git a/packages/contracts/test/exchange/signature_validator.ts b/packages/contracts/test/exchange/signature_validator.ts index ef154bf9b..f2bb42c75 100644 --- a/packages/contracts/test/exchange/signature_validator.ts +++ b/packages/contracts/test/exchange/signature_validator.ts @@ -9,8 +9,8 @@ import { TestSignatureValidatorContract, TestSignatureValidatorSignatureValidatorApprovalEventArgs, } from '../../generated_contract_wrappers/test_signature_validator'; -import { TestValidatorContract } from '../../generated_contract_wrappers/test_validator'; -import { TestWalletContract } from '../../generated_contract_wrappers/test_wallet'; +import { ValidatorContract } from '../../generated_contract_wrappers/validator'; +import { WalletContract } from '../../generated_contract_wrappers/wallet'; import { addressUtils } from '../utils/address_utils'; import { artifacts } from '../utils/artifacts'; import { expectContractCallFailed } from '../utils/assertions'; @@ -29,8 +29,8 @@ describe('MixinSignatureValidator', () => { let signedOrder: SignedOrder; let orderFactory: OrderFactory; let signatureValidator: TestSignatureValidatorContract; - let testWallet: TestWalletContract; - let testValidator: TestValidatorContract; + let testWallet: WalletContract; + let testValidator: ValidatorContract; let signerAddress: string; let signerPrivateKey: Buffer; let notSignerAddress: string; @@ -53,14 +53,14 @@ describe('MixinSignatureValidator', () => { provider, txDefaults, ); - testWallet = await TestWalletContract.deployFrom0xArtifactAsync( - artifacts.TestWallet, + testWallet = await WalletContract.deployFrom0xArtifactAsync( + artifacts.Wallet, provider, txDefaults, signerAddress, ); - testValidator = await TestValidatorContract.deployFrom0xArtifactAsync( - artifacts.TestValidator, + testValidator = await ValidatorContract.deployFrom0xArtifactAsync( + artifacts.Validator, provider, txDefaults, signerAddress, diff --git a/packages/contracts/test/utils/artifacts.ts b/packages/contracts/test/utils/artifacts.ts index d3f808218..854c1cd06 100644 --- a/packages/contracts/test/utils/artifacts.ts +++ b/packages/contracts/test/utils/artifacts.ts @@ -18,9 +18,9 @@ import * as TestAssetProxyOwner from '../../artifacts/TestAssetProxyOwner.json'; import * as TestLibBytes from '../../artifacts/TestLibBytes.json'; import * as TestLibs from '../../artifacts/TestLibs.json'; import * as TestSignatureValidator from '../../artifacts/TestSignatureValidator.json'; -import * as TestValidator from '../../artifacts/TestValidator.json'; -import * as TestWallet from '../../artifacts/TestWallet.json'; import * as TokenRegistry from '../../artifacts/TokenRegistry.json'; +import * as Validator from '../../artifacts/Validator.json'; +import * as Wallet from '../../artifacts/Wallet.json'; import * as EtherToken from '../../artifacts/WETH9.json'; import * as Whitelist from '../../artifacts/Whitelist.json'; import * as ZRX from '../../artifacts/ZRXToken.json'; @@ -45,8 +45,8 @@ export const artifacts = { TestLibBytes: (TestLibBytes as any) as ContractArtifact, TestLibs: (TestLibs as any) as ContractArtifact, TestSignatureValidator: (TestSignatureValidator as any) as ContractArtifact, - TestValidator: (TestValidator as any) as ContractArtifact, - TestWallet: (TestWallet as any) as ContractArtifact, + Validator: (Validator as any) as ContractArtifact, + Wallet: (Wallet as any) as ContractArtifact, TokenRegistry: (TokenRegistry as any) as ContractArtifact, Whitelist: (Whitelist as any) as ContractArtifact, ZRX: (ZRX as any) as ContractArtifact, -- cgit v1.2.3