Using LibBytes for bytes

This commit is contained in:
Remco Bloemen 2018-06-13 12:02:32 +02:00
parent afd83e59b8
commit 2ea0b839d3
6 changed files with 38 additions and 26 deletions

View File

@ -26,6 +26,8 @@ import "./libs/LibTransferErrors.sol";
contract MixinERC20Transfer is
LibTransferErrors
{
using LibBytes for bytes;
/// @dev Internal version of `transferFrom`.
/// @param assetData Encoded byte array.
/// @param from Address to transfer asset from.
@ -40,7 +42,7 @@ contract MixinERC20Transfer is
internal
{
// Decode asset data.
address token = LibBytes.readAddress(assetData, 0);
address token = assetData.readAddress(0);
// Transfer tokens.
// We do a raw call so we can check the success separate

View File

@ -26,6 +26,8 @@ import "./libs/LibTransferErrors.sol";
contract MixinERC721Transfer is
LibTransferErrors
{
using LibBytes for bytes;
/// @dev Internal version of `transferFrom`.
/// @param assetData Encoded byte array.
/// @param from Address to transfer asset from.
@ -77,10 +79,10 @@ contract MixinERC721Transfer is
)
{
// Decode asset data.
token = LibBytes.readAddress(assetData, 0);
tokenId = LibBytes.readUint256(assetData, 20);
token = assetData.readAddress(0);
tokenId = assetData.readUint256(20);
if (assetData.length > 52) {
receiverData = LibBytes.readBytes(assetData, 52);
receiverData = assetData.readBytes(52);
}
return (

View File

@ -24,6 +24,7 @@ import "../../utils/LibBytes/LibBytes.sol";
contract AssetProxyOwner is
MultiSigWalletWithTimeLock
{
using LibBytes for bytes;
event AssetProxyRegistration(address assetProxyContract, bool isRegistered);
@ -103,7 +104,7 @@ contract AssetProxyOwner is
pure
returns (bool)
{
bytes4 first4Bytes = LibBytes.readFirst4(data);
bytes4 first4Bytes = data.readFirst4();
require(REMOVE_AUTHORIZED_ADDRESS_SELECTOR == first4Bytes);
return true;
}

View File

@ -30,6 +30,8 @@ contract MixinSignatureValidator is
MSignatureValidator,
MTransactions
{
using LibBytes for bytes;
// Personal message headers
string constant ETH_PERSONAL_MESSAGE = "\x19Ethereum Signed Message:\n32";
string constant TREZOR_PERSONAL_MESSAGE = "\x19Ethereum Signed Message:\n\x20";
@ -101,7 +103,7 @@ contract MixinSignatureValidator is
);
// Ensure signature is supported
uint8 signatureTypeRaw = uint8(LibBytes.popLastByte(signature));
uint8 signatureTypeRaw = uint8(signature.popLastByte());
require(
signatureTypeRaw < uint8(SignatureType.NSignatureTypes),
SIGNATURE_UNSUPPORTED
@ -143,8 +145,8 @@ contract MixinSignatureValidator is
LENGTH_65_REQUIRED
);
v = uint8(signature[0]);
r = LibBytes.readBytes32(signature, 1);
s = LibBytes.readBytes32(signature, 33);
r = signature.readBytes32(1);
s = signature.readBytes32(33);
recovered = ecrecover(hash, v, r, s);
isValid = signerAddress == recovered;
return isValid;
@ -156,8 +158,8 @@ contract MixinSignatureValidator is
LENGTH_65_REQUIRED
);
v = uint8(signature[0]);
r = LibBytes.readBytes32(signature, 1);
s = LibBytes.readBytes32(signature, 33);
r = signature.readBytes32(1);
s = signature.readBytes32(33);
recovered = ecrecover(
keccak256(abi.encodePacked(ETH_PERSONAL_MESSAGE, hash)),
v,
@ -198,7 +200,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 = LibBytes.popLast20Bytes(signature);
address validatorAddress = signature.popLast20Bytes();
// Ensure signer has approved validator.
if (!allowedValidators[signerAddress][validatorAddress]) {
@ -230,8 +233,8 @@ contract MixinSignatureValidator is
LENGTH_65_REQUIRED
);
v = uint8(signature[0]);
r = LibBytes.readBytes32(signature, 1);
s = LibBytes.readBytes32(signature, 33);
r = signature.readBytes32(1);
s = signature.readBytes32(33);
recovered = ecrecover(
keccak256(abi.encodePacked(TREZOR_PERSONAL_MESSAGE, hash)),
v,

View File

@ -31,6 +31,8 @@ contract MixinWrapperFunctions is
LibExchangeErrors,
MExchangeCore
{
using LibBytes for bytes;
/// @dev Fills the input order. Reverts if exact takerAssetFillAmount not filled.
/// @param order Order struct containing order specifications.
/// @param takerAssetFillAmount Desired amount of takerAsset to sell.

View File

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