forked from web3-storage/web3.storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name.js
67 lines (59 loc) · 1.68 KB
/
name.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
import * as Name from 'w3name'
import * as uint8arrays from 'uint8arrays'
import { config } 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))
}
/**
* Publish the given value as an IPNS record to w3name under the given key.
* @param {string} keyId
* @param {string} value
*/
export async function publish (keyId, value) {
const b64SigningKey = config.get(`name.${keyId}`)
if (!b64SigningKey) {
throw new Error('missing signing key')
}
const name = await Name.from(uint8arrays.fromString(b64SigningKey, 'base64pad'))
/** @type {Name.Revision} */
let revision
try {
revision = await Name.resolve(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(revision, name.key)
}
/**
* Resolve the w3name record for the given key.
* @param {string} keyId
*/
export async function resolve (keyId) {
const name = Name.parse(keyId)
const revision = await Name.resolve(name)
console.log(revision.value)
}
/**
* Retrieve the signing key associated with the given key.
* @param {string} keyId
*/
export function getKey (keyId) {
const signingKey = config.get(`name.${keyId}`)
if (!signingKey) {
throw new Error('missing signing key for the provided <keyId>')
}
console.log(signingKey)
}
/**
* @param {string} keyId
*/
export function rm (keyId) {
config.delete(`name.${keyId}`)
}