@@ -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
3379export interface IntoSigner {
@@ -36,54 +82,39 @@ export interface IntoSigner {
3682
3783export 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}
0 commit comments