@@ -138,7 +138,7 @@ export class DID {
138138 if ( capability ) {
139139 this . _capability = capability
140140 this . _parentId = this . _capability . p . iss
141- if ( this . _parentId . startsWith ( 'did:pkh:eip155:1:' ) ) {
141+ if ( this . _parentId ? .startsWith ( 'did:pkh:eip155:1:' ) ) {
142142 // Lower case ethereum address for compatibility with Ceramic
143143 this . _parentId = this . _parentId . toLowerCase ( )
144144 }
@@ -268,25 +268,6 @@ export class DID {
268268 return this . _id
269269 }
270270
271- /**
272- * Request a JWS from this did's client
273- * @param options
274- * @param payload
275- */
276- async requestJWS < T extends string | Record < string , any > > (
277- options : CreateJWSOptions ,
278- payload : T
279- ) : Promise < GeneralJWS > {
280- if ( this . _client == null ) throw new Error ( 'No provider available' )
281- if ( this . _id == null ) throw new Error ( 'DID is not authenticated' )
282- const { jws } = await this . _client . request ( 'did_createJWS' , {
283- did : this . _id ,
284- ...options ,
285- payload,
286- } )
287- return jws
288- }
289-
290271 /**
291272 * Create a JWS encoded signature over the given payload.
292273 * Will be signed by the currently authenticated DID.
@@ -306,6 +287,25 @@ export class DID {
306287 } )
307288 }
308289
290+ /**
291+ * Request a JWS from this did's client
292+ * @param options
293+ * @param payload
294+ */
295+ async requestJWS < T extends string | Record < string , any > > (
296+ options : CreateJWSOptions ,
297+ payload : T
298+ ) : Promise < GeneralJWS > {
299+ if ( this . _client == null ) throw new Error ( 'No provider available' )
300+ if ( this . _id == null ) throw new Error ( 'DID is not authenticated' )
301+ const { jws } = await this . _client . request ( 'did_createJWS' , {
302+ did : this . _id ,
303+ ...options ,
304+ payload,
305+ } )
306+ return jws
307+ }
308+
309309 /**
310310 * Create an IPFS compatibe DagJWS encoded signature over the given payload.
311311 * Will be signed by the currently authenticated DID.
@@ -472,6 +472,69 @@ export type VerifyCacaoParameters = {
472472 disableExpirationCheck ?: boolean
473473}
474474
475+ export type CreateJWSUsingParameters < T extends string | Record < string , any > > = {
476+ capability ?: Cacao
477+ payload : T
478+ options : CreateJWSOptions
479+ request : ( options : CreateJWSOptions , payload : T ) => Promise < DagJWS >
480+ }
481+
482+ export async function createJWSUsing < T extends string | Record < string , any > > (
483+ params : CreateJWSUsingParameters < T >
484+ ) : Promise < DagJWS > {
485+ if ( params . capability ) {
486+ const exp = params . capability . p . exp
487+ if ( exp && Date . parse ( exp ) < Date . now ( ) ) {
488+ throw new Error ( 'Capability is expired, cannot create a valid signature' )
489+ }
490+ const cacaoBlock = await CacaoBlock . fromCacao ( params . capability )
491+ const capCID = CID . asCID ( cacaoBlock . cid )
492+ if ( ! capCID ) {
493+ throw new Error (
494+ `Capability CID of the JWS cannot be set to the capability payload cid as they are incompatible`
495+ )
496+ }
497+ params . options . protected = params . options . protected || { }
498+ params . options . protected . cap = `ipfs://${ capCID ?. toString ( ) } `
499+ }
500+ return await params . request ( params . options , params . payload )
501+ }
502+
503+ export type CreateDagJWSUsingParameters = {
504+ capability ?: Cacao
505+ payload : Record < string , any >
506+ options : CreateJWSOptions
507+ request : ( options : CreateJWSOptions , payload : string ) => Promise < DagJWS >
508+ }
509+
510+ export async function createDagJWSUsing (
511+ params : CreateDagJWSUsingParameters
512+ ) : Promise < DagJWSResult > {
513+ const { cid, linkedBlock } = await encodePayload ( params . payload )
514+ const payloadCid = encodeBase64Url ( cid . bytes )
515+ Object . assign ( params . options , { linkedBlock : encodeBase64 ( linkedBlock ) } )
516+ const jws = await createJWSUsing ( {
517+ capability : params . capability ,
518+ payload : payloadCid ,
519+ options : params . options ,
520+ request : params . request ,
521+ } )
522+
523+ const compatibleCID = CID . asCID ( cid )
524+ if ( ! compatibleCID ) {
525+ throw new Error (
526+ 'CID of the JWS cannot be set to the encoded payload cid as they are incompatible'
527+ )
528+ }
529+ jws . link = compatibleCID
530+
531+ if ( params . capability ) {
532+ const cacaoBlock = await CacaoBlock . fromCacao ( params . capability )
533+ return { jws, linkedBlock, cacaoBlock : cacaoBlock . bytes }
534+ }
535+ return { jws, linkedBlock }
536+ }
537+
475538export type VerifyJWSUsingParameters = {
476539 resolve : ( url : string ) => Promise < DIDResolutionResult >
477540 verifyCacao : ( cacao : Cacao , opts : VerifyCacaoParameters ) => Promise < void >
@@ -511,7 +574,8 @@ export async function verifyJWSUsing(params: VerifyJWSUsingParameters): Promise<
511574 const signerDid = didResolutionResult . didDocument ?. id
512575 if (
513576 options . issuer &&
514- options . issuer === options . capability ?. p . iss &&
577+ options . capability &&
578+ issuerEquals ( options . issuer , options . capability ?. p . iss ) &&
515579 signerDid === options . capability . p . aud
516580 ) {
517581 await params . verifyCacao ( options . capability , {
@@ -553,67 +617,4 @@ export async function verifyJWSUsing(params: VerifyJWSUsingParameters): Promise<
553617 // If an error is thrown it means that the payload is a CID.
554618 }
555619 return { kid, payload, didResolutionResult }
556- }
557-
558- export type CreateDagJWSUsingParameters = {
559- capability ?: Cacao
560- payload : Record < string , any >
561- options : CreateJWSOptions
562- request : ( options : CreateJWSOptions , payload : string ) => Promise < DagJWS >
563- }
564-
565- export async function createDagJWSUsing (
566- params : CreateDagJWSUsingParameters
567- ) : Promise < DagJWSResult > {
568- const { cid, linkedBlock } = await encodePayload ( params . payload )
569- const payloadCid = encodeBase64Url ( cid . bytes )
570- Object . assign ( params . options , { linkedBlock : encodeBase64 ( linkedBlock ) } )
571- const jws = await createJWSUsing ( {
572- capability : params . capability ,
573- payload : payloadCid ,
574- options : params . options ,
575- request : params . request ,
576- } )
577-
578- const compatibleCID = CID . asCID ( cid )
579- if ( ! compatibleCID ) {
580- throw new Error (
581- 'CID of the JWS cannot be set to the encoded payload cid as they are incompatible'
582- )
583- }
584- jws . link = compatibleCID
585-
586- if ( params . capability ) {
587- const cacaoBlock = await CacaoBlock . fromCacao ( params . capability )
588- return { jws, linkedBlock, cacaoBlock : cacaoBlock . bytes }
589- }
590- return { jws, linkedBlock }
591- }
592-
593- export type CreateJWSUsingParameters < T extends string | Record < string , any > > = {
594- capability ?: Cacao
595- payload : T
596- options : CreateJWSOptions
597- request : ( options : CreateJWSOptions , payload : T ) => Promise < DagJWS >
598- }
599-
600- export async function createJWSUsing < T extends string | Record < string , any > > (
601- params : CreateJWSUsingParameters < T >
602- ) : Promise < DagJWS > {
603- if ( params . capability ) {
604- const exp = params . capability . p . exp
605- if ( exp && Date . parse ( exp ) < Date . now ( ) ) {
606- throw new Error ( 'Capability is expired, cannot create a valid signature' )
607- }
608- const cacaoBlock = await CacaoBlock . fromCacao ( params . capability )
609- const capCID = CID . asCID ( cacaoBlock . cid )
610- if ( ! capCID ) {
611- throw new Error (
612- `Capability CID of the JWS cannot be set to the capability payload cid as they are incompatible`
613- )
614- }
615- params . options . protected = params . options . protected || { }
616- params . options . protected . cap = `ipfs://${ capCID ?. toString ( ) } `
617- }
618- return await params . request ( params . options , params . payload )
619620}
0 commit comments