Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add w3 CLI script for w3s driver #17

Merged
merged 7 commits into from
Feb 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions scripts/w3.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env node

// A tool acting like w3 CLI which enables executing a command with the given UCAN key and delegation proof.
// To install the dependencies, execute the following command
// npm i multiformats @ipld/[email protected] @ucanto/[email protected] @web3-storage/[email protected]

import fs from "fs";
import { CID } from "multiformats";
import { CarReader } from "@ipld/car";
import { derive } from "@ucanto/principal/ed25519";
import { importDAG } from "@ucanto/core/delegation";
import { AgentData } from "@web3-storage/access";
import { Client } from "@web3-storage/w3up-client";
leszko marked this conversation as resolved.
Show resolved Hide resolved

async function getClient() {
// create a client with UCAN private key passed in the env variable
const principal = await derive(
Buffer.from(process.env.W3_PRINCIPAL_KEY, "base64")
);
const data = await AgentData.create({ principal });
const client = new Client(data);

// create a space with the delegation proof passsed in the env variable
const blocks = [];
const reader = await CarReader.fromBytes(
Buffer.from(process.env.W3_DELEGATION_PROOF, "base64")
);
for await (const block of reader.blocks()) {
blocks.push(block);
}
const proof = importDAG(blocks);

const space = await client.addSpace(proof);
await client.setCurrentSpace(space.did());

return client;
}

async function storeAdd(carPath) {
const client = await getClient();

let blob;
try {
const data = await fs.promises.readFile(carPath);
blob = new Blob([data]);
} catch (err) {
console.log(err);
process.exit(1);
}

const cid = await client.capability.store.add(blob);
console.log(cid.toString());
}

async function uploadAdd(root, carCids) {
const client = await getClient();

let rootCID;
try {
rootCID = CID.parse(root);
} catch (err) {
console.error(`Error: failed to parse root CID: ${root}: ${err.message}`);
process.exit(1);
}

const shards = [];
for (const cid of carCids) {
try {
shards.push(CID.parse(cid));
} catch (err) {
console.error(`Error: failed to parse shard CID: ${cid}: ${err.message}`);
process.exit(1);
}
}

await client.capability.upload.add(rootCID, shards);
console.log(root);
}

(async () => {
try {
const command = process.argv.slice(2, 5);
const args = process.argv.slice(5);
if (command.join(" ") === "can store add") {
const carPath = args[0];
await storeAdd(carPath);
} else if (command.join(" ") === "can upload add") {
const root = args[0];
const carCids = args.slice(1);
await uploadAdd(root, carCids);
} else {
console.log(`Invalid command: ${command.join(" ")}`);
process.exit(1);
}
} catch (err) {
console.log(err);
process.exit(1);
}
})();