123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- const debug = require('debug')('joystream:discovery:publish')
- const PUBLISH_KEY = 'self'
- function bufferFrom(data) {
- return Buffer.from(JSON.stringify(data), 'utf-8')
- }
- function encodeServiceInfo(info) {
- return bufferFrom({
- serialized: JSON.stringify(info),
- })
- }
- class PublisherClient {
-
- constructor(ipfs) {
- this.ipfs = ipfs || require('ipfs-http-client')('localhost', '5001', { protocol: 'http' })
- }
-
- async publish(serviceInfo) {
- const keys = await this.ipfs.key.list()
- let servicesKey = keys.find((key) => key.name === PUBLISH_KEY)
-
-
-
- if (PUBLISH_KEY !== 'self' && !servicesKey) {
- debug('generating ipns services key')
- servicesKey = await this.ipfs.key.gen(PUBLISH_KEY, {
- type: 'rsa',
- size: 2048,
- })
- }
- if (!servicesKey) {
- throw new Error('No IPFS publishing key available!')
- }
- debug('adding service info file to node')
- const files = await this.ipfs.add(encodeServiceInfo(serviceInfo))
- debug('publishing...')
- const { name, value } = await this.ipfs.name.publish(files[0].hash, {
- key: PUBLISH_KEY,
- resolve: false,
-
-
- })
- debug(`published ipns name: ${name} -> ${value}`)
-
-
-
- return servicesKey.id
- }
- }
- module.exports = {
- PublisherClient,
- }
|