-
Notifications
You must be signed in to change notification settings - Fork 22
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
refactor!: access-client store decoupling #228
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
be9cc5e
refactor: wip
3a64e3f
fix: fix
9c4f926
fix: conf store
ab321fc
feat: make AgentData a class
144048b
fix: awake peer type in options
b6e0ff0
feat: add static agent constructor methods
0031558
refactor: simplify more, auto open
2b1623f
fix: autoopen param
a34f06c
fix: from is not async
c266517
fix: tests
e63e7dd
fix: no need to await
f133954
fix: remove service()
958fb23
feat: drivers
071ef05
Merge branch 'main' into refactor/access-client
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { Signer } from '@ucanto/principal' | ||
import { Signer as EdSigner } from '@ucanto/principal/ed25519' | ||
import { importDAG } from '@ucanto/core/delegation' | ||
import { CID } from 'multiformats' | ||
|
||
/** @typedef {import('./types').AgentDataModel} AgentDataModel */ | ||
|
||
/** @implements {AgentDataModel} */ | ||
export class AgentData { | ||
/** @type {(data: import('./types').AgentDataExport) => Promise<void> | void} */ | ||
#save | ||
|
||
/** | ||
* @param {import('./types').AgentDataModel} data | ||
* @param {import('./types').AgentDataOptions} [options] | ||
*/ | ||
constructor(data, options = {}) { | ||
this.meta = data.meta | ||
this.principal = data.principal | ||
this.spaces = data.spaces | ||
this.delegations = data.delegations | ||
this.currentSpace = data.currentSpace | ||
this.#save = options.store ? options.store.save : () => {} | ||
} | ||
|
||
/** | ||
* Create a new AgentData instance from the passed initialization data. | ||
* | ||
* @param {Partial<import('./types').AgentDataModel>} [init] | ||
* @param {import('./types').AgentDataOptions} [options] | ||
*/ | ||
static async create(init = {}, options = {}) { | ||
const agentData = new AgentData( | ||
{ | ||
meta: { name: 'agent', type: 'device', ...init.meta }, | ||
principal: init.principal ?? (await EdSigner.generate()), | ||
spaces: init.spaces ?? new Map(), | ||
delegations: init.delegations ?? new Map(), | ||
currentSpace: init.currentSpace, | ||
}, | ||
options | ||
) | ||
if (options.store) { | ||
await options.store.save(agentData.export()) | ||
} | ||
return agentData | ||
} | ||
|
||
/** | ||
* Instantiate AgentData from previously exported data. | ||
* | ||
* @param {import('./types').AgentDataExport} raw | ||
* @param {import('./types').AgentDataOptions} [options] | ||
*/ | ||
static fromExport(raw, options) { | ||
/** @type {import('./types').AgentDataModel['delegations']} */ | ||
const dels = new Map() | ||
|
||
for (const [key, value] of raw.delegations) { | ||
dels.set(key, { | ||
delegation: importDAG( | ||
value.delegation.map((d) => ({ | ||
cid: CID.parse(d.cid), | ||
bytes: d.bytes, | ||
})) | ||
), | ||
meta: value.meta, | ||
}) | ||
} | ||
|
||
return new AgentData( | ||
{ | ||
meta: raw.meta, | ||
// @ts-expect-error | ||
principal: Signer.from(raw.principal), | ||
currentSpace: raw.currentSpace, | ||
spaces: raw.spaces, | ||
delegations: dels, | ||
}, | ||
options | ||
) | ||
} | ||
|
||
/** | ||
* Export data in a format safe to pass to `structuredClone()`. | ||
*/ | ||
export() { | ||
/** @type {import('./types').AgentDataExport} */ | ||
const raw = { | ||
meta: this.meta, | ||
principal: this.principal.toArchive(), | ||
currentSpace: this.currentSpace, | ||
spaces: this.spaces, | ||
delegations: new Map(), | ||
} | ||
for (const [key, value] of this.delegations) { | ||
raw.delegations.set(key, { | ||
meta: value.meta, | ||
delegation: [...value.delegation.export()].map((b) => ({ | ||
cid: b.cid.toString(), | ||
bytes: b.bytes, | ||
})), | ||
}) | ||
} | ||
return raw | ||
} | ||
|
||
/** | ||
* @param {import('@ucanto/interface').DID} did | ||
* @param {import('./types').SpaceMeta} meta | ||
* @param {import('@ucanto/interface').Delegation} [proof] | ||
*/ | ||
async addSpace(did, meta, proof) { | ||
this.spaces.set(did, meta) | ||
await (proof ? this.addDelegation(proof) : this.#save(this.export())) | ||
} | ||
|
||
/** | ||
* @param {import('@ucanto/interface').DID} did | ||
*/ | ||
async setCurrentSpace(did) { | ||
this.currentSpace = did | ||
await this.#save(this.export()) | ||
} | ||
|
||
/** | ||
* @param {import('@ucanto/interface').Delegation} delegation | ||
* @param {import('./types').DelegationMeta} [meta] | ||
*/ | ||
async addDelegation(delegation, meta) { | ||
this.delegations.set(delegation.cid.toString(), { | ||
delegation, | ||
meta: meta ?? {}, | ||
}) | ||
await this.#save(this.export()) | ||
} | ||
|
||
/** | ||
* @param {import('@ucanto/interface').UCANLink} cid | ||
*/ | ||
async removeDelegation(cid) { | ||
this.delegations.delete(cid.toString()) | ||
await this.#save(this.export()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
who knows which signer to use for the agent principal ? With the current code its always ed but browser need rsa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now,
w3up-client
/w3ui
would choose RSA. I don't think the store implementation should choose. You can put Ed25519 keys in IndexedDB. It's the environment/application that should choose this.We should create an
agent-data.browser.js
which just importsagent-data.js
and overrides though.