forked from onflow/freshmint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlindNFT.template.cdc
359 lines (286 loc) · 11.8 KB
/
BlindNFT.template.cdc
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import NonFungibleToken from {{{ imports.NonFungibleToken }}}
import MetadataViews from {{{ imports.MetadataViews }}}
import FungibleToken from {{{ imports.FungibleToken }}}
import FreshmintEncoding from {{{ imports.FreshmintEncoding }}}
import FreshmintMetadataViews from {{{ imports.FreshmintMetadataViews }}}
pub contract {{ contractName }}: NonFungibleToken {
pub let version: String
pub event ContractInitialized()
pub event Withdraw(id: UInt64, from: Address?)
pub event Deposit(id: UInt64, to: Address?)
pub event Minted(id: UInt64, hash: [UInt8])
pub event Revealed(id: UInt64)
pub event Burned(id: UInt64)
pub let CollectionStoragePath: StoragePath
pub let CollectionPublicPath: PublicPath
pub let CollectionPrivatePath: PrivatePath
pub let AdminStoragePath: StoragePath
/// The total number of {{ contractName }} NFTs that have been minted.
///
pub var totalSupply: UInt64
/// A placeholder image used to display NFTs that have not yet been revealed.
///
pub let placeholderImage: String
{{> royalties-field contractName=contractName }}
{{> collection-metadata-field }}
pub struct Metadata {
/// A salt that is published when the metadata is revealed.
///
/// The salt is a byte array that is prepended to the
/// encoded metadata values before generating the metadata hash.
///
pub let salt: [UInt8]
/// The core metadata fields for a {{ contractName }} NFT.
///
{{#each fields}}
pub let {{ this.name }}: {{ this.asCadenceTypeString }}
{{/each}}
/// Optional attributes for a {{ contractName }} NFT.
///
pub let attributes: {String: String}
init(
salt: [UInt8],
{{#each fields}}
{{ this.name }}: {{ this.asCadenceTypeString }},
{{/each}}
attributes: {String: String}
) {
self.salt = salt
{{#each fields}}
self.{{ this.name }} = {{ this.name }}
{{/each}}
self.attributes = attributes
}
/// Encode this metadata object as a byte array.
///
/// This can be used to hash the metadata and verify its integrity.
///
pub fun encode(): [UInt8] {
return self.salt
{{#each fields}}
.concat({{ this.getCadenceEncodingTemplate }})
{{/each}}
}
pub fun hash(): [UInt8] {
return HashAlgorithm.SHA3_256.hash(self.encode())
}
}
/// This dictionary holds the metadata for all NFTs
/// minted by this contract.
///
/// When an NFT is revealed, its metadata is added to this
/// dictionary.
///
access(contract) let metadata: {UInt64: Metadata}
/// Return the metadata for an NFT.
///
/// This function returns nil if the NFT has not yet been revealed.
///
pub fun getMetadata(nftID: UInt64): Metadata? {
return {{ contractName }}.metadata[nftID]
}
/// This dictionary stores all NFT IDs minted by this contract,
/// indexed by their metadata hash.
///
/// It is populated at mint time and later used to validate
/// metadata hashes at reveal time.
///
/// This dictionary is indexed by hash rather than by ID so that
/// the contract (and client software) can prevent duplicate mints.
///
access(contract) let nftsByHash: {String: UInt64}
pub fun getNFTIDByHash(hash: String): UInt64? {
return {{ contractName }}.nftsByHash[hash]
}
pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
pub let id: UInt64
/// A hash of the NFT's metadata.
///
/// The metadata hash is known at mint time and
/// is generated by hashing the set of metadata fields
/// for this NFT. The hash can later be used to verify
/// that the correct metadata fields are revealed.
///
pub let hash: [UInt8]
init(hash: [UInt8]) {
self.id = self.uuid
self.hash = hash
}
/// Return the metadata for this NFT.
///
/// This function returns nil if the NFT metadata has
/// not yet been revealed.
///
pub fun getMetadata(): Metadata? {
return {{ contractName }}.metadata[self.id]
}
pub fun getViews(): [Type] {
if self.getMetadata() != nil {
{{#if views }}
return [
{{#each views}}
{{{ this.cadenceTypeString }}}{{#unless @last }},{{/unless}}
{{/each}}
]
{{ else }}
return []
{{/if}}
}
return [
{{#each views}}
{{#unless this.requiresMetadata }}
{{{ this.cadenceTypeString }}},
{{/unless}}
{{/each}}
Type<MetadataViews.Display>(),
Type<FreshmintMetadataViews.BlindNFT>()
]
}
pub fun resolveView(_ view: Type): AnyStruct? {
{{#if views }}
if let metadata = self.getMetadata() {
switch view {
{{#each views}}
{{> viewCase view=this metadata="metadata" }}
{{/each}}
}
return nil
}
{{ else }}
if self.getMetadata() != nil {
return nil
}
{{/if}}
switch view {
case Type<MetadataViews.Display>():
return MetadataViews.Display(
name: "{{ contractName }}",
description: "This NFT is not yet revealed.",
thumbnail: FreshmintMetadataViews.ipfsFile(file: {{ contractName }}.placeholderImage)
)
case Type<FreshmintMetadataViews.BlindNFT>():
return FreshmintMetadataViews.BlindNFT(hash: self.hash)
{{#each views}}
{{#unless this.requiresMetadata }}
{{> viewCase view=this }}
{{/unless}}
{{/each}}
}
return nil
}
{{#each views}}
{{#if this.cadenceResolverFunction }}
{{> (lookup . "id") view=this contractName=../contractName }}
{{/if}}
{{/each}}
destroy() {
{{ contractName }}.totalSupply = {{ contractName }}.totalSupply - (1 as UInt64)
emit Burned(id: self.id)
}
}
{{> collection contractName=contractName }}
/// The administrator resource used to mint and reveal NFTs.
///
pub resource Admin {
/// Mint a new NFT.
///
/// To mint a blind NFT, specify its metadata hash
/// that can later be used to verify the revealed NFT.
///
pub fun mintNFT(hash: [UInt8]): @{{ contractName }}.NFT {
let hexHash = String.encodeHex(hash)
// Prevent multiple NFTs from being minted with the same metadata hash.
assert(
{{ contractName }}.nftsByHash[hexHash] == nil,
message: "an NFT has already been minted with hash=".concat(hexHash)
)
let nft <- create {{ contractName }}.NFT(hash: hash)
emit Minted(id: nft.id, hash: hash)
// Save the metadata hash so that it can later be validated on reveal.
{{ contractName }}.nftsByHash[hexHash] = nft.id
{{ contractName }}.totalSupply = {{ contractName }}.totalSupply + (1 as UInt64)
return <- nft
}
/// Reveal a minted NFT.
///
/// To reveal an NFT, publish its complete metadata and unique salt value.
///
pub fun revealNFT(id: UInt64, metadata: Metadata) {
pre {
{{ contractName }}.metadata[id] == nil : "NFT has already been revealed"
}
// An NFT cannot be revealed unless the provided metadata values
// match the hash that was specified at mint time.
let hash = String.encodeHex(metadata.hash())
if let mintedID = {{ contractName }}.getNFTIDByHash(hash: hash) {
assert(
id == mintedID,
message: "the provided metadata hash matches NFT with ID="
.concat(mintedID.toString())
.concat(", but expected ID=")
.concat(id.toString())
)
} else {
panic("the provided metadata hash does not match any minted NFTs")
}
{{ contractName }}.metadata[id] = metadata
emit Revealed(id: id)
}
}
/// Return a public path that is scoped to this contract.
///
pub fun getPublicPath(suffix: String): PublicPath {
return PublicPath(identifier: "{{ contractName }}_".concat(suffix))!
}
/// Return a private path that is scoped to this contract.
///
pub fun getPrivatePath(suffix: String): PrivatePath {
return PrivatePath(identifier: "{{ contractName }}_".concat(suffix))!
}
/// Return a storage path that is scoped to this contract.
///
pub fun getStoragePath(suffix: String): StoragePath {
return StoragePath(identifier: "{{ contractName }}_".concat(suffix))!
}
/// Return a collection name with an optional bucket suffix.
///
pub fun makeCollectionName(bucketName maybeBucketName: String?): String {
if let bucketName = maybeBucketName {
return "Collection_".concat(bucketName)
}
return "Collection"
}
/// Return a queue name with an optional bucket suffix.
///
pub fun makeQueueName(bucketName maybeBucketName: String?): String {
if let bucketName = maybeBucketName {
return "Queue_".concat(bucketName)
}
return "Queue"
}
priv fun initAdmin(admin: AuthAccount) {
// Create an empty collection and save it to storage
let collection <- {{ contractName }}.createEmptyCollection()
admin.save(<- collection, to: {{ contractName }}.CollectionStoragePath)
admin.link<&{{ contractName }}.Collection>({{ contractName }}.CollectionPrivatePath, target: {{ contractName }}.CollectionStoragePath)
admin.link<&{{ contractName }}.Collection{NonFungibleToken.CollectionPublic, {{ contractName }}.{{ contractName }}CollectionPublic, MetadataViews.ResolverCollection}>({{ contractName }}.CollectionPublicPath, target: {{ contractName }}.CollectionStoragePath)
// Create an admin resource and save it to storage
let adminResource <- create Admin()
admin.save(<- adminResource, to: self.AdminStoragePath)
}
init(collectionMetadata: MetadataViews.NFTCollectionDisplay, royalties: [MetadataViews.Royalty], placeholderImage: String{{#unless saveAdminResourceToContractAccount }}, admin: AuthAccount{{/unless}}) {
self.version = "{{ freshmintVersion }}"
self.CollectionPublicPath = {{ contractName }}.getPublicPath(suffix: "Collection")
self.CollectionStoragePath = {{ contractName }}.getStoragePath(suffix: "Collection")
self.CollectionPrivatePath = {{ contractName }}.getPrivatePath(suffix: "Collection")
self.AdminStoragePath = {{ contractName }}.getStoragePath(suffix: "Admin")
self.placeholderImage = placeholderImage
self.royalties = royalties
self.collectionMetadata = collectionMetadata
self.totalSupply = 0
self.metadata = {}
self.nftsByHash = {}
self.initAdmin(admin: {{#if saveAdminResourceToContractAccount }}self.account{{ else }}admin{{/if}})
emit ContractInitialized()
}
}