From afd83e59b8bf2b755e589562ab12b300aaf12601 Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Wed, 13 Jun 2018 11:53:22 +0200 Subject: Make LibBytes a library --- .../protocol/AssetProxy/MixinERC20Transfer.sol | 3 +- .../protocol/AssetProxy/MixinERC721Transfer.sol | 7 ++--- .../protocol/AssetProxyOwner/AssetProxyOwner.sol | 3 +- .../protocol/Exchange/MixinSignatureValidator.sol | 18 ++++++------ .../current/test/TestLibBytes/TestLibBytes.sol | 32 ++++++++++------------ .../contracts/current/utils/LibBytes/LibBytes.sol | 2 +- 6 files changed, 30 insertions(+), 35 deletions(-) (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC20Transfer.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC20Transfer.sol index 4af39a00b..34da13fcc 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC20Transfer.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC20Transfer.sol @@ -24,7 +24,6 @@ import "../../tokens/ERC20Token/IERC20Token.sol"; import "./libs/LibTransferErrors.sol"; contract MixinERC20Transfer is - LibBytes, LibTransferErrors { /// @dev Internal version of `transferFrom`. @@ -41,7 +40,7 @@ contract MixinERC20Transfer is internal { // Decode asset data. - address token = readAddress(assetData, 0); + address token = LibBytes.readAddress(assetData, 0); // Transfer tokens. // We do a raw call so we can check the success separate diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol index 6e3156e8a..028f4ed8a 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol @@ -24,7 +24,6 @@ import "../../tokens/ERC721Token/ERC721Token.sol"; import "./libs/LibTransferErrors.sol"; contract MixinERC721Transfer is - LibBytes, LibTransferErrors { /// @dev Internal version of `transferFrom`. @@ -78,10 +77,10 @@ contract MixinERC721Transfer is ) { // Decode asset data. - token = readAddress(assetData, 0); - tokenId = readUint256(assetData, 20); + token = LibBytes.readAddress(assetData, 0); + tokenId = LibBytes.readUint256(assetData, 20); if (assetData.length > 52) { - receiverData = readBytes(assetData, 52); + receiverData = LibBytes.readBytes(assetData, 52); } return ( diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxyOwner/AssetProxyOwner.sol b/packages/contracts/src/contracts/current/protocol/AssetProxyOwner/AssetProxyOwner.sol index 7f5f056b5..4d5e5f786 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxyOwner/AssetProxyOwner.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxyOwner/AssetProxyOwner.sol @@ -22,7 +22,6 @@ import "../../multisig/MultiSigWalletWithTimeLock.sol"; import "../../utils/LibBytes/LibBytes.sol"; contract AssetProxyOwner is - LibBytes, MultiSigWalletWithTimeLock { @@ -104,7 +103,7 @@ contract AssetProxyOwner is pure returns (bool) { - bytes4 first4Bytes = readFirst4(data); + bytes4 first4Bytes = LibBytes.readFirst4(data); require(REMOVE_AUTHORIZED_ADDRESS_SELECTOR == first4Bytes); return true; } diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol index 881d6e7b3..4a6bb5de5 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol @@ -26,7 +26,6 @@ import "./interfaces/IWallet.sol"; import "./interfaces/IValidator.sol"; contract MixinSignatureValidator is - LibBytes, LibExchangeErrors, MSignatureValidator, MTransactions @@ -102,7 +101,7 @@ contract MixinSignatureValidator is ); // Ensure signature is supported - uint8 signatureTypeRaw = uint8(popLastByte(signature)); + uint8 signatureTypeRaw = uint8(LibBytes.popLastByte(signature)); require( signatureTypeRaw < uint8(SignatureType.NSignatureTypes), SIGNATURE_UNSUPPORTED @@ -144,8 +143,8 @@ contract MixinSignatureValidator is LENGTH_65_REQUIRED ); v = uint8(signature[0]); - r = readBytes32(signature, 1); - s = readBytes32(signature, 33); + r = LibBytes.readBytes32(signature, 1); + s = LibBytes.readBytes32(signature, 33); recovered = ecrecover(hash, v, r, s); isValid = signerAddress == recovered; return isValid; @@ -157,8 +156,8 @@ contract MixinSignatureValidator is LENGTH_65_REQUIRED ); v = uint8(signature[0]); - r = readBytes32(signature, 1); - s = readBytes32(signature, 33); + r = LibBytes.readBytes32(signature, 1); + s = LibBytes.readBytes32(signature, 33); recovered = ecrecover( keccak256(abi.encodePacked(ETH_PERSONAL_MESSAGE, hash)), v, @@ -199,7 +198,8 @@ contract MixinSignatureValidator is // | 0x14 + x | 1 | Signature type is always "\x06" | } else if (signatureType == SignatureType.Validator) { // Pop last 20 bytes off of signature byte array. - address validatorAddress = popLast20Bytes(signature); + address validatorAddress = LibBytes.popLast20Bytes(signature); + // Ensure signer has approved validator. if (!allowedValidators[signerAddress][validatorAddress]) { return false; @@ -230,8 +230,8 @@ contract MixinSignatureValidator is LENGTH_65_REQUIRED ); v = uint8(signature[0]); - r = readBytes32(signature, 1); - s = readBytes32(signature, 33); + r = LibBytes.readBytes32(signature, 1); + s = LibBytes.readBytes32(signature, 33); recovered = ecrecover( keccak256(abi.encodePacked(TREZOR_PERSONAL_MESSAGE, hash)), v, diff --git a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol index d9b246b29..fc9d1e5c4 100644 --- a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol +++ b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol @@ -21,9 +21,7 @@ pragma experimental ABIEncoderV2; import "../../utils/LibBytes/LibBytes.sol"; -contract TestLibBytes is - LibBytes -{ +contract TestLibBytes { /// @dev Pops the last byte off of a byte array by modifying its length. /// @param b Byte array that will be modified. @@ -33,7 +31,7 @@ contract TestLibBytes is pure returns (bytes memory, bytes1 result) { - result = popLastByte(b); + result = LibBytes.popLastByte(b); return (b, result); } @@ -45,7 +43,7 @@ contract TestLibBytes is pure returns (bytes memory, address result) { - result = popLast20Bytes(b); + result = LibBytes.popLast20Bytes(b); return (b, result); } @@ -58,7 +56,7 @@ contract TestLibBytes is pure returns (bool equal) { - equal = areBytesEqual(lhs, rhs); + equal = LibBytes.areBytesEqual(lhs, rhs); return equal; } @@ -89,7 +87,7 @@ contract TestLibBytes is pure returns (address result) { - result = readAddress(b, index); + result = LibBytes.readAddress(b, index); return result; } @@ -106,7 +104,7 @@ contract TestLibBytes is pure returns (bytes memory) { - writeAddress(b, index, input); + LibBytes.writeAddress(b, index, input); return b; } @@ -122,7 +120,7 @@ contract TestLibBytes is pure returns (bytes32 result) { - result = readBytes32(b, index); + result = LibBytes.readBytes32(b, index); return result; } @@ -139,7 +137,7 @@ contract TestLibBytes is pure returns (bytes memory) { - writeBytes32(b, index, input); + LibBytes.writeBytes32(b, index, input); return b; } @@ -155,7 +153,7 @@ contract TestLibBytes is pure returns (uint256 result) { - result = readUint256(b, index); + result = LibBytes.readUint256(b, index); return result; } @@ -172,7 +170,7 @@ contract TestLibBytes is pure returns (bytes memory) { - writeUint256(b, index, input); + LibBytes.writeUint256(b, index, input); return b; } @@ -184,7 +182,7 @@ contract TestLibBytes is pure returns (bytes4 result) { - result = readFirst4(b); + result = LibBytes.readFirst4(b); return result; } @@ -200,7 +198,7 @@ contract TestLibBytes is pure returns (bytes memory result) { - result = readBytes(b, index); + result = LibBytes.readBytes(b, index); return result; } @@ -218,7 +216,7 @@ contract TestLibBytes is pure returns (bytes memory) { - writeBytes(b, index, input); + LibBytes.writeBytes(b, index, input); return b; } @@ -243,10 +241,10 @@ contract TestLibBytes is require(dest + length <= mem.length); // Get pointer to memory contents - uint256 offset = getMemAddress(mem) + 32; + uint256 offset = LibBytes.getMemAddress(mem) + 32; // Execute memCopy adjusted for memory array location - memCopy(offset + dest, offset + source, length); + LibBytes.memCopy(offset + dest, offset + source, length); // Return modified memory contents return mem; diff --git a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol index cb63e38e6..05cf53df5 100644 --- a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol +++ b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol @@ -18,7 +18,7 @@ pragma solidity ^0.4.24; -contract LibBytes { +library LibBytes { // Revert reasons string constant GREATER_THAN_ZERO_LENGTH_REQUIRED = "GREATER_THAN_ZERO_LENGTH_REQUIRED"; -- cgit