Skip to content

Commit 7ec971c

Browse files
committed
feat: worker threads
Utilizes worker threads for did verification and signing. Additionally uses worker threads for model and model instance validation.
1 parent 4f86082 commit 7ec971c

40 files changed

+292
-172
lines changed

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"cors": "^2.8.5",
6262
"cross-eventsource": "^1.0.0",
6363
"did-resolver": "^4.0.1",
64-
"dids": "^5.0.0",
64+
"dids": "5.1.0-next.0",
6565
"express": "^4.18.2",
6666
"http-status-codes": "^2.2.0",
6767
"ipfs-http-client": "^60.0.0",

packages/cli/src/__tests__/ceramic-multi-daemon.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const makeCeramicCore = async (ipfs: IpfsApi, stateStoreDirectory: string): Prom
2727
})
2828

2929
const handler = new TileDocumentHandler()
30+
await handler.init()
3031
handler.verifyJWS = (): Promise<void> => {
3132
return
3233
}

packages/cli/src/__tests__/make-ceramic-core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export async function makeCeramicCore(
1919
})
2020

2121
const handler = new TileDocumentHandler()
22+
await handler.init()
2223
;(handler as any).verifyJWS = (): Promise<void> => {
2324
return
2425
}

packages/cli/src/ceramic-daemon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export class CeramicDaemon {
281281
opts.ipfs?.host
282282
)
283283

284-
const [modules, params] = Ceramic._processConfig(ipfs, ceramicConfig)
284+
const [modules, params] = await Ceramic._processConfig(ipfs, ceramicConfig)
285285
const diagnosticsLogger = modules.loggerProvider.getDiagnosticsLogger()
286286
diagnosticsLogger.imp(
287287
`Starting Ceramic Daemon with @ceramicnetwork/cli package version ${version}, with js-ceramic repo git hash ${commitHash}, and with config: \n${JSON.stringify(

packages/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"@types/lodash.clonedeep": "^4.5.6",
6666
"@types/logfmt": "^1.2.2",
6767
"@types/node": "^18.0.3",
68-
"dids": "^5.0.0",
68+
"dids": "5.1.0-next.0",
6969
"express": "^4.18.2",
7070
"get-port": "^7.0.0",
7171
"ipfs-core-types": "^0.14.0",

packages/common/src/ceramic-signer.ts

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,52 @@ export interface UnderlyingCeramicSigner {
2828
createDagJWS(payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult>
2929
verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult>
3030
asController(): Promise<string>
31+
withDid(did: DID): void
32+
did: DID
33+
}
34+
35+
class DidUnderlyingCeramicSigner implements UnderlyingCeramicSigner {
36+
private _did?: DID
37+
38+
constructor(did?: DID) {
39+
this._did = did
40+
}
41+
42+
ensureDid(): void {
43+
if (!this._did) {
44+
throw new Error('No DID')
45+
}
46+
}
47+
48+
async ensureAuthenticated(): Promise<void> {
49+
this.ensureDid()
50+
if (!this._did.authenticated) {
51+
await this._did.authenticate()
52+
}
53+
}
54+
createJWS<T extends string | Record<string, any>>(
55+
payload: T,
56+
options?: CreateJWSOptions
57+
): Promise<DagJWS> {
58+
return this._did.createJWS(payload, options)
59+
}
60+
createDagJWS(payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult> {
61+
return this._did.createDagJWS(payload, options)
62+
}
63+
verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult> {
64+
this.ensureDid()
65+
return this._did.verifyJWS(jws, options)
66+
}
67+
async asController(): Promise<string> {
68+
return this._did.hasParent ? this._did.parent : this._did.id
69+
}
70+
withDid(did: DID): void {
71+
this._did = did
72+
}
73+
get did(): DID {
74+
this.ensureDid()
75+
return this._did
76+
}
3177
}
3278

3379
export interface IntoSigner {
@@ -36,54 +82,39 @@ export interface IntoSigner {
3682

3783
export class CeramicSigner implements IntoSigner {
3884
private isAuthenticated: boolean
39-
private reqs?: UnderlyingCeramicSigner
85+
private reqs: UnderlyingCeramicSigner
4086

41-
constructor(reqs?: UnderlyingCeramicSigner) {
87+
constructor(reqs: UnderlyingCeramicSigner) {
4288
this.isAuthenticated = false
4389
this.reqs = reqs
4490
}
4591

92+
get did(): DID {
93+
return this.reqs.did
94+
}
95+
4696
get signer(): CeramicSigner {
4797
return this
4898
}
4999

50100
static invalid(): CeramicSigner {
51-
return new CeramicSigner()
101+
return new CeramicSigner(new DidUnderlyingCeramicSigner())
52102
}
53103

54104
static fromDID(did: DID): CeramicSigner {
55-
const signer = new CeramicSigner()
105+
const signer = new CeramicSigner(new DidUnderlyingCeramicSigner())
56106
signer.withDid(did)
57107
return signer
58108
}
59109

60-
public withDid(did: DID) {
61-
this.reqs = {
62-
createDagJWS: (payload, options) => did.createDagJWS(payload, options),
63-
createJWS: (payload, options) => did.createJWS(payload, options),
64-
verifyJWS: (payload, options) => did.verifyJWS(payload, options),
65-
async ensureAuthenticated(): Promise<void> {
66-
if (!did.authenticated) {
67-
await did.authenticate()
68-
}
69-
},
70-
async asController(): Promise<string> {
71-
return did.hasParent ? did.parent : did.id
72-
},
73-
}
74-
}
75-
76-
private assertRequirements(): Promise<void> {
77-
if (!this.reqs) {
78-
return Promise.reject('Requirements not met for signing. Was a DID set?')
79-
}
110+
public withDid(did: DID): void {
111+
this.reqs.withDid(did)
80112
}
81113

82114
async createJWS<T extends string | Record<string, any>>(
83115
payload: T,
84116
options?: CreateJWSOptions
85117
): Promise<DagJWS> {
86-
await this.assertRequirements()
87118
if (!this.isAuthenticated) {
88119
await this.reqs.ensureAuthenticated()
89120
this.isAuthenticated = true
@@ -95,7 +126,6 @@ export class CeramicSigner implements IntoSigner {
95126
payload: Record<string, any>,
96127
options?: CreateJWSOptions
97128
): Promise<DagJWSResult> {
98-
await this.assertRequirements()
99129
if (!this.isAuthenticated) {
100130
await this.reqs.ensureAuthenticated()
101131
this.isAuthenticated = true
@@ -104,7 +134,6 @@ export class CeramicSigner implements IntoSigner {
104134
}
105135

106136
async asController(): Promise<string> {
107-
await this.assertRequirements()
108137
if (!this.isAuthenticated) {
109138
await this.reqs.ensureAuthenticated()
110139
this.isAuthenticated = true
@@ -113,7 +142,6 @@ export class CeramicSigner implements IntoSigner {
113142
}
114143

115144
async verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult> {
116-
await this.assertRequirements()
117145
return this.reqs.verifyJWS(jws, options)
118146
}
119147
}

packages/common/src/stream-writer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { StreamID } from '@ceramicnetwork/streamid'
22
import { CreateOpts, LoadOpts, UpdateOpts, CeramicCommit, Stream, AnchorOpts } from './index.js'
3-
import type { IntoSigner } from './ceramic-signer.js'
3+
import type { CeramicSigner, IntoSigner } from './ceramic-signer.js'
44
import type { AnchorStatus } from './index.js'
5+
import type { DID } from 'dids'
56

67
export interface StreamWriter extends IntoSigner {
78
/**
@@ -35,4 +36,9 @@ export interface StreamWriter extends IntoSigner {
3536
* @param opts used to load the current Stream state
3637
*/
3738
requestAnchor(streamId: StreamID | string, opts?: LoadOpts & AnchorOpts): Promise<AnchorStatus>
39+
40+
/**
41+
* Create a signer from a DID
42+
*/
43+
signerFromDID(did: DID): CeramicSigner
3844
}

packages/common/src/stream.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,9 @@ export interface StreamHandler<T extends Stream> {
337337
api: StreamReaderWriter,
338338
state?: StreamState
339339
): Promise<StreamState>
340+
341+
/**
342+
* Do initialization associated with this StreamConstructor
343+
*/
344+
init(): Promise<void>
340345
}

packages/common/src/utils/signature_utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Cacao } from '@didtools/cacao'
22
import type { CommitData } from '../index.js'
33
import type { StreamID } from '@ceramicnetwork/streamid'
4+
import type { Verifiers } from '@didtools/cacao'
45
import { getEIP191Verifier } from '@didtools/pkh-ethereum'
56
import { getSolanaVerifier } from '@didtools/pkh-solana'
67
import { getStacksVerifier } from '@didtools/pkh-stacks'
@@ -10,7 +11,7 @@ import { CeramicSigner } from '../ceramic-signer.js'
1011
const DEFAULT_CACAO_REVOCATION_PHASE_OUT = 24 * 60 * 60
1112

1213
// Register supported CACAO Verifiers
13-
const verifiersCACAO = {
14+
export const DEFAULT_VERIFIERS: Verifiers = {
1415
...getEIP191Verifier(),
1516
...getSolanaVerifier(),
1617
...getStacksVerifier(),
@@ -47,7 +48,7 @@ export class SignatureUtils {
4748
issuer: controller,
4849
capability: cacao,
4950
revocationPhaseOutSecs: DEFAULT_CACAO_REVOCATION_PHASE_OUT,
50-
verifiers: verifiersCACAO,
51+
// verifiers: DEFAULT_VERIFIERS,
5152
})
5253
} catch (e: any) {
5354
const original = e.message ? e.message : String(e)

packages/common/src/utils/stream-utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ export class StreamUtils {
146146
cloned.next.metadata.model = state.next.metadata.model.toString()
147147
}
148148
if (state.metadata?.unique && state.type != TILE_TYPE_ID) {
149-
cloned.metadata.unique = uint8arrays.toString(cloned.metadata.unique, 'base64')
149+
cloned.metadata.unique = uint8arrays.toString(
150+
Uint8Array.from(state.metadata.unique),
151+
'base64'
152+
)
150153
}
151154

152155
cloned.doctype = StreamType.nameByCode(cloned.type)

0 commit comments

Comments
 (0)