For dApps & ServicesLedger Wallet ProviderAPI Reference

API reference

This page describes the public API of @ledgerhq/ledger-wallet-provider: the initializeLedgerProvider function and the LedgerEIP1193Provider class.


initializeLedgerProvider(options)

Initializes the Ledger Wallet Provider and injects the UI components into the DOM. Registers the provider with the EIP-6963 discovery protocol.

Signature

function initializeLedgerProvider(
  options: InitializeLedgerProviderOptions
): () => void

See Configuration for the full options reference.

Returns a cleanup function. Call it to remove the provider and UI components from the DOM and to deregister the eip6963:requestProvider listener.

const cleanup = initializeLedgerProvider({ /* options */ })
 
// When unmounting (e.g. route change, app teardown):
cleanup()

If the browser does not support Web HID or Web Bluetooth, initializeLedgerProvider returns a no-op function without registering the provider or mounting any UI. See Requirements.


LedgerEIP1193Provider

Implements the EIP-1193 Ethereum Provider JavaScript API for Ledger hardware signers.

You obtain an instance via the eip6963:announceProvider event (see Get started):

window.addEventListener('eip6963:announceProvider', (event: CustomEvent) => {
  const { provider } = event.detail // LedgerEIP1193Provider
})
window.dispatchEvent(new Event('eip6963:requestProvider'))

Methods

request(args): Promise<unknown>

request({ method, params }: RequestArguments): Promise<unknown>

Makes a JSON-RPC request to the provider. For signing and account-selection methods, the provider queues the request while the Ledger UI is open and executes it when the UI closes. Rejects with a ProviderRpcError on failure.

For the full list of supported methods, see Supported methods.

on(eventName, listener): this

on<TEvent extends keyof ProviderEvent>(
  eventName: TEvent,
  listener: (args: ProviderEvent[TEvent]) => void,
): this

Registers an event listener. Returns this for chaining.

removeListener(eventName, listener): this

removeListener<TEvent extends keyof ProviderEvent>(
  eventName: TEvent,
  listener: (args: ProviderEvent[TEvent]) => void,
): this

Removes a previously registered listener. Returns this for chaining.

isConnected(): boolean

isConnected(): boolean

Returns true if the provider has a connected account.

disconnect(code?, message?, data?): Promise<void>

disconnect(code?: number, message?: string, data?: unknown): Promise<void>

Disconnects the provider, clears the selected account and chain, and fires a disconnect event. The code parameter follows the CloseEvent status code convention (default: 1000).


Supported methods

Handled locally by the provider

MethodDescription
eth_requestAccountsOpens the Ledger UI for signer and account selection; returns the selected address
eth_accountsReturns the currently selected account address
eth_chainIdReturns the current chain ID as a hex string
eth_sendTransactionSigns and broadcasts a transaction
eth_signTransactionSigns a transaction without broadcasting
eth_signRawTransactionSigns a raw transaction
eth_sendRawTransactionSigns and broadcasts a raw transaction
eth_signSigns a message
personal_signSigns a personal message (EIP-191)
eth_signTypedDataSigns typed structured data (EIP-712)
eth_signTypedData_v4Signs typed structured data v4 (EIP-712)
wallet_switchEthereumChainSwitches to a supported chain ID

Forwarded to node RPC

MethodDescription
eth_blockNumberReturns the current block number
eth_getBalanceReturns the balance of an address
eth_getCodeReturns the code at a given address
eth_estimateGasEstimates gas for a transaction
eth_callExecutes a read-only call

Events

EventPayload typeFires when
accountsChangedstring[]The selected account changes
chainChangedstring (hex chain ID)The active chain changes
connect{ chainId: string }The provider connects with an account
disconnectProviderRpcErrorThe provider disconnects
message{ type: string; data: unknown }The provider emits a message

Error codes

request() rejects with a ProviderRpcError whose code is one of the following:

CodeNameWhen it occurs
4001UserRejectedRequestUser rejected the request or closed the modal
4100UnauthorizedProvider not connected, no account selected, or address mismatch
4200UnsupportedMethodMethod is not in the supported list
4900DisconnectedProvider is disconnected when a method that requires a connection is called
4901ChainDisconnectedRequested chain is not in the supported chain list
-32602InvalidParamsInvalid parameters (e.g. malformed typed data)
-32603InternalErrorInternal error (e.g. provider busy, broadcast failed, blind signing disabled)

See also

Ledger
Copyright © Ledger SAS. All rights reserved. Ledger, Ledger Stax, Ledger Flex, Ledger Nano, Ledger Nano S, Ledger OS, Ledger Wallet, [LEDGER] (logo), [L] (logo) are trademarks owned by Ledger SAS.