-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support storing ArrayBuffers in conf (#1236)
I made a change to use ArrayBuffers in AgentDataExport, which was not supported by our JSON.stringify function. Tweak the stringify replacer to handle this cleanly and add a test to verify it works as expected.
- Loading branch information
Showing
3 changed files
with
82 additions
and
2 deletions.
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
78 changes: 78 additions & 0 deletions
78
packages/access-client/test/stores/store-conf.node.test.js
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,78 @@ | ||
import assert from 'assert' | ||
import { top } from '@web3-storage/capabilities/top' | ||
import { Signer as EdSigner } from '@ucanto/principal/ed25519' | ||
import * as RSASigner from '@ucanto/principal/rsa' | ||
import { AgentData } from '../../src/agent-data.js' | ||
import { StoreConf } from '../../src/stores/store-conf.js' | ||
|
||
describe('Conf store', () => { | ||
it('should create and load data', async () => { | ||
const data = await AgentData.create({ | ||
principal: await RSASigner.generate({ extractable: false }), | ||
}) | ||
|
||
const store = new StoreConf({ profile: 'test-access-db-' + Date.now() }) | ||
await store.open() | ||
await store.save(data.export()) | ||
|
||
const exportData = await store.load() | ||
assert(exportData) | ||
|
||
// no accounts or delegations yet | ||
assert.equal(exportData.spaces.size, 0) | ||
assert.equal(exportData.delegations.size, 0) | ||
|
||
// default meta | ||
assert.equal(exportData.meta.name, 'agent') | ||
assert.equal(exportData.meta.type, 'device') | ||
}) | ||
|
||
it('should round trip delegations', async () => { | ||
const store = new StoreConf({ profile: 'test-access-db-' + Date.now() }) | ||
await store.open() | ||
|
||
const data0 = await AgentData.create() | ||
const signer = await EdSigner.generate() | ||
const del0 = await top.delegate({ | ||
issuer: signer, | ||
audience: data0.principal, | ||
with: signer.did(), | ||
expiration: Infinity, | ||
}) | ||
|
||
await data0.addDelegation(del0, { | ||
audience: { name: 'test', type: 'device' }, | ||
}) | ||
await store.save(data0.export()) | ||
|
||
const exportData1 = await store.load() | ||
assert(exportData1) | ||
|
||
const data1 = AgentData.fromExport(exportData1) | ||
|
||
const { delegation: del1 } = | ||
data1.delegations.get(del0.cid.toString()) ?? {} | ||
assert(del1) | ||
assert.equal(del1.cid.toString(), del0.cid.toString()) | ||
assert.equal(del1.issuer.did(), del0.issuer.did()) | ||
assert.equal(del1.audience.did(), del0.audience.did()) | ||
assert.equal(del1.capabilities[0].can, del0.capabilities[0].can) | ||
assert.equal(del1.capabilities[0].with, del0.capabilities[0].with) | ||
}) | ||
|
||
it('should be resettable', async () => { | ||
const principal = await RSASigner.generate({ extractable: false }) | ||
const data = await AgentData.create({ principal }) | ||
|
||
const store = new StoreConf({ profile: 'test-access-db-' + Date.now() }) | ||
await store.open() | ||
await store.save(data.export()) | ||
|
||
const exportData = await store.load() | ||
assert.equal(exportData?.principal.id, principal.did()) | ||
|
||
await store.reset() | ||
const resetExportData = await store.load() | ||
assert.equal(resetExportData?.principal.id, undefined) | ||
}) | ||
}) |