-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
25a350d
Add w3 CLI script which accepts UCAN private key and the delegation p…
leszko b59b485
Move w3 to a separate directory with package.json
leszko 2f67aba
rename index.js to livepeer-w3.js
leszko 927fdef
Add usage of livepeer-w3 CLI
leszko 0a8466c
Update livepeer-w3.js dependencies
leszko f7a56b9
Read W3S private key from env variable instead of a global variable
leszko 365b3c6
Fix unit tests
leszko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env node | ||
|
||
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"; | ||
|
||
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); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "livepeer-w3", | ||
"type": "module", | ||
"version": "1.0.0", | ||
"description": "W3 CLI which enables executing a command with the given UCAN key and delegation proof", | ||
"main": "livepeer-w3.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@ipld/car": "^5.1.0", | ||
"@web3-storage/w3up-client": "^4.1.0", | ||
"multiformats": "^11.0.1" | ||
}, | ||
"bin": { | ||
"livepeer-w3": "livepeer-w3.js" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there is an opp to simplify how this key is configured. I see that right now the flow is:
W3sUcanKey
with the valueW3sUcanKey
and set it as an env var when calling the livepeer-w3 scriptAn alternative might be:
W3_PRINCIPAL_KEY
in the environment that catalyst-api is running in i.e. in the Docker containerW3_PRINCIPAL_KEY
env var value directly without any additional value passing which reduces the # of code paths that have to be aware of this valueI see we've already merged the relevant catalyst-api PRs so maybe consider whether this might be a worthwhile simplification separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My initial thinking was that not storing the private key in the environment variable is more secure because even if you access the container, you still cannot access it.
However, now I think you're right because anyway we pass the key in the env variable to catalyst. So, maybe let's get rid of the Catalyst flag and just use the private key passed in the
W3_PRINCIPAL_KEY
. Thanks for this comment @yondonfu