Resolver.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. class Resolver {
  2. constructor ({
  3. runtime
  4. }) {
  5. this.runtime = runtime
  6. }
  7. constructUrl (protocol, host, port) {
  8. port = port ? `:${port}` : ''
  9. return `${protocol}:://${host}${port}`
  10. }
  11. async resolveServiceInformation(accountId) {
  12. let isActor = await this.runtime.identities.isActor(accountId)
  13. if (!isActor) {
  14. throw new Error('Cannot discover non actor account service info')
  15. }
  16. const identity = await this.resolveIdentity(accountId)
  17. if (identity == null) {
  18. // dont waste time trying to resolve if no identity was found
  19. throw new Error('no identity to resolve');
  20. }
  21. return this.resolve(accountId)
  22. }
  23. // lookup ipns identity from chain corresponding to accountId
  24. // return null if no identity found or record is expired
  25. async resolveIdentity(accountId) {
  26. const info = await this.runtime.discovery.getAccountInfo(accountId)
  27. return info ? info.identity.toString() : null
  28. }
  29. }
  30. Resolver.Error = {};
  31. Resolver.Error.UnrecognizedProtocol = class UnrecognizedProtocol extends Error {
  32. constructor(message) {
  33. super(message);
  34. this.name = 'UnrecognizedProtocol';
  35. }
  36. }
  37. module.exports = {
  38. Resolver
  39. }