@@ -162,7 +162,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
162
162
expectSendToDeviceMessage ( "m.key.verification.request" ) ,
163
163
aliceClient . getCrypto ( ) ! . requestDeviceVerification ( TEST_USER_ID , TEST_DEVICE_ID ) ,
164
164
] ) ;
165
- const transactionId = request . transactionId ;
165
+ const transactionId = request . transactionId ! ;
166
166
expect ( transactionId ) . toBeDefined ( ) ;
167
167
expect ( request . phase ) . toEqual ( VerificationPhase . Requested ) ;
168
168
expect ( request . roomId ) . toBeUndefined ( ) ;
@@ -188,32 +188,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
188
188
}
189
189
190
190
// The dummy device replies with an m.key.verification.ready...
191
- returnToDeviceMessageFromSync ( {
192
- type : "m.key.verification.ready" ,
193
- content : {
194
- from_device : TEST_DEVICE_ID ,
195
- methods : [ "m.sas.v1" ] ,
196
- transaction_id : transactionId ,
197
- } ,
198
- } ) ;
191
+ returnToDeviceMessageFromSync ( buildReadyMessage ( transactionId , [ "m.sas.v1" ] ) ) ;
199
192
await waitForVerificationRequestChanged ( request ) ;
200
193
expect ( request . phase ) . toEqual ( VerificationPhase . Ready ) ;
201
194
expect ( request . otherDeviceId ) . toEqual ( TEST_DEVICE_ID ) ;
202
195
203
196
// ... and picks a method with m.key.verification.start
204
- returnToDeviceMessageFromSync ( {
205
- type : "m.key.verification.start" ,
206
- content : {
207
- from_device : TEST_DEVICE_ID ,
208
- method : "m.sas.v1" ,
209
- transaction_id : transactionId ,
210
- hashes : [ "sha256" ] ,
211
- key_agreement_protocols : [ "curve25519-hkdf-sha256" ] ,
212
- message_authentication_codes : [ "hkdf-hmac-sha256.v2" ] ,
213
- // we have to include "decimal" per the spec.
214
- short_authentication_string : [ "decimal" , "emoji" ] ,
215
- } ,
216
- } ) ;
197
+ returnToDeviceMessageFromSync ( buildSasStartMessage ( transactionId ) ) ;
198
+
217
199
// as soon as the Changed event arrives, `verifier` should be defined
218
200
const verifier = await new Promise < Verifier > ( ( resolve ) => {
219
201
function onChange ( ) {
@@ -354,7 +336,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
354
336
expectSendToDeviceMessage ( "m.key.verification.request" ) ,
355
337
aliceClient . getCrypto ( ) ! . requestDeviceVerification ( TEST_USER_ID , TEST_DEVICE_ID ) ,
356
338
] ) ;
357
- const transactionId = request . transactionId ;
339
+ const transactionId = request . transactionId ! ;
358
340
359
341
const toDeviceMessage = requestBody . messages [ TEST_USER_ID ] [ TEST_DEVICE_ID ] ;
360
342
expect ( toDeviceMessage . methods ) . toContain ( "m.qr_code.show.v1" ) ;
@@ -364,14 +346,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
364
346
expect ( toDeviceMessage . transaction_id ) . toEqual ( transactionId ) ;
365
347
366
348
// The dummy device replies with an m.key.verification.ready, with an indication we can scan the QR code
367
- returnToDeviceMessageFromSync ( {
368
- type : "m.key.verification.ready" ,
369
- content : {
370
- from_device : TEST_DEVICE_ID ,
371
- methods : [ "m.qr_code.scan.v1" ] ,
372
- transaction_id : transactionId ,
373
- } ,
374
- } ) ;
349
+ returnToDeviceMessageFromSync ( buildReadyMessage ( transactionId , [ "m.qr_code.scan.v1" ] ) ) ;
375
350
await waitForVerificationRequestChanged ( request ) ;
376
351
expect ( request . phase ) . toEqual ( VerificationPhase . Ready ) ;
377
352
@@ -441,33 +416,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
441
416
expectSendToDeviceMessage ( "m.key.verification.request" ) ,
442
417
aliceClient . getCrypto ( ) ! . requestDeviceVerification ( TEST_USER_ID , TEST_DEVICE_ID ) ,
443
418
] ) ;
444
- const transactionId = request . transactionId ;
419
+ const transactionId = request . transactionId ! ;
445
420
446
421
// The dummy device replies with an m.key.verification.ready...
447
- returnToDeviceMessageFromSync ( {
448
- type : "m.key.verification.ready" ,
449
- content : {
450
- from_device : TEST_DEVICE_ID ,
451
- methods : [ "m.sas.v1" ] ,
452
- transaction_id : transactionId ,
453
- } ,
454
- } ) ;
422
+ returnToDeviceMessageFromSync ( buildReadyMessage ( transactionId , [ "m.sas.v1" ] ) ) ;
455
423
await waitForVerificationRequestChanged ( request ) ;
456
424
457
425
// ... and picks a method with m.key.verification.start
458
- returnToDeviceMessageFromSync ( {
459
- type : "m.key.verification.start" ,
460
- content : {
461
- from_device : TEST_DEVICE_ID ,
462
- method : "m.sas.v1" ,
463
- transaction_id : transactionId ,
464
- hashes : [ "sha256" ] ,
465
- key_agreement_protocols : [ "curve25519-hkdf-sha256" ] ,
466
- message_authentication_codes : [ "hkdf-hmac-sha256.v2" ] ,
467
- // we have to include "decimal" per the spec.
468
- short_authentication_string : [ "decimal" , "emoji" ] ,
469
- } ,
470
- } ) ;
426
+ returnToDeviceMessageFromSync ( buildSasStartMessage ( transactionId ) ) ;
471
427
await waitForVerificationRequestChanged ( request ) ;
472
428
expect ( request . phase ) . toEqual ( VerificationPhase . Started ) ;
473
429
@@ -614,3 +570,32 @@ function calculateMAC(olmSAS: Olm.SAS, input: string, info: string): string {
614
570
function encodeUnpaddedBase64 ( uint8Array : ArrayBuffer | Uint8Array ) : string {
615
571
return Buffer . from ( uint8Array ) . toString ( "base64" ) . replace ( / = + $ / g, "" ) ;
616
572
}
573
+
574
+ /** build an m.key.verification.ready to-device message originating from the dummy device */
575
+ function buildReadyMessage ( transactionId : string , methods : string [ ] ) : { type : string ; content : object } {
576
+ return {
577
+ type : "m.key.verification.ready" ,
578
+ content : {
579
+ from_device : TEST_DEVICE_ID ,
580
+ methods : methods ,
581
+ transaction_id : transactionId ,
582
+ } ,
583
+ } ;
584
+ }
585
+
586
+ /** build an m.key.verification.start to-device message suitable for the SAS flow, originating from the dummy device */
587
+ function buildSasStartMessage ( transactionId : string ) : { type : string ; content : object } {
588
+ return {
589
+ type : "m.key.verification.start" ,
590
+ content : {
591
+ from_device : TEST_DEVICE_ID ,
592
+ method : "m.sas.v1" ,
593
+ transaction_id : transactionId ,
594
+ hashes : [ "sha256" ] ,
595
+ key_agreement_protocols : [ "curve25519-hkdf-sha256" ] ,
596
+ message_authentication_codes : [ "hkdf-hmac-sha256.v2" ] ,
597
+ // we have to include "decimal" per the spec.
598
+ short_authentication_string : [ "decimal" , "emoji" ] ,
599
+ } ,
600
+ } ;
601
+ }
0 commit comments