gun icon indicating copy to clipboard operation
gun copied to clipboard

[Doc] how can one find the root node of a given user?

Open rozek opened this issue 2 years ago • 3 comments

Well, it's really difficult to find the relevant bits of information in the docs - sorting out all wrong or outdated parts and collecting everything else in order to combine it to s.th. useful...

Right now, I'm trying to find the root node of a user given by an alias. This is my current unerstanding:

  const Gun     = new GUN()
  const GunUser = Gun.user() // creates an environment for an anonymous user

/**** find Alice's root node ****/

  let Alice = await (async () => {
    return new Promise((resolve,reject) => {
      Gun.get('~@alice').once((Data) => {
        for (const Key in Data) {
          if ((Key !== '_') && Data.hasOwnProperty(Key)) {
            let Value = Data[Key]
            if (
              (typeof Value === 'object') && (Value != null) &&
              (Value['#'] === Key)
            ) { return resolve(Gun.user(Key.slice(1))) }
          }
        }
        reject('alias not found')
      })
    })
  })()

  console.log('Alice\'s root node',Alice) // shows s.th.
  console.log('Alice\'s address',Alice.get('address')) // fails with "Alice.get is not a function"

This gives me s.th. which looks like the contents of Alice's root node - but I cannot .get or .put from there.

So: how do I find a user's root node in order to inspect any public information found there?

rozek avatar May 16 '23 14:05 rozek

Very, very strange...

If I change the above code to

  const Gun     = new GUN()
  const GunUser = Gun.user() // creates an environment for an anonymous user

/**** find Alice's root node ****/

  let AliceKey = await (async () => {
    return new Promise((resolve,reject) => {
      Gun.get('~@alice').once((Data) => {
        for (const Key in Data) {
          if ((Key !== '_') && Data.hasOwnProperty(Key)) {
            let Value = Data[Key]
            if (
              (typeof Value === 'object') && (Value != null) &&
              (Value['#'] === Key)
            ) { return resolve(Key.slice(1)) }
          }
        }
        return reject('alias not found')
      })
    })
  })()

  let Alice = Gun.user(AliceKey)

  console.log('Alice\'s root node',Alice)
  console.log('Alice\'s address',Alice.get('address'))

then everything works as foreseen...

Does anybody know why I can't resolve with a GUN node?

rozek avatar May 16 '23 14:05 rozek

Bit late here, but perhaps this has to do with how nodes can be awaited: see docs here

in short:

gun.get("some-key-here") is not the same as await gun.get("some-key-here"). Awaiting it will give you the value of the node. If you wish to return the actual gun chain, you can wrap it in an object like so:

async () => {
  const node = gun.get("some-key-here");
  return {node}; // we must wrap so that awaiting it will not cause us to promisify the node.
}

gustavcorpas avatar Oct 19 '23 18:10 gustavcorpas

If you have the user's username and password, you can authenticate first using GunUser.auth(username, password) And then get or put on that user's node using GunUser.get or GunUser.put. In short, gun.user() works as the root node for the current (logged in/authenticated) user.

trPrince avatar Mar 13 '24 09:03 trPrince