@@ -156,7 +156,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
156
156
expectSendToDeviceMessage ( "m.key.verification.request" ) ,
157
157
aliceClient . getCrypto ( ) ! . requestDeviceVerification ( TEST_USER_ID , TEST_DEVICE_ID ) ,
158
158
] ) ;
159
- const transactionId = request . transactionId ;
159
+ const transactionId = request . transactionId ! ;
160
160
expect ( transactionId ) . toBeDefined ( ) ;
161
161
expect ( request . phase ) . toEqual ( VerificationPhase . Requested ) ;
162
162
expect ( request . roomId ) . toBeUndefined ( ) ;
@@ -175,32 +175,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
175
175
}
176
176
177
177
// The dummy device replies with an m.key.verification.ready...
178
- returnToDeviceMessageFromSync ( {
179
- type : "m.key.verification.ready" ,
180
- content : {
181
- from_device : TEST_DEVICE_ID ,
182
- methods : [ "m.sas.v1" ] ,
183
- transaction_id : transactionId ,
184
- } ,
185
- } ) ;
178
+ returnToDeviceMessageFromSync ( buildReadyMessage ( transactionId , [ "m.sas.v1" ] ) ) ;
186
179
await waitForVerificationRequestChanged ( request ) ;
187
180
expect ( request . phase ) . toEqual ( VerificationPhase . Ready ) ;
188
181
expect ( request . otherDeviceId ) . toEqual ( TEST_DEVICE_ID ) ;
189
182
190
183
// ... and picks a method with m.key.verification.start
191
- returnToDeviceMessageFromSync ( {
192
- type : "m.key.verification.start" ,
193
- content : {
194
- from_device : TEST_DEVICE_ID ,
195
- method : "m.sas.v1" ,
196
- transaction_id : transactionId ,
197
- hashes : [ "sha256" ] ,
198
- key_agreement_protocols : [ "curve25519-hkdf-sha256" ] ,
199
- message_authentication_codes : [ "hkdf-hmac-sha256.v2" ] ,
200
- // we have to include "decimal" per the spec.
201
- short_authentication_string : [ "decimal" , "emoji" ] ,
202
- } ,
203
- } ) ;
184
+ returnToDeviceMessageFromSync ( buildSasStartMessage ( transactionId ) ) ;
185
+
204
186
// as soon as the Changed event arrives, `verifier` should be defined
205
187
const verifier = await new Promise < Verifier > ( ( resolve ) => {
206
188
function onChange ( ) {
@@ -341,7 +323,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
341
323
expectSendToDeviceMessage ( "m.key.verification.request" ) ,
342
324
aliceClient . getCrypto ( ) ! . requestDeviceVerification ( TEST_USER_ID , TEST_DEVICE_ID ) ,
343
325
] ) ;
344
- const transactionId = request . transactionId ;
326
+ const transactionId = request . transactionId ! ;
345
327
346
328
const toDeviceMessage = requestBody . messages [ TEST_USER_ID ] [ TEST_DEVICE_ID ] ;
347
329
expect ( toDeviceMessage . methods ) . toContain ( "m.qr_code.show.v1" ) ;
@@ -351,14 +333,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
351
333
expect ( toDeviceMessage . transaction_id ) . toEqual ( transactionId ) ;
352
334
353
335
// The dummy device replies with an m.key.verification.ready, with an indication we can scan the QR code
354
- returnToDeviceMessageFromSync ( {
355
- type : "m.key.verification.ready" ,
356
- content : {
357
- from_device : TEST_DEVICE_ID ,
358
- methods : [ "m.qr_code.scan.v1" ] ,
359
- transaction_id : transactionId ,
360
- } ,
361
- } ) ;
336
+ returnToDeviceMessageFromSync ( buildReadyMessage ( transactionId , [ "m.qr_code.scan.v1" ] ) ) ;
362
337
await waitForVerificationRequestChanged ( request ) ;
363
338
expect ( request . phase ) . toEqual ( VerificationPhase . Ready ) ;
364
339
@@ -428,33 +403,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
428
403
expectSendToDeviceMessage ( "m.key.verification.request" ) ,
429
404
aliceClient . getCrypto ( ) ! . requestDeviceVerification ( TEST_USER_ID , TEST_DEVICE_ID ) ,
430
405
] ) ;
431
- const transactionId = request . transactionId ;
406
+ const transactionId = request . transactionId ! ;
432
407
433
408
// The dummy device replies with an m.key.verification.ready...
434
- returnToDeviceMessageFromSync ( {
435
- type : "m.key.verification.ready" ,
436
- content : {
437
- from_device : TEST_DEVICE_ID ,
438
- methods : [ "m.sas.v1" ] ,
439
- transaction_id : transactionId ,
440
- } ,
441
- } ) ;
409
+ returnToDeviceMessageFromSync ( buildReadyMessage ( transactionId , [ "m.sas.v1" ] ) ) ;
442
410
await waitForVerificationRequestChanged ( request ) ;
443
411
444
412
// ... and picks a method with m.key.verification.start
445
- returnToDeviceMessageFromSync ( {
446
- type : "m.key.verification.start" ,
447
- content : {
448
- from_device : TEST_DEVICE_ID ,
449
- method : "m.sas.v1" ,
450
- transaction_id : transactionId ,
451
- hashes : [ "sha256" ] ,
452
- key_agreement_protocols : [ "curve25519-hkdf-sha256" ] ,
453
- message_authentication_codes : [ "hkdf-hmac-sha256.v2" ] ,
454
- // we have to include "decimal" per the spec.
455
- short_authentication_string : [ "decimal" , "emoji" ] ,
456
- } ,
457
- } ) ;
413
+ returnToDeviceMessageFromSync ( buildSasStartMessage ( transactionId ) ) ;
458
414
await waitForVerificationRequestChanged ( request ) ;
459
415
expect ( request . phase ) . toEqual ( VerificationPhase . Started ) ;
460
416
@@ -601,3 +557,32 @@ function calculateMAC(olmSAS: Olm.SAS, input: string, info: string): string {
601
557
function encodeUnpaddedBase64 ( uint8Array : ArrayBuffer | Uint8Array ) : string {
602
558
return Buffer . from ( uint8Array ) . toString ( "base64" ) . replace ( / = + $ / g, "" ) ;
603
559
}
560
+
561
+ /** build an m.key.verification.ready to-device message originating from the dummy device */
562
+ function buildReadyMessage ( transactionId : string , methods : string [ ] ) : { type : string ; content : object } {
563
+ return {
564
+ type : "m.key.verification.ready" ,
565
+ content : {
566
+ from_device : TEST_DEVICE_ID ,
567
+ methods : methods ,
568
+ transaction_id : transactionId ,
569
+ } ,
570
+ } ;
571
+ }
572
+
573
+ /** build an m.key.verification.start to-device message suitable for the SAS flow, originating from the dummy device */
574
+ function buildSasStartMessage ( transactionId : string ) : { type : string ; content : object } {
575
+ return {
576
+ type : "m.key.verification.start" ,
577
+ content : {
578
+ from_device : TEST_DEVICE_ID ,
579
+ method : "m.sas.v1" ,
580
+ transaction_id : transactionId ,
581
+ hashes : [ "sha256" ] ,
582
+ key_agreement_protocols : [ "curve25519-hkdf-sha256" ] ,
583
+ message_authentication_codes : [ "hkdf-hmac-sha256.v2" ] ,
584
+ // we have to include "decimal" per the spec.
585
+ short_authentication_string : [ "decimal" , "emoji" ] ,
586
+ } ,
587
+ } ;
588
+ }
0 commit comments