publish.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const ipfsClient = require('ipfs-http-client')
  2. const ipfs = ipfsClient('localhost', '5001', { protocol: 'http' })
  3. const debug = require('debug')('joystream:discovery:publish')
  4. /**
  5. * The name of the key used for publishing. We use same key used by the ipfs node
  6. * for the network identitiy, to make it possible to identify the ipfs node of the storage
  7. * provider and use `ipfs ping` to check on the uptime of a particular node.
  8. */
  9. const PUBLISH_KEY = 'self'
  10. /**
  11. * Applies JSON serialization on the data object and converts the utf-8
  12. * string to a Buffer.
  13. * @param {object} data - json object
  14. * @returns {Buffer}
  15. */
  16. function bufferFrom (data) {
  17. return Buffer.from(JSON.stringify(data), 'utf-8')
  18. }
  19. /**
  20. * Encodes the service info into a standard format see. /storage-node/docs/json-signing.md
  21. * To be able to add a signature over the json data. Signing is not currently implemented.
  22. * @param {object} info - json object
  23. * @returns {Buffer}
  24. */
  25. function encodeServiceInfo (info) {
  26. return bufferFrom({
  27. serialized: JSON.stringify(info)
  28. })
  29. }
  30. /**
  31. * Publishes the service information, encoded using the standard defined in encodeServiceInfo()
  32. * to ipfs, using the local ipfs node's PUBLISH_KEY, and returns the key id used to publish.
  33. * What we refer to as the ipns id.
  34. * @param {object} service_info - the service information to publish
  35. * @returns {string} - the ipns id
  36. */
  37. async function publish (service_info) {
  38. const keys = await ipfs.key.list()
  39. let services_key = keys.find((key) => key.name === PUBLISH_KEY)
  40. // An ipfs node will always have the self key.
  41. // If the publish key is specified as anything else and it doesn't exist
  42. // we create it.
  43. if (PUBLISH_KEY !== 'self' && !services_key) {
  44. debug('generating ipns services key')
  45. services_key = await ipfs.key.gen(PUBLISH_KEY, {
  46. type: 'rsa',
  47. size: 2048
  48. })
  49. }
  50. if (!services_key) {
  51. throw new Error('No IPFS publishing key available!')
  52. }
  53. debug('adding service info file to node')
  54. const files = await ipfs.add(encodeServiceInfo(service_info))
  55. debug('publishing...')
  56. const published = await ipfs.name.publish(files[0].hash, {
  57. key: PUBLISH_KEY,
  58. resolve: false
  59. // lifetime: // string - Time duration of the record. Default: 24h
  60. // ttl: // string - Time duration this record should be cached
  61. })
  62. // The name and ipfs hash of the published service information file, eg.
  63. // {
  64. // name: 'QmUNQCkaU1TRnc1WGixqEP3Q3fazM8guSdFRsdnSJTN36A',
  65. // value: '/ipfs/QmcSjtVMfDSSNYCxNAb9PxNpEigCw7h1UZ77gip3ghfbnA'
  66. // }
  67. // .. The name is equivalent to the key id that was used.
  68. debug(published)
  69. // Return the key id under which the content was published. Which is used
  70. // to lookup the actual ipfs content id of the published service information
  71. return services_key.id
  72. }
  73. module.exports = {
  74. publish
  75. }