Skip to content

Commit cf14cae

Browse files
authoredMay 16, 2022
fix(data-store-json): structuredClone (#885)
Fixes #857
1 parent ada73c3 commit cf14cae

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed
 

‎packages/data-store-json/src/identifier/did-store.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AbstractDIDStore } from '@veramo/did-manager'
33

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

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

@@ -66,12 +66,12 @@ export class DIDStoreJson extends AbstractDIDStore {
6666

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

69-
return structuredClone(identifier)
69+
return deserialize(serialize(identifier))
7070
}
7171

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

8383
async import(args: IIdentifier) {
84-
const oldTree = structuredClone(this.cacheTree, { lossy: true })
84+
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
8585
this.cacheTree.dids[args.did] = args
8686
args.keys.forEach((key) => {
8787
this.cacheTree.keys[key.kid] = {
@@ -101,6 +101,6 @@ export class DIDStoreJson extends AbstractDIDStore {
101101
(!args.provider || (args.provider && iid.provider === args.provider)) &&
102102
(!args.alias || (args.alias && iid.alias === args.alias)),
103103
)
104-
return structuredClone(result)
104+
return deserialize(serialize(result))
105105
}
106106
}

‎packages/data-store-json/src/identifier/key-store.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AbstractKeyStore } from '@veramo/key-manager'
33

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

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

@@ -41,15 +41,15 @@ export class KeyStoreJson extends AbstractKeyStore {
4141

4242
async get({ kid }: { kid: string }): Promise<IKey> {
4343
if (this.cacheTree.keys[kid]) {
44-
return structuredClone(this.cacheTree.keys[kid])
44+
return deserialize(serialize(this.cacheTree.keys[kid]))
4545
} else {
4646
throw Error('not_found: Key not found')
4747
}
4848
}
4949

5050
async delete({ kid }: { kid: string }) {
5151
if (this.cacheTree.keys[kid]) {
52-
const oldTree = structuredClone(this.cacheTree, { lossy: true })
52+
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
5353
delete this.cacheTree.keys[kid]
5454
await this.notifyUpdate(oldTree, this.cacheTree)
5555
return true
@@ -59,7 +59,7 @@ export class KeyStoreJson extends AbstractKeyStore {
5959
}
6060

6161
async import(args: IKey) {
62-
const oldTree = structuredClone(this.cacheTree, { lossy: true })
62+
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
6363
this.cacheTree.keys[args.kid] = args
6464
await this.notifyUpdate(oldTree, this.cacheTree)
6565
return true
@@ -68,7 +68,7 @@ export class KeyStoreJson extends AbstractKeyStore {
6868
async list(args: {} = {}): Promise<ManagedKeyInfo[]> {
6969
const keys = Object.values(this.cacheTree.keys).map((key: IKey) => {
7070
const { kid, publicKeyHex, type, meta, kms } = key
71-
return { kid, publicKeyHex, type, meta: structuredClone(meta), kms } as ManagedKeyInfo
71+
return { kid, publicKeyHex, type, meta: deserialize(serialize(meta)), kms } as ManagedKeyInfo
7272
})
7373
return keys
7474
}

‎packages/data-store-json/src/identifier/private-key-store.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ImportablePrivateKey, ManagedPrivateKey } from '@veramo/key-manager/src
33
import { v4 as uuid4 } from 'uuid'
44
import Debug from 'debug'
55
import { DiffCallback, VeramoJsonCache, VeramoJsonStore } from '../types'
6-
import structuredClone from '@ungap/structured-clone'
6+
import { serialize, deserialize } from '@ungap/structured-clone'
77

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

@@ -42,7 +42,7 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
4242
}
4343

4444
async get({ alias }: { alias: string }): Promise<ManagedPrivateKey> {
45-
const key = structuredClone(this.cacheTree.privateKeys[alias])
45+
const key = deserialize(serialize(this.cacheTree.privateKeys[alias]))
4646
if (!key) throw Error('not_found: PrivateKey not found')
4747
if (this.secretBox && key.privateKeyHex) {
4848
key.privateKeyHex = await this.secretBox.decrypt(key.privateKeyHex)
@@ -54,7 +54,7 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
5454
debug(`Deleting private key data for alias=${alias}`)
5555
const privateKeyEntry = this.cacheTree.privateKeys[alias]
5656
if (privateKeyEntry) {
57-
const oldTree = structuredClone(this.cacheTree, { lossy: true })
57+
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
5858
delete this.cacheTree.privateKeys[alias]
5959
await this.notifyUpdate(oldTree, this.cacheTree)
6060
}
@@ -64,10 +64,10 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
6464
async import(args: ImportablePrivateKey): Promise<ManagedPrivateKey> {
6565
debug('Saving private key data', args.alias)
6666
const alias = args.alias || uuid4()
67-
const key: ManagedPrivateKey = structuredClone({
67+
const key: ManagedPrivateKey = deserialize(serialize({
6868
...args,
6969
alias,
70-
})
70+
}))
7171
if (this.secretBox && key.privateKeyHex) {
7272
const copy = key.privateKeyHex
7373
key.privateKeyHex = await this.secretBox.encrypt(copy)
@@ -79,14 +79,14 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
7979
)
8080
}
8181

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

8686
return key
8787
}
8888

8989
async list(): Promise<Array<ManagedPrivateKey>> {
90-
return structuredClone(Object.values(this.cacheTree.privateKeys))
90+
return deserialize(serialize(Object.values(this.cacheTree.privateKeys)))
9191
}
9292
}

0 commit comments

Comments
 (0)
Please sign in to comment.