-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3406 from dessaya/tokenuri-hack
erc721 tokenURI(): return NFT uri+name+description
- Loading branch information
Showing
6 changed files
with
79 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,48 @@ | ||
package evm | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/samber/lo" | ||
|
||
"github.com/iotaledger/wasp/packages/isc" | ||
) | ||
|
||
// This hack is so that the ERC721 tokenURI view function returns the NFT name and description | ||
// for explorers | ||
type PackedNFTURI struct { | ||
Name string `json:"name"` | ||
Description string `json:"description,omitempty"` | ||
Image string `json:"image"` | ||
} | ||
|
||
const dataURLPrefix = "data:application/json;base64" | ||
|
||
func EncodePackedNFTURI(metadata *isc.IRC27NFTMetadata) string { | ||
return dataURLPrefix + "," + base64.StdEncoding.EncodeToString(lo.Must(json.Marshal(PackedNFTURI{ | ||
Name: metadata.Name, | ||
Description: metadata.Description, | ||
Image: metadata.URI, | ||
}))) | ||
} | ||
|
||
func DecodePackedNFTURI(uri string) (*PackedNFTURI, error) { | ||
parts := strings.Split(uri, ",") | ||
if len(parts) != 2 { | ||
return nil, errors.New("cannot decode packed NFT URI: expected valid data URL") | ||
} | ||
if parts[0] != dataURLPrefix { | ||
return nil, errors.New("cannot decode packed NFT URI: expected valid data URL") | ||
} | ||
b, err := base64.StdEncoding.DecodeString(parts[1]) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot decode packed NFT URI: %w", err) | ||
} | ||
var p *PackedNFTURI | ||
err = json.Unmarshal(b, &p) | ||
return p, err | ||
} |