-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds some simple CLI commands for managing w3 names.
- Loading branch information
Alan Shaw
authored
Jan 19, 2022
1 parent
0bb853c
commit 3fc09db
Showing
7 changed files
with
184 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,39 @@ | ||
import fs from 'fs' | ||
import Conf from 'conf' | ||
import { Web3Storage } from 'web3.storage' | ||
|
||
export const API = 'https://api.web3.storage' | ||
|
||
export const config = new Conf({ | ||
projectName: 'w3', | ||
projectVersion: getPkg().version, | ||
configFileMode: 0o600 | ||
}) | ||
|
||
export function getPkg () { | ||
return JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url))) | ||
} | ||
|
||
/** | ||
* Get a new API client configured either from opts or config | ||
* @param {object} opts | ||
* @param {string} [opts.api] | ||
* @param {string} [opts.token] | ||
* @param {boolean} [opts.json] | ||
*/ | ||
export function getClient ({ | ||
api = config.get('api') || API, | ||
token = config.get('token'), | ||
json = false | ||
}) { | ||
if (!token) { | ||
console.log('! run `w3 token` to set an API token to use') | ||
process.exit(-1) | ||
} | ||
const endpoint = new URL(api) | ||
if (api !== API && !json) { | ||
// note if we're using something other than prod. | ||
console.log(`⁂ using ${endpoint.hostname}`) | ||
} | ||
return new Web3Storage({ token, endpoint }) | ||
} |
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,59 @@ | ||
import * as Name from 'web3.storage/name' | ||
import * as uint8arrays from 'uint8arrays' | ||
import { config, getClient } from './lib.js' | ||
|
||
export async function create () { | ||
const name = await Name.create() | ||
config.set(`name.${name}`, uint8arrays.toString(name.key.bytes, 'base64pad')) | ||
console.log(name.toString()) | ||
} | ||
|
||
export function list () { | ||
Object.keys(config.store.name || {}).forEach(keyId => console.log(keyId)) | ||
} | ||
|
||
/** | ||
* @param {string} keyId | ||
* @param {string} value | ||
* @param {string} [opts.api] | ||
* @param {string} [opts.token] | ||
*/ | ||
export async function publish (keyId, value, opts) { | ||
const b64SigningKey = config.get(`name.${keyId}`) | ||
if (!b64SigningKey) { | ||
throw new Error('missing signing key') | ||
} | ||
|
||
const client = getClient(opts) | ||
const name = await Name.from(uint8arrays.fromString(b64SigningKey, 'base64pad')) | ||
/** @type {Name.Revision} */ | ||
let revision | ||
try { | ||
revision = await Name.resolve(client, name) | ||
revision = await Name.increment(revision, value) | ||
} catch (err) { | ||
if (!err.message.includes('not found')) throw err | ||
revision = await Name.v0(name, value) | ||
} | ||
|
||
await Name.publish(client, revision, name.key) | ||
} | ||
|
||
/** | ||
* @param {string} keyId | ||
* @param {object} opts | ||
* @param {string} [opts.api] | ||
* @param {string} [opts.token] | ||
*/ | ||
export async function resolve (keyId, opts) { | ||
const name = Name.parse(keyId) | ||
const revision = await Name.resolve(getClient(opts), name) | ||
console.log(revision.value) | ||
} | ||
|
||
/** | ||
* @param {string} keyId | ||
*/ | ||
export function rm (keyId) { | ||
config.delete(`name.${keyId}`) | ||
} |
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