Skip to content

Instantly share code, notes, and snippets.

@8bu
Created February 5, 2021 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8bu/4f178abdce0fbce462d39de36f95c00b to your computer and use it in GitHub Desktop.
Save 8bu/4f178abdce0fbce462d39de36f95c00b to your computer and use it in GitHub Desktop.
0x/subprovider replace Ledger default transport factory to WebUSB
import { LedgerEthereumClient, Web3ProviderEngine as ProviderEngine } from '@0x/subproviders/lib/src'; // https://github.com/0xProject/0x-monorepo/issues/1400
import { LedgerSubprovider } from '@0x/subproviders/lib/src/subproviders/ledger'; // https://github.com/0xProject/0x-monorepo/issues/1400
import CacheSubprovider from 'web3-provider-engine/subproviders/cache';
import Eth from '@ledgerhq/hw-app-eth';
import TransportWebUSB from '@ledgerhq/hw-transport-webusb';
export enum LedgerDerivationPath {
'Legacy' = "44'/60'/0'",
'LedgerLive' = "44'/60'/0'/0",
}
// ? Replace default U2F transport to WebUSB transport
export async function ledgerEthereumBrowserClientFactoryAsync(): Promise<LedgerEthereumClient> {
const ledgerConnection = await TransportWebUSB.create();
const ledgerEthClient = new Eth(ledgerConnection);
return ledgerEthClient;
}
export const getLedgerProvider = (baseDerivationPath: LedgerDerivationPath) => {
const rpcUrl = process.env.ETH_RPC_URL;
const networkId = DEFAULT_NETWORK;
const engine = new ProviderEngine({ pollingInterval: 12000 });
const subprovider = new LedgerSubprovider({
networkId,
ledgerEthereumClientFactoryAsync: ledgerEthereumBrowserClientFactoryAsync,
baseDerivationPath,
});
engine.addProvider(subprovider);
engine.addProvider(new CacheSubprovider());
engine.addProvider(new RPCSubprovider(rpcUrl));
engine.start();
return { provider: engine, subprovider, chainId: networkId };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment