|
| 1 | +<!-- Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS --> |
| 2 | + |
| 3 | +<template> |
| 4 | + <div |
| 5 | + v-if="step === Steps.ChooseCode" |
| 6 | + class="choose-code" |
| 7 | + > |
| 8 | + <sas-code-choice |
| 9 | + :choices="claimer.greeterSasChoices" |
| 10 | + @select="selectGreeterCode" |
| 11 | + /> |
| 12 | + </div> |
| 13 | + |
| 14 | + <div |
| 15 | + v-if="step === Steps.ProvideCode" |
| 16 | + class="provide-code" |
| 17 | + > |
| 18 | + <sas-code-provide :code="claimer.claimerSasCode" /> |
| 19 | + </div> |
| 20 | +</template> |
| 21 | + |
| 22 | +<script lang="ts"> |
| 23 | +export enum CodeExchangeInterruptedReason { |
| 24 | + NoCodeSelected = 'no-code-selected', |
| 25 | + WrongCodeSelected = 'wrong-code-selected', |
| 26 | + SignifyTrustFailed = 'signify-trust-failed', |
| 27 | + WaitTrustFailed = 'wait-trust-failed', |
| 28 | +} |
| 29 | +</script> |
| 30 | + |
| 31 | +<script setup lang="ts"> |
| 32 | +import SasCodeChoice from '@/components/sas-code/SasCodeChoice.vue'; |
| 33 | +import SasCodeProvide from '@/components/sas-code/SasCodeProvide.vue'; |
| 34 | +import { ShamirClaimer } from '@/parsec'; |
| 35 | +import { nextTick, ref } from 'vue'; |
| 36 | +
|
| 37 | +enum Steps { |
| 38 | + ChooseCode = 'shamir-guest-choose-code', |
| 39 | + ProvideCode = 'shamir-guest-provide-code', |
| 40 | +} |
| 41 | +
|
| 42 | +const step = ref<Steps>(Steps.ChooseCode); |
| 43 | +
|
| 44 | +const props = defineProps<{ |
| 45 | + claimer: ShamirClaimer; |
| 46 | +}>(); |
| 47 | +
|
| 48 | +const emits = defineEmits<{ |
| 49 | + (e: 'interrupted', reason: CodeExchangeInterruptedReason): void; |
| 50 | + (e: 'codeExchanged'): void; |
| 51 | +}>(); |
| 52 | +
|
| 53 | +async function selectGreeterCode(code: string | null): Promise<void> { |
| 54 | + if (code === null) { |
| 55 | + await props.claimer.denyTrust(); |
| 56 | + emits('interrupted', CodeExchangeInterruptedReason.NoCodeSelected); |
| 57 | + return; |
| 58 | + } |
| 59 | + if (code !== props.claimer.greeterSasCode) { |
| 60 | + await props.claimer.denyTrust(); |
| 61 | + emits('interrupted', CodeExchangeInterruptedReason.WrongCodeSelected); |
| 62 | + return; |
| 63 | + } |
| 64 | + console.log('SIGNIFY TRUST'); |
| 65 | + const result = await props.claimer.signifyTrust(); |
| 66 | + if (!result) { |
| 67 | + emits('interrupted', CodeExchangeInterruptedReason.SignifyTrustFailed); |
| 68 | + return; |
| 69 | + } |
| 70 | + console.log('SIGNIFY TRUST RETURNED'); |
| 71 | + step.value = Steps.ProvideCode; |
| 72 | + await nextTick(); |
| 73 | + console.log('WAIT TRUST', props.claimer.claimerSasCode); |
| 74 | + const waitResult = await props.claimer.waitGreeterTrust(); |
| 75 | + if (!waitResult.ok) { |
| 76 | + emits('interrupted', CodeExchangeInterruptedReason.WaitTrustFailed); |
| 77 | + return; |
| 78 | + } |
| 79 | + emits('codeExchanged'); |
| 80 | +} |
| 81 | +</script> |
| 82 | + |
| 83 | +<style scoped lang="scss"></style> |
0 commit comments