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";
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

View File

@@ -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 (

View File

@@ -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;
}

View File

@@ -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,

View File

@@ -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;

View File

@@ -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";