@@ -114,7 +114,40 @@ export class PersistingWebCryptoAdapter implements CryptoAdapter {
114114 return { keyId, publicKey : bufferToMultibaseHex ( spki ) } ;
115115 }
116116
117- private async ensureKey ( keyId : string ) : Promise < CryptoKeyPair > {
117+ async getPublicKey ( keyId : string , _context ?: string ) : Promise < string > {
118+ const pair = await this . ensureKeyInMemory ( keyId ) ;
119+ const spki = await crypto . subtle . exportKey ( "spki" , pair . publicKey ) ;
120+ return bufferToMultibaseHex ( spki ) ;
121+ }
122+
123+ async sign ( keyId : string , payload : string ) : Promise < string > {
124+ const pair = await this . ensureKeyInMemory ( keyId ) ;
125+ const data = new TextEncoder ( ) . encode ( payload ) ;
126+ const sig = await crypto . subtle . sign ( SIGN_ALG , pair . privateKey , data ) ;
127+ return bufferToBase64 ( sig ) ;
128+ }
129+
130+ /** CryptoAdapter: sign with keyId and context (context ignored). */
131+ async signPayload (
132+ keyId : string ,
133+ _context : string ,
134+ payload : string ,
135+ ) : Promise < string > {
136+ return this . sign ( keyId , payload ) ;
137+ }
138+
139+ /** CryptoAdapter: ensure key exists (load from storage); return created: false (we don't create on demand by keyId). */
140+ async ensureKey ( keyId : string , _context : string ) : Promise < { created : boolean } > {
141+ if ( keyStore . has ( keyId ) ) return { created : false } ;
142+ const stored = readStoredKeys ( ) [ keyId ] ;
143+ if ( ! stored ?. privateKeyPkcs8Base64 ) {
144+ throw new Error ( `Key not found: ${ keyId } . Provision an eVault first to create a key.` ) ;
145+ }
146+ await this . ensureKeyInMemory ( keyId ) ;
147+ return { created : false } ;
148+ }
149+
150+ private async ensureKeyInMemory ( keyId : string ) : Promise < CryptoKeyPair > {
118151 let pair = keyStore . get ( keyId ) ;
119152 if ( pair ) return pair ;
120153 const stored = readStoredKeys ( ) [ keyId ] ;
@@ -137,17 +170,4 @@ export class PersistingWebCryptoAdapter implements CryptoAdapter {
137170 keyStore . set ( keyId , pair ) ;
138171 return pair ;
139172 }
140-
141- async getPublicKey ( keyId : string ) : Promise < string > {
142- const pair = await this . ensureKey ( keyId ) ;
143- const spki = await crypto . subtle . exportKey ( "spki" , pair . publicKey ) ;
144- return bufferToMultibaseHex ( spki ) ;
145- }
146-
147- async sign ( keyId : string , payload : string ) : Promise < string > {
148- const pair = await this . ensureKey ( keyId ) ;
149- const data = new TextEncoder ( ) . encode ( payload ) ;
150- const sig = await crypto . subtle . sign ( SIGN_ALG , pair . privateKey , data ) ;
151- return bufferToBase64 ( sig ) ;
152- }
153173}
0 commit comments