Local WEB3.js provider
The local provider works by intercepting calls to eth_sendTransaction
and replaces them with
calls to sendSignedTransaction
after locally signing the transaction.
Usage
The LocalProvider can be used wherever web3 can. It creates an object that wraps the regular web3 object.
To instantiate a LocalProvider
you have to provide 2 parameters:
- a string or array of strings containing Ethereum private keys
- another provider (Http, WebSocket or IPS) which will be used to actually communicate with the node
Installation
npm install web3-local-signing-provider
example
const provider = new LocalProvider([
'fdb2886b1ff5a0e60f9a4684e385aa7b77f064730304143f08ba96ca1a17effa',
'8d8697970c933b856a02c5c2a9e1ead92b434d6cb724a0635219a1568a4cfd51'
],
new Web3.providers.HttpProvider('http://localhost:8535'))
const web3 = provider.web3
The LocalProvider creates its own web3 object which can be used as in the example above. An alternative is to create another Web3 object and use the LocalProvider as its provider. Like so
const provider = new LocalProvider([
'fdb2886b1ff5a0e60f9a4684e385aa7b77f064730304143f08ba96ca1a17effa',
'8d8697970c933b856a02c5c2a9e1ead92b434d6cb724a0635219a1568a4cfd51'
],
new Web3.providers.HttpProvider('http://localhost:8535'))
const web3 = new Web3(provider)
sending transactions
Once instantiated, the LocalProvider Web3 object can be used the same way you would use a web3 object connected to a node with an unlocked account.
const test = async () => {
try {
let receipt = await web3.eth.sendTransaction({
from: '0x0f21f6fb13310ac0e17205840a91da93119efbec',
to: '0x205161Cec3b55cA9a5997eeaf983B798D5Dc8408',
gasPrice: 12,
value: 1e17
})
console.log(receipt)
} catch (err) {
console.log(err)
}
}
Future development
Some features could be added which would be useful:
- [] Disable the
TIMEOUTBLOCK
and wait forever for a transaction to be mined - [] Store pending transactions to
LocalStorage
and check their status on page reload - [] Make the varible
TIMEOUTBLOCK
configurable to tune the error messagetransaction not mined after 50 blocks
- [] Enable subscribing to events using
getPastEvents
to allow getting events from Infura - [] Add Hardware Wallet (Ledger, Trezor) signing ability
- [] Use EthGasStation to estimate gas https://ethgasstation.info/json/ethgasAPI.json