import React, { useState } from 'react'; import Alert from './alert'; import { AccountType } from '../util/enum'; import { bech32ToChecksum, convertQaToCommaStr } from '../util/utils'; import { LedgerZilliqa } from '../ledger-zilliqa'; import $ from "jquery"; import { logger } from '../util/logger'; import { ZilSdk } from '../zilliqa-api'; interface LedgerAccount { hwIndex: number, bech32Address: string, balance: string } function LedgerWallet(props: any) { const defaultLedgerIndex = 0; const [ledgerIndex, setLedgerIndex] = useState(0); const [ledgerAccounts, setLedgerAccounts] = useState([] as LedgerAccount[]); const handleClose = () => { setLedgerIndex(defaultLedgerIndex); setLedgerAccounts([] as LedgerAccount[]); } const getLedgerAccounts = async (currentLedgerIndex: number) => { try { Alert('info', "Info", "Please follow the instructions on the device."); const transport = await LedgerZilliqa.getTransport(); const ledger = new LedgerZilliqa(transport); const result = await ledger!.getPublicAddress(currentLedgerIndex); const currAddress = result.pubAddr; const balance = await ZilSdk.getBalance(currAddress); const currLedgerAccount: LedgerAccount = { hwIndex: currentLedgerIndex, bech32Address: currAddress, balance: balance } const tempLedgerAccounts: LedgerAccount [] = ledgerAccounts.slice(); tempLedgerAccounts.push(currLedgerAccount); setLedgerAccounts([...tempLedgerAccounts]); setLedgerIndex(currentLedgerIndex); } catch (err) { console.error("error getting ledger index: %o", err); Alert('error', 'Unable to access ledger', 'Have you unlock the PIN code?'); } } const unlockWallet = async (hwIndex: number) => { logger("unlock by hardware ledger"); if (typeof ledgerAccounts[hwIndex] === undefined) { console.error("no such ledger wallet index: %o", hwIndex); Alert('error', 'Unable to access account', 'Are you sure it is correct?'); return null; } try { const selectedLedgerAddress = ledgerAccounts[hwIndex].bech32Address; // clear modal state $("#ledger-connect-modal").modal("hide"); // show loading state props.onWalletLoadingCallback(); logger("ledger wallet address: %o", selectedLedgerAddress); logger("ledger wallet index: %o", hwIndex); const base16Address = bech32ToChecksum(selectedLedgerAddress).toLowerCase(); // no error // call parent function to redirect to dashboard props.onSuccessCallback(base16Address, selectedLedgerAddress, AccountType.LEDGER, hwIndex); } catch (err) { console.error("error unlocking ledger...:%o", err); Alert('error', 'Unable to access ledger', 'Have you unlock the PIN code?'); // clear modal state $("#ledger-connect-modal").modal("hide"); handleClose(); } } return (