Created
February 5, 2021 04:27
-
-
Save 8bu/4f178abdce0fbce462d39de36f95c00b to your computer and use it in GitHub Desktop.
0x/subprovider replace Ledger default transport factory to WebUSB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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