Skip to content

Commit

Permalink
fix(data-store-json): structuredClone (#885)
Browse files Browse the repository at this point in the history
Fixes #857
  • Loading branch information
simonas-notcat authored May 16, 2022
1 parent ada73c3 commit cf14cae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions packages/data-store-json/src/identifier/did-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AbstractDIDStore } from '@veramo/did-manager'

import Debug from 'debug'
import { DiffCallback, VeramoJsonCache, VeramoJsonStore } from '../types'
import structuredClone from '@ungap/structured-clone'
import { serialize, deserialize } from '@ungap/structured-clone'

const debug = Debug('veramo:data-store-json:did-store')

Expand Down Expand Up @@ -66,12 +66,12 @@ export class DIDStoreJson extends AbstractDIDStore {

if (!identifier) throw Error('Identifier not found')

return structuredClone(identifier)
return deserialize(serialize(identifier))
}

async delete({ did }: { did: string }) {
if (this.cacheTree.dids[did]) {
const oldTree = structuredClone(this.cacheTree, { lossy: true })
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
delete this.cacheTree.dids[did]
// FIXME: delete key associations?
await this.notifyUpdate(oldTree, this.cacheTree)
Expand All @@ -81,7 +81,7 @@ export class DIDStoreJson extends AbstractDIDStore {
}

async import(args: IIdentifier) {
const oldTree = structuredClone(this.cacheTree, { lossy: true })
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
this.cacheTree.dids[args.did] = args
args.keys.forEach((key) => {
this.cacheTree.keys[key.kid] = {
Expand All @@ -101,6 +101,6 @@ export class DIDStoreJson extends AbstractDIDStore {
(!args.provider || (args.provider && iid.provider === args.provider)) &&
(!args.alias || (args.alias && iid.alias === args.alias)),
)
return structuredClone(result)
return deserialize(serialize(result))
}
}
10 changes: 5 additions & 5 deletions packages/data-store-json/src/identifier/key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AbstractKeyStore } from '@veramo/key-manager'

import Debug from 'debug'
import { DiffCallback, VeramoJsonCache, VeramoJsonStore } from '../types'
import structuredClone from '@ungap/structured-clone'
import { serialize, deserialize } from '@ungap/structured-clone'

const debug = Debug('veramo:data-store-json:key-store')

Expand Down Expand Up @@ -41,15 +41,15 @@ export class KeyStoreJson extends AbstractKeyStore {

async get({ kid }: { kid: string }): Promise<IKey> {
if (this.cacheTree.keys[kid]) {
return structuredClone(this.cacheTree.keys[kid])
return deserialize(serialize(this.cacheTree.keys[kid]))
} else {
throw Error('not_found: Key not found')
}
}

async delete({ kid }: { kid: string }) {
if (this.cacheTree.keys[kid]) {
const oldTree = structuredClone(this.cacheTree, { lossy: true })
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
delete this.cacheTree.keys[kid]
await this.notifyUpdate(oldTree, this.cacheTree)
return true
Expand All @@ -59,7 +59,7 @@ export class KeyStoreJson extends AbstractKeyStore {
}

async import(args: IKey) {
const oldTree = structuredClone(this.cacheTree, { lossy: true })
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
this.cacheTree.keys[args.kid] = args
await this.notifyUpdate(oldTree, this.cacheTree)
return true
Expand All @@ -68,7 +68,7 @@ export class KeyStoreJson extends AbstractKeyStore {
async list(args: {} = {}): Promise<ManagedKeyInfo[]> {
const keys = Object.values(this.cacheTree.keys).map((key: IKey) => {
const { kid, publicKeyHex, type, meta, kms } = key
return { kid, publicKeyHex, type, meta: structuredClone(meta), kms } as ManagedKeyInfo
return { kid, publicKeyHex, type, meta: deserialize(serialize(meta)), kms } as ManagedKeyInfo
})
return keys
}
Expand Down
14 changes: 7 additions & 7 deletions packages/data-store-json/src/identifier/private-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ImportablePrivateKey, ManagedPrivateKey } from '@veramo/key-manager/src
import { v4 as uuid4 } from 'uuid'
import Debug from 'debug'
import { DiffCallback, VeramoJsonCache, VeramoJsonStore } from '../types'
import structuredClone from '@ungap/structured-clone'
import { serialize, deserialize } from '@ungap/structured-clone'

const debug = Debug('veramo:data-store-json:private-key-store')

Expand Down Expand Up @@ -42,7 +42,7 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
}

async get({ alias }: { alias: string }): Promise<ManagedPrivateKey> {
const key = structuredClone(this.cacheTree.privateKeys[alias])
const key = deserialize(serialize(this.cacheTree.privateKeys[alias]))
if (!key) throw Error('not_found: PrivateKey not found')
if (this.secretBox && key.privateKeyHex) {
key.privateKeyHex = await this.secretBox.decrypt(key.privateKeyHex)
Expand All @@ -54,7 +54,7 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
debug(`Deleting private key data for alias=${alias}`)
const privateKeyEntry = this.cacheTree.privateKeys[alias]
if (privateKeyEntry) {
const oldTree = structuredClone(this.cacheTree, { lossy: true })
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
delete this.cacheTree.privateKeys[alias]
await this.notifyUpdate(oldTree, this.cacheTree)
}
Expand All @@ -64,10 +64,10 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
async import(args: ImportablePrivateKey): Promise<ManagedPrivateKey> {
debug('Saving private key data', args.alias)
const alias = args.alias || uuid4()
const key: ManagedPrivateKey = structuredClone({
const key: ManagedPrivateKey = deserialize(serialize({
...args,
alias,
})
}))
if (this.secretBox && key.privateKeyHex) {
const copy = key.privateKeyHex
key.privateKeyHex = await this.secretBox.encrypt(copy)
Expand All @@ -79,14 +79,14 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
)
}

const oldTree = structuredClone(this.cacheTree, { lossy: true })
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
this.cacheTree.privateKeys[key.alias] = key
await this.notifyUpdate(oldTree, this.cacheTree)

return key
}

async list(): Promise<Array<ManagedPrivateKey>> {
return structuredClone(Object.values(this.cacheTree.privateKeys))
return deserialize(serialize(Object.values(this.cacheTree.privateKeys)))
}
}

0 comments on commit cf14cae

Please sign in to comment.