From 8eea385e526bd73a9cfb3a674c31168a7b161f30 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 25 Jan 2024 12:15:32 +0000 Subject: [PATCH] feat: add reset command (#170) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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! --- bin.js | 6 ++++++ index.js | 14 ++++++++++++++ lib.js | 7 ++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/bin.js b/bin.js index 26125ee..97867b0 100755 --- a/bin.js +++ b/bin.js @@ -25,6 +25,7 @@ import { usageReport, getPlan, createKey, + reset, } from './index.js' import { storeAdd, @@ -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 { diff --git a/index.js b/index.js index 0a95edb..f80e1b5 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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.') +} diff --git a/lib.js b/lib.js index 24cc359..25b2a0b 100644 --- a/lib.js +++ b/lib.js @@ -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(