@tronweb3/tronwallet-adapter-ledger
This package provides an adapter for the Ledger Wallet.
Demo
import { LedgerAdapter } from '@tronweb3/tronwallet-adapter-ledger';
import TronWeb from 'tronweb';
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
headers: { 'TRON-PRO-API-KEY': 'your api key' },
});
const adapter = new LedgerAdapter({
accountNumber: 5,
getDerivationPath(index) {
return `44'/195'/0'/0/${index}`;
},
});
await adapter.connect();
console.log(adapter.address);
const unSignedTransaction = await tronWeb.transactionBuilder.sendTrx(targetAddress, 100, adapter.address);
const signedTransaction = await adapter.signTransaction(unSignedTransaction);
await tronWeb.trx.sendRawTransaction(signedTransaction);
Documentation
API
-
Constructor(config: LedgerAdapterConfig)
interface LedgerAdapterConfig {
openUrlWhenWalletNotFound?: boolean;
accountNumber?: number;
beforeConnect?: () => Promise<unknown> | unknown;
selectAccount?: (params: { accounts: Account[]; ledgerUtils: LedgerUtils }) => Promise<Account>;
getDerivationPath?: (index: number) => string;
}
interface Account {
index: number;
path: string;
address: string;
}
interface LedgerUtils {
getAccounts: (from: number, to: number) => Promise<Account[]>;
getAddress: (index: number, display: boolean) => Promise<{ publicKey: string; address: string }>;
}
-
Property: ledgerUtils
ledgerUtils
on LedgerAdapter is used to get useful functions to interact with Ledger directly. ledgerUtils
is defined as last section.
-
getAccounts(from: number, to: number)
is a wrapped function to get multiple accounts by index range from ledger.
For example:
const adapter = new LedgerAdapter();
const accounts = await adapter.ledgerUtils.getAcccounts(0, 5);
-
getAddress: (index: number, display: boolean)
is a raw function to request an address from ledger.
If display
is true, will request user to approve on ledger.
For example, following code will request user approve on Ledger to confirm to connect their ledger.
const adapter = new LedgerAdapter();
const result = await adapter.ledgerUtils.getAddress(0, true);
Caveats
multiSign()
and switchChain(chainId: string)
are not supported.
For more information about tronwallet adapters, please refer to @tronweb3/tronwallet-adapters