Skip to content

Commit

Permalink
Add new methods to VerificationRequest (#3441)
Browse files Browse the repository at this point in the history
* Add new method `VerificationRequest.getQRCodeBytes`

... which requires fewer complicated classes than the existing `qrCodeData`

* Add new property `VerificationRequest.otherDeviceId`

... to save going via `.channel`

* Add more methods to `VerificationRequest`

... to avoid the need for `channel`

* Use new methods in integration tests
  • Loading branch information
richvdh authored Jun 7, 2023
1 parent 2d7fdde commit c2942dd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
12 changes: 7 additions & 5 deletions spec/integ/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
expectSendToDeviceMessage("m.key.verification.request"),
aliceClient.requestVerification(TEST_USER_ID, [TEST_DEVICE_ID]),
]);
const transactionId = request.channel.transactionId;
const transactionId = request.transactionId;
expect(transactionId).toBeDefined();
expect(request.phase).toEqual(Phase.Requested);
expect(request.roomId).toBeUndefined();

let toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
expect(toDeviceMessage.methods).toContain("m.sas.v1");
Expand All @@ -149,6 +150,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
});
await waitForVerificationRequestChanged(request);
expect(request.phase).toEqual(Phase.Ready);
expect(request.otherDeviceId).toEqual(TEST_DEVICE_ID);

// ... and picks a method with m.key.verification.start
returnToDeviceMessageFromSync({
Expand Down Expand Up @@ -270,7 +272,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
expectSendToDeviceMessage("m.key.verification.request"),
aliceClient.requestVerification(TEST_USER_ID, [TEST_DEVICE_ID]),
]);
const transactionId = request.channel.transactionId;
const transactionId = request.transactionId;

const toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
expect(toDeviceMessage.methods).toContain("m.qr_code.show.v1");
Expand All @@ -292,9 +294,9 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
expect(request.phase).toEqual(Phase.Ready);

// we should now have QR data we can display
const qrCodeData = request.qrCodeData!;
expect(qrCodeData).toBeTruthy();
const qrCodeBuffer = qrCodeData.getBuffer();
const qrCodeBuffer = request.getQRCodeBytes()!;
expect(qrCodeBuffer).toBeTruthy();

// https://spec.matrix.org/v1.7/client-server-api/#qr-code-format
expect(qrCodeBuffer.subarray(0, 6).toString("latin1")).toEqual("MATRIX");
expect(qrCodeBuffer.readUint8(6)).toEqual(0x02); // version
Expand Down
35 changes: 34 additions & 1 deletion src/crypto/verification/request/VerificationRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ export class VerificationRequest<C extends IVerificationChannel = IVerificationC
return true;
}

/**
* Unique ID for this verification request.
*
* An ID isn't assigned until the first message is sent, so this may be `undefined` in the early phases.
*/
public get transactionId(): string | undefined {
return this.channel.transactionId;
}

/**
* For an in-room verification, the ID of the room.
*/
public get roomId(): string | undefined {
return this.channel.roomId;
}

public get invalid(): boolean {
return this.phase === PHASE_UNSENT;
}
Expand Down Expand Up @@ -257,11 +273,23 @@ export class VerificationRequest<C extends IVerificationChannel = IVerificationC
return !this.observeOnly && this._phase !== PHASE_DONE && this._phase !== PHASE_CANCELLED;
}

/** Only set after a .ready if the other party can scan a QR code */
/** Only set after a .ready if the other party can scan a QR code
*
* @deprecated Prefer `getQRCodeBytes`.
*/
public get qrCodeData(): QRCodeData | null {
return this._qrCodeData;
}

/**
* Get the data for a QR code allowing the other device to verify this one, if it supports it.
*
* Only set after a .ready if the other party can scan a QR code, otherwise undefined.
*/
public getQRCodeBytes(): Buffer | undefined {
return this._qrCodeData?.getBuffer();
}

/** Checks whether the other party supports a given verification method.
* This is useful when setting up the QR code UI, as it is somewhat asymmetrical:
* if the other party supports SCAN_QR, we should show a QR code in the UI, and vice versa.
Expand Down Expand Up @@ -347,6 +375,11 @@ export class VerificationRequest<C extends IVerificationChannel = IVerificationC
return this.channel.userId!;
}

/** The device id of the other party in this request, for requests happening over to-device messages only. */
public get otherDeviceId(): string | undefined {
return this.channel.deviceId;
}

public get isSelfVerification(): boolean {
return this.client.getUserId() === this.otherUserId;
}
Expand Down

0 comments on commit c2942dd

Please sign in to comment.