12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- const ipfsClient = require('ipfs-http-client')
- const ipfs = ipfsClient('localhost', '5001', { protocol: 'http' })
- 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),
- })
- }
- async function publish(serviceInfo) {
- const keys = await ipfs.key.list()
- let servicesKey = keys.find(key => key.name === PUBLISH_KEY)
-
-
-
- if (PUBLISH_KEY !== 'self' && !servicesKey) {
- debug('generating ipns services key')
- servicesKey = await 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 ipfs.add(encodeServiceInfo(serviceInfo))
- debug('publishing...')
- const published = await ipfs.name.publish(files[0].hash, {
- key: PUBLISH_KEY,
- resolve: false,
-
-
- })
-
-
-
-
-
-
- debug(published)
-
-
- return servicesKey.id
- }
- module.exports = {
- publish,
- }
|