This web3 provider allows Ethereum transactions to be signed with a Ledger device.
- Lightweight, with minimal dependencies
- Easy to use; just plug it in wherever a web3 provider is expected
Yarn:
$ yarn add web3-provider-ledger
npm:
$ npm install --save web3-provider-ledger
Let's assume we are using the ethjs
library. This library, like Web3,
is designed to be constructed with an instance of a web3 provider.
import Eth from 'ethjs';
import LedgerProvider from 'web3-provider-ledger';
const eth = new Eth(new LedgerProvider());
That's all you need to do in order to get an instance of ethjs, but this particular instance is only capable of generating signed transactions.
Here's one way you might end up with a transaction for interacting with a smart contract:
// Use the ethjs contract API to build a convenience wrapper for a contract
const myContract = eth.contract(myContractAbi).at(myContractAddress);
// Get the raw signed transaction
const tx = await myContract.myFunction(...myFunctionArgs);
At this point, tx
gives us a signed transaction. We still need to send
the transaction, which requires a network-capable provider. For this, you
can use the built-in Eth.HttpProvider
or look for an injected provider
via the global web3.currentProvider
.
Here's what this might look like:
// Constract a network-capable instance of ethjs
const ethNet = new Eth(web3.currentProvider);
// Use the network-capable provider to *send* the transaction
const txId = await ethNet.sendRawTransaction(tx);
import Eth from 'ethjs';
import LedgerDevice from 'web3-provider-ledger/ledger-device';
import SignerProvider from 'ethjs-provider-signer';
const ledgerDevice = new LedgerDevice({ appId: origin, u2f });
const provider = new SignerProvider('https://ropsten.infura.io', {
signTransaction: async (transaction, callback) => {
const signedTransaction = await ledgerDevice.signTransaction(transaction);
callback(null, signedTransaction);
},
accounts: async (callback) => {
const accounts = await ledgerDevice.listAddresses();
callback(null, accounts);
}
});
const eth = new Eth(provider);
// `eth` is now configured to use the Ledger device for signing and
// Infura for sending transactions to the Ethereum network
See the API Reference for detailed code-level documentation.
In addition to the provider, this library includes a LedgerDevice
,
which allows operations to be performed directly on the device. This
can be useful for account discovery (LedgerDevice#listAddresses()
),
which can be used to allow users to choose which account they would like
to use. The index of the preferred account can then be provided to a new
device via the accountIndex
attribute, and this device can be given to
LedgerProvider
via its device
attribute.
For example, here is how you might get a list of account addresses on the device:
import LedgerDevice from 'web3-provider-ledger/device';
const device = new LedgerDevice({ appId: origin, u2f });
const accounts = await device.listAddresses();
Let's say the user has selected the account at index 3
. To use that account,
you would then construct the provider as follows:
import Eth from 'ethjs';
import LedgerDevice from 'web3-provider-ledger/device';
import LedgerProvider from 'web3-provider-ledger';
// Simple form
const eth = new Eth(new LedgerProvider({ accountIndex: 3 }));
// Advanced form (equivalent result to the simple form above)
const eth = new Eth(new LedgerProvider({
device: new LedgerDevice({ accountIndex: 3, appId: origin, u2f })
}));
See CONTRIBUTING.md.
See CODE_OF_CONDUCT.md.
See SECURITY.md.
This library is licensed under the MIT license.