diff --git a/src/Utils/validate-connection.ts b/src/Utils/validate-connection.ts index 9ae2075..32a1387 100644 --- a/src/Utils/validate-connection.ts +++ b/src/Utils/validate-connection.ts @@ -131,23 +131,24 @@ export const configureSuccessfulPairing = ( const bizName = businessNode?.attrs.name const jid = deviceNode.attrs.jid - const { details, hmac } = proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content as Buffer) - // check HMAC matches - const advSign = hmacSign(details!, Buffer.from(advSecretKey, 'base64')) + const { details, hmac, accountType } = proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content as Buffer) + const isHostedAccount = accountType !== undefined && accountType === proto.ADVEncryptionType.HOSTED + + const hmacPrefix = isHostedAccount ? Buffer.from([6, 5]) : Buffer.alloc(0) + const advSign = hmacSign(Buffer.concat([hmacPrefix, details!]), Buffer.from(advSecretKey, 'base64')) if (Buffer.compare(hmac!, advSign) !== 0) { throw new Boom('Invalid account signature') } const account = proto.ADVSignedDeviceIdentity.decode(details!) const { accountSignatureKey, accountSignature, details: deviceDetails } = account - // verify the device signature matches const accountMsg = Buffer.concat([Buffer.from([6, 0]), deviceDetails!, signedIdentityKey.public]) if (!Curve.verify(accountSignatureKey!, accountMsg, accountSignature!)) { throw new Boom('Failed to verify account signature') } - // sign the details with our identity key - const deviceMsg = Buffer.concat([Buffer.from([6, 1]), deviceDetails!, signedIdentityKey.public, accountSignatureKey!]) + const devicePrefix = isHostedAccount ? Buffer.from([6, 6]) : Buffer.from([6, 1]) + const deviceMsg = Buffer.concat([devicePrefix, deviceDetails!, signedIdentityKey.public, accountSignatureKey!]) account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg) const identity = createSignalIdentity(jid, accountSignatureKey!)