1- import React , { useCallback , useEffect , useMemo , useState } from 'react'
1+ import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
22import type { ReactNode } from 'react' ;
33import { LedgerAdapter } from '@tronweb3/tronwallet-adapters' ;
44import { AdapterState } from '@tronweb3/tronwallet-abstract-adapter' ;
5- import { Box , Button , Typography , Alert , TextField } from '@mui/material' ;
5+ import { Box , Button , Typography , Alert , TextField , DialogContent , DialogContentText } from '@mui/material' ;
66import { tronWeb } from './tronweb' ;
7+ import DialogTitle from '@mui/material/DialogTitle' ;
8+ import Dialog from '@mui/material/Dialog' ;
9+ import { selectAccount as openSelectAccountModal } from './LedgerDemo/selectAccount.js' ;
10+ import type { SelectAccount } from '@tronweb3/tronwallet-adapter-ledger/lib/types/LedgerWallet.js' ;
11+ export const receiver = 'TMDKznuDWaZwfZHcM61FVFstyYNmK6Njk1' ;
712
813export function LedgerAdapterDemo ( ) {
914 const [ connectState , setConnectState ] = useState ( AdapterState . NotFound ) ;
1015 const [ account , setAccount ] = useState ( '' ) ;
1116 const [ signMessage , setSignMessage ] = useState ( 'Hello, Adapter' ) ;
12- const [ signedMessage , setSignedMessage ] = useState ( '' )
13- const receiver = 'TMDKznuDWaZwfZHcM61FVFstyYNmK6Njk1' ;
14-
17+ const [ signedMessage , setSignedMessage ] = useState ( '' ) ;
18+
1519 const [ open , setOpen ] = useState ( false ) ;
16- const adapter = useMemo ( ( ) => new LedgerAdapter ( { accountNumber : 2 } ) , [ ] ) ;
20+ const [ loadingVisible , setLoadingVisible ] = useState ( false ) ;
21+ const [ verifyAddressVisible , setVerifyAddressVisible ] = useState ( false ) ;
22+
23+ async function onConnect ( ) {
24+ try {
25+ await adapter . connect ( ) ;
26+ // eslint-disable-next-line no-useless-catch
27+ } catch ( e : any ) {
28+ alert ( e . message ) ;
29+ throw e ;
30+ } finally {
31+ setLoadingVisible ( false ) ;
32+ setVerifyAddressVisible ( false ) ;
33+ }
34+ }
35+
36+ const selectAccount = useCallback < SelectAccount > ( async function ( { accounts, ledgerUtils } ) {
37+ const account = await openSelectAccountModal ( { accounts, getAccounts : ledgerUtils . getAccounts } ) ;
38+ return account ;
39+ } , [ ] ) ;
40+ const adapter = useMemo (
41+ ( ) =>
42+ new LedgerAdapter ( {
43+ accountNumber : 1 ,
44+ getDerivationPath ( index : number ) {
45+ return `44'/195'/0'/0/${ index } ` ;
46+ } ,
47+ } ) ,
48+ [ ]
49+ ) ;
1750
1851 useEffect ( ( ) => {
19- console . log ( '--- new adapter ----' ) ;
2052 setConnectState ( adapter . state ) ;
2153 setAccount ( adapter . address || '' ) ;
2254
2355 adapter . on ( 'connect' , ( ) => {
24- console . log ( '--- connect' , adapter . address ) ;
56+ console . log ( '[Adapter Event] connect' , adapter . address ) ;
2557 setAccount ( adapter . address || '' ) ;
2658 } ) ;
2759 adapter . on ( 'stateChanged' , ( state ) => {
28- console . log ( '--- state change' , state ) ;
60+ console . log ( '[Adapter Event] state change' , state ) ;
2961 setConnectState ( state ) ;
3062 } ) ;
3163 adapter . on ( 'accountsChanged' , ( data ) => {
32- console . log ( '--- account changed' , data ) ;
64+ console . log ( '[Adapter Event] account changed' , data ) ;
3365 setAccount ( data as string ) ;
3466 } ) ;
3567
3668 adapter . on ( 'disconnect' , ( ) => {
37- console . log ( '--- disconnect' ) ;
69+ console . log ( '[Adapter Event] disconnect' ) ;
3870 } ) ;
3971
4072 return ( ) => {
@@ -45,20 +77,25 @@ export function LedgerAdapterDemo() {
4577 async function onSignTransaction ( ) {
4678 const transaction = await tronWeb . transactionBuilder . sendTrx ( receiver , tronWeb . toSun ( 0.1 ) , adapter . address ) ;
4779 const signedTransaction = await adapter . signTransaction ( transaction ) ;
48- // const signedTransaction = await tronWeb.trx.sign(transaction);
4980 const res = await tronWeb . trx . sendRawTransaction ( signedTransaction ) ;
5081 setOpen ( true ) ;
5182 }
5283
53- const onSignMessage = useCallback ( async function ( ) {
54- const res = await adapter . signMessage ( signMessage ) ;
55- setSignedMessage ( res ) ;
56- } , [ adapter , signMessage , setSignedMessage ] ) ;
84+ const onSignMessage = useCallback (
85+ async function ( ) {
86+ const res = await adapter . signMessage ( signMessage ) ;
87+ setSignedMessage ( res ) ;
88+ } ,
89+ [ adapter , signMessage , setSignedMessage ]
90+ ) ;
5791
58- const onVerifyMessage = useCallback ( async function ( ) {
59- const address = await tronWeb . trx . verifyMessageV2 ( signMessage , signedMessage ) ;
60- alert ( address === adapter . address ? '验证成功' : '验证失败' )
61- } , [ signMessage , signedMessage , adapter ] )
92+ const onVerifyMessage = useCallback (
93+ async function ( ) {
94+ const address = await tronWeb . trx . verifyMessageV2 ( signMessage , signedMessage ) ;
95+ alert ( address === adapter . address ? 'success verify' : 'failed verify' ) ;
96+ } ,
97+ [ signMessage , signedMessage , adapter ]
98+ ) ;
6299
63100 return (
64101 < Box sx = { { width : '100%' , maxWidth : 500 } } >
@@ -68,35 +105,57 @@ export function LedgerAdapterDemo() {
68105 </ Typography >
69106 < Detail > { account } </ Detail >
70107
71-
72108 < Typography variant = "h6" gutterBottom >
73109 Current connection status:
74110 < span style = { { color : adapter ?. connected ? '#08f108' : 'orange' } } > { connectState } </ span >
75111 </ Typography >
76112 < Typography variant = "h6" gutterBottom >
77- < TextField value = { signMessage } onChange = { e => setSignMessage ( e . target . value ) } > </ TextField >
113+ < TextField label = "Message to sign" size = "small" value = { signMessage } onChange = { ( e ) => setSignMessage ( e . target . value ) } > </ TextField >
78114 </ Typography >
79115 < Detail >
80- < Button variant = "contained" disabled = { adapter ?. connected } onClick = { ( ) => adapter ?. connect ( ) } >
116+ < Button variant = "contained" disabled = { adapter ?. connected } onClick = { onConnect } >
81117 Connect
82- </ Button >
118+ </ Button >
119+
83120 < Button variant = "contained" disabled = { ! adapter ?. connected } onClick = { ( ) => adapter ?. disconnect ( ) } >
84121 Disconnect
85- </ Button >
86- < Button variant = "contained" disabled = { ! adapter ?. connected } onClick = { onSignTransaction } > Transfer</ Button >
122+ </ Button >
123+
124+ < Button variant = "contained" disabled = { ! adapter ?. connected } onClick = { onSignTransaction } >
125+ Transfer
126+ </ Button >
87127 </ Detail >
88128 < Detail >
89- < Button variant = "contained" onClick = { onSignMessage } > Sign Message</ Button >
90- < Button variant = "contained" disabled = { ! signedMessage } onClick = { onVerifyMessage } > Verify Signed Message</ Button >
129+ < Button variant = "contained" onClick = { onSignMessage } >
130+ Sign Message
131+ </ Button >
132+
133+ < Button variant = "contained" disabled = { ! signedMessage } onClick = { onVerifyMessage } >
134+ Verify Signed Message
135+ </ Button >
91136 </ Detail >
92137 { open && (
93138 < Alert onClose = { ( ) => setOpen ( false ) } severity = "success" sx = { { width : '100%' , marginTop : 1 } } >
94- Success! You can confirm your transfer on{ ' ' }
139+ Success! You can confirm your transfer on
95140 < a target = "_blank" rel = "noreferrer" href = { `https://nile.tronscan.org/#/address/${ adapter . address } ` } >
96141 Tron Scan
97142 </ a >
98143 </ Alert >
99144 ) }
145+
146+ < Dialog onClose = { ( ) => setLoadingVisible ( false ) } open = { loadingVisible } >
147+ < DialogTitle > Connecting to Ledger....</ DialogTitle >
148+ < DialogContent >
149+ < DialogContentText > Please connect your ledger and enter TRON app...</ DialogContentText >
150+ </ DialogContent >
151+ </ Dialog >
152+
153+ < Dialog onClose = { ( ) => setVerifyAddressVisible ( false ) } open = { verifyAddressVisible } >
154+ < DialogTitle > Verify on Ledger....</ DialogTitle >
155+ < DialogContent >
156+ < DialogContentText > Please approve on your ledger</ DialogContentText >
157+ </ DialogContent >
158+ </ Dialog >
100159 </ Box >
101160 ) ;
102161}
@@ -108,4 +167,3 @@ function Detail(props: { children: ReactNode }) {
108167 </ Typography >
109168 ) ;
110169}
111-
0 commit comments