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 contract MixinERC20Transfer is
LibTransferErrors LibTransferErrors
{ {
using LibBytes for bytes;
/// @dev Internal version of `transferFrom`. /// @dev Internal version of `transferFrom`.
/// @param assetData Encoded byte array. /// @param assetData Encoded byte array.
/// @param from Address to transfer asset from. /// @param from Address to transfer asset from.
@ -40,7 +42,7 @@ contract MixinERC20Transfer is
internal internal
{ {
// Decode asset data. // Decode asset data.
address token = LibBytes.readAddress(assetData, 0); address token = assetData.readAddress(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

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

View File

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

View File

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

View File

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

View File

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