Skip to content

Commit

Permalink
feat: add reset command (#170)
Browse files Browse the repository at this point in the history
Adds a reset command so you can reset you agent and login again to gain
access to spaces. Retains agent DID.

I called this `reset` and not `logout` because we can't guarantee the
agent retains any delegations that are not associated with the currently
logged in account(s).

```console
$ w3 whoami
did:key:z6MkiK3h93N8kE6jmAgTWd33QmuyDcsMnaAeQrGjp9iixT2B

$ w3 space ls
  did:key:z6MkoEBnTj96thGj7H7c1DrdNHF4AiA9VPDRVqTmp4teYd6B 
  did:key:z6MketbAFtbeqDeVHxSzSSDH2PfMTquQ3vzZCcPFweXBGe3R 
  did:key:z6MkgVNAd3rDAtvYwp5NscNteZjqW496dERwkMWiDKcZ13xa 
  did:key:z6MkjCoKJcunQgzihb4tXBYDd9xunhpGNoC14HEypgAe5cNW 
  did:key:z6MkfUw42NQV6Gs8ZLr4sujwRFZGQ789LVuRfkgRxFnTkRBz 
* did:key:z6MkwDuRThQcyWjqNsK54yKAmzfsiH6BTkASyiucThMtHt1T Test

$ w3 reset   
⁂ Agent reset.

$ w3 whoami
did:key:z6MkiK3h93N8kE6jmAgTWd33QmuyDcsMnaAeQrGjp9iixT2B

$ w3 space ls

$ w3 login alan.shaw@protocol.ai
⁂ Agent was authorized by did:mailto:protocol.ai:alan.shaw

$ w3 space ls                   
  did:key:z6MkoEBnTj96thGj7H7c1DrdNHF4AiA9VPDRVqTmp4teYd6B 
  did:key:z6MketbAFtbeqDeVHxSzSSDH2PfMTquQ3vzZCcPFweXBGe3R 
  did:key:z6MkgVNAd3rDAtvYwp5NscNteZjqW496dERwkMWiDKcZ13xa 
* did:key:z6MkwDuRThQcyWjqNsK54yKAmzfsiH6BTkASyiucThMtHt1T Test
  did:key:z6MkmyzUC328sSpm8Mncq9aUZuXLsBtARMXhZnEqfWcq11sw Claimer
  did:key:z6MkjCoKJcunQgzihb4tXBYDd9xunhpGNoC14HEypgAe5cNW 
  did:key:z6MkfUw42NQV6Gs8ZLr4sujwRFZGQ789LVuRfkgRxFnTkRBz 
```

...and look I gained access to a space I didn't have before!
  • Loading branch information
Alan Shaw authored Jan 25, 2024
1 parent 96966ba commit 8eea385
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
usageReport,
getPlan,
createKey,
reset,
} from './index.js'
import {
storeAdd,
Expand Down Expand Up @@ -305,6 +306,11 @@ cli
.option('--json', 'output as json')
.action(createKey)

cli
.command('reset')
.describe('Remove all proofs/delegations from the store but retain the agent DID.')
.action(reset)

// show help text if no command provided
cli.command('help [cmd]', 'Show help text', { default: true }).action((cmd) => {
try {
Expand Down
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { CarWriter } from '@ipld/car'
import { filesFromPaths } from 'files-from-path'
import * as Account from './account.js'
import { spaceAccess } from '@web3-storage/w3up-client/capability/access'
import { AgentData } from '@web3-storage/access'
import * as Space from './space.js'
import {
getClient,
getStore,
checkPathsExist,
filesize,
filesizeMB,
Expand Down Expand Up @@ -644,3 +646,15 @@ export async function createKey({ json }) {
console.log(key)
}
}

export const reset = async () => {
const store = getStore()
const exportData = await store.load()
if (exportData) {
let data = AgentData.fromExport(exportData)
// do not reset the principal
data = await AgentData.create({ principal: data.principal, meta: data.meta })
await store.save(data.export())
}
console.log('⁂ Agent reset.')
}
7 changes: 6 additions & 1 deletion lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ export function filesizeMB(bytes) {
return `${(bytes / 1000 / 1000).toFixed(1)}MB`
}

/** Get a configured w3up store used by the CLI. */
export function getStore() {
return new StoreConf({ profile: process.env.W3_STORE_NAME ?? 'w3cli' })
}

/**
* Get a new API client configured from env vars.
*/
export function getClient() {
const store = new StoreConf({ profile: process.env.W3_STORE_NAME ?? 'w3cli' })
const store = getStore()

if (process.env.W3_ACCESS_SERVICE_URL || process.env.W3_UPLOAD_SERVICE_URL) {
console.warn(
Expand Down

0 comments on commit 8eea385

Please sign in to comment.