fix: add support for tokenURI encoded images #4482
Merged
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.
Explanation
We encountered cases where tokenURI we fetched from the contract is an encoded image:
data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaW5ZTWluIG1lZXQiIHZpZXdCb3g9IjAgMCAzNTAgMzUwIj48c3R5bGU+LmJhc2UgeyBmaWxsOiBibGFjazsgZm9udC1mYW1pbHk6IHNlcmlmOyBmb250LXNpemU6IDE0cHg7fTwvc3R5bGU+PHJlY3Qgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0id2hpdGUiIHJ5PSI0MCIgcng9IjQwIiBzdHlsZT0ic3Ryb2tlOiNGRjJGMDA7IHN0cm9rZS13aWR0aDo1OyBvcGFjaXR5OjAuOSIgLz48dGV4dCB4PSI1MCUiIHk9IjIwJSIgY2xhc3M9ImJhc2UiIGRvbWluYW50LWJhc2VsaW5lPSJtaWRkbGUiIHRleHQtYW5jaG9yPSJtaWRkbGUiPkV4YW1wbGUgTkZUPC90ZXh0Pjx0ZXh0IHg9IjUwJSIgeT0iMzAlIiBjbGFzcz0iYmFzZSIgZG9taW5hbnQtYmFzZWxpbmU9Im1pZGRsZSIgdGV4dC1hbmNob3I9Im1pZGRsZSI+VG9rZW4gSUQ6IDM8L3RleHQ+PHRleHQgeD0iMS4yJSIgeT0iOTAlIiBjbGFzcz0iYmFzZSIgZG9taW5hbnQtYmFzZWxpbmU9Im1pZGRsZSIgdGV4dC1hbmNob3I9ImxlZnQiIHN0eWxlPSJmb250LXNpemU6MTBweDsiPiBQb3dlcmVkIGJ5IFJBRCBUZWFtPC90ZXh0Pjwvc3ZnPg==
In this case, the code will try to call handleFetch to fetch metadata and fail, and eventually return image: null, which on the client side will result in displaying a default fallback image.
This PR adds a check for this case, and returns the encoded data as the image.
References
Changelog
@metamask/assets-controllers
getNftInformationFromTokenURI
to see if the fetched tokenURI is an encoded image, in which case, we return it as the image.Checklist