Configuring Ledger's Wallet API
After installing the necessary packages for the Ledger's Wallet API, the next step is to set up the appropriate configuration through a layer that will call "Transport". This involves creating a transport provider to enable communication with the Wallet API.
We'll achieve this by creating a file named TransportProvider.tsx
. This file contains a particular component that uses the Ledger Wallet API client to create a communication transport HOC (Higher Order Component).
This is what this looks like:
import { WalletAPIProvider } from "@ledgerhq/wallet-api-client-react";
import { Transport, WindowMessageTransport } from "@ledgerhq/wallet-api-client";
function TransportProvider({ children }) {
function getWalletAPITransport(): Transport {
if (typeof window === "undefined") {
return {
onMessage: undefined,
send: () => {
}
};
}
const transport = new WindowMessageTransport();
transport.connect();
return transport;
}
const transport = getWalletAPITransport();
return (
<WalletAPIProvider transport={transport}>{children}</WalletAPIProvider>
);
}
export default TransportProvider;
Let's break it down:
- We import the necessary components and types from the Ledger's Wallet API packages.
- The
TransportProvider
function component is created. This component receives its children as a prop and renders them within theWalletAPIProvider
. - The
getWalletAPITransport
function provides a transport method for theWalletAPIProvider
. It checks if thewindow
object is defined (which it wouldn't be during server-side rendering) and returns an empty transport object if it's not. - If the
window
object is available, it initialises aWindowMessageTransport
, connects it, and returns it as the transport method. This allows the app to communicate with the Ledger Wallet API through window messages. - The
WalletAPIProvider
is provided with the transport object returned bygetWalletAPITransport
. This setup allows the rest of your app (represented by{children}
) to communicate with the Ledger Wallet API.
After creating this TransportProvider.tsx
file, you need to wrap your <App />
with the TransportProvider
in your root file. This would typically look like the following:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import TransportProvider from './TransportProvider';
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<TransportProvider>
<App />
</TransportProvider>
</React.StrictMode>
);
With this setup, the entire application can access Ledger's Wallet API and communicate with it via the TransportProvider
.
You are now almost ready to use the wallet API's functionalities. The last step is to make your Live App ready to be registered by creating your manifest. You can head to this section to learn about how to create your manifest