-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVWBL.ts
583 lines (550 loc) · 20.2 KB
/
VWBL.ts
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import axios from "axios";
import { utils } from "ethers";
import * as fs from "fs";
import { uploadEncryptedFile, uploadMetadata, uploadThumbnail } from "../../storage/aws/upload";
import {
createRandomKey,
decryptFile,
decryptStream,
decryptString,
encryptFile,
encryptStream,
encryptString,
} from "../../util/cryptoHelper";
import { getMimeType, toBase64FromBlob } from "../../util/fileHelper";
import { VWBLBase } from "../base";
import { VWBLNFT, VWBLNFTEthers } from "../blockchain";
import { ExtractMetadata, Metadata, PlainMetadata } from "../metadata";
import {
ConstructorProps,
EncryptLogic,
EthersConstructorProps,
ProgressSubscriber,
StepStatus,
UploadContentType,
UploadEncryptedFile,
UploadMetadata,
UploadMetadataType,
UploadThumbnail,
VWBLEthersOption,
VWBLOption,
} from "../types";
import { VWBLViewer } from "../viewer";
export class VWBL extends VWBLBase {
public opts: VWBLOption | VWBLEthersOption;
public nft: VWBLNFT | VWBLNFTEthers;
public viewer?: VWBLViewer;
constructor(props: ConstructorProps | EthersConstructorProps) {
super(props);
this.opts = props;
this.nft =
"web3" in props
? new VWBLNFT(props.web3, props.contractAddress, props.uploadMetadataType === UploadMetadataType.IPFS)
: new VWBLNFTEthers(
props.contractAddress,
props.uploadMetadataType === UploadMetadataType.IPFS,
props.ethersProvider,
props.ethersSigner
);
if (props.dataCollectorAddress) {
this.viewer =
"web3" in props
? new VWBLViewer({
provider: props.web3,
dataCollectorAddress: props.dataCollectorAddress,
})
: new VWBLViewer({
provider: props.ethersProvider,
dataCollectorAddress: props.dataCollectorAddress,
});
}
}
/**
* Sign to VWBL
*
* @remarks
* You need to call this method before you send a transaction(eg. mint NFT, decrypt NFT Data)
* @param targetContract - Optional: the contract to operate on. (default: this.nft)
*/
sign = async (targetContract?: string) => {
"web3" in this.opts
? await this._sign(this.opts.web3, targetContract)
: await this._sign(this.opts.ethersSigner, targetContract);
};
/**
* Create VWBL NFT
*
* @remarks
* The following happens: Minting NFT, Uploading encrypted data, Uploading metadata, Setting key to VWBL Network
* By default, metadata will be uploaded to Amazon S3.
* You need to pass `uploadFileCallback` and `uploadMetadataCallBack` if you upload metadata to a storage other than Amazon S3.
*
* @param name - The NFT name
* @param description - The NFT description
* @param plainFile - The data that only NFT owner can view
* @param thumbnailImage - The NFT image
* @param royaltiesPercentage - This percentage of the sale price will be paid to the NFT creator every time the NFT is sold or re-sold
* @param encryptLogic - Select ether "base64" or "binary". Selection criteria: "base64" -> sutable for small data. "binary" -> sutable for large data.
* @param uploadEncryptedFileCallback - Optional: the function for uploading encrypted data
* @param uploadThumbnailCallback - Optional: the function for uploading thumbnail
* @param uploadMetadataCallBack - Optional: the function for uploading metadata
* @param subscriber - Optional: the subscriber for seeing progress
* @returns
*/
managedCreateToken = async (
name: string,
description: string,
plainFile: File | File[],
thumbnailImage: File,
royaltiesPercentage: number,
encryptLogic: EncryptLogic = "base64",
uploadEncryptedFileCallback?: UploadEncryptedFile,
uploadThumbnailCallback?: UploadThumbnail,
uploadMetadataCallBack?: UploadMetadata,
subscriber?: ProgressSubscriber
) => {
if (!this.signature) {
throw "please sign first";
}
const { uploadContentType, uploadMetadataType, awsConfig, vwblNetworkUrl } = this.opts;
// 1. mint token
const documentId = utils.hexlify(utils.randomBytes(32));
const tokenId = await this.nft.mintToken(vwblNetworkUrl, royaltiesPercentage, documentId);
subscriber?.kickStep(StepStatus.MINT_TOKEN);
// 2. create key in frontend
const key = createRandomKey();
subscriber?.kickStep(StepStatus.CREATE_KEY);
// 3. encrypt data
console.log("encrypt data");
const plainFileArray = [plainFile].flat();
const uuid = createRandomKey();
const uploadEncryptedFunction =
uploadContentType === UploadContentType.S3 ? uploadEncryptedFile : uploadEncryptedFileCallback;
const uploadThumbnailFunction =
uploadContentType === UploadContentType.S3 ? uploadThumbnail : uploadThumbnailCallback;
if (!uploadEncryptedFunction || !uploadThumbnailFunction) {
throw new Error("please specify upload file type or give callback");
}
subscriber?.kickStep(StepStatus.ENCRYPT_DATA);
// 4. upload data
console.log("upload data");
const isRunningOnBrowser = typeof window !== "undefined";
const encryptedDataUrls = await Promise.all(
plainFileArray.map(async (file) => {
const encryptedContent =
encryptLogic === "base64"
? encryptString(await toBase64FromBlob(file), key)
: isRunningOnBrowser
? await encryptFile(file, key)
: encryptStream(fs.createReadStream((file as any).path), key);
return await uploadEncryptedFunction(file.name, encryptedContent, uuid, awsConfig);
})
);
const thumbnailImageUrl = await uploadThumbnailFunction(thumbnailImage, uuid, awsConfig);
subscriber?.kickStep(StepStatus.UPLOAD_CONTENT);
// 5. upload metadata
console.log("upload meta data");
const uploadMetadataFunction =
uploadMetadataType === UploadMetadataType.S3 ? uploadMetadata : uploadMetadataCallBack;
if (!uploadMetadataFunction) {
throw new Error("please specify upload metadata type or give callback");
}
const mimeType = getMimeType(plainFileArray[0]);
await uploadMetadataFunction(
tokenId,
name,
description,
thumbnailImageUrl,
encryptedDataUrls,
mimeType,
encryptLogic,
awsConfig
);
subscriber?.kickStep(StepStatus.UPLOAD_METADATA);
// 6. set key to vwbl-network
console.log("set key");
const chainId =
"web3" in this.opts ? await this.opts.web3.eth.getChainId() : await this.opts.ethersSigner.getChainId();
const signerAddress =
"web3" in this.opts
? await this._getAddressBySigner(this.opts.web3)
: await this._getAddressBySigner(this.opts.ethersSigner);
await this.api.setKey(documentId, chainId, key, this.signature, signerAddress);
subscriber?.kickStep(StepStatus.SET_KEY);
return tokenId;
};
/**
* Create VWBL NFT which metadata on IPFS.
*
* @remarks
* The following happens: Minting NFT, Uploading encrypted data, Uploading metadata, Setting key to VWBL Network
* metadata will be uploaded to IPFS.
* You need to pass `uploadFileCallback` and `uploadMetadataCallBack` if you upload metadata to a storage other than Amazon S3.
*
* @param name - The NFT name
* @param description - The NFT description
* @param plainFile - The data that only NFT owner can view
* @param thumbnailImage - The NFT image
* @param royaltiesPercentage - This percentage of the sale price will be paid to the NFT creator every time the NFT is sold or re-sold
* @param encryptLogic - Select ether "base64" or "binary". Selection criteria: "base64" -> sutable for small data. "binary" -> sutable for large data.
* @param subscriber - Optional: the subscriber for seeing progress
* @returns
*/
managedCreateTokenForIPFS = async (
name: string,
description: string,
plainFile: File | File[],
thumbnailImage: File,
royaltiesPercentage: number,
encryptLogic: EncryptLogic = "base64",
subscriber?: ProgressSubscriber
) => {
if (!this.signature) {
throw "please sign first";
}
const { vwblNetworkUrl } = this.opts;
// 1. create key in frontend
const key = createRandomKey();
subscriber?.kickStep(StepStatus.CREATE_KEY);
// 2. encrypt data
console.log("encrypt data");
const plainFileArray = [plainFile].flat();
subscriber?.kickStep(StepStatus.ENCRYPT_DATA);
// 3. upload data
console.log("upload data");
const encryptedDataUrls = await Promise.all(
plainFileArray.map(async (file) => {
const encryptedContent =
encryptLogic === "base64" ? encryptString(await toBase64FromBlob(file), key) : await encryptFile(file, key);
console.log(typeof encryptedContent);
return await this.uploadToIpfs?.uploadEncryptedFile(encryptedContent);
})
);
const thumbnailImageUrl = await this.uploadToIpfs?.uploadThumbnail(thumbnailImage);
subscriber?.kickStep(StepStatus.UPLOAD_CONTENT);
// 4. upload metadata
console.log("upload meta data");
const mimeType = getMimeType(plainFileArray[0]);
const metadataUrl = await this.uploadToIpfs?.uploadMetadata(
name,
description,
thumbnailImageUrl as string,
encryptedDataUrls as string[],
mimeType,
encryptLogic
);
subscriber?.kickStep(StepStatus.UPLOAD_METADATA);
// 5. mint token
const documentId = utils.hexlify(utils.randomBytes(32));
const tokenId = await this.nft.mintTokenForIPFS(
metadataUrl as string,
vwblNetworkUrl,
royaltiesPercentage,
documentId
);
subscriber?.kickStep(StepStatus.MINT_TOKEN);
// 6. set key to vwbl-network
console.log("set key");
const chainId =
"web3" in this.opts ? await this.opts.web3.eth.getChainId() : await this.opts.ethersSigner.getChainId();
const signerAddress =
"web3" in this.opts
? await this._getAddressBySigner(this.opts.web3)
: await this._getAddressBySigner(this.opts.ethersSigner);
await this.api.setKey(documentId, chainId, key, this.signature, signerAddress);
subscriber?.kickStep(StepStatus.SET_KEY);
return tokenId;
};
/**
* Set key to VWBL Network
*
* @param tokenId - The ID of NFT
* @param key - The key generated by {@link VWBL.createKey}
* @param hasNonce
* @param autoMigration
*
*/
setKey = async (tokenId: number, key: string, hasNonce?: boolean, autoMigration?: boolean): Promise<void> => {
const { documentId } = await this.nft.getTokenInfo(tokenId);
const chainId =
"web3" in this.opts ? await this.opts.web3.eth.getChainId() : await this.opts.ethersSigner.getChainId();
const signerAddress =
"web3" in this.opts
? await this._getAddressBySigner(this.opts.web3)
: await this._getAddressBySigner(this.opts.ethersSigner);
return await this._setKey(documentId, chainId, key, signerAddress, hasNonce, autoMigration);
};
/**
* Mint new NFT
*
* @param royaltiesPercentage - This percentage of the sale price will be paid to the NFT creator every time the NFT is sold or re-sold
* @returns The ID of minted NFT
*/
mintToken = async (royaltiesPercentage: number): Promise<number> => {
const { vwblNetworkUrl } = this.opts;
const documentId = utils.hexlify(utils.randomBytes(32));
return await this.nft.mintToken(vwblNetworkUrl, royaltiesPercentage, documentId);
};
/**
* Approves `operator` to transfer the given `tokenId`
*
* @param operator - The wallet address
* @param tokenId - The ID of NFT
*/
approve = async (operator: string, tokenId: number): Promise<void> => {
await this.nft.approve(operator, tokenId);
};
/**
* Get the approved address for a `tokenId`
*
* @param tokenId - The ID of NFT
* @return The Wallet address that was approved
*/
getApproved = async (tokenId: number): Promise<string> => {
return await this.nft.getApproved(tokenId);
};
/**
* Allows `operator` to transfer all tokens that a person who calls this function
*
* @param operator - The wallet address
*/
setApprovalForAll = async (operator: string): Promise<void> => {
await this.nft.setApprovalForAll(operator);
};
/**
* Tells whether an `operator` is approved by a given `owner`
*
* @param owner - The wallet address of a NFT owner
* @param operator - The wallet address of an operator
* @returns
*/
isApprovedForAll = async (owner: string, operator: string): Promise<boolean> => {
return await this.nft.isApprovedForAll(owner, operator);
};
/**
* Transfer NFT
*
* @param to - The address that NFT will be transfered
* @param tokenId - The ID of NFT
*/
safeTransfer = async (to: string, tokenId: number): Promise<void> => {
await this.nft.safeTransfer(to, tokenId);
};
/**
* Uplod Metadata
*
* @remarks
* By default, metadata will be uploaded to Amazon S3.
* You need to pass `uploadMetadataCallBack` if you upload metadata to a storage other than Amazon S3.
*
* @param tokenId - The ID of NFT
* @param name - The NFT name
* @param description - The NFT description
* @param thumbnailImageUrl - The URL of the thumbnail image
* @param encryptedDataUrls - The URL of the encrypted file data
* @param mimeType - The mime type of encrypted file data
* @param encryptLogic - Select ether "base64" or "binary". Selection criteria: "base64" -> sutable for small data. "binary" -> sutable for large data.
* @param uploadMetadataCallBack - Optional: the function for uploading metadata
*/
uploadMetadata = async (
tokenId: number,
name: string,
description: string,
thumbnailImageUrl: string,
encryptedDataUrls: string[],
mimeType: string,
encryptLogic: EncryptLogic,
uploadMetadataCallBack?: UploadMetadata
): Promise<void> => {
const { uploadMetadataType, awsConfig } = this.opts;
const uploadMetadataFunction =
uploadMetadataType === UploadMetadataType.S3 ? uploadMetadata : uploadMetadataCallBack;
if (!uploadMetadataFunction) {
throw new Error("please specify upload metadata type or give callback");
}
await uploadMetadataFunction(
tokenId,
name,
description,
thumbnailImageUrl,
encryptedDataUrls,
mimeType,
encryptLogic,
awsConfig
);
};
/**
* Uplod Metadata to IPFS
*
* @remarks
* Metadata will be uploaded to IPFS.
*
* @param tokenId - The ID of NFT
* @param name - The NFT name
* @param description - The NFT description
* @param thumbnailImageUrl - The URL of the thumbnail image
* @param encryptedDataUrls - The URL of the encrypted file data
* @param mimeType - The mime type of encrypted file data
* @param encryptLogic - Select ether "base64" or "binary". Selection criteria: "base64" -> sutable for small data. "binary" -> sutable for large data.
*/
uploadMetadataToIPFS = async (
name: string,
description: string,
thumbnailImageUrl: string,
encryptedDataUrls: string[],
mimeType: string,
encryptLogic: EncryptLogic
): Promise<string> => {
const metadataUrl = await this.uploadToIpfs?.uploadMetadata(
name,
description,
thumbnailImageUrl,
encryptedDataUrls,
mimeType,
encryptLogic
);
return metadataUrl as string;
};
/**
* Get all NFT metadata owned by a person who call this method
*
* @returns Array of token metadata
*/
getOwnTokens = async (): Promise<Metadata[]> => {
if (!this.signature) {
throw "please sign first";
}
const ownTokenIds = await this.nft.getOwnTokenIds();
return (await Promise.all(ownTokenIds.map(this.getMetadata.bind(this)))).filter(
(extractMetadata): extractMetadata is Metadata => extractMetadata !== undefined
);
};
/**
* Get token IDs owned by someone who called this function
*
* @returns Array of token IDs
*/
getOwnTokenIds = async (): Promise<number[]> => {
return await this.nft.getOwnTokenIds();
};
/**
* Get NFT metadata from given `tokenId`
*
* @remarks
* Check if a person call this method is a NFT owner, and if so, return a decrypted data.
*
* @param tokenId - The ID of NFT
* @returns Token metadata and an address of NFT owner
*/
getTokenById = async (tokenId: number): Promise<(ExtractMetadata | Metadata) & { owner: string }> => {
const isOwnerOrMinter = (await this.nft.isOwnerOf(tokenId)) || (await this.nft.isMinterOf(tokenId));
const owner = await this.nft.getOwner(tokenId);
const metadata = isOwnerOrMinter ? await this.extractMetadata(tokenId) : await this.getMetadata(tokenId);
if (!metadata) {
throw new Error("metadata not found");
}
return { ...metadata, owner };
};
/**
* Get token ids by minter address
* @param address - minter address
* @returns Token ids
*/
getTokenByMinter = async (address: string): Promise<number[]> => {
return await this.nft.getTokenByMinter(address);
};
/**
* Get NFT metadata from given `tokenId`
*
* @param tokenId - The ID of NFT
* @returns Token metadata
*/
getMetadata = async (tokenId: number): Promise<Metadata | undefined> => {
const metadataUrl = await this.nft.getMetadataUrl(tokenId);
const metadata = (await axios.get(metadataUrl).catch(() => undefined))?.data;
// delete token if metadata is not found
if (!metadata) {
return undefined;
}
return {
id: tokenId,
name: metadata.name,
description: metadata.description,
image: metadata.image,
mimeType: metadata.mime_type,
encryptLogic: metadata.encrypt_logic,
};
};
/**
* Get NFT metadata from given `tokenId`
*
* @remarks
* This method should be called by NFT owner.
*
* @param tokenId The ID of NFT
* @param contractAddress Optional: The contractAddress of any VWBL Token(ERC721 or ERC1155).
* @returns Token metadata
*/
extractMetadata = async (tokenId: number, contractAddress?: string): Promise<ExtractMetadata | undefined> => {
if (!this.signature) {
throw "please sign first";
}
if (contractAddress && !this.viewer) {
throw "please set dataCollectorAddress to constructor";
}
const metadataUrl =
contractAddress && this.viewer
? await this.viewer.getMetadataUrl(contractAddress, tokenId)
: await this.nft.getMetadataUrl(tokenId);
const metadata: PlainMetadata = (await axios.get(metadataUrl).catch(() => undefined))?.data;
// delete token if metadata is not found
if (!metadata) {
return undefined;
}
const documentId =
contractAddress && this.viewer
? await this.viewer.getDocumentId(contractAddress, tokenId)
: (await this.nft.getTokenInfo(tokenId)).documentId;
const chainId =
"web3" in this.opts ? await this.opts.web3.eth.getChainId() : await this.opts.ethersSigner.getChainId();
const signerAddress =
"web3" in this.opts
? await this._getAddressBySigner(this.opts.web3)
: await this._getAddressBySigner(this.opts.ethersSigner);
const decryptKey = await this.api.getKey(documentId, chainId, this.signature, signerAddress);
const encryptedDataUrls = metadata.encrypted_data;
const isRunningOnBrowser = typeof window !== "undefined";
const encryptLogic = metadata.encrypt_logic ?? "base64";
const ownDataArray = await Promise.all(
encryptedDataUrls.map(async (encryptedDataUrl) => {
const encryptedData = (
await axios.get(encryptedDataUrl, {
responseType: encryptLogic === "base64" ? "text" : isRunningOnBrowser ? "arraybuffer" : "stream",
})
).data;
return encryptLogic === "base64"
? decryptString(encryptedData, decryptKey)
: isRunningOnBrowser
? await decryptFile(encryptedData, decryptKey)
: decryptStream(encryptedData, decryptKey);
})
);
const ownFiles = ownDataArray.filter((ownData): ownData is ArrayBuffer => typeof ownData !== "string");
const ownDataBase64 = ownDataArray.filter((ownData): ownData is string => typeof ownData === "string");
const fileName = encryptedDataUrls[0]
.split("/")
.slice(-1)[0]
.replace(/\.vwbl/, "");
return {
id: tokenId,
name: metadata.name,
description: metadata.description,
image: metadata.image,
mimeType: metadata.mime_type,
encryptLogic: metadata.encrypt_logic,
ownDataBase64,
ownFiles,
fileName,
};
};
}