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: add reset command #170

Merged
merged 1 commit into from
Jan 25, 2024
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
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 { 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 @@ -568,7 +570,7 @@
}
}

export async function whoami() {

Check warning on line 573 in index.js

View workflow job for this annotation

GitHub Actions / Test

Missing JSDoc comment
const client = await getClient()
console.log(client.did())
}
Expand Down Expand Up @@ -650,3 +652,15 @@
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 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I read the code and didn't see how it would not just re-import the same proofs fromExport. This (imho) makes it more obvious what's going on. just a idea

Suggested change
data = await AgentData.create({ principal: data.principal, meta: data.meta })
data = await AgentData.create({ principal: data.principal, meta: data.meta, delegations: undefined })

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