query.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var SEA = require('./sea');
  2. var Gun = SEA.Gun;
  3. // This is internal func queries public key(s) for alias.
  4. const queryGunAliases = (alias, gunRoot) => new Promise((resolve, reject) => {
  5. // load all public keys associated with the username alias we want to log in with.
  6. gunRoot.get('~@'+alias).get((rat, rev) => {
  7. rev.off();
  8. if (!rat.put) {
  9. // if no user, don't do anything.
  10. const err = 'No user!'
  11. Gun.log(err)
  12. return reject({ err })
  13. }
  14. // then figuring out all possible candidates having matching username
  15. const aliases = []
  16. let c = 0
  17. // TODO: how about having real chainable map without callback ?
  18. Gun.obj.map(rat.put, (at, pub) => {
  19. if (!pub.slice || '~' !== pub.slice(0, 1)) {
  20. // TODO: ... this would then be .filter((at, pub))
  21. return
  22. }
  23. ++c
  24. // grab the account associated with this public key.
  25. gunRoot.get(pub).get((at, ev) => {
  26. pub = pub.slice(1)
  27. ev.off()
  28. --c
  29. if (at.put){
  30. aliases.push({ pub, at })
  31. }
  32. if (!c && (c = -1)) {
  33. resolve(aliases)
  34. }
  35. })
  36. })
  37. if (!c) {
  38. reject({ err: 'Public key does not exist!' })
  39. }
  40. })
  41. })
  42. module.exports = queryGunAliases