Skip to content
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

Support data URLs #2

Closed
fulldecent opened this issue Oct 1, 2021 · 7 comments
Closed

Support data URLs #2

fulldecent opened this issue Oct 1, 2021 · 7 comments

Comments

@fulldecent
Copy link

As written, ERC-721 expects the tokenURI to return:

A distinct Uniform Resource Identifier (URI) for a given asset. ... URIs are defined in RFC 3986.

And it IS possible to encode a JSON file in a RFC 3986 URI using Data URLs (RFC 2397).

All you need to do is prepend this string to the JSON:

data:application/json,
@bmeredith
Copy link
Owner

I really appreciate you checking this out @fulldecent

In regards to the data URLs - although generating metadata for ERC-721 was the driving use-case behind this library, it was made as a general-purpose library.

Part of the reasoning for this is:

  1. There may be other applications or smart contracts that would be able to utilize this library to generate JSON.
  2. There has been a rise in popularity of building JSON metadata within an ERC-721 smart contract, but then base64 encoding the resulting JSON or base64 encoding the tokenURI itself.

For these reasons, I have left it up to the client to decide how they will use the resulting JSON. I would love to hear your thoughts, though.

@fulldecent
Copy link
Author

Great, it's really nice to have general support. Perhaps something that might be in-scope for this project then is just to add an implementation note for people wanting to use this for ERC-721 metadata.

If your metadata does not contain any RFC3986 reserved characters, simply prepend data:application/json,.

If you may have reserved characters, here is the more general approach...

Here is a basic example NFT contract that implements tokenURI entirely on-chain...

Because I want to recommend this project and just don't want to see people make rookie mistakes on production contracts.

@bmeredith
Copy link
Owner

That's a great idea.

I can probably get something in there about that within the next week or so. Otherwise I'm happy to accept a PR as well.

@bmeredith
Copy link
Owner

@fulldecent I've gone ahead and created a PR for this issue. If you have time to check it out, I would really appreciate your thoughts on it before merging in.

@fulldecent
Copy link
Author

For number 2:

If the metadata does contain reserved characters, prepend data:application/json;base64, then encode the produced JSON as base64.


P.S. if you have a preferred base64 approach you can mention that here.


P.S. OnChain Monkey uses this approach for ERC-721 JSON unchain with Base64 encoding.

@bmeredith
Copy link
Owner

Thanks for the feedback. I've gone ahead and pushed a change to the PR recommending the use of base64 encoding instead.

I'm currently holding off on mentioning a recommended library for the time being as I am waiting on the base64 library to get added to the official OpenZeppelin utils library. Once that is in place, I'll be adding a note regarding it, though.

OpenZeppelin/openzeppelin-contracts#2884

@fulldecent
Copy link
Author

Cool thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants