-
Notifications
You must be signed in to change notification settings - Fork 4
/
publishToIpfs.ts
341 lines (283 loc) · 8.5 KB
/
publishToIpfs.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
import {
CID,
create,
globSource,
IPFSHTTPClient,
Options,
urlSource,
} from "ipfs-http-client";
import chalk from "chalk";
import { clearLine } from "readline";
import { SceneConfiguration } from "../../src/Scene/Config/types/scene";
import {
Element,
ElementNodes,
ElementType,
} from "../../src/Scene/Config/types/elements";
import marbleScene, {
marbleSceneMods,
} from "../../src/Scene/Config/marbleScene";
import temp from "temp";
import http from "https"; // or 'https' for https:// URLs
import fs from "fs";
import { AvailableModifications } from "../../src/Scene/Config/types/modifications";
import "dotenv/config";
import { NFTStorage, File, CIDString } from "nft.storage";
import path from "path";
import mime from "mime";
const NFT_STORAGE_KEY = process.env.NFT_STORAGE_KEY;
temp.track();
interface AddResult {
cid: CID;
size: number;
path: string;
mode?: number;
// @ts-ignore
mtime?: Mtime;
}
const infura = { host: "ipfs.infura.io", port: 5001, protocol: "https" };
// run your own ipfs daemon: https://docs.ipfs.io/how-to/command-line-quick-start/#install-ipfs
const localhost = { host: "127.0.0.1", port: 5002, protocol: "http" };
const nftStorage = new NFTStorage({ token: NFT_STORAGE_KEY });
const pinata: Options = {
host: "api.pinata.cloud",
apiPath: "psa",
port: 443,
protocol: "https",
headers: {
Authorization: `Bearer ${process.env.PINATA_JWT}`,
},
};
const nftStorageParams: Options = {
host: "api.nft.storage",
apiPath: "pins",
port: 443,
protocol: "https",
headers: {
Authorization: `Bearer ${NFT_STORAGE_KEY}`,
["Content-Type"]: "application/json",
Accept: "*/*",
},
};
const ipfs = create(nftStorageParams);
const ipfsGateway = "https://ipfs.io/ipfs/";
// const pinataGateway = 'https://landa.mypinata.cloud';
const ipnsGateway = "https://ipfs.io/ipns/";
const addOptions = {
pin: true,
wrapWithDirectory: true,
};
const downloadTempFile = async (remoteUrl: string) => {
const tempLocation = await temp.open("tempFile");
const file = fs.createWriteStream(tempLocation.path);
return new Promise<string>((resolve, reject) => {
http.get(remoteUrl, function (response) {
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
console.log("Download Completed");
resolve(tempLocation.path);
});
});
});
};
const saveTempFileString = async (contents: string) => {
const tempLocation = await temp.open("tempFile");
await fs.promises.writeFile(tempLocation.path, contents);
console.log('wrote to', tempLocation.path)
return tempLocation.path;
}
const publishFileUsingIpfsPost = async (url: string) => {
console.log("publishing file", url);
// const tempFile = await downloadTempFile(url)
const urlS = urlSource(url);
// @ts-ignore
const result = await ipfs.add(urlS);
return toIpfsAddress(result.cid);
};
const publishBlobToNftStorage = async (url: string, overrideFileType = undefined) => {
const filePath = await downloadTempFile(url);
const file = await fileFromPath(filePath, overrideFileType);
const cid = await nftStorage.storeBlob(file);
// @ts-ignore
return toIpfsAddress(cid);
}
/**
* A helper to read a file from a location on disk and return a File object.
* Note that this reads the entire file into memory and should not be used for
* very large files.
* @param {string} filePath the path to a file to store
* @returns {File} a File object containing the file content
*/
async function fileFromPath(filePath: string, overrideFileType = undefined) {
const content = await fs.promises.readFile(filePath);
// @ts-ignore
const type = overrideFileType || mime.getType(filePath);
return new File([content], path.basename(filePath), { type });
}
const toIpfsAddress = (cid: CID | CIDString) => `ipfs://${cid.toString()}`;
const publishElementsToIps = async (elements: ElementNodes) => {
const result = await Promise.all(
Object.entries(elements).map(async ([key, element]) => {
const children = element.children
? await publishElementsToIps(element.children)
: undefined;
if (element.elementType === ElementType.Model) {
return {
key,
element: {
...element,
children,
modelConfig: {
...element.modelConfig,
fileUrl: element.modelConfig.fileUrl
? await publishBlobToNftStorage(element.modelConfig.fileUrl, "application/octet-stream")
: undefined,
},
},
};
}
if (element.elementType === ElementType.Image) {
const { imageConfig } = element;
return {
key,
element: {
...element,
children,
imageConfig: {
...imageConfig,
fileUrl: imageConfig.fileUrl
? await publishBlobToNftStorage(imageConfig.fileUrl)
: undefined,
},
},
};
}
if (element.elementType === ElementType.Video) {
const { videoConfig } = element;
return {
key,
element: {
...element,
children,
videoConfig: {
...videoConfig,
file: {
originalUrl: videoConfig?.file?.originalUrl
? await publishBlobToNftStorage(videoConfig.file.originalUrl)
: undefined,
},
},
},
};
}
return {
key,
element: {
...element,
children,
},
};
})
);
return result.reduce((acc: ElementNodes, { key, element }) => {
return {
...acc,
[key]: element,
};
}, {});
};
const publishFilesInGraphToIpfs = async (
config: SceneConfiguration
): Promise<SceneConfiguration> => {
return {
environment: {
fileUrl: config.environment?.fileUrl
? await publishBlobToNftStorage(config.environment.fileUrl)
: undefined,
},
elements: config.elements
? await publishElementsToIps(config.elements)
: undefined,
};
};
const publishJsonToIpfs = async (json: object) => {
const storedJsonPath = await saveTempFileString(JSON.stringify(json));
const file = await fileFromPath(storedJsonPath);
// file.type = 'text/json';
// const file =new Blob([JSON.stringify(json)], {
// type: 'text/json'
// });
const modIpfsCid = await nftStorage.storeBlob(file );
return modIpfsCid;
}
const publishConfigToIpfs = async (config: SceneConfiguration) => {
const withFileOnIpFs = await publishFilesInGraphToIpfs(config);
// console.log(require('util').inspect(withFileOnIpFs, {depth: null}));=
const configIpfsUrl = await ipfs.add(JSON.stringify(withFileOnIpFs, null, 2));
return toIpfsAddress(configIpfsUrl.cid);
};
export const pushDirectoryToIPFS = async (path: string) => {
const file = ipfs.addAll(globSource(path, "**/*"), addOptions);
const resp: AddResult[] = [];
for await (const f of file) {
resp.push(f);
}
return resp;
};
export interface Erc721Token {
name: string;
description?: string;
animation_url: string;
scene_config: SceneConfiguration;
}
const publishToken = async ({
name,
sceneConfig,
}: {
name: string;
sceneConfig: SceneConfiguration;
}) => {
const configJson = await publishFilesInGraphToIpfs(sceneConfig);
console.log({
configJson,
sceneConfig,
});
const tokenMetadata: Erc721Token = {
name,
animation_url: "",
scene_config: configJson,
};
const tokenIpfsCif = await publishJsonToIpfs(tokenMetadata);
// const tokenIpfsCif = await ipfs.add(JSON.stringify(tokenMetadata, null, 2));
console.log('published token:');
console.log(JSON.stringify(tokenMetadata, null, 2));
// @ts-ignore
const tokenIpfsCifUrl = toIpfsAddress(tokenIpfsCif);
console.log("token ipfs address:", tokenIpfsCifUrl);
};
const publishNft = async ({
name,
sceneConfig,
availableMods,
}: {
name: string;
sceneConfig: SceneConfiguration;
availableMods: AvailableModifications;
}) => {
await publishToken({ name, sceneConfig });
console.log('token published');
await Promise.all(
Object.entries(availableMods).map(async ([key, mod]) => {
const modIpfsCid = await publishJsonToIpfs(mod);
const modIpfsCifUrl = toIpfsAddress(modIpfsCid);
console.log("mod ipfs address:", key, modIpfsCifUrl);
})
);
};
publishNft({
sceneConfig: marbleScene,
availableMods: marbleSceneMods,
name: "marble scene",
});