From cb16004443bb3a958e1ac9a1a316dab198a37aba Mon Sep 17 00:00:00 2001 From: b00ste Date: Thu, 12 Oct 2023 17:13:32 +0300 Subject: [PATCH] refactor: replace `BytesLib` with assembly block --- .../extensions/LSP8CompatibleERC721.sol | 27 ++++++++++++------- .../LSP8CompatibleERC721InitAbstract.sol | 27 ++++++++++++------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol b/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol index 6136f4cf1..dc82cb0e0 100644 --- a/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol +++ b/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol @@ -102,15 +102,24 @@ abstract contract LSP8CompatibleERC721 is uint256 /* tokenId */ ) public view virtual returns (string memory) { bytes memory data = _getData(_LSP4_METADATA_KEY); - - // offset = bytes4(hashSig) + bytes32(contentHash) -> 4 + 32 = 36 - uint256 offset = 36; - - bytes memory uriBytes = BytesLib.slice( - data, - offset, - data.length - offset - ); + uint256 requiredLength = data.length - 36; + + bytes memory uriBytes; + assembly { + uriBytes := mload(0x40) + mstore(uriBytes, requiredLength) + for { + let i := 0 + } lt(i, requiredLength) { + i := add(i, 0x20) + } { + mstore( + add(uriBytes, add(i, 0x20)), + mload(add(data, add(i, 68))) + ) + } + mstore(0x40, add(uriBytes, add(0x20, requiredLength))) + } return string(uriBytes); } diff --git a/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol b/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol index 851a0e361..60ef44e97 100644 --- a/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol +++ b/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol @@ -110,15 +110,24 @@ abstract contract LSP8CompatibleERC721InitAbstract is uint256 /* tokenId */ ) public view virtual returns (string memory) { bytes memory data = _getData(_LSP4_METADATA_KEY); - - // offset = bytes4(hashSig) + bytes32(contentHash) -> 4 + 32 = 36 - uint256 offset = 36; - - bytes memory uriBytes = BytesLib.slice( - data, - offset, - data.length - offset - ); + uint256 requiredLength = data.length - 36; + + bytes memory uriBytes; + assembly { + uriBytes := mload(0x40) + mstore(uriBytes, requiredLength) + for { + let i := 0 + } lt(i, requiredLength) { + i := add(i, 0x20) + } { + mstore( + add(uriBytes, add(i, 0x20)), + mload(add(data, add(i, 68))) + ) + } + mstore(0x40, add(uriBytes, add(0x20, requiredLength))) + } return string(uriBytes); }