Skip to content

Commit

Permalink
refactor: replace BytesLib with assembly block
Browse files Browse the repository at this point in the history
  • Loading branch information
b00ste committed Oct 12, 2023
1 parent abeac4f commit cb16004
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check warning on line 108 in contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol

View workflow job for this annotation

GitHub Actions / build

Avoid to use inline assembly. It is acceptable only in rare cases
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check warning on line 116 in contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol

View workflow job for this annotation

GitHub Actions / build

Avoid to use inline assembly. It is acceptable only in rare cases
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);
}

Expand Down

0 comments on commit cb16004

Please sign in to comment.