Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose accounts list on client instance #1109

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })