-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy patharweave.ts
669 lines (560 loc) · 25 KB
/
arweave.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
import Arweave from "arweave"
import Transaction from "arweave/node/lib/transaction"
import crypto from "crypto"
import ora from "ora"
import inquirer from "inquirer"
import chalk from "chalk"
import fs from "fs"
import { Command } from "commander"
const MAX_RETRIES = 3;
export const createArTx = async (arweave: Arweave, data: Buffer, wallet: any, contentType: string) => {
let tags = new Tags()
tags.addTag('Content-Type', contentType)
tags.addTag('User-Agent', "lighthouse")
tags.addTag('User-Agent-Version', "2.0.0")
tags.addTag('Type', 'file')
tags.addTag('File-Hash', hashFile(data))
let tx = await arweave.createTransaction({ data }, wallet)
tags.addTagsToTransaction(tx)
return tx
}
export const signArTx = async (arweave: Arweave, tx: Transaction, wallet: any) => {
await arweave.transactions.sign(tx, wallet)
return tx
}
export const submitArTx = async (arweave: Arweave, tx: Transaction) => new Promise(async (resolve, reject) => {
let uploader = await arweave.transactions.getUploader(tx)
try {
while (!uploader.isComplete) {
await uploader.uploadChunk()
}
} catch (err) {
if (uploader.lastResponseStatus > 0) {
return reject({
status: uploader.lastResponseStatus,
statusText: uploader.lastResponseError,
})
}
}
resolve(tx.id)
})
export interface manifestData {
txid: string,
path: string,
}
export const createManifestTx = async (arweave: Arweave, txs: manifestData[], wallet: any) => {
let paths: any = {}
txs.forEach(({ txid, path }) => {
paths[path] = { id: txid }
})
const data = {
manifest: 'arweave/paths',
version: '0.1.0',
index: {
path: "-1",
},
paths,
}
let tx = await arweave.createTransaction(
{
data: JSON.stringify(data),
},
wallet,
)
let tags = new Tags()
tags.addTag('Type', 'manifest')
tags.addTag('Content-Type', 'application/x.arweave-manifest+json')
tags.addTagsToTransaction(tx)
return tx
}
class Tags {
_tags = new Map();
constructor() {
this._tags = new Map();
}
get tags() {
return Array.from(this._tags.entries()).map(([name, value]) => ({ name, value }));
}
addTag(key: any, value: any) {
this._tags.set(key, value);
}
addTags(tags: any) {
tags.forEach(({ name, value }: any) => this.addTag(name, value));
}
addTagsToTransaction(tx: Transaction) {
this.tags.forEach(({ name, value }) => tx.addTag(name, value));
}
}
const hashFile = (data: Buffer) => {
const hash = crypto.createHash('sha256')
hash.update(data)
return hash.digest('hex')
}
export const contentTypeOf = (name: string) => {
if (name.endsWith(".png")) return "image/png"
if (name.endsWith(".jpg")) return "image/jpeg"
if (name.endsWith(".jpeg")) return "image/jpeg"
if (name.endsWith(".gif")) return "image/gif"
if (name.endsWith(".svg")) return "image/svg+xml"
if (name.endsWith(".webp")) return "image/webp"
if (name.endsWith(".bmp")) return "image/bmp"
if (name.endsWith(".ico")) return "image/vnd.microsoft.icon"
if (name.endsWith(".tiff")) return "image/tiff"
if (name.endsWith(".tif")) return "image/tiff"
if (name.endsWith(".avif")) return "image/avif"
if (name.endsWith(".apng")) return "image/apng"
if (name.endsWith(".jfif")) return "image/jpeg"
if (name.endsWith(".pjpeg")) return "image/jpeg"
if (name.endsWith(".pjp")) return "image/jpeg"
if (name.endsWith('.html') || name.endsWith('.htm')) return 'text/html'
if (name.endsWith(".mp4")) return "video/mp4"
if (name.endsWith(".webm")) return "video/webm"
if (name.endsWith(".avi")) return "video/x-msvideo"
if (name.endsWith(".flv")) return "video/x-flv"
if (name.endsWith(".mov")) return "video/quicktime"
if (name.endsWith(".wmv")) return "video/x-ms-wmv"
if (name.endsWith(".mp3")) return "audio/mpeg"
if (name.endsWith(".wav")) return "audio/wav"
return "application/octet-stream"
}
export const command_metadata = (root: Command) => {
root
.command("validate")
.description("Validate JSON metadata files")
.action(() => {
let spinner = ora("Validating files").start()
let files = fs.readdirSync("./assets").filter((file) => file.endsWith(".json"))
//validate images
for (let file of files) {
try {
let metadata = JSON.parse(fs.readFileSync("./assets/" + file, "utf-8"))
if (!metadata.image) {
spinner.fail("Image field not found in metadata file " + file)
return
} else {
if (!fs.existsSync("./assets/" + metadata.image)) {
spinner.fail("Image not found for metadata file " + file)
return
}
}
} catch (e) {
spinner.fail("Invalid metadata file " + file)
return
}
}
spinner.succeed("Files valid")
})
root
.command("upload")
.description("Upload JSON metadata files to Arweave")
.option("-w --wallet <wallet>", "Path to Arweave keypair JSON file")
.option("-u --url <url>", "Arweave node URL (default: https://arweave.net)")
.action(async (answers) => {
//get wallet
let walletPath = answers.wallet ? answers.wallet : null
if (!answers.wallet) {
let response = await inquirer.prompt([
{
type: "input",
name: "wallet",
message: "Path to arweave keypair JSON file",
}
])
walletPath = response.wallet
}
//arrange host
let host = answers.url ? answers.url : ""
let port = ""
let protocol = ""
let fullurl = answers.url ? answers.url : ""
if (host) {
if (host.startsWith("https://")) {
port = "443"
protocol = "https"
host = host.split("https://")[1]
//remove port from host if exists
if (host.indexOf(":") !== -1)
host = host.split(":")[0]
} else if (host.startsWith("http://")) {
protocol = "http"
//if host has a port
if (host.indexOf(":") !== -1)
port = host.split(":")[2]
else
port = "80"
host = host.split("http://")[1]
//remove port from host if exists
if (host.indexOf(":") !== -1)
host = host.split(":")[0]
} else
return console.log(chalk.red("Invalid url"))
if (fullurl.endsWith("/"))
fullurl = fullurl.substring(0, fullurl.length - 1)
} else {
host = "arweave.net"
port = "443"
protocol = "https"
fullurl = "https://arweave.net"
}
let spinner = ora("Validating files").start()
let files = fs.readdirSync("./assets").filter((file) => file.endsWith(".json"))
if (files.length === 0) {
spinner.fail("No files found in assets folder")
return
}
//validate images
for (let file of files) {
try {
let metadata = JSON.parse(fs.readFileSync("./assets/" + file, "utf-8"))
if (!metadata.image) {
spinner.fail("Image field not found in metadata file " + file)
return
} else {
if (!fs.existsSync("./assets/" + metadata.image)) {
spinner.fail("Image not found for metadata file " + file)
return
}
}
} catch (e) {
spinner.fail("Invalid metadata file " + file)
return
}
}
spinner.succeed("Files validated")
//init arweave
let arweave = Arweave.init({
host,
port,
protocol,
})
//logs
let logs: any = []
//setup cache
let cache: any = {
images: [],
metadata: [],
imagesManifest: "",
metadataManifest: ""
}
//if cache exists, load it
if (fs.existsSync("./cache.json")) {
let cachefile = JSON.parse(fs.readFileSync("./cache.json", "utf-8"))
//ask if use cache
let useCache = await inquirer.prompt([
{
type: "confirm",
name: "useCache",
message: "Would you like to use the cache file for previous content?",
default: true
}
])
if (useCache.useCache) {
cache = cachefile
//check if cache is valid
if (!Array.isArray(cache.images) || !Array.isArray(cache.metadata) || typeof cache.imagesManifest !== "string" || typeof cache.metadataManifest !== "string") {
spinner.fail("Invalid cache file")
return
}
}
}
//load wallet
let wallet = JSON.parse(fs.readFileSync(walletPath, "utf-8"))
//calculate cost
let totalSize = 0
for (let file of files) {
let metadata = JSON.parse(fs.readFileSync("./assets/" + file, "utf-8"))
let metadataFileSize = Buffer.from(JSON.stringify(metadata)).length
let imageFileSize = fs.statSync("./assets/" + metadata.image).size
if (cache.metadata.filter((m: any) => m.name === file.split(".json")[0]).length > 0)
metadataFileSize = 0
if (cache.images.filter((image: any) => image.name === metadata.image).length > 0)
imageFileSize = 0
totalSize += metadataFileSize + imageFileSize
}
let cost = await arweave.transactions.getPrice(totalSize)
let constInAr = arweave.ar.winstonToAr(cost)
//ask for confirmation
let confirm = await inquirer.prompt([
{
type: "confirm",
name: "confirm",
message: "This will cost ~" + constInAr + " AR. Continue? (this is an estimate and may vary depending on the network and file sizes)",
default: true
}
])
if (!confirm.confirm)
return
//check balance
let address = await arweave.wallets.jwkToAddress(wallet)
let balance = await arweave.wallets.getBalance(address)
if (BigInt(balance) < BigInt(cost)) {
spinner.fail("Insufficient AR balance")
return
}
//log failed
let failedImages = []
let failedMetadata = []
spinner = ora("Uploading images").start()
let counter = 0
//upload images
for (let i = 0; i < files.length; i++) {
let retryCount = 0;
let file = files[i]
let metadata = JSON.parse(fs.readFileSync("./assets/" + file, "utf-8"))
//if metadata.image is not in cache.images, upload it
if (cache.images.filter((image: any) => image.name === metadata.image).length > 0) {
counter++
continue
}
let image = fs.readFileSync("./assets/" + metadata.image)
while (retryCount < MAX_RETRIES) {
try {
let tx = await createArTx(arweave, image, wallet, contentTypeOf(metadata.image))
tx = await signArTx(arweave, tx, wallet)
await submitArTx(arweave, tx)
cache.images.push({
name: metadata.image,
txid: tx.id
})
counter++
spinner.text = "Uploading images (" + counter + "/" + files.length + ") - (failed: " + failedImages.length + ")"
break;
} catch (e: any) {
try {
if (e.message && e.message.indexOf("429") !== -1) {
let waitTime = (2 ** retryCount) * 60000; // Exponential backoff
spinner.text = `Rate limit reached, waiting ${waitTime / 60000} minutes`;
await delay(waitTime);
retryCount++;
} else {
failedImages.push(metadata.image);
counter++;
spinner.text = "Uploading images (" + counter + "/" + files.length + ") - (failed: " + failedImages.length + ")";
logs.push({
type: "error",
message: "Failed to upload image " + metadata.image,
error: e
});
break;
}
} catch (e) {
failedImages.push(metadata.image);
counter++;
spinner.text = "Uploading images (" + counter + "/" + files.length + ") - (failed: " + failedImages.length + ")";
logs.push({
type: "error",
message: "Failed to upload image " + metadata.image,
error: e
});
break;
}
}
// If the retry count has reached the maximum, log the failure and stop the CLI
if (retryCount === MAX_RETRIES) {
logs.push({
type: "fatal",
message: `Failed to upload image ${metadata.image} after ${MAX_RETRIES} retries.`,
error: `Error 429: Too Many Requests`
});
// Save logs before exiting
saveLogs(logs);
//save cache before exiting
fs.writeFileSync("./cache.json", JSON.stringify(cache, null, 4))
throw new Error(`Failed to upload image ${metadata.image} after ${MAX_RETRIES} retries. (429)`);
}
}
}
spinner.succeed("Images complete")
//save cache
fs.writeFileSync("./cache.json", JSON.stringify(cache, null, 4))
//save logs
saveLogs(logs)
if (failedImages.length > 0) {
console.log(chalk.red("Failed to upload " + failedImages.length + " images"))
console.log()
console.log("re run upload command to upload failed images")
return
}
spinner = ora("Creating images manifest").start()
//create images manifest
if (cache.imagesManifest === "") {
let manifest: manifestData[] = []
for (let image of cache.images) {
manifest.push({
txid: image.txid,
path: image.name
})
}
try {
let manifestTx = await createManifestTx(arweave, manifest, wallet)
manifestTx = await signArTx(arweave, manifestTx, wallet)
await submitArTx(arweave, manifestTx)
cache.imagesManifest = manifestTx.id
} catch (e) {
spinner.fail("Failed to upload images manifest")
console.log("re run upload command to retry upload")
logs.push({
type: "error",
message: "Failed to upload image manifest",
error: e
})
saveLogs(logs)
return
}
}
spinner.succeed("Images manifest uploaded")
spinner = ora("Uploading metadata").start()
counter = 0
//save cache
fs.writeFileSync("./cache.json", JSON.stringify(cache, null, 4))
//upload metadata
for (let i = 0; i < files.length; i++) {
let retryCount = 0;
let file = files[i]
let metadata = JSON.parse(fs.readFileSync("./assets/" + file, "utf-8"))
//replace image with imagesManifest + txid
metadata.image = fullurl + "/" + cache.imagesManifest + "/" + metadata.image
//if metadata is not in cache.metadata, upload it
if (cache.metadata.filter((m: any) => m.name === file.split(".json")[0]).length > 0) {
counter++
continue
}
while (retryCount < MAX_RETRIES) {
try {
let tx = await createArTx(arweave, Buffer.from(JSON.stringify(metadata)), wallet, "application/json")
tx = await signArTx(arweave, tx, wallet)
await submitArTx(arweave, tx)
cache.metadata.push({
name: file.split(".json")[0],
txid: tx.id
})
counter++
spinner.text = "Uploading metadata (" + counter + "/" + files.length + ") - (failed: " + failedMetadata.length + ")"
break;
} catch (e: any) {
try {
if (e.message && e.message.indexOf("429") !== -1) {
let waitTime = (2 ** retryCount) * 60000; // Exponential backoff
spinner.text = `Rate limit reached, waiting ${waitTime / 60000} minutes`;
await delay(waitTime);
retryCount++;
} else {
failedMetadata.push(file)
counter++
spinner.text = "Uploading metadata (" + counter + "/" + files.length + ") - (failed: " + failedMetadata.length + ")"
logs.push({
type: "error",
message: "Failed to upload metadata " + file,
error: e
})
break;
}
} catch (e) {
failedMetadata.push(file)
counter++
spinner.text = "Uploading metadata (" + counter + "/" + files.length + ") - (failed: " + failedMetadata.length + ")"
logs.push({
type: "error",
message: "Failed to upload metadata " + file,
error: e
})
break;
}
}
}
// If the retry count has reached the maximum, log the failure and stop the CLI
if (retryCount === MAX_RETRIES) {
logs.push({
type: "fatal",
message: `Failed to upload metadata ${counter} after ${MAX_RETRIES} retries.`,
error: `Error 429: Too Many Requests`
});
// Save logs before exiting
saveLogs(logs);
//save cache before exiting
fs.writeFileSync("./cache.json", JSON.stringify(cache, null, 4))
throw new Error(`Failed to upload metadata ${counter} after ${MAX_RETRIES} retries. (429)`);
}
}
spinner.succeed("Metadata complete")
saveLogs(logs)
//save cache
fs.writeFileSync("./cache.json", JSON.stringify(cache, null, 4))
if (failedMetadata.length > 0) {
console.log(chalk.red("Failed to upload " + failedMetadata.length + " metadata files"))
console.log("re run upload command to upload failed metadatas")
return
}
spinner = ora("Creating metadata manifest").start()
//create metadata manifest
let manifest: manifestData[] = []
for (let metadata of cache.metadata) {
manifest.push({
txid: metadata.txid,
path: metadata.name
})
}
try {
let manifestTx = await createManifestTx(arweave, manifest, wallet)
manifestTx = await signArTx(arweave, manifestTx, wallet)
await submitArTx(arweave, manifestTx)
cache.metadataManifest = manifestTx.id
} catch (e) {
spinner.fail("Failed to upload metadata manifest")
console.log("re run upload command to retry")
logs.push({
type: "error",
message: "Failed to upload image manifest",
error: e
})
saveLogs(logs)
return
}
spinner.succeed("Metadata manifest uploaded")
//save cache
fs.writeFileSync("./cache.json", JSON.stringify(cache, null, 4))
console.log(chalk.green("Upload complete"))
//print metadata manifest
console.log("Root url: " + chalk.green(fullurl + "/" + cache.metadataManifest))
//ask for save to config
let save = await inquirer.prompt([
{
type: "confirm",
name: "save",
message: "Save metadata manifest to config.json?",
default: true
}
])
if (save.save) {
//if config.json exists
if (fs.existsSync("./config.json")) {
let config = JSON.parse(fs.readFileSync("./config.json", "utf-8"))
config.token_uri = fullurl + "/" + cache.metadataManifest
fs.writeFileSync("./config.json", JSON.stringify(config, null, 4))
} else {
//create config.json with token_uri
let config = {
token_uri: fullurl + "/" + cache.metadataManifest
}
fs.writeFileSync("./config.json", JSON.stringify(config, null, 4))
}
console.log(chalk.green("Done!"))
}
})
}
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const saveLogs = (logs: any) => {
//add logs to log file if exists
if (fs.existsSync("./logs.json")) {
let logFile: any = []
try {
logFile = JSON.parse(fs.readFileSync("./logs.json", "utf-8"))
logFile.push(logs)
} catch (e) {
logFile = logs
}
fs.writeFileSync("./logs.json", JSON.stringify(logFile, null, 4))
} else {
fs.writeFileSync("./logs.json", JSON.stringify(logs, null, 4))
}
}