Skip to content

Commit

Permalink
feat: expose accounts list on client instance (#1109)
Browse files Browse the repository at this point in the history
Adds a `accounts()` method on the `Client` that allows users to list
accounts they are logged into.
  • Loading branch information
Alan Shaw authored Nov 9, 2023
1 parent 5ce43bc commit 1810684
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/w3up-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ sequenceDiagram
- [`uploadCAR`](#uploadcar)
- [`agent`](#agent)
- [`authorize`](#authorize)
- [`accounts`](#accounts)
- [`currentSpace`](#currentspace)
- [`setCurrentSpace`](#setcurrentspace)
- [`spaces`](#spaces)
Expand Down Expand Up @@ -513,6 +514,14 @@ function authorize (email: string, options?: { signal?: AbortSignal }): Promise<

Authorize the current agent to use capabilities granted to the passed email account.

### `accounts`

```ts
function accounts (): Record<DID, Account>
```

List all accounts the agent has stored access to.

### `currentSpace`

```ts
Expand Down Expand Up @@ -730,7 +739,6 @@ export interface Capability<
nb?: Caveats
}
export type Ability = `${string}/${string}` | "*"
export type Resource = `${string}:${string}`
Expand Down
9 changes: 9 additions & 0 deletions packages/w3up-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Upload as UploadCapabilities,
} from '@web3-storage/capabilities'
import { Base } from './base.js'
import * as Account from './account.js'
import { Space } from './space.js'
import { Delegation as AgentDelegation } from './delegation.js'
import { StoreClient } from './capability/store.js'
Expand Down Expand Up @@ -59,6 +60,14 @@ export class Client extends Base {
}
/* c8 ignore stop */

/**
* List all accounts that agent has stored access to. Returns a dictionary
* of accounts keyed by their `did:mailto` identifier.
*/
accounts() {
return Account.list(this)
}

/**
* Uploads a file to the service and returns the root data CID for the
* generated DAG.
Expand Down
37 changes: 37 additions & 0 deletions packages/w3up-client/test/client-accounts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as Test from './test.js'
import * as Account from '../src/account.js'

/**
* @type {Test.Suite}
*/
export const testClientAccounts = {
'list accounts': async (assert, { client, mail, grantAccess }) => {
const email = 'alice@web.mail'

assert.deepEqual(client.accounts(), {}, 'no accounts yet')

const login = Account.login(client, email)
const message = await mail.take()
assert.deepEqual(message.to, email)
await grantAccess(message)
const session = await login
assert.equal(session.error, undefined)
assert.equal(session.ok?.did(), Account.fromEmail(email))
assert.equal(session.ok?.toEmail(), email)
assert.equal(session.ok?.proofs.length, 2)

assert.deepEqual(client.accounts(), {}, 'no accounts have been saved')
await session.ok?.save()
const accounts = client.accounts()

assert.deepEqual(Object.values(accounts).length, 1)
assert.ok(accounts[Account.fromEmail(email)])

const account = accounts[Account.fromEmail(email)]
assert.equal(account.toEmail(), email)
assert.equal(account.did(), Account.fromEmail(email))
assert.equal(account.proofs.length, 2)
},
}

Test.test({ 'Client accounts': testClientAccounts })

0 comments on commit 1810684

Please sign in to comment.