11import { AgentContext , Kms } from "@credo-ts/core" ;
22import { ec as EC } from "elliptic" ;
3- import _sodium from "libsodium-wrappers" ;
3+
4+ import { SodiumWrapper } from "@nmshd/crypto" ;
45import sjcl from "sjcl" ;
56import { KeyStorage } from "./KeyStorage" ;
67
@@ -15,8 +16,8 @@ export class EnmshedHolderKeyManagmentService implements Kms.KeyManagementServic
1516
1617 public readonly backend = EnmshedHolderKeyManagmentService . backend ;
1718
18- private readonly b64url = ( bytes : Uint8Array ) => _sodium . to_base64 ( bytes , _sodium . base64_variants . URLSAFE_NO_PADDING ) ;
19- private readonly b64urlDecode = ( b64url : string ) => _sodium . from_base64 ( b64url , _sodium . base64_variants . URLSAFE_NO_PADDING ) ;
19+ private readonly b64url = ( bytes : Uint8Array ) => SodiumWrapper . sodium . to_base64 ( bytes , ( SodiumWrapper . sodium as any ) . base64_variants . URLSAFE_NO_PADDING ) ;
20+ private readonly b64urlDecode = ( b64url : string ) => SodiumWrapper . sodium . from_base64 ( b64url , ( SodiumWrapper . sodium as any ) . base64_variants . URLSAFE_NO_PADDING ) ;
2021
2122 // please note: we cannot use buffer here - because it is not available in the browser
2223 // and yes it could be pollyfilled but that extends the bundle size for no good reason
@@ -75,7 +76,7 @@ export class EnmshedHolderKeyManagmentService implements Kms.KeyManagementServic
7576 public async createKey < Type extends Kms . KmsCreateKeyType > ( agentContext : AgentContext , options : Kms . KmsCreateKeyOptions < Type > ) : Promise < Kms . KmsCreateKeyReturn < Type > > {
7677 options . keyId ??= "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" . replace ( / [ x y ] / g, function ( c ) {
7778 // Use libsodium's randombytes_uniform for secure random number generation
78- const r = _sodium . randombytes_uniform ( 16 ) ;
79+ const r = SodiumWrapper . sodium . randombytes_uniform ( 16 ) ;
7980 const v = c === "x" ? r : ( r & 0x3 ) | 0x8 ;
8081 return v . toString ( 16 ) ;
8182 } ) ;
@@ -114,12 +115,9 @@ export class EnmshedHolderKeyManagmentService implements Kms.KeyManagementServic
114115 return { keyId : options . keyId , publicJwk : publicJwk as Kms . KmsJwkPublic } as Kms . KmsCreateKeyReturn < Type > ;
115116 }
116117
117- await _sodium . ready ;
118- const sodium = _sodium ;
119-
120- const { keyType, publicKey, privateKey } = sodium . crypto_sign_keypair ( ) ;
118+ const { keyType, publicKey, privateKey } = SodiumWrapper . sodium . crypto_sign_keypair ( ) ;
121119 agentContext . config . logger . debug ( `EKM: Created OKP key pair with id ${ options . keyId } and keyType ${ keyType } ` ) ;
122- const seed = privateKey . slice ( 0 , sodium . crypto_sign_SEEDBYTES ) ;
120+ const seed = privateKey . slice ( 0 , ( SodiumWrapper . sodium as any ) . crypto_sign_SEEDBYTES ) ;
123121
124122 // Public JWK
125123 const publicJwk = {
@@ -192,10 +190,8 @@ export class EnmshedHolderKeyManagmentService implements Kms.KeyManagementServic
192190 } as Kms . KmsSignReturn ) ;
193191 }
194192
195- await _sodium . ready ;
196- const sodium = _sodium ;
197- const decode = ( bytes : string ) => sodium . from_base64 ( bytes , sodium . base64_variants . URLSAFE_NO_PADDING ) ;
198- // get the priavte key bytes
193+ const decode = ( bytes : string ) => SodiumWrapper . sodium . from_base64 ( bytes , ( SodiumWrapper . sodium as any ) . base64_variants . URLSAFE_NO_PADDING ) ;
194+ // get the private key bytes
199195 if ( privateKey . d === undefined ) {
200196 throw new Error ( "Private key does not contain 'd' parameter" ) ;
201197 }
@@ -213,7 +209,7 @@ export class EnmshedHolderKeyManagmentService implements Kms.KeyManagementServic
213209 fullPrivateKeyBytes . set ( publicKeyBytes , privateKeyBytes . length ) ;
214210
215211 // and use it to sign the data
216- const signature = sodium . crypto_sign_detached ( options . data , fullPrivateKeyBytes ) ;
212+ const signature = SodiumWrapper . sodium . crypto_sign_detached ( options . data , fullPrivateKeyBytes ) ;
217213
218214 return {
219215 signature : signature as Uint8Array < ArrayBuffer > // I hope this cast doesn't paper over something
@@ -396,6 +392,6 @@ export class EnmshedHolderKeyManagmentService implements Kms.KeyManagementServic
396392 }
397393 public randomBytes ( agentContext : AgentContext , options : Kms . KmsRandomBytesOptions ) : Kms . KmsRandomBytesReturn {
398394 agentContext . config . logger . debug ( `EKM: Generating ${ options . length } random bytes` ) ;
399- return _sodium . randombytes_buf ( options . length ) ; // Uint8Array
395+ return SodiumWrapper . sodium . randombytes_buf ( options . length ) ; // Uint8Array
400396 }
401397}
0 commit comments