discovery.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. const debug = require('debug')('joystream:runtime:discovery');
  3. /*
  4. * Add discovery related functionality to the substrate API.
  5. */
  6. class DiscoveryApi
  7. {
  8. static async create(base)
  9. {
  10. const ret = new DiscoveryApi();
  11. ret.base = base;
  12. await ret.init();
  13. return ret;
  14. }
  15. async init(account_file)
  16. {
  17. debug('Init');
  18. }
  19. /*
  20. * Get Bootstrap endpoints
  21. */
  22. async getBootstrapEndpoints() {
  23. return this.base.api.query.discovery.bootstrapEndpoints()
  24. }
  25. /*
  26. * Get AccountInfo of an accountId
  27. */
  28. async getAccountInfo(accountId) {
  29. const decoded = this.base.identities.keyring.decodeAddress(accountId, true)
  30. const info = await this.base.api.query.discovery.accountInfoByAccountId(decoded)
  31. // Not an Option so we use default value check to know if info was found
  32. return info.expires_at.eq(0) ? null : info
  33. }
  34. /*
  35. * Set AccountInfo of an accountId
  36. */
  37. async setAccountInfo(accountId, ipnsId, ttl) {
  38. const isActor = await this.base.identities.isActor(accountId)
  39. if (isActor) {
  40. const tx = this.base.api.tx.discovery.setIpnsId(ipnsId, ttl)
  41. return this.base.signAndSend(accountId, tx)
  42. } else {
  43. throw new Error('Cannot set AccountInfo for non actor account')
  44. }
  45. }
  46. /*
  47. * Clear AccountInfo of an accountId
  48. */
  49. async unsetAccountInfo(accountId) {
  50. var tx = this.base.api.tx.discovery.unsetIpnsId()
  51. return this.base.signAndSend(accountId, tx)
  52. }
  53. }
  54. module.exports = {
  55. DiscoveryApi: DiscoveryApi,
  56. }