-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_server_mint_nft.go
266 lines (209 loc) · 7.75 KB
/
msg_server_mint_nft.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package keeper
import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/ipgit2860/vcoa/x/vnft/types"
sdkctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)
func generatePubKeys(n int) []cryptotypes.PubKey {
pks := make([]cryptotypes.PubKey, n)
for i := 0; i < n; i++ {
pks[i] = secp256k1.GenPrivKey().PubKey()
}
return pks
}
func (k msgServer) MintNft(goCtx context.Context, msg *types.MsgMintNft) (*types.MsgMintNftResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// TODO: Handling the message
has := k.nftKeeper.HasClass(ctx, msg.ClassId)
if !has {
return nil, types.ErrFailedToMarshalData.Wrapf("%s, %s", "No such class ID", msg.ClassId)
}
// check if nft class transferable
class, hasClass := k.nftKeeper.GetClass(ctx, msg.ClassId)
if !hasClass {
return nil, types.ErrNftClassNotFound.Wrapf("Class id %s not found", msg.ClassId)
}
// Unmarshal class data
var classData types.ClassData
if err := classData.Unmarshal(class.Data.Value); err != nil {
return nil, types.ErrFailedToUnmarshalData.Wrapf(err.Error())
}
type format struct {
CField []string `protobuf:"bytes,1,opt,name=cField,proto3" json:"cField,omitempty"`
CType []string `protobuf:"bytes,2,opt,name=cType,proto3" json:"cType,omitempty"`
COptional []bool `protobuf:"bytes,3,opt,name=cOptional,proto3" json:"cOptional,omitempty"`
}
type metadata struct {
Version int `protobuf:"bytes,1,opt,name=version,proto3" json:"version,omitempty"`
Format types.JsonInput `protobuf:"bytes,2,opt,name=format,proto3" json:"format,omitempty"`
MultiAuthor bool `protobuf:"bytes,3,opt,name=multiAuthor,proto3" json:"multiAuthor,omitempty"`
Burnable bool `protobuf:"bytes,4,opt,name=burnable,proto3" json:"burnable,omitempty"`
Transferable bool `protobuf:"bytes,5,opt,name=transferable,proto3" json:"transferable,omitempty"`
}
var classMeta metadata
json.Unmarshal([]byte(classData.Metadata), &classMeta)
var f format
json.Unmarshal([]byte(classMeta.Format), &f)
var token nft.NFT
userAddress, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return nil, types.ErrFailedToGetUserAddress.Wrapf("User Address %s", err.Error())
}
address := userAddress
fmt.Println("msg.Creator: ", msg.Creator)
fmt.Println("userAddress: ", userAddress)
token.ClassId = msg.ClassId
token.Uri = msg.Uri
token.UriHash = msg.UriHash
databyte := []byte(msg.Data.Metadata)
var meta types.JsonInput
if json.Unmarshal(databyte, &meta) != nil {
return nil, types.ErrFailedToMarshalData.Wrapf("Unmarshal %s", "error")
}
var nftMeta types.JsonInput
fmt.Println("meta: ", meta)
fmt.Println("class.Name: ", class.Name)
fmt.Println("classMeta.Version: ", classMeta.Version)
// totalSupply := k.nftKeeper.GetTotalSupply(ctx, class.Id)
token.Id, err = types.NewNftId(class.Name, classMeta.Version, token.UriHash, msg.Creator)
fmt.Println("token.Id: ", token.Id)
if err != nil {
return nil, types.ErrFailedToCreateNftId.Wrapf("%s", err.Error())
}
pubKey := "" // public key of the creator
if class.Name == "ID" {
token.Uri = ""
token.UriHash = ""
type nftID struct {
PubKey string
Record []string
}
var rd nftID
json.Unmarshal([]byte(meta), &rd)
fmt.Printf("rd: %s %d\n", rd, len(rd.Record))
if len(rd.Record) < 1 {
return nil, types.ErrFailedToMarshalData.Wrapf("Not enough record to process")
}
pubKey = rd.PubKey
rd.Record[0] = strings.ReplaceAll(rd.Record[0], " ", "")
rd.Record[0] = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(rd.Record[0], "")
rd.Record[0] = strings.ToLower(rd.Record[0])
if rd.Record[0] == "" {
return nil, types.ErrFailedToMarshalData.Wrapf("Unmarshal %s", "error")
}
nj := "{\""
if len(rd.Record) >= 2 {
rd.Record[1] = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(rd.Record[1], "") // remove all unknown char
nj = nj + "address" + "\": \"" + address.String() + "\", \"" + f.CField[0] + "\": \"" + rd.Record[0] + "\", \"" + f.CField[1] + "\": \"" + rd.Record[1]
} else {
nj = nj + "address" + "\": \"" + address.String() + "\", \"" + f.CField[0] + "\": \"" + rd.Record[0] + "\", \"" + f.CField[1] + "\": \""
}
nj = nj + "\"}"
nftMeta = types.JsonInput(nj)
fmt.Println("nftMeta: ", nftMeta)
token.Id = "vconft-" + class.Name + "-" + rd.Record[0]
} else {
type nftMetaType struct {
PubKey string
Record []string
}
var rd nftMetaType
json.Unmarshal([]byte(meta), &rd)
fmt.Printf("rd: %s %d\n", rd, len(rd.Record))
pubKey = rd.PubKey
if rd.Record[0] == "" {
return nil, types.ErrFailedToMarshalData.Wrapf("Unmarshal %s", "error")
}
nj := "{\""
nj = nj + "address" + "\": \"" + address.String() + "\""
for index, element := range f.CField {
if element != "tag" {
rd.Record[index] = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(rd.Record[index], "") // remove all unknown char
} else {
rd.Record[index] = regexp.MustCompile(`[^\w\s-]+`).ReplaceAllString(rd.Record[index], "") // remove all unknown char, numbers
rd.Record[index] = strings.ToLower(rd.Record[index])
}
nj = nj + ",\"" + element + "\":\"" + rd.Record[index] + "\""
}
nj = nj + "}"
nftMeta = types.JsonInput(nj)
fmt.Println("nftMeta: ", nftMeta)
}
var pubKeyBytes []byte
if pubKey != "" {
pubKeyBytes, err = hex.DecodeString(pubKey)
if err != nil {
return nil, types.ErrFailedToMarshalData.Wrapf("%s", err.Error())
}
fmt.Println("pubKey: ", pubKey)
fmt.Println("pubKeyBytes: ", pubKeyBytes)
fmt.Println("&secp256k1.PubKey{Key: pubKeyBytes}: ", &secp256k1.PubKey{Key: pubKeyBytes})
} else {
// for no public key was given
fmt.Println("msg.UriHash: ", msg.UriHash)
pk, _ := base64.StdEncoding.DecodeString(msg.UriHash)
creatorPubKey := &secp256k1.PubKey{Key: pk}
var aa sdk.AccAddress
aa.Unmarshal(creatorPubKey.Address())
fmt.Println("pk: ", pk)
fmt.Println("creatorPubKey: ", creatorPubKey)
fmt.Println("aa: ", aa.String())
pubKeyBytes = creatorPubKey.Key
fmt.Println("pubKeyBytes: ", pubKeyBytes)
}
if !classMeta.Transferable {
// scramble pubKey to make no one knows the private key
var hashBytes []byte
h := sha256.New()
h.Write(pubKeyBytes[1:])
bs := h.Sum(nil)
hashBytes = append(hashBytes, pubKeyBytes[0])
hashBytes = append(hashBytes, bs...)
fmt.Println("hashBytes: ", hashBytes)
hashedKey := &secp256k1.PubKey{Key: hashBytes}
fmt.Println("&secp256k1.PubKey{Key: hashBytes}: ", hashedKey)
var mAddr sdk.AccAddress
mAddr.Unmarshal(hashedKey.Address())
userAddress = mAddr
// to make sure mAddr is never used for this class
balance := k.nftKeeper.GetBalance(ctx, msg.ClassId, mAddr)
if balance > 0 {
return nil, types.ErrOwnerAddressUsedBefore.Wrapf("Address registered before")
}
}
fmt.Println()
fmt.Println()
fmt.Println("-----------------------------------------------------------------------------")
var nftmeta types.JsonInput
if json.Unmarshal([]byte(nftMeta), &nftmeta) != nil {
return nil, types.ErrFailedToMarshalData.Wrapf("Unmarshal %s", "error")
}
nftData := types.NftData{
Metadata: nftmeta,
}
classDataInAny, err := sdkctypes.NewAnyWithValue(&nftData)
if err != nil {
return nil, types.ErrFailedToMarshalData.Wrapf("%s", err.Error())
}
token.Data = classDataInAny
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("%s", err.Error())
}
err = k.nftKeeper.Mint(ctx, token, userAddress)
if err != nil {
return nil, types.ErrFailedToMintNFT.Wrapf("%s", err.Error())
}
return &types.MsgMintNftResponse{}, nil
}