Skip to content

Commit 7125288

Browse files
committed
🚧 (sample): Modularize CAL intercaptor
1 parent ee5596e commit 7125288

File tree

21 files changed

+797
-341
lines changed

21 files changed

+797
-341
lines changed

apps/sample/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.1.3",
44
"private": true,
55
"scripts": {
6-
"flask-dev": "FLASK_DEBUG=1 pip3 install -r requirements.txt && python3 -m flask --app api/index run -h 0.0.0.0 -p 5328",
6+
"flask-dev": "FLASK_DEBUG=1 pip3 install -r ../../packages/tools/cal-interceptor/requirements.txt && python3 -m flask --app ../../packages/tools/cal-interceptor/api/index run -h 0.0.0.0 -p 5328",
77
"next-dev": "next dev",
88
"dev": "concurrently \"pnpm run next-dev\" \"pnpm run flask-dev\"",
99
"dev:default-mock": "next dev",
@@ -17,6 +17,7 @@
1717
"test:playwright": "pnpm playwright test"
1818
},
1919
"dependencies": {
20+
"@ledgerhq/cal-interceptor": "workspace:*",
2021
"@ledgerhq/context-module": "workspace:*",
2122
"@ledgerhq/device-management-kit": "workspace:*",
2223
"@ledgerhq/device-management-kit-flipper-plugin-client": "workspace:*",

apps/sample/src/components/CalView/ERC7730TesterDrawer.tsx

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import React, { useCallback, useEffect, useState } from "react";
1+
import React, { useCallback, useEffect, useMemo, useState } from "react";
2+
import {
3+
addERC7730Descriptor,
4+
ERC7730Client,
5+
fetchAndStoreCertificates,
6+
} from "@ledgerhq/cal-interceptor";
27
import { Button, Divider, Flex, Input, Text } from "@ledgerhq/react-ui";
38

49
import { Block } from "@/components/Block";
@@ -10,10 +15,9 @@ export function ERC7730TesterDrawer() {
1015
isActive,
1116
startInterception,
1217
stopInterception,
13-
storeDescriptor,
14-
storeCertificates,
1518
clearStoredDescriptors,
1619
getStoredDescriptorCount,
20+
interceptor,
1721
} = useCalInterceptor();
1822
const { calConfig, setCalConfig } = useCalConfig();
1923

@@ -22,6 +26,9 @@ export function ERC7730TesterDrawer() {
2226
const [error, setError] = useState<string | null>(null);
2327
const [descriptorCount, setDescriptorCount] = useState<number>(0);
2428

29+
// Create ERC7730 client
30+
const client = useMemo(() => new ERC7730Client(), []);
31+
2532
useEffect(() => {
2633
setDescriptorCount(getStoredDescriptorCount());
2734
}, [getStoredDescriptorCount]);
@@ -37,7 +44,7 @@ export function ERC7730TesterDrawer() {
3744
}, [isActive, calConfig, setCalConfig]);
3845

3946
const addERC7730 = useCallback(async () => {
40-
if (!erc7730Input.trim()) {
47+
if (!erc7730Input.trim() || !interceptor) {
4148
setError("Please enter ERC7730 descriptor");
4249
return;
4350
}
@@ -46,81 +53,43 @@ export function ERC7730TesterDrawer() {
4653
setError(null);
4754

4855
try {
49-
const response = await fetch("/api/process-erc7730-descriptor", {
50-
method: "POST",
51-
headers: {
52-
"Content-Type": "application/json",
53-
},
54-
body: erc7730Input.trim(),
55-
});
56-
57-
if (!response.ok) {
58-
const errorText = await response.text();
59-
setError(`HTTP ${response.status}: ${errorText}`);
60-
setIsLoading(false);
61-
return;
62-
}
63-
64-
// Get the processed descriptors from the server
65-
const result = await response.json();
66-
const { descriptors } = result;
67-
68-
// Store each descriptor in localStorage via provider
69-
Object.entries(descriptors).forEach(([key, descriptorData]) => {
70-
const [chainId, address] = key.split(":");
71-
storeDescriptor(parseInt(chainId), address, descriptorData);
56+
// Use the helper function to process and store descriptor
57+
await addERC7730Descriptor({
58+
descriptor: erc7730Input.trim(),
59+
interceptor,
60+
client,
61+
autoStart: true,
7262
});
7363

74-
// Update descriptor count
64+
// Update UI state
7565
setDescriptorCount(getStoredDescriptorCount());
7666
setError(null);
77-
setIsLoading(false);
7867
setERC7730Input("");
79-
80-
// Activate interceptor if not already active
81-
if (!isActive) {
82-
startInterception();
83-
}
8468
} catch (error: unknown) {
8569
const errorMessage =
8670
error instanceof Error ? error.message : String(error);
8771
setError(`Failed to add ERC7730 descriptor: ${errorMessage}`);
72+
} finally {
8873
setIsLoading(false);
8974
}
90-
}, [
91-
erc7730Input,
92-
storeDescriptor,
93-
getStoredDescriptorCount,
94-
isActive,
95-
startInterception,
96-
]);
75+
}, [erc7730Input, interceptor, client, getStoredDescriptorCount]);
9776

9877
// Fetch and store certificates when interceptor becomes active
9978
useEffect(() => {
100-
if (isActive) {
101-
const fetchCertificates = async () => {
79+
if (isActive && interceptor) {
80+
const loadCertificates = async () => {
10281
try {
10382
console.log("Fetching CAL certificates...");
104-
const response = await fetch("/api/certificates");
105-
106-
if (!response.ok) {
107-
console.error(
108-
`Failed to fetch certificates: HTTP ${response.status}`,
109-
);
110-
return;
111-
}
112-
113-
const certificates = await response.json();
114-
storeCertificates(certificates);
83+
await fetchAndStoreCertificates(interceptor, client);
11584
console.log("Certificates fetched and stored successfully");
11685
} catch (error) {
11786
console.error("Failed to fetch certificates:", error);
11887
}
11988
};
12089

121-
fetchCertificates();
90+
loadCertificates();
12291
}
123-
}, [isActive, storeCertificates]);
92+
}, [isActive, interceptor, client]);
12493

12594
const toggleInterceptor = useCallback(() => {
12695
if (isActive) {

apps/sample/src/hooks/useXhrInterceptor.ts

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)