Skip to content

feat(frontend): integrate pow into loader and guard logic #5890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/frontend/src/lib/components/guard/AddressGuard.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { validateBtcAddressMainnet } from '$btc/services/btc-address.services';
import { POW_ENABLED } from '$env/pow.env';
import { validateEthAddress } from '$eth/services/eth-address.services';
import {
networkBitcoinMainnetEnabled,
Expand All @@ -17,11 +18,13 @@
let signerAllowanceLoaded = false;

const loadSignerAllowanceAndValidateAddresses = async () => {
const { success: initSignerAllowanceSuccess } = await initSignerAllowance();
if (!POW_ENABLED) {
const { success: initSignerAllowanceSuccess } = await initSignerAllowance();

if (!initSignerAllowanceSuccess) {
// Sign-out is handled within the service.
return;
if (!initSignerAllowanceSuccess) {
// Sign-out is handled within the service.
return;
}
}

signerAllowanceLoaded = true;
Expand Down
41 changes: 22 additions & 19 deletions src/frontend/src/lib/components/loaders/Loaders.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@
import LoaderMetamask from '$lib/components/loaders/LoaderMetamask.svelte';
import LoaderUserProfile from '$lib/components/loaders/LoaderUserProfile.svelte';
import LoaderWallets from '$lib/components/loaders/LoaderWallets.svelte';
import PowProtector from '$lib/components/pow/PowProtector.svelte';
import UserSnapshotWorker from '$lib/components/rewards/UserSnapshotWorker.svelte';
</script>

<LoaderUserProfile>
<AddressGuard>
<Loader>
<UrlGuard>
<RewardGuard>
<LoaderEthBalances>
<LoaderWallets>
<ExchangeWorker>
<LoaderMetamask>
<UserSnapshotWorker>
<slot />
</UserSnapshotWorker>
</LoaderMetamask>
</ExchangeWorker>
</LoaderWallets>
</LoaderEthBalances>
</RewardGuard>
</UrlGuard>
</Loader>
</AddressGuard>
<PowProtector>
<AddressGuard>
<Loader>
<UrlGuard>
<RewardGuard>
<LoaderEthBalances>
<LoaderWallets>
<ExchangeWorker>
<LoaderMetamask>
<UserSnapshotWorker>
<slot />
</UserSnapshotWorker>
</LoaderMetamask>
</ExchangeWorker>
</LoaderWallets>
</LoaderEthBalances>
</RewardGuard>
</UrlGuard>
</Loader>
</AddressGuard>
</PowProtector>
</LoaderUserProfile>

<!-- This listener is kept outside of the Loaders tree to prevent slow page loading on localhost/e2e -->
Expand Down
18 changes: 11 additions & 7 deletions src/frontend/src/lib/services/loader.services.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { POW_ENABLED } from '$env/pow.env';

Check failure on line 1 in src/frontend/src/lib/services/loader.services.ts

View workflow job for this annotation

GitHub Actions / build-pr

Cannot find module '$env/pow.env' or its corresponding type declarations.

Check failure on line 1 in src/frontend/src/lib/services/loader.services.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find module '$env/pow.env' or its corresponding type declarations.
import { allowSigning } from '$lib/api/backend.api';
import { loadAddresses, loadIdbAddresses } from '$lib/services/addresses.services';
import { errorSignOut, nullishSignOut, signOut } from '$lib/services/auth.services';
Expand Down Expand Up @@ -30,7 +31,10 @@
try {
const { identity } = get(authStore);

await allowSigning({ identity });
await allowSigning({
identity,
nonce: [] // Default empty nonce

Check failure on line 36 in src/frontend/src/lib/services/loader.services.ts

View workflow job for this annotation

GitHub Actions / build-pr

Object literal may only specify known properties, and 'nonce' does not exist in type 'CanisterApiFunctionParams<AllowSigningParams>'.

Check failure on line 36 in src/frontend/src/lib/services/loader.services.ts

View workflow job for this annotation

GitHub Actions / test

Object literal may only specify known properties, and 'nonce' does not exist in type 'CanisterApiFunctionParams<AllowSigningParams>'.
});
} catch (_err: unknown) {
// In the event of any error, we sign the user out, as we assume that the Oisy Wallet cannot function without ETH or Bitcoin addresses.
await errorSignOut(get(i18n).init.error.allow_signing);
Expand Down Expand Up @@ -100,14 +104,14 @@

// We are loading the addresses from the backend. Consequently, we aim to animate this operation and offer the user an explanation of what is happening. To achieve this, we will present this information within a modal.
setProgressModal(true);
if (!POW_ENABLED) {
const { success: initSignerAllowanceSuccess } = await initSignerAllowance();

const { success: initSignerAllowanceSuccess } = await initSignerAllowance();

if (!initSignerAllowanceSuccess) {
// Sign-out is handled within the service.
return;
if (!initSignerAllowanceSuccess) {
// Sign-out is handled within the service.
return;
}
}

const { success: addressSuccess } = await loadAddresses(
err?.map(({ networkId }) => networkId) ?? []
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('loader.services', () => {
await initLoader(mockParams);

expect(allowSigning).toHaveBeenCalledOnce();
expect(allowSigning).toHaveBeenNthCalledWith(1, { identity: mockIdentity });
expect(allowSigning).toHaveBeenNthCalledWith(1, { identity: mockIdentity, nonce: [] });

expect(loadAddresses).toHaveBeenCalledOnce();
expect(loadAddresses).toHaveBeenNthCalledWith(1, [ICP_NETWORK_ID, SOLANA_MAINNET_NETWORK_ID]);
Expand Down
Loading