aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol
diff options
context:
space:
mode:
authorRemco Bloemen <remco@wicked.ventures>2018-02-10 09:43:35 +0800
committerAmir Bandeali <abandeali1@gmail.com>2018-04-21 04:56:16 +0800
commit2fe4e380d1824794b42b463c5fa589e6fdd1118a (patch)
tree573f416c3dde7b4f6efc09a9f1580fc18c83a4a7 /packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol
parenta7f47016980e7b4ad0ff6400cc741c4168ed64a3 (diff)
downloaddexon-0x-contracts-2fe4e380d1824794b42b463c5fa589e6fdd1118a.tar
dexon-0x-contracts-2fe4e380d1824794b42b463c5fa589e6fdd1118a.tar.gz
dexon-0x-contracts-2fe4e380d1824794b42b463c5fa589e6fdd1118a.tar.bz2
dexon-0x-contracts-2fe4e380d1824794b42b463c5fa589e6fdd1118a.tar.lz
dexon-0x-contracts-2fe4e380d1824794b42b463c5fa589e6fdd1118a.tar.xz
dexon-0x-contracts-2fe4e380d1824794b42b463c5fa589e6fdd1118a.tar.zst
dexon-0x-contracts-2fe4e380d1824794b42b463c5fa589e6fdd1118a.zip
Abstract signature to opaque bytearray
Diffstat (limited to 'packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol')
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol77
1 files changed, 77 insertions, 0 deletions
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol
new file mode 100644
index 000000000..919dbe312
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol
@@ -0,0 +1,77 @@
+/*
+
+ Copyright 2017 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.19;
+
+import "./mixins/MSignatureValidator.sol";
+
+/// @dev Provides MSignatureValidator
+contract MixinSignatureValidator is
+ MSignatureValidator
+{
+ enum SignatureType {
+ Invalid,
+ Ecrecover
+ }
+
+ function isValidSignature(
+ bytes32 hash,
+ address signer,
+ bytes signature)
+ public view
+ returns (bool isValid)
+ {
+ require(signature.length >= 1);
+
+ // Select signature type
+ SignatureType signatureType = SignatureType(uint8(signature[0]));
+ if (signatureType != SignatureType.Ecrecover) {
+ valid = false;
+ return;
+ }
+
+ // Verify using ecrecover
+ require(signature.length == 66);
+ uint8 v = uint8(signature[1]);
+ bytes32 r = get32(signature, 2);
+ bytes32 s = get32(signature, 34);
+ address recovered = ecrecover(
+ keccak256("\x19Ethereum Signed Message:\n32", hash),
+ v,
+ r,
+ s
+ );
+ isValid = signer == recovered;
+ }
+
+ function get32(bytes b, uint256 index)
+ private pure
+ returns (bytes32 result)
+ {
+ require(b.length >= index + 32);
+
+ // Arrays are prefixed by a 256 bit length parameter
+ index += 32;
+
+ // Read the bytes32 from array memory
+ assembly {
+ result := mload(add(b, index))
+ }
+ }
+
+}