1- import { AgentContext } from "@credo-ts/core" ;
2- import {
3- KeyManagementService ,
4- KmsCreateKeyOptions ,
5- KmsCreateKeyReturn ,
6- KmsCreateKeyType ,
7- KmsDecryptOptions ,
8- KmsDecryptReturn ,
9- KmsDeleteKeyOptions ,
10- KmsEncryptOptions ,
11- KmsEncryptReturn ,
12- KmsImportKeyOptions ,
13- KmsImportKeyReturn ,
14- KmsJwkPrivate ,
15- KmsJwkPublic ,
16- KmsOperation ,
17- KmsRandomBytesOptions ,
18- KmsRandomBytesReturn ,
19- KmsSignOptions ,
20- KmsSignReturn ,
21- KmsVerifyOptions ,
22- KmsVerifyReturn
23- } from "@credo-ts/core/build/modules/kms" ;
1+ import { AgentContext , Kms } from "@credo-ts/core" ;
242import { ec as EC } from "elliptic" ;
253import _sodium from "libsodium-wrappers" ;
264import sjcl from "sjcl" ;
@@ -31,7 +9,7 @@ export interface JwkKeyPair {
319 keyType ?: string ;
3210}
3311
34- export class EnmshedHolderKeyManagmentService implements KeyManagementService {
12+ export class EnmshedHolderKeyManagmentService implements Kms . KeyManagementService {
3513 public static readonly backend = "fakeKeyManagementService" ;
3614
3715 public readonly backend = EnmshedHolderKeyManagmentService . backend ;
@@ -70,7 +48,7 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
7048 // console.log(`FKM: global instance state ${JSON.stringify(Array.from((globalThis as any).fakeKeyStorage.entries()))}`);
7149 }
7250
73- public isOperationSupported ( agentContext : AgentContext , operation : KmsOperation ) : boolean {
51+ public isOperationSupported ( agentContext : AgentContext , operation : Kms . KmsOperation ) : boolean {
7452 agentContext . config . logger . debug ( `EKM: Checking if operation is supported: ${ JSON . stringify ( operation ) } ` ) ;
7553 if ( operation . operation === "createKey" ) {
7654 if ( operation . type . kty === "OKP" ) {
@@ -98,18 +76,16 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
9876 }
9977 return false ;
10078 }
101-
102- public getPublicKey ( agentContext : AgentContext , keyId : string ) : Promise < KmsJwkPublic > {
79+ public getPublicKey ( agentContext : AgentContext , keyId : string ) : Promise < Kms . KmsJwkPublic > {
10380 const keyPair = this . keystore . get ( keyId ) ;
10481 if ( ! keyPair ) {
10582 agentContext . config . logger . error ( `EKM: Key with id ${ keyId } not found` ) ;
10683 throw new Error ( `Key with id ${ keyId } not found` ) ;
10784 }
10885
109- return Promise . resolve ( ( JSON . parse ( keyPair ) as JwkKeyPair ) . publicKey as KmsJwkPublic ) ;
86+ return Promise . resolve ( ( JSON . parse ( keyPair ) as JwkKeyPair ) . publicKey as Kms . KmsJwkPublic ) ;
11087 }
111-
112- public async createKey < Type extends KmsCreateKeyType > ( agentContext : AgentContext , options : KmsCreateKeyOptions < Type > ) : Promise < KmsCreateKeyReturn < Type > > {
88+ public async createKey < Type extends Kms . KmsCreateKeyType > ( agentContext : AgentContext , options : Kms . KmsCreateKeyOptions < Type > ) : Promise < Kms . KmsCreateKeyReturn < Type > > {
11389 options . keyId ??= "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" . replace ( / [ x y ] / g, function ( c ) {
11490 // Use libsodium's randombytes_uniform for secure random number generation
11591 const r = _sodium . randombytes_uniform ( 16 ) ;
@@ -151,8 +127,8 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
151127 this . updateGlobalInstance ( this . keystore ) ;
152128 return await Promise . resolve ( {
153129 keyId : options . keyId ,
154- publicJwk : publicJwk as KmsJwkPublic
155- } as KmsCreateKeyReturn < Type > ) ;
130+ publicJwk : publicJwk as Kms . KmsJwkPublic
131+ } as Kms . KmsCreateKeyReturn < Type > ) ;
156132 }
157133
158134 await _sodium . ready ;
@@ -186,16 +162,14 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
186162 this . updateGlobalInstance ( this . keystore ) ;
187163 return await Promise . resolve ( {
188164 keyId : options . keyId ,
189- publicJwk : publicJwk as KmsJwkPublic
190- } as KmsCreateKeyReturn < Type > ) ;
165+ publicJwk : publicJwk as Kms . KmsJwkPublic
166+ } as Kms . KmsCreateKeyReturn < Type > ) ;
191167 }
192-
193- public importKey < Jwk extends KmsJwkPrivate > ( agentContext : AgentContext , options : KmsImportKeyOptions < Jwk > ) : Promise < KmsImportKeyReturn < Jwk > > {
168+ public importKey < Jwk extends Kms . KmsJwkPrivate > ( agentContext : AgentContext , options : Kms . KmsImportKeyOptions < Jwk > ) : Promise < Kms . KmsImportKeyReturn < Jwk > > {
194169 agentContext . config . logger . debug ( `EKM: Importing key with ${ JSON . stringify ( options ) } ` ) ;
195170 throw new Error ( "Method not implemented." ) ;
196171 }
197-
198- public deleteKey ( agentContext : AgentContext , options : KmsDeleteKeyOptions ) : Promise < boolean > {
172+ public deleteKey ( agentContext : AgentContext , options : Kms . KmsDeleteKeyOptions ) : Promise < boolean > {
199173 if ( this . keystore . has ( options . keyId ) ) {
200174 agentContext . config . logger . debug ( `EKM: Deleting key with id ${ options . keyId } ` ) ;
201175 this . keystore . delete ( options . keyId ) ;
@@ -204,8 +178,7 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
204178 }
205179 throw new Error ( `key with id ${ options . keyId } not found. and cannot be deleted` ) ;
206180 }
207-
208- public async sign ( agentContext : AgentContext , options : KmsSignOptions ) : Promise < KmsSignReturn > {
181+ public async sign ( agentContext : AgentContext , options : Kms . KmsSignOptions ) : Promise < Kms . KmsSignReturn > {
209182 agentContext . config . logger . debug ( `EKM: Signing data with key id ${ options . keyId } using algorithm ${ options . algorithm } ` ) ;
210183
211184 const stringifiedKeyPair = this . keystore . get ( options . keyId ) ;
@@ -236,7 +209,7 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
236209
237210 return await Promise . resolve ( {
238211 signature : signatureBytes
239- } as KmsSignReturn ) ;
212+ } as Kms . KmsSignReturn ) ;
240213 }
241214
242215 await _sodium . ready ;
@@ -263,11 +236,11 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
263236 const signature = sodium . crypto_sign_detached ( options . data , fullPrivateKeyBytes ) ;
264237
265238 return {
266- signature : signature
239+ signature : signature as Uint8Array < ArrayBuffer > // I hope this cast doesn't paper over something
267240 } ;
268241 }
269242
270- public verify ( agentContext : AgentContext , options : KmsVerifyOptions ) : Promise < KmsVerifyReturn > {
243+ public verify ( agentContext : AgentContext , options : Kms . KmsVerifyOptions ) : Promise < Kms . KmsVerifyReturn > {
271244 agentContext . config . logger . debug ( `EKM: Verifying signature with key id ${ options . key . keyId } using algorithm ${ options . algorithm } ` ) ;
272245 // Use P-256 (aka secp256r1)
273246 const ec = new EC ( "p256" ) ;
@@ -293,7 +266,7 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
293266 const dataHash = ec . hash ( ) . update ( options . data ) . digest ( ) ;
294267 try {
295268 const verified = key . verify ( dataHash , signature ) ;
296- return Promise . resolve ( { verified : verified } as KmsVerifyReturn ) ;
269+ return Promise . resolve ( { verified : verified } as Kms . KmsVerifyReturn ) ;
297270 } catch ( e ) {
298271 agentContext . config . logger . error ( `EKM: Error during signature verification: ${ e } ` ) ;
299272 throw e ;
@@ -387,7 +360,7 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
387360 return hashBuf . subarray ( 0 , keyLength / 8 ) ;
388361 }
389362
390- public encrypt ( agentContext : AgentContext , options : KmsEncryptOptions ) : Promise < KmsEncryptReturn > {
363+ public encrypt ( agentContext : AgentContext , options : Kms . KmsEncryptOptions ) : Promise < Kms . KmsEncryptReturn > {
391364 try {
392365 // encryption via A-256-GCM
393366 // we will call the services side bob and the incoming side alice
@@ -437,12 +410,11 @@ export class EnmshedHolderKeyManagmentService implements KeyManagementService {
437410 }
438411 }
439412
440- public decrypt ( agentContext : AgentContext , options : KmsDecryptOptions ) : Promise < KmsDecryptReturn > {
413+ public decrypt ( agentContext : AgentContext , options : Kms . KmsDecryptOptions ) : Promise < Kms . KmsDecryptReturn > {
441414 agentContext . config . logger . debug ( `EKM: Decrypting data with key id ${ options . key . keyId } using options ${ options } ` ) ;
442415 throw new Error ( "Method not implemented." ) ;
443416 }
444-
445- public randomBytes ( agentContext : AgentContext , options : KmsRandomBytesOptions ) : KmsRandomBytesReturn {
417+ public randomBytes ( agentContext : AgentContext , options : Kms . KmsRandomBytesOptions ) : Kms . KmsRandomBytesReturn {
446418 agentContext . config . logger . debug ( `EKM: Generating ${ options . length } random bytes` ) ;
447419 return _sodium . randombytes_buf ( options . length ) ; // Uint8Array
448420 }
0 commit comments