From 2ea0b839d3f704ea85c992912b86db0cb49f80ea Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Wed, 13 Jun 2018 12:02:32 +0200 Subject: Using LibBytes for bytes --- .../protocol/AssetProxy/MixinERC20Transfer.sol | 4 +++- .../protocol/AssetProxy/MixinERC721Transfer.sol | 8 ++++--- .../protocol/AssetProxyOwner/AssetProxyOwner.sol | 3 ++- .../protocol/Exchange/MixinSignatureValidator.sol | 19 ++++++++------- .../protocol/Exchange/MixinWrapperFunctions.sol | 2 ++ .../current/test/TestLibBytes/TestLibBytes.sol | 28 ++++++++++++---------- 6 files changed, 38 insertions(+), 26 deletions(-) (limited to 'packages') diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC20Transfer.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC20Transfer.sol index 34da13fcc..0e7f3fc89 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC20Transfer.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC20Transfer.sol @@ -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 diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol index 028f4ed8a..2df9725b4 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol @@ -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 ( diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxyOwner/AssetProxyOwner.sol b/packages/contracts/src/contracts/current/protocol/AssetProxyOwner/AssetProxyOwner.sol index 4d5e5f786..261ce8f34 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxyOwner/AssetProxyOwner.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxyOwner/AssetProxyOwner.sol @@ -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; } diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol index 4a6bb5de5..cbb55bfce 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol @@ -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, diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol index 724f95518..87375d4bf 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol @@ -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. diff --git a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol index fc9d1e5c4..96020906c 100644 --- a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol +++ b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol @@ -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); -- cgit v1.2.3