-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
99 lines (82 loc) · 2.6 KB
/
app.js
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
const IPFS = require("ipfs-core");
const NFTStorage = require("nft.storage");
require("dotenv").config();
async function basicIPFS() {
const ipfs = await IPFS.create();
const { cid } = await ipfs.add(
JSON.stringify({
name: "samarth",
})
);
console.info(cid);
}
async function nftStorage() {
try {
const API_TOKEN = process.env.API_TOKEN;
console.log(API_TOKEN);
const obj = { token: API_TOKEN.toString() };
const client = new NFTStorage.NFTStorage(obj);
const cid = await client.storeDirectory([
new NFTStorage.File(["hello world"], "patient.json"),
new NFTStorage.File(
[JSON.stringify({ from: "incognito" }, null, 2)],
"metadata.json"
),
]);
var spawn = require("child_process").spawn;
var process = spawn("python", ["./nucypher/nucypher.py", cid]);
process.stdout.on("data", function (data) {
console.log(data);
});
//
} catch (err) {
console.log("err");
basicIPFS();
}
}
async function pinTokenData(tokenId) {
const { metadata, metadataURI } = await this.getNFTMetadata(tokenId);
const { image: assetURI } = metadata;
console.log(`Pinning asset data (${assetURI}) for token id ${tokenId}....`);
await this.pin(assetURI);
console.log(`Pinning metadata (${metadataURI}) for token id ${tokenId}...`);
await this.pin(metadataURI);
return { assetURI, metadataURI };
}
async function pin(cidOrURI) {
const cid = extractCID(cidOrURI);
await this._configurePinningService();
const pinned = await this.isPinned(cid);
if (pinned) {
return;
}
await this.ipfs.pin.remote.add(cid, { service: config.pinningService.name });
}
async function createNFTFromAssetData(content, options) {
const filePath = options.path;
const basename = path.basename(filePath);
const ipfsPath = "/nft/" + basename;
const { cid: assetCid } = await this.ipfs.add({ path: ipfsPath, content });
const assetURI = ensureIpfsUriPrefix(assetCid) + "/" + basename;
const metadata = await this.makeNFTMetadata(assetURI, options);
const { cid: metadataCid } = await this.ipfs.add({
path: "/nft/metadata.json",
content: JSON.stringify(metadata),
});
const metadataURI = ensureIpfsUriPrefix(metadataCid) + "/metadata.json";
let ownerAddress = options.owner;
if (!ownerAddress) {
ownerAddress = await this.defaultOwnerAddress();
}
const tokenId = await this.mintToken(ownerAddress, metadataURI);
return {
tokenId,
metadata,
assetURI,
metadataURI,
assetGatewayURL: makeGatewayURL(assetURI),
metadataGatewayURL: makeGatewayURL(metadataURI),
};
}
nftStorage();
//basicIPFS();