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

fix: replace base64.StdEncoding with base64.RawStdEncoding #356

Merged
merged 1 commit into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions modules/nft/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var (
ClassKeySchema = fmt.Sprintf("%s%s", Namespace, "schema")
TokenKeyName = fmt.Sprintf("%s%s", Namespace, "name")
TokenKeyURIhash = fmt.Sprintf("%s%s", Namespace, "uri_hash")

Base64 = base64.StdEncoding
)

type (
Expand Down Expand Up @@ -70,7 +72,7 @@ func (cb ClassBuilder) BuildMetadata(class nft.Class) (string, error) {
err := json.Unmarshal([]byte(metadata.Data), &kvals)
if err != nil && IsIBCDenom(class.Id) {
//when classData is not a legal json, there is no need to parse the data
return base64.RawStdEncoding.EncodeToString([]byte(metadata.Data)), nil
return Base64.EncodeToString([]byte(metadata.Data)), nil
}
//note: if metadata.Data is null, it may cause map to be redefined as nil
if kvals == nil {
Expand All @@ -95,12 +97,12 @@ func (cb ClassBuilder) BuildMetadata(class nft.Class) (string, error) {
if err != nil {
return "", err
}
return base64.RawStdEncoding.EncodeToString(data), nil
return Base64.EncodeToString(data), nil
}

// Build create a class from ics721 packetData
func (cb ClassBuilder) Build(classID, classURI, classData string) (nft.Class, error) {
classDataBz, err := base64.RawStdEncoding.DecodeString(classData)
classDataBz, err := Base64.DecodeString(classData)
if err != nil {
return nft.Class{}, err
}
Expand Down Expand Up @@ -267,7 +269,7 @@ func (tb TokenBuilder) BuildMetadata(token nft.NFT) (string, error) {
err := json.Unmarshal([]byte(nftMetadata.Data), &kvals)
if err != nil && IsIBCDenom(token.ClassId) {
//when nftMetadata is not a legal json, there is no need to parse the data
return base64.RawStdEncoding.EncodeToString([]byte(nftMetadata.Data)), nil
return Base64.EncodeToString([]byte(nftMetadata.Data)), nil
}
//note: if nftMetadata.Data is null, it may cause map to be redefined as nil
if kvals == nil {
Expand All @@ -280,12 +282,12 @@ func (tb TokenBuilder) BuildMetadata(token nft.NFT) (string, error) {
if err != nil {
return "", err
}
return base64.RawStdEncoding.EncodeToString(data), nil
return Base64.EncodeToString(data), nil
}

// Build create a nft from ics721 packet data
func (tb TokenBuilder) Build(classId, tokenId, tokenURI, tokenData string) (nft.NFT, error) {
tokenDataBz, err := base64.RawStdEncoding.DecodeString(tokenData)
tokenDataBz, err := Base64.DecodeString(tokenData)
if err != nil {
return nft.NFT{}, err
}
Expand Down
5 changes: 2 additions & 3 deletions modules/nft/types/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"encoding/base64"
"encoding/json"
"testing"

Expand Down Expand Up @@ -166,7 +165,7 @@ func TestClassBuilder_BuildMetadata(t *testing.T) {
class.Data = any

got, err := cb.BuildMetadata(class)
want := base64.RawStdEncoding.EncodeToString([]byte(tt.want))
want := Base64.EncodeToString([]byte(tt.want))
if (err != nil) != tt.wantErr {
t.Errorf("ClassBuilder.BuildMetadata() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -229,7 +228,7 @@ func TestClassBuilder_Build(t *testing.T) {
cdc: cdc,
getModuleAddress: getModuleAddress,
}
classDataRaw := base64.RawStdEncoding.EncodeToString([]byte(tt.args.classData))
classDataRaw := Base64.EncodeToString([]byte(tt.args.classData))
result, err := cb.Build(classID, classURI, classDataRaw)
if (err != nil) != tt.wantErr {
t.Errorf("ClassBuilder.BuildMetadata() error = %v, wantErr %v", err, tt.wantErr)
Expand Down