Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"cors": "^2.8.5",
"cross-eventsource": "^1.0.0",
"did-resolver": "^4.0.1",
"dids": "^5.0.0",
"dids": "5.1.0-next.0",
"express": "^4.18.2",
"http-status-codes": "^2.2.0",
"ipfs-http-client": "^60.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/__tests__/ceramic-multi-daemon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const makeCeramicCore = async (ipfs: IpfsApi, stateStoreDirectory: string): Prom
})

const handler = new TileDocumentHandler()
await handler.init()
handler.verifyJWS = (): Promise<void> => {
return
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/__tests__/make-ceramic-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function makeCeramicCore(
})

const handler = new TileDocumentHandler()
await handler.init()
;(handler as any).verifyJWS = (): Promise<void> => {
return
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/ceramic-daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class CeramicDaemon {
opts.ipfs?.host
)

const [modules, params] = Ceramic._processConfig(ipfs, ceramicConfig)
const [modules, params] = await Ceramic._processConfig(ipfs, ceramicConfig)
const diagnosticsLogger = modules.loggerProvider.getDiagnosticsLogger()
diagnosticsLogger.imp(
`Starting Ceramic Daemon with @ceramicnetwork/cli package version ${version}, with js-ceramic repo git hash ${commitHash}, and with config: \n${JSON.stringify(
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@types/lodash.clonedeep": "^4.5.6",
"@types/logfmt": "^1.2.2",
"@types/node": "^18.0.3",
"dids": "^5.0.0",
"dids": "5.1.0-next.0",
"express": "^4.18.2",
"get-port": "^7.0.0",
"ipfs-core-types": "^0.14.0",
Expand Down
84 changes: 56 additions & 28 deletions packages/common/src/ceramic-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,52 @@ export interface UnderlyingCeramicSigner {
createDagJWS(payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult>
verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult>
asController(): Promise<string>
withDid(did: DID): void
did: DID
}

class DidUnderlyingCeramicSigner implements UnderlyingCeramicSigner {
private _did?: DID

constructor(did?: DID) {
this._did = did
}

ensureDid(): void {
if (!this._did) {
throw new Error('No DID')
}
}

async ensureAuthenticated(): Promise<void> {
this.ensureDid()
if (!this._did.authenticated) {
await this._did.authenticate()
}
}
createJWS<T extends string | Record<string, any>>(
payload: T,
options?: CreateJWSOptions
): Promise<DagJWS> {
return this._did.createJWS(payload, options)
}
createDagJWS(payload: Record<string, any>, options?: CreateJWSOptions): Promise<DagJWSResult> {
return this._did.createDagJWS(payload, options)
}
verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult> {
this.ensureDid()
return this._did.verifyJWS(jws, options)
}
async asController(): Promise<string> {
return this._did.hasParent ? this._did.parent : this._did.id
}
withDid(did: DID): void {
this._did = did
}
get did(): DID {
this.ensureDid()
return this._did
}
}

export interface IntoSigner {
Expand All @@ -36,54 +82,39 @@ export interface IntoSigner {

export class CeramicSigner implements IntoSigner {
private isAuthenticated: boolean
private reqs?: UnderlyingCeramicSigner
private reqs: UnderlyingCeramicSigner

constructor(reqs?: UnderlyingCeramicSigner) {
constructor(reqs: UnderlyingCeramicSigner) {
this.isAuthenticated = false
this.reqs = reqs
}

get did(): DID {
return this.reqs.did
}

get signer(): CeramicSigner {
return this
}

static invalid(): CeramicSigner {
return new CeramicSigner()
return new CeramicSigner(new DidUnderlyingCeramicSigner())
}

static fromDID(did: DID): CeramicSigner {
const signer = new CeramicSigner()
const signer = new CeramicSigner(new DidUnderlyingCeramicSigner())
signer.withDid(did)
return signer
}

public withDid(did: DID) {
this.reqs = {
createDagJWS: (payload, options) => did.createDagJWS(payload, options),
createJWS: (payload, options) => did.createJWS(payload, options),
verifyJWS: (payload, options) => did.verifyJWS(payload, options),
async ensureAuthenticated(): Promise<void> {
if (!did.authenticated) {
await did.authenticate()
}
},
async asController(): Promise<string> {
return did.hasParent ? did.parent : did.id
},
}
}

private assertRequirements(): Promise<void> {
if (!this.reqs) {
return Promise.reject('Requirements not met for signing. Was a DID set?')
}
public withDid(did: DID): void {
this.reqs.withDid(did)
}

async createJWS<T extends string | Record<string, any>>(
payload: T,
options?: CreateJWSOptions
): Promise<DagJWS> {
await this.assertRequirements()
if (!this.isAuthenticated) {
await this.reqs.ensureAuthenticated()
this.isAuthenticated = true
Expand All @@ -95,7 +126,6 @@ export class CeramicSigner implements IntoSigner {
payload: Record<string, any>,
options?: CreateJWSOptions
): Promise<DagJWSResult> {
await this.assertRequirements()
if (!this.isAuthenticated) {
await this.reqs.ensureAuthenticated()
this.isAuthenticated = true
Expand All @@ -104,7 +134,6 @@ export class CeramicSigner implements IntoSigner {
}

async asController(): Promise<string> {
await this.assertRequirements()
if (!this.isAuthenticated) {
await this.reqs.ensureAuthenticated()
this.isAuthenticated = true
Expand All @@ -113,7 +142,6 @@ export class CeramicSigner implements IntoSigner {
}

async verifyJWS(jws: string | DagJWS, options?: VerifyJWSOptions): Promise<VerifyJWSResult> {
await this.assertRequirements()
return this.reqs.verifyJWS(jws, options)
}
}
8 changes: 7 additions & 1 deletion packages/common/src/stream-writer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { StreamID } from '@ceramicnetwork/streamid'
import { CreateOpts, LoadOpts, UpdateOpts, CeramicCommit, Stream, AnchorOpts } from './index.js'
import type { IntoSigner } from './ceramic-signer.js'
import type { CeramicSigner, IntoSigner } from './ceramic-signer.js'
import type { AnchorStatus } from './index.js'
import type { DID } from 'dids'

export interface StreamWriter extends IntoSigner {
/**
Expand Down Expand Up @@ -35,4 +36,9 @@ export interface StreamWriter extends IntoSigner {
* @param opts used to load the current Stream state
*/
requestAnchor(streamId: StreamID | string, opts?: LoadOpts & AnchorOpts): Promise<AnchorStatus>

/**
* Create a signer from a DID
*/
signerFromDID(did: DID): CeramicSigner
}
10 changes: 10 additions & 0 deletions packages/common/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,14 @@ export interface StreamHandler<T extends Stream> {
api: StreamReaderWriter,
state?: StreamState
): Promise<StreamState>

/**
* Do initialization associated with this StreamConstructor
*/
init(): Promise<void>

/**
* Shutdown resources associated with this StreamConstructor
*/
shutdown(): Promise<void>
}
5 changes: 3 additions & 2 deletions packages/common/src/utils/signature_utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Cacao } from '@didtools/cacao'
import type { CommitData, StreamState } from '../stream.js'
import type { StreamID } from '@ceramicnetwork/streamid'
import type { Verifiers } from '@didtools/cacao'
import { getEIP191Verifier } from '@didtools/pkh-ethereum'
import { getSolanaVerifier } from '@didtools/pkh-solana'
import { getStacksVerifier } from '@didtools/pkh-stacks'
Expand All @@ -11,7 +12,7 @@ import { StreamUtils } from './stream-utils.js'
const DEFAULT_CACAO_REVOCATION_PHASE_OUT_SECS = 24 * 60 * 60

// Register supported CACAO Verifiers
const verifiersCACAO = {
export const DEFAULT_VERIFIERS: Verifiers = {
...getEIP191Verifier(),
...getSolanaVerifier(),
...getStacksVerifier(),
Expand Down Expand Up @@ -48,7 +49,7 @@ export class SignatureUtils {
issuer: controller,
capability: cacao,
revocationPhaseOutSecs: DEFAULT_CACAO_REVOCATION_PHASE_OUT_SECS,
verifiers: verifiersCACAO,
// verifiers: DEFAULT_VERIFIERS,
})
} catch (e: any) {
const original = e.message ? e.message : String(e)
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"cartonne": "^3.0.1",
"codeco": "^1.1.0",
"dag-jose": "^4.0.0",
"dids": "^5.0.0",
"dids": "5.1.0-next.0",
"dids-threads": "^1.2.3",
"it-all": "^3.0.1",
"it-batch": "^3.0.1",
"it-first": "^3.0.4",
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/__tests__/handlers-map.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jest } from '@jest/globals'
import { HandlersMap } from '../handlers-map.js'
import { defaultHandlers, HandlersMap } from '../handlers-map.js'
import { Stream, StreamHandler, LoggerProvider } from '@ceramicnetwork/common'
import { Caip10LinkHandler } from '@ceramicnetwork/stream-caip10-link-handler'
import { ModelHandler } from '@ceramicnetwork/stream-model-handler'
Expand All @@ -10,8 +10,8 @@ const loggerProvider = new LoggerProvider()
const logger = loggerProvider.getDiagnosticsLogger()

describe('constructor', () => {
test('default handlers', () => {
const handlers = new HandlersMap(logger)
test('default handlers', async () => {
const handlers = new HandlersMap(logger, await defaultHandlers())
expect(handlers.get('tile')).toBeInstanceOf(TileDocumentHandler)
expect(handlers.get('caip10-link')).toBeInstanceOf(Caip10LinkHandler)
expect(handlers.get('model')).toBeInstanceOf(ModelHandler)
Expand All @@ -24,16 +24,16 @@ describe('constructor', () => {
})
})

test('set and get', () => {
test('set and get', async () => {
const customHandler = { name: 'custom', type: 13 } as unknown as StreamHandler<Stream>
const handlers = new HandlersMap(logger)
const handlers = new HandlersMap(logger, await defaultHandlers())
expect(() => handlers.get('custom')).toThrow()
handlers.add(customHandler)
expect(() => handlers.get('custom')).toThrow()
expect(handlers.get(13)).toBe(customHandler)
})

test('get non-existing', () => {
const handlers = new HandlersMap(logger)
test('get non-existing', async () => {
const handlers = new HandlersMap(logger, await defaultHandlers())
expect(() => handlers.get('custom')).toThrow()
})
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/initialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Ceramic integration', () => {
it('cannot create Ceramic instance on network not supported by our anchor service', async () => {
const tmpDirectory = await tmp.tmpName()
const databaseConnectionString = new URL(`sqlite://${tmpDirectory}/ceramic.sqlite`)
const [modules, params] = Ceramic._processConfig(ipfs1, {
const [modules, params] = await Ceramic._processConfig(ipfs1, {
networkName: 'local',
indexing: { db: databaseConnectionString.href, models: [] },
})
Expand Down
Loading