-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3.js
68 lines (55 loc) · 1.82 KB
/
web3.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
"use strict";
import { Signer } from "@ucanto/principal/ed25519";
import { CarReader } from "@ipld/car";
import { importDAG } from "@ucanto/core/delegation";
import * as Client from "@web3-storage/w3up-client";
import { StoreMemory } from "@web3-storage/access/stores/store-memory";
import { Blob } from "node:buffer";
/**
* Class representing an Uploader.
*/
class Uploader {
/**
* Create an uploader.
* @param {Object} client - The client object.
* @param {string} gateway - The gateway URL.
*/
constructor(client, gateway) {
this.client = client;
this.gateway = gateway;
}
/**
* Upload a file.
* @param {Buffer} buffer - The file data as a buffer.
* @return {Object} The upload response containing the URI and CID.
* @throws {Error} If an error occurs during upload.
*/
async uploadFile(buffer) {
const blob = new Blob([buffer]);
const response = await this.client.uploadFile(blob);
const cid = response.toString();
const uri = `${this.gateway}/${cid}`;
return { uri, cid };
}
}
/**
* Create a new uploader with a given key, proof, and gateway.
* @param {string} key - The key for the uploader.
* @param {string} proof - The proof for the uploader.
* @param {string} gateway - The gateway for the uploader.
* @return {Uploader} A new Uploader instance.
*/
export async function w3(key, proof, gateway) {
const reader = await CarReader.fromBytes(Buffer.from(proof, "base64"));
const principal = Signer.parse(key);
const store = new StoreMemory();
const client = await Client.create({ principal, store });
const blocks = [];
for await (const block of reader.blocks()) {
blocks.push(block);
}
const dag = importDAG(blocks);
const space = await client.addSpace(dag);
await client.setCurrentSpace(space.did());
return new Uploader(client, gateway);
}