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" ;
27import { Button , Divider , Flex , Input , Text } from "@ledgerhq/react-ui" ;
38
49import { 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 ) {
0 commit comments