npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tronweb3/tronwallet-adapter-ledger

v1.1.8

Published

Wallet adapter for the Ledger wallet.

Downloads

3,069

Readme

@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({
    // Initial total accounts to get once connection is created
    accountNumber: 5,
    // Custom derivate path for address
    getDerivationPath(index) {
        return `44'/195'/0'/0/${index}`;
    },
});
// connect
await adapter.connect();

// then you can get address
console.log(adapter.address);

// create a send TRX transaction
const unSignedTransaction = await tronWeb.transactionBuilder.sendTrx(targetAddress, 100, adapter.address);
// using adapter to sign the transaction
const signedTransaction = await adapter.signTransaction(unSignedTransaction);
// broadcast the transaction
await tronWeb.trx.sendRawTransaction(signedTransaction);

Documentation

API

  • Constructor(config: LedgerAdapterConfig)

    interface LedgerAdapterConfig {
        /**
         * Set if open Wallet's website when wallet is not installed.
         * Default is true.
         */
        openUrlWhenWalletNotFound?: boolean;
        /**
         * Initial total accounts to get once connection is created, default is 1
         */
        accountNumber?: number;
    
        /**
         * Hook function to call before connecting to ledger and geting accounts.
         * By default, a modal will popup to reminder user to prepare the ledger and enter Tron app.
         * You can specify a function to disable this modal.
         */
        beforeConnect?: () => Promise<unknown> | unknown;
    
        /**
         * Hook function to call after connecting to ledger and geting initial accounts.
         * The function should return the selected account including the index of account.
         * Following operations such as `signMessage` will use the selected account.
         */
        selectAccount?: (params: { accounts: Account[]; ledgerUtils: LedgerUtils }) => Promise<Account>;
    
        /**
         * Function to get derivate BIP44 path by index.
         * Default is `44'/195'/${index}'/0/0`
         */
        getDerivationPath?: (index: number) => string;
    }
    interface Account {
        /**
         * The index to get BIP44 path.
         */
        index: number;
        /**
         * The BIP44 path to derivate address.
         */
        path: string;
        /**
         * The derivated address.
         */
        address: string;
    }
    interface LedgerUtils {
        /**
         * Get accounts from ledger by index. `from` is included and `to` is excluded.
         * User can use the function to load more accounts.
         */
        getAccounts: (from: number, to: number) => Promise<Account[]>;
        /**
         * Request to get an address with specified index using getDerivationPath(index) to get BIP44 path.
         * If `display` is true, will request user to approve on ledger.
         * The promise will resove if user approve and reject if user cancel the operation.
         */
        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();
      // get 5 accounts from ledger
      const accounts = await adapter.ledgerUtils.getAcccounts(0, 5);
      // [{ address: string, index: 0, path: "44'/195'/0'/0/0" }, ...]
    • 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);
      // { address: 'some address', publicKey: 'publicKey for address' }

Caveats

  • multiSign() and switchChain(chainId: string) are not supported.

For more information about tronwallet adapters, please refer to @tronweb3/tronwallet-adapters