Refactor SendAcceptVerificationStage

This commit is contained in:
RMidhunSuresh 2023-03-14 23:25:00 +05:30
parent d60214da10
commit d41746e8b7
No known key found for this signature in database

View File

@ -13,40 +13,41 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
import anotherjson from "another-json";
import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
import {HASHES_LIST, MAC_LIST, SAS_SET, KEY_AGREEMENT_LIST} from "./constants";
import {CancelTypes, VerificationEventTypes} from "../channel/types";
import {SendKeyStage} from "./SendKeyStage";
// from element-web
function intersection<T>(anArray: T[], aSet: Set<T>): T[] {
return Array.isArray(anArray) ? anArray.filter((x) => aSet.has(x)) : [];
}
export class SendAcceptVerificationStage extends BaseSASVerificationStage {
async completeStage() {
await this.log.wrap("SendAcceptVerificationStage.completeStage", async (log) => {
const { content } = this.channel.startMessage;
const keyAgreement = intersection(KEY_AGREEMENT_LIST, new Set(content.key_agreement_protocols))[0];
const hashMethod = intersection(HASHES_LIST, new Set(content.hashes))[0];
const macMethod = intersection(MAC_LIST, new Set(content.message_authentication_codes))[0];
const sasMethods = intersection(content.short_authentication_string, SAS_SET);
if (!(keyAgreement !== undefined && hashMethod !== undefined && macMethod !== undefined && sasMethods.length)) {
const {content: startMessage} = this.channel.startMessage;
const keyAgreement = intersection(KEY_AGREEMENT_LIST, new Set(startMessage.key_agreement_protocols))[0];
const hashMethod = intersection(HASHES_LIST, new Set(startMessage.hashes))[0];
const macMethod = intersection(MAC_LIST, new Set(startMessage.message_authentication_codes))[0];
const sasMethod = intersection(startMessage.short_authentication_string, SAS_SET);
if (!keyAgreement || !hashMethod || !macMethod || !sasMethod.length) {
await this.channel.cancelVerification(CancelTypes.UnknownMethod);
return;
}
const ourPubKey = this.olmSAS.get_pubkey();
const commitmentStr = ourPubKey + anotherjson.stringify(content);
const contentToSend = {
const commitmentStr = ourPubKey + anotherjson.stringify(startMessage);
const content = {
key_agreement_protocol: keyAgreement,
hash: hashMethod,
message_authentication_code: macMethod,
short_authentication_string: sasMethods,
short_authentication_string: sasMethod,
commitment: this.olmUtil.sha256(commitmentStr),
};
await this.channel.send(VerificationEventTypes.Accept, contentToSend, log);
await this.channel.send(VerificationEventTypes.Accept, content, log);
await this.channel.waitForEvent(VerificationEventTypes.Key);
this.setNextStage(new SendKeyStage(this.options));
});
}
}
function intersection<T>(anArray: T[], aSet: Set<T>): T[] {
return Array.isArray(anArray) ? anArray.filter((x) => aSet.has(x)) : [];
}