Skip to content

Commit 5a9c725

Browse files
committed
refactor: update CipherProvider handling and decryption logic in invoice dashboard and form
- Refactored CipherProvider initialization to use reactive statements in both create-invoice-form and view-requests components. - Enhanced decryption toggle functionality with localStorage persistence in view-requests. - Improved type definitions for cipherProvider to include session signatures method. - Cleaned up code for better readability and maintainability.
1 parent 900f8da commit 5a9c725

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

packages/create-invoice-form/src/lib/create-invoice-form.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
export let wagmiConfig: WagmiConfig;
3030
export let requestNetwork: RequestNetwork | null | undefined;
3131
export let currencies: CurrencyTypes.CurrencyInput[] = [];
32-
let cipherProvider: CipherProviderTypes.ICipherProvider | undefined = requestNetwork?.getCipherProvider();
32+
let cipherProvider: CipherProviderTypes.ICipherProvider | undefined;
3333
3434
let account: GetAccountReturnType;
3535
let isTimeout = false;
@@ -115,6 +115,8 @@
115115
totalAmount: 0,
116116
};
117117
118+
$: cipherProvider = requestNetwork?.getCipherProvider();
119+
118120
$: {
119121
if (wagmiConfig) {
120122
account = getAccount(wagmiConfig);

packages/invoice-dashboard/src/lib/view-requests.svelte

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,23 @@
4141
import { CurrencyManager } from "@requestnetwork/currency";
4242
import { onDestroy, onMount, tick } from "svelte";
4343
import { formatUnits } from "viem";
44-
import { debounce, formatAddress } from "../utils";
44+
import { debounce, formatAddress, getEthersSigner } from "../utils";
4545
import { Drawer, InvoiceView } from "./dashboard";
4646
import { getPaymentNetworkExtension } from "@requestnetwork/payment-detection";
4747
import { CipherProviderTypes, CurrencyTypes } from "@requestnetwork/types";
48-
import { checkStatus } from "@requestnetwork/shared-utils/checkStatus";
48+
import { checkStatus } from "@requestnetwork/shared-utils/checkStatus";
49+
import { ethers } from "ethers";
4950
5051
export let config: IConfig;
5152
export let wagmiConfig: WagmiConfig;
5253
export let requestNetwork: RequestNetwork | null | undefined;
5354
export let currencies: CurrencyTypes.CurrencyInput[] = [];
5455
55-
let cipherProvider: CipherProviderTypes.ICipherProvider | undefined = requestNetwork?.getCipherProvider();
56+
let cipherProvider: CipherProviderTypes.ICipherProvider & {
57+
getSessionSignatures: (signer: ethers.Signer, walletAddress: `0x${string}`) => Promise<any>;
58+
} | undefined;
5659
57-
let sliderValueForDecryption = cipherProvider?.isDecryptionEnabled() ? "on" : "off";
60+
let sliderValueForDecryption = JSON.parse(localStorage?.getItem('isDecryptionEnabled') ?? "false") ? "on" : "off";
5861
5962
let signer: `0x${string}` | undefined;
6063
let activeConfig = config ? config : defaultConfig;
@@ -99,6 +102,7 @@
99102
$: {
100103
if (account?.address) {
101104
tick().then(() => {
105+
enableDecryption();
102106
getRequests();
103107
});
104108
}
@@ -128,6 +132,13 @@
128132
if (typeof unwatchAccount === "function") unwatchAccount();
129133
});
130134
135+
$: cipherProvider = requestNetwork?.getCipherProvider() as CipherProviderTypes.ICipherProvider & {
136+
getSessionSignatures: (
137+
signer: ethers.Signer,
138+
walletAddress: `0x${string}`
139+
) => Promise<any>;
140+
};
141+
131142
$: {
132143
signer = account?.address;
133144
}
@@ -389,17 +400,28 @@
389400
const handleRemoveSelectedRequest = () => {
390401
activeRequest = undefined;
391402
};
392-
393403
394-
$: sliderValueForDecryption, getRequests();
395-
396-
$: {
404+
const enableDecryption = async () => {
405+
loading = true;
397406
if(sliderValueForDecryption === 'on') {
398-
cipherProvider?.enableDecryption(true);
407+
try {
408+
const signer = await getEthersSigner(wagmiConfig);
409+
if (signer && account?.address) {
410+
await cipherProvider?.getSessionSignatures(signer, account.address);
411+
cipherProvider?.enableDecryption(true);
412+
localStorage?.setItem('isDecryptionEnabled', JSON.stringify(true));
413+
}
414+
} catch (error) {
415+
console.error("Failed to enable decryption:", error);
416+
}
399417
} else {
400418
cipherProvider?.enableDecryption(false);
419+
localStorage?.setItem('isDecryptionEnabled', JSON.stringify(false));
401420
}
421+
await getRequests();
422+
loading = false;
402423
}
424+
$: sliderValueForDecryption, enableDecryption();
403425
404426
</script>
405427

0 commit comments

Comments
 (0)