-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { type Decoder, decode } from 'codeco' | ||
import type { DID, VerifyJWSResult } from 'dids' | ||
|
||
import { SignedEvent } from './codecs.js' | ||
import { getSignedEventPayload } from './signing.js' | ||
|
||
export type SignedEventContainer<Payload> = { | ||
signed: true | ||
payload: Payload | ||
verified: VerifyJWSResult | ||
cacaoBlock?: Uint8Array | ||
} | ||
|
||
export type UnsignedEventContainer<Payload> = { | ||
signed: false | ||
payload: Payload | ||
} | ||
|
||
export type EventContainer<Payload> = | ||
| SignedEventContainer<Payload> | ||
| UnsignedEventContainer<Payload> | ||
|
||
export function unsignedEventToContainer<Payload>( | ||
codec: Decoder<unknown, Payload>, | ||
event: unknown, | ||
): UnsignedEventContainer<Payload> { | ||
return { signed: false, payload: decode(codec, event) } | ||
} | ||
|
||
export async function signedEventToContainer<Payload>( | ||
did: DID, | ||
codec: Decoder<unknown, Payload>, | ||
event: SignedEvent, | ||
): Promise<SignedEventContainer<Payload>> { | ||
const cid = event.jws.link | ||
if (cid == null) { | ||
throw new Error('Missing linked block CID') | ||
} | ||
const [verified, payload] = await Promise.all([ | ||
did.verifyJWS(event.jws), | ||
getSignedEventPayload(codec, event), | ||
]) | ||
return { signed: true, verified, payload, cacaoBlock: event.cacaoBlock } | ||
} | ||
|
||
export async function eventToContainer<Payload>( | ||
did: DID, | ||
codec: Decoder<unknown, Payload>, | ||
event: unknown, | ||
): Promise<EventContainer<Payload>> { | ||
return SignedEvent.is(event) | ||
? await signedEventToContainer(did, codec, event) | ||
: unsignedEventToContainer(codec, event) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { randomStreamID } from '@ceramic-sdk/identifiers' | ||
import { getAuthenticatedDID } from '@ceramic-sdk/key-did' | ||
import { asDIDString } from '@didtools/codecs' | ||
|
||
import { InitEventPayload } from '../src/codecs.js' | ||
import { | ||
type SignedEventContainer, | ||
eventToContainer, | ||
} from '../src/container.js' | ||
import { signEvent } from '../src/signing.js' | ||
|
||
const authenticatedDID = await getAuthenticatedDID(new Uint8Array(32)) | ||
|
||
const testEventPayload: InitEventPayload = { | ||
data: null, | ||
header: { | ||
controllers: [asDIDString(authenticatedDID.id)], | ||
model: randomStreamID(), | ||
sep: 'test', | ||
}, | ||
} | ||
|
||
const encodedTestPayload = InitEventPayload.encode(testEventPayload) | ||
|
||
test('eventToContainer() verifies the signed event signature and extract the payload', async () => { | ||
const signed = await signEvent(authenticatedDID, encodedTestPayload) | ||
const container = await eventToContainer( | ||
authenticatedDID, | ||
InitEventPayload, | ||
signed, | ||
) | ||
expect(container.signed).toBe(true) | ||
expect( | ||
(container as SignedEventContainer<InitEventPayload>).verified, | ||
).toBeDefined() | ||
expect(container.payload).toEqual(testEventPayload) | ||
}) |
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