TBD54566975 / web5-js

Monorepo for the Web5 JS TypeScript implementation
https://tbd54566975.github.io/web5-js/
Apache License 2.0
118 stars 44 forks source link

[api] Unexpected error: Expected 1 but found 2 stored identities. When calling `Web5.connect()` #680

Open LiranCohen opened 3 weeks ago

LiranCohen commented 3 weeks ago

When using Web5.connect() without any options, there have been occurrences where the following error is thrown: Uncaught (in promise) Error: connect() failed due to unexpected state: Expected 1 but found 2 stored identities.

When connect() is called and no identities are returned from the agent, a new identity is created. There is some unknown scenario where userAgent.identities.list() is returning zero results, although there was one that existed in the past.

On a subsequent call of connect() it will then suddenly have 2 identities. https://github.com/TBD54566975/web5-js/blob/main/packages/api/src/web5.ts#L205-L248

For the time being we will do a few things to remedy the stuck situation, as well as investigate further as to why this happens.

  1. Check the status returned by processRequest within the DwnDidStore.getAllRecords() and throw an error along with the error detail if it does not return a 200 status. https://github.com/TBD54566975/web5-js/blob/main/packages/agent/src/store-did.ts#L48-L57
  2. If the user passes a connectedDid and the list returns more than one identity, attempt to find the identity which matches the connectedDid, if it exists use that identity, otherwise fail.
csuwildcat commented 3 weeks ago

Hopefully connectedDid is the DID identifier, not some more complex thing?

LiranCohen commented 3 weeks ago

Hopefully connectedDid is the DID identifier, not some more complex thing?

Yup! It is set to the identity.did.uri further down the file if there is no agent passed.