hyperns-service
Version:
HyperNS service, based on autobase
113 lines (91 loc) • 3.51 kB
JavaScript
const Hyperswarm = require('hyperswarm')
const IdEnc = require('hypercore-id-encoding')
const { command, flag, arg } = require('paparam')
const RAM = require('random-access-memory')
const Corestore = require('corestore')
const LookupClient = require('./lookup-client')
const RegisterClient = require('./register-client')
const register = command('register',
arg('<autobaseKey>', 'Public key of the name service\'s autobase'),
arg('<blindPeerKey>', 'Public key of the blind peer through which the request will be routed'),
arg('<name>', 'Name to register'),
arg('<publicKey>', 'Public key the name should point to'),
flag('--blind-peer|-b [blindPeers]', 'Public key of a blind-peer that can be used to contact you. Can be more than 1.').multiple(),
async function ({ args, flags }) {
const blindPeerKey = IdEnc.decode(args.blindPeerKey)
const autobaseKey = IdEnc.decode(args.autobaseKey)
const publicKey = IdEnc.decode(args.publicKey)
const { name } = args
const rawBlindPeers = flags.blindPeer || []
const blindPeers = rawBlindPeers.map(k => IdEnc.decode(k))
const swarm = new Hyperswarm()
const client = new RegisterClient(blindPeerKey, autobaseKey, swarm)
client.open()
console.log('Awaiting connection to blind peer...')
try {
await client.ensureConnected()
} catch (e) {
console.error('Failed to connect to blind peer')
process.exit(1)
}
console.log('Registering record with blind peer...')
const autobaseLength = await client.register(
name, publicKey, blindPeers
)
console.log(`Successfully requested to register ${name} -> ${IdEnc.normalize(publicKey)}`)
console.log(`autobase length: ${autobaseLength}`)
swarm.destroy()
}
)
const lookup = command('lookup',
arg('<nameServiceKey>', 'Public key of the name service\'s database (autobase view)'),
arg('<encryptionKey>', 'Encryption key of the name service\'s database'),
arg('<name>', 'Name to lookup'),
// Enable storage once we settle on the usage pattern
// flag('--storage|-s [path]', 'storage path, defaults to ./hyperns-cli'),
async function ({ args }) {
const nameServiceKey = IdEnc.decode(args.nameServiceKey)
const encryptionKey = IdEnc.decode(args.encryptionKey)
const { name } = args
if (!name) {
console.error('Name must be specified')
process.exit(1)
}
const swarm = new Hyperswarm()
const store = new Corestore(RAM.reusable())
swarm.on('connection', (conn) => {
store.replicate(conn)
})
const client = new LookupClient(
nameServiceKey, encryptionKey, swarm, store.namespace('hyperns-lookup')
)
await client.ready()
console.log('Loading database...')
try {
await client.ensureDbLoaded()
} catch (e) {
console.error(e.message)
process.exit(1)
}
console.log('Looking up...')
const record = await client.lookup(name)
console.log('Result:\n')
console.log(formatRecord(record))
await client.close()
await swarm.destroy()
await store.close()
}
)
function formatRecord (record) {
if (!record) return 'Not found'
const res = [`${record.name} -> ${IdEnc.normalize(record.publicKey)}`]
if (record.blindPeers.length === 0) return res[0]
res.push(' Blind peers:')
for (const p of record.blindPeers) {
res.push(` - ${IdEnc.normalize(p)}`)
}
return res.join('\n')
}
const cmd = command('hyperns-cli', register, lookup)
cmd.parse()