-
Notifications
You must be signed in to change notification settings - Fork 25
Add URI resolution for Ethscriptions in ERC721 Collection #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.24; | ||
|
|
||
| import "./TestSetup.sol"; | ||
| import {LibString} from "solady/utils/LibString.sol"; | ||
|
|
||
| contract CollectionURIResolutionTest is TestSetup { | ||
| using LibString for *; | ||
| bytes32 constant COLLECTION_TX_HASH = keccak256("collection_uri_test"); | ||
| bytes32 constant IMAGE_ETSC_TX_HASH = keccak256("image_ethscription"); | ||
|
|
||
| address alice = makeAddr("alice"); | ||
|
|
||
| function setUp() public override { | ||
| super.setUp(); | ||
| } | ||
|
|
||
| function test_RegularHTTPURIPassesThrough() public { | ||
| // Create collection with regular HTTP URI | ||
| string memory regularUri = "https://example.com/logo.png"; | ||
|
|
||
| bytes32 collectionId = _createCollectionWithLogo(regularUri); | ||
|
|
||
| // Get collection metadata | ||
| ERC721EthscriptionsCollectionManager.CollectionMetadata memory metadata = | ||
| collectionsHandler.getCollection(collectionId); | ||
|
|
||
| assertEq(metadata.logoImageUri, regularUri, "Should preserve regular URI"); | ||
|
|
||
| // contractURI should also pass it through | ||
| address collectionAddr = metadata.collectionContract; | ||
| ERC721EthscriptionsCollection collection = ERC721EthscriptionsCollection(collectionAddr); | ||
| string memory contractUri = collection.contractURI(); | ||
|
|
||
| assertTrue(bytes(contractUri).length > 0, "Should have contractURI"); | ||
| assertTrue(contractUri.contains(regularUri), "Should contain original URI"); | ||
| } | ||
|
|
||
| function test_DataURIPassesThrough() public { | ||
| // Create collection with data URI | ||
| string memory dataUri = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAiIGhlaWdodD0iMTAiPjwvc3ZnPg=="; | ||
|
|
||
| bytes32 collectionId = _createCollectionWithLogo(dataUri); | ||
|
|
||
| ERC721EthscriptionsCollectionManager.CollectionMetadata memory metadata = | ||
| collectionsHandler.getCollection(collectionId); | ||
|
|
||
| assertEq(metadata.logoImageUri, dataUri, "Should preserve data URI"); | ||
| } | ||
|
|
||
| function test_EthscriptionReferenceResolvesToMediaURI() public { | ||
| // First create an ethscription with image content | ||
| string memory imageContent = "data:image/png;base64,iVBORw0KGgo="; | ||
|
|
||
| Ethscriptions.CreateEthscriptionParams memory imageParams = Ethscriptions.CreateEthscriptionParams({ | ||
| ethscriptionId: IMAGE_ETSC_TX_HASH, | ||
| contentUriSha: sha256(bytes(imageContent)), | ||
| initialOwner: alice, | ||
| content: bytes(imageContent), | ||
| mimetype: "image/png", | ||
| esip6: false, | ||
| protocolParams: Ethscriptions.ProtocolParams({ | ||
| protocolName: "", | ||
| operation: "", | ||
| data: "" | ||
| }) | ||
| }); | ||
|
|
||
| vm.prank(alice); | ||
| ethscriptions.createEthscription(imageParams); | ||
|
|
||
| // Create collection with esc:// reference to the image | ||
| string memory escUri = string.concat( | ||
| "esc://ethscriptions/", | ||
| uint256(IMAGE_ETSC_TX_HASH).toHexString(), | ||
| "/data" | ||
| ); | ||
|
|
||
| bytes32 collectionId = _createCollectionWithLogo(escUri); | ||
|
|
||
| // Get collection and check contractURI resolves the reference | ||
| ERC721EthscriptionsCollectionManager.CollectionMetadata memory metadata = | ||
| collectionsHandler.getCollection(collectionId); | ||
|
|
||
| // Stored value should be the esc:// URI | ||
| assertEq(metadata.logoImageUri, escUri, "Should store esc:// URI"); | ||
|
|
||
| // contractURI should resolve it to the media URI | ||
| address collectionAddr = metadata.collectionContract; | ||
| ERC721EthscriptionsCollection collection = ERC721EthscriptionsCollection(collectionAddr); | ||
| string memory contractUri = collection.contractURI(); | ||
|
|
||
| // Should contain a data URI (resolved from the referenced ethscription) | ||
| assertTrue(contractUri.contains("data:"), "Should contain resolved data URI"); | ||
| } | ||
|
|
||
| function test_InvalidEthscriptionReferenceReturnsEmpty() public { | ||
| // Reference to non-existent ethscription | ||
| bytes32 fakeId = keccak256("nonexistent"); | ||
| string memory escUri = string.concat( | ||
| "esc://ethscriptions/", | ||
| uint256(fakeId).toHexString(), | ||
| "/data" | ||
| ); | ||
|
|
||
| bytes32 collectionId = _createCollectionWithLogo(escUri); | ||
|
|
||
| ERC721EthscriptionsCollectionManager.CollectionMetadata memory metadata = | ||
| collectionsHandler.getCollection(collectionId); | ||
| address collectionAddr = metadata.collectionContract; | ||
| ERC721EthscriptionsCollection collection = ERC721EthscriptionsCollection(collectionAddr); | ||
|
|
||
| // Should not revert, just return empty/placeholder | ||
| string memory contractUri = collection.contractURI(); | ||
| assertTrue(bytes(contractUri).length > 0, "Should return contractURI without reverting"); | ||
| } | ||
|
|
||
| function test_MalformedEscURIReturnsEmpty() public { | ||
| // Various malformed esc:// URIs | ||
| string[] memory badUris = new string[](4); | ||
| badUris[0] = "esc://ethscriptions/notahexid/data"; | ||
| badUris[1] = "esc://ethscriptions/0x123/data"; // Too short | ||
| badUris[2] = "esc://ethscriptions/"; // Incomplete | ||
| badUris[3] = "esc://wrong/0x1234567890123456789012345678901234567890123456789012345678901234/data"; | ||
|
|
||
| for (uint i = 0; i < badUris.length; i++) { | ||
| // Use unique collection ID for each iteration | ||
| bytes32 uniqueCollectionId = keccak256(abi.encodePacked("malformed_test", i)); | ||
| bytes32 collectionId = _createCollectionWithLogoAndId(badUris[i], uniqueCollectionId); | ||
|
|
||
| ERC721EthscriptionsCollectionManager.CollectionMetadata memory metadata = | ||
| collectionsHandler.getCollection(collectionId); | ||
| address collectionAddr = metadata.collectionContract; | ||
| ERC721EthscriptionsCollection collection = ERC721EthscriptionsCollection(collectionAddr); | ||
|
|
||
| // Should not revert | ||
| string memory contractUri = collection.contractURI(); | ||
| assertTrue(bytes(contractUri).length > 0, "Should return contractURI without reverting"); | ||
| } | ||
| } | ||
|
|
||
| // -------------------- Helpers -------------------- | ||
|
|
||
| function _createCollectionWithLogo(string memory logoUri) private returns (bytes32) { | ||
| return _createCollectionWithLogoAndId(logoUri, COLLECTION_TX_HASH); | ||
| } | ||
|
|
||
| function _createCollectionWithLogoAndId(string memory logoUri, bytes32 collectionId) private returns (bytes32) { | ||
| ERC721EthscriptionsCollectionManager.CollectionParams memory metadata = | ||
| ERC721EthscriptionsCollectionManager.CollectionParams({ | ||
| name: "Test Collection", | ||
| symbol: "TEST", | ||
| maxSupply: 100, | ||
| description: "Test collection", | ||
| logoImageUri: logoUri, | ||
| bannerImageUri: "", | ||
| backgroundColor: "", | ||
| websiteLink: "", | ||
| twitterLink: "", | ||
| discordLink: "", | ||
| merkleRoot: bytes32(0) | ||
| }); | ||
|
|
||
| string memory collectionContent = string.concat( | ||
| 'data:application/json,', | ||
| '{"p":"erc-721-ethscriptions-collection","op":"create_collection"}' | ||
| ); | ||
|
|
||
| Ethscriptions.CreateEthscriptionParams memory params = Ethscriptions.CreateEthscriptionParams({ | ||
| ethscriptionId: collectionId, | ||
| contentUriSha: sha256(bytes(collectionContent)), | ||
| initialOwner: alice, | ||
| content: bytes(collectionContent), | ||
| mimetype: "application/json", | ||
| esip6: true, // Allow duplicate content URI | ||
| protocolParams: Ethscriptions.ProtocolParams({ | ||
| protocolName: "erc-721-ethscriptions-collection", | ||
| operation: "create_collection", | ||
| data: abi.encode(metadata) | ||
| }) | ||
| }); | ||
|
|
||
| vm.prank(alice); | ||
| ethscriptions.createEthscription(params); | ||
|
|
||
| return collectionId; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment states "using JSONParserLib (reverts on invalid)" but the code explicitly catches the revert and returns an empty string. The comment should be updated to reflect the actual behavior, e.g., "Parse hex string to bytes32 (returns empty on invalid)".