11// Mock dependencies
2- jest . mock ( '@docknetwork/universal-wallet/storage/edv-http-storage' , ( ) => jest . fn ( ) ) ;
2+ jest . mock ( '@docknetwork/universal-wallet/storage/edv-http-storage' , ( ) =>
3+ jest . fn ( ) ,
4+ ) ;
35jest . mock ( './hmac' , ( ) => jest . fn ( ) ) ;
46jest . mock ( '@digitalbazaar/ed25519-verification-key-2018' , ( ) => ( {
57 Ed25519VerificationKey2018 : jest . fn ( ) ,
@@ -96,16 +98,19 @@ describe('EDVService', () => {
9698 . spyOn ( service , 'initializeFromMasterKey' )
9799 . mockResolvedValue ( undefined ) ;
98100
99- // Mock utilCryptoService at the instance level by mocking the entire method
100- const originalMethod = service . initializeFromMnemonic . bind ( service ) ;
101-
102101 // We can't easily mock the imported utilCryptoService, so we test via
103102 // initializeFromMasterKey being called correctly
104103 // Instead, test with a real (but simple) flow using spyOn
105- jest . spyOn ( service , 'initializeFromMnemonic' ) . mockImplementation ( async ( { mnemonic, edvUrl, authKey} ) => {
106- // Simulate what the real method does: convert mnemonic to masterKey, then call initializeFromMasterKey
107- return mockInitFromMasterKey ( { masterKey : mockMasterKey , edvUrl, authKey} ) ;
108- } ) ;
104+ jest
105+ . spyOn ( service , 'initializeFromMnemonic' )
106+ . mockImplementation ( async ( { mnemonic, edvUrl, authKey} ) => {
107+ // Simulate what the real method does: convert mnemonic to masterKey, then call initializeFromMasterKey
108+ return mockInitFromMasterKey ( {
109+ masterKey : mockMasterKey ,
110+ edvUrl,
111+ authKey,
112+ } ) ;
113+ } ) ;
109114
110115 await service . initializeFromMnemonic ( {
111116 mnemonic : 'test mnemonic' ,
@@ -125,33 +130,47 @@ describe('EDVService', () => {
125130 it ( 'should encrypt with proper Uint8Array inputs' , async ( ) => {
126131 const masterKey = new Uint8Array ( [ 1 , 2 , 3 ] ) ;
127132 const encryptionKey = Buffer . from ( [ 4 , 5 , 6 ] ) ;
128- const iv = Buffer . from ( [ 7 , 8 , 9 ] ) ;
133+ const testIv = Buffer . from ( [ 7 , 8 , 9 ] ) ;
129134
130135 // Mock the encrypt/decrypt methods entirely to avoid crypto mock issues
131- jest . spyOn ( service , 'encryptMasterKey' ) . mockImplementation ( async ( mk , ek , iv ) => {
132- // Verify the type conversion happens
133- const convertedMk = mk instanceof Uint8Array ? mk : new Uint8Array ( Object . values ( mk ) ) ;
134- expect ( convertedMk ) . toBeInstanceOf ( Uint8Array ) ;
135- return new Uint8Array ( [ 99 , 98 , 97 ] ) ;
136- } ) ;
136+ jest
137+ . spyOn ( service , 'encryptMasterKey' )
138+ . mockImplementation ( async ( mk , ek , mockIv ) => {
139+ // Verify the type conversion happens
140+ const convertedMk =
141+ mk instanceof Uint8Array ? mk : new Uint8Array ( Object . values ( mk ) ) ;
142+ expect ( convertedMk ) . toBeInstanceOf ( Uint8Array ) ;
143+ return new Uint8Array ( [ 99 , 98 , 97 ] ) ;
144+ } ) ;
137145
138- const result = await service . encryptMasterKey ( masterKey , encryptionKey , iv ) ;
146+ const result = await service . encryptMasterKey (
147+ masterKey ,
148+ encryptionKey ,
149+ testIv ,
150+ ) ;
139151 expect ( result ) . toBeInstanceOf ( Uint8Array ) ;
140152 } ) ;
141153
142154 it ( 'should handle serialized plain object masterKey' , async ( ) => {
143155 const serializedMasterKey = { 0 : 1 , 1 : 2 , 2 : 3 } ;
144156 const encryptionKey = Buffer . from ( [ 4 , 5 , 6 ] ) ;
145- const iv = Buffer . from ( [ 7 , 8 , 9 ] ) ;
146-
147- jest . spyOn ( service , 'encryptMasterKey' ) . mockImplementation ( async ( mk , ek , iv ) => {
148- const convertedMk = mk instanceof Uint8Array ? mk : new Uint8Array ( Object . values ( mk ) ) ;
149- expect ( convertedMk ) . toBeInstanceOf ( Uint8Array ) ;
150- expect ( convertedMk ) . toEqual ( new Uint8Array ( [ 1 , 2 , 3 ] ) ) ;
151- return new Uint8Array ( [ 99 , 98 , 97 ] ) ;
152- } ) ;
157+ const testIv = Buffer . from ( [ 7 , 8 , 9 ] ) ;
158+
159+ jest
160+ . spyOn ( service , 'encryptMasterKey' )
161+ . mockImplementation ( async ( mk , ek , mockIv ) => {
162+ const convertedMk =
163+ mk instanceof Uint8Array ? mk : new Uint8Array ( Object . values ( mk ) ) ;
164+ expect ( convertedMk ) . toBeInstanceOf ( Uint8Array ) ;
165+ expect ( convertedMk ) . toEqual ( new Uint8Array ( [ 1 , 2 , 3 ] ) ) ;
166+ return new Uint8Array ( [ 99 , 98 , 97 ] ) ;
167+ } ) ;
153168
154- const result = await service . encryptMasterKey ( serializedMasterKey , encryptionKey , iv ) ;
169+ const result = await service . encryptMasterKey (
170+ serializedMasterKey ,
171+ encryptionKey ,
172+ testIv ,
173+ ) ;
155174 expect ( result ) . toBeInstanceOf ( Uint8Array ) ;
156175 } ) ;
157176 } ) ;
@@ -164,15 +183,18 @@ describe('EDVService', () => {
164183 const serializedIv = { 0 : 7 , 1 : 8 , 2 : 9 } ;
165184
166185 // Verify the conversion works for each input
167- const convertedEncKey = serializedEncryptedKey instanceof Uint8Array
168- ? serializedEncryptedKey
169- : new Uint8Array ( Object . values ( serializedEncryptedKey ) ) ;
170- const convertedDecKey = serializedDecryptionKey instanceof Uint8Array
171- ? serializedDecryptionKey
172- : new Uint8Array ( Object . values ( serializedDecryptionKey ) ) ;
173- const convertedIv = serializedIv instanceof Uint8Array
174- ? serializedIv
175- : new Uint8Array ( Object . values ( serializedIv ) ) ;
186+ const convertedEncKey =
187+ serializedEncryptedKey instanceof Uint8Array
188+ ? serializedEncryptedKey
189+ : new Uint8Array ( Object . values ( serializedEncryptedKey ) ) ;
190+ const convertedDecKey =
191+ serializedDecryptionKey instanceof Uint8Array
192+ ? serializedDecryptionKey
193+ : new Uint8Array ( Object . values ( serializedDecryptionKey ) ) ;
194+ const convertedIv =
195+ serializedIv instanceof Uint8Array
196+ ? serializedIv
197+ : new Uint8Array ( Object . values ( serializedIv ) ) ;
176198
177199 expect ( convertedEncKey ) . toBeInstanceOf ( Uint8Array ) ;
178200 expect ( convertedEncKey ) . toEqual ( new Uint8Array ( [ 10 , 20 , 30 ] ) ) ;
@@ -185,31 +207,35 @@ describe('EDVService', () => {
185207 it ( 'should handle mixed input types correctly' , async ( ) => {
186208 const encryptedKey = new Uint8Array ( [ 10 , 20 , 30 ] ) ;
187209 const serializedDecryptionKey = { 0 : 4 , 1 : 5 , 2 : 6 } ;
188- const iv = Buffer . from ( [ 7 , 8 , 9 ] ) ;
210+ const testIv = Buffer . from ( [ 7 , 8 , 9 ] ) ;
189211
190212 // Verify each input is correctly identified and converted
191213 expect ( encryptedKey instanceof Uint8Array ) . toBe ( true ) ;
192214 expect ( serializedDecryptionKey instanceof Uint8Array ) . toBe ( false ) ;
193215
194- const convertedDecKey = new Uint8Array ( Object . values ( serializedDecryptionKey ) ) ;
216+ const convertedDecKey = new Uint8Array (
217+ Object . values ( serializedDecryptionKey ) ,
218+ ) ;
195219 expect ( convertedDecKey ) . toBeInstanceOf ( Uint8Array ) ;
196220 expect ( convertedDecKey ) . toEqual ( new Uint8Array ( [ 4 , 5 , 6 ] ) ) ;
197221
198222 // Buffer is a Uint8Array subclass
199- expect ( iv instanceof Uint8Array ) . toBe ( true ) ;
223+ expect ( testIv instanceof Uint8Array ) . toBe ( true ) ;
200224 } ) ;
201225
202226 it ( 'should throw error when decryption fails' , async ( ) => {
203- jest . spyOn ( service , 'decryptMasterKey' ) . mockRejectedValue (
204- new Error ( 'Decryption failed: Invalid key or corrupted data' )
205- ) ;
227+ jest
228+ . spyOn ( service , 'decryptMasterKey' )
229+ . mockRejectedValue (
230+ new Error ( 'Decryption failed: Invalid key or corrupted data' ) ,
231+ ) ;
206232
207233 await expect (
208234 service . decryptMasterKey (
209235 new Uint8Array ( [ 1 ] ) ,
210236 Buffer . from ( [ 2 ] ) ,
211- Buffer . from ( [ 3 ] )
212- )
237+ Buffer . from ( [ 3 ] ) ,
238+ ) ,
213239 ) . rejects . toThrow ( 'Decryption failed' ) ;
214240 } ) ;
215241 } ) ;
0 commit comments