Make LibBytes a library

This commit is contained in:
Remco Bloemen
2018-06-13 11:53:22 +02:00
parent 7f84049538
commit afd83e59b8
6 changed files with 30 additions and 35 deletions

View File

@@ -24,7 +24,6 @@ import "../../tokens/ERC20Token/IERC20Token.sol";
import "./libs/LibTransferErrors.sol"; import "./libs/LibTransferErrors.sol";
contract MixinERC20Transfer is contract MixinERC20Transfer is
LibBytes,
LibTransferErrors LibTransferErrors
{ {
/// @dev Internal version of `transferFrom`. /// @dev Internal version of `transferFrom`.
@@ -41,7 +40,7 @@ contract MixinERC20Transfer is
internal internal
{ {
// Decode asset data. // Decode asset data.
address token = readAddress(assetData, 0); address token = LibBytes.readAddress(assetData, 0);
// Transfer tokens. // Transfer tokens.
// We do a raw call so we can check the success separate // We do a raw call so we can check the success separate

View File

@@ -24,7 +24,6 @@ import "../../tokens/ERC721Token/ERC721Token.sol";
import "./libs/LibTransferErrors.sol"; import "./libs/LibTransferErrors.sol";
contract MixinERC721Transfer is contract MixinERC721Transfer is
LibBytes,
LibTransferErrors LibTransferErrors
{ {
/// @dev Internal version of `transferFrom`. /// @dev Internal version of `transferFrom`.
@@ -78,10 +77,10 @@ contract MixinERC721Transfer is
) )
{ {
// Decode asset data. // Decode asset data.
token = readAddress(assetData, 0); token = LibBytes.readAddress(assetData, 0);
tokenId = readUint256(assetData, 20); tokenId = LibBytes.readUint256(assetData, 20);
if (assetData.length > 52) { if (assetData.length > 52) {
receiverData = readBytes(assetData, 52); receiverData = LibBytes.readBytes(assetData, 52);
} }
return ( return (

View File

@@ -22,7 +22,6 @@ import "../../multisig/MultiSigWalletWithTimeLock.sol";
import "../../utils/LibBytes/LibBytes.sol"; import "../../utils/LibBytes/LibBytes.sol";
contract AssetProxyOwner is contract AssetProxyOwner is
LibBytes,
MultiSigWalletWithTimeLock MultiSigWalletWithTimeLock
{ {
@@ -104,7 +103,7 @@ contract AssetProxyOwner is
pure pure
returns (bool) returns (bool)
{ {
bytes4 first4Bytes = readFirst4(data); bytes4 first4Bytes = LibBytes.readFirst4(data);
require(REMOVE_AUTHORIZED_ADDRESS_SELECTOR == first4Bytes); require(REMOVE_AUTHORIZED_ADDRESS_SELECTOR == first4Bytes);
return true; return true;
} }

View File

@@ -26,7 +26,6 @@ import "./interfaces/IWallet.sol";
import "./interfaces/IValidator.sol"; import "./interfaces/IValidator.sol";
contract MixinSignatureValidator is contract MixinSignatureValidator is
LibBytes,
LibExchangeErrors, LibExchangeErrors,
MSignatureValidator, MSignatureValidator,
MTransactions MTransactions
@@ -102,7 +101,7 @@ contract MixinSignatureValidator is
); );
// Ensure signature is supported // Ensure signature is supported
uint8 signatureTypeRaw = uint8(popLastByte(signature)); uint8 signatureTypeRaw = uint8(LibBytes.popLastByte(signature));
require( require(
signatureTypeRaw < uint8(SignatureType.NSignatureTypes), signatureTypeRaw < uint8(SignatureType.NSignatureTypes),
SIGNATURE_UNSUPPORTED SIGNATURE_UNSUPPORTED
@@ -144,8 +143,8 @@ contract MixinSignatureValidator is
LENGTH_65_REQUIRED LENGTH_65_REQUIRED
); );
v = uint8(signature[0]); v = uint8(signature[0]);
r = readBytes32(signature, 1); r = LibBytes.readBytes32(signature, 1);
s = readBytes32(signature, 33); s = LibBytes.readBytes32(signature, 33);
recovered = ecrecover(hash, v, r, s); recovered = ecrecover(hash, v, r, s);
isValid = signerAddress == recovered; isValid = signerAddress == recovered;
return isValid; return isValid;
@@ -157,8 +156,8 @@ contract MixinSignatureValidator is
LENGTH_65_REQUIRED LENGTH_65_REQUIRED
); );
v = uint8(signature[0]); v = uint8(signature[0]);
r = readBytes32(signature, 1); r = LibBytes.readBytes32(signature, 1);
s = readBytes32(signature, 33); s = LibBytes.readBytes32(signature, 33);
recovered = ecrecover( recovered = ecrecover(
keccak256(abi.encodePacked(ETH_PERSONAL_MESSAGE, hash)), keccak256(abi.encodePacked(ETH_PERSONAL_MESSAGE, hash)),
v, v,
@@ -199,7 +198,8 @@ contract MixinSignatureValidator is
// | 0x14 + x | 1 | Signature type is always "\x06" | // | 0x14 + x | 1 | Signature type is always "\x06" |
} else if (signatureType == SignatureType.Validator) { } else if (signatureType == SignatureType.Validator) {
// Pop last 20 bytes off of signature byte array. // Pop last 20 bytes off of signature byte array.
address validatorAddress = popLast20Bytes(signature); address validatorAddress = LibBytes.popLast20Bytes(signature);
// Ensure signer has approved validator. // Ensure signer has approved validator.
if (!allowedValidators[signerAddress][validatorAddress]) { if (!allowedValidators[signerAddress][validatorAddress]) {
return false; return false;
@@ -230,8 +230,8 @@ contract MixinSignatureValidator is
LENGTH_65_REQUIRED LENGTH_65_REQUIRED
); );
v = uint8(signature[0]); v = uint8(signature[0]);
r = readBytes32(signature, 1); r = LibBytes.readBytes32(signature, 1);
s = readBytes32(signature, 33); s = LibBytes.readBytes32(signature, 33);
recovered = ecrecover( recovered = ecrecover(
keccak256(abi.encodePacked(TREZOR_PERSONAL_MESSAGE, hash)), keccak256(abi.encodePacked(TREZOR_PERSONAL_MESSAGE, hash)),
v, v,

View File

@@ -21,9 +21,7 @@ pragma experimental ABIEncoderV2;
import "../../utils/LibBytes/LibBytes.sol"; import "../../utils/LibBytes/LibBytes.sol";
contract TestLibBytes is contract TestLibBytes {
LibBytes
{
/// @dev Pops the last byte off of a byte array by modifying its length. /// @dev Pops the last byte off of a byte array by modifying its length.
/// @param b Byte array that will be modified. /// @param b Byte array that will be modified.
@@ -33,7 +31,7 @@ contract TestLibBytes is
pure pure
returns (bytes memory, bytes1 result) returns (bytes memory, bytes1 result)
{ {
result = popLastByte(b); result = LibBytes.popLastByte(b);
return (b, result); return (b, result);
} }
@@ -45,7 +43,7 @@ contract TestLibBytes is
pure pure
returns (bytes memory, address result) returns (bytes memory, address result)
{ {
result = popLast20Bytes(b); result = LibBytes.popLast20Bytes(b);
return (b, result); return (b, result);
} }
@@ -58,7 +56,7 @@ contract TestLibBytes is
pure pure
returns (bool equal) returns (bool equal)
{ {
equal = areBytesEqual(lhs, rhs); equal = LibBytes.areBytesEqual(lhs, rhs);
return equal; return equal;
} }
@@ -89,7 +87,7 @@ contract TestLibBytes is
pure pure
returns (address result) returns (address result)
{ {
result = readAddress(b, index); result = LibBytes.readAddress(b, index);
return result; return result;
} }
@@ -106,7 +104,7 @@ contract TestLibBytes is
pure pure
returns (bytes memory) returns (bytes memory)
{ {
writeAddress(b, index, input); LibBytes.writeAddress(b, index, input);
return b; return b;
} }
@@ -122,7 +120,7 @@ contract TestLibBytes is
pure pure
returns (bytes32 result) returns (bytes32 result)
{ {
result = readBytes32(b, index); result = LibBytes.readBytes32(b, index);
return result; return result;
} }
@@ -139,7 +137,7 @@ contract TestLibBytes is
pure pure
returns (bytes memory) returns (bytes memory)
{ {
writeBytes32(b, index, input); LibBytes.writeBytes32(b, index, input);
return b; return b;
} }
@@ -155,7 +153,7 @@ contract TestLibBytes is
pure pure
returns (uint256 result) returns (uint256 result)
{ {
result = readUint256(b, index); result = LibBytes.readUint256(b, index);
return result; return result;
} }
@@ -172,7 +170,7 @@ contract TestLibBytes is
pure pure
returns (bytes memory) returns (bytes memory)
{ {
writeUint256(b, index, input); LibBytes.writeUint256(b, index, input);
return b; return b;
} }
@@ -184,7 +182,7 @@ contract TestLibBytes is
pure pure
returns (bytes4 result) returns (bytes4 result)
{ {
result = readFirst4(b); result = LibBytes.readFirst4(b);
return result; return result;
} }
@@ -200,7 +198,7 @@ contract TestLibBytes is
pure pure
returns (bytes memory result) returns (bytes memory result)
{ {
result = readBytes(b, index); result = LibBytes.readBytes(b, index);
return result; return result;
} }
@@ -218,7 +216,7 @@ contract TestLibBytes is
pure pure
returns (bytes memory) returns (bytes memory)
{ {
writeBytes(b, index, input); LibBytes.writeBytes(b, index, input);
return b; return b;
} }
@@ -243,10 +241,10 @@ contract TestLibBytes is
require(dest + length <= mem.length); require(dest + length <= mem.length);
// Get pointer to memory contents // Get pointer to memory contents
uint256 offset = getMemAddress(mem) + 32; uint256 offset = LibBytes.getMemAddress(mem) + 32;
// Execute memCopy adjusted for memory array location // 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 modified memory contents
return mem; return mem;

View File

@@ -18,7 +18,7 @@
pragma solidity ^0.4.24; pragma solidity ^0.4.24;
contract LibBytes { library LibBytes {
// Revert reasons // Revert reasons
string constant GREATER_THAN_ZERO_LENGTH_REQUIRED = "GREATER_THAN_ZERO_LENGTH_REQUIRED"; string constant GREATER_THAN_ZERO_LENGTH_REQUIRED = "GREATER_THAN_ZERO_LENGTH_REQUIRED";