Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 08d2bfe

Browse files
committed
Merge pull request #7 from ethereumjs/feature/provider-engine
Add provider-engine integration
2 parents bc1f289 + f0b0c8f commit 08d2bfe

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ Instance methods:
8787
* `deriveChild(index)` - derive a node based on a child index
8888
* `getWallet()` - return a `Wallet` instance as seen above
8989

90+
## Provider Engine
91+
92+
The Wallet can be easily plugged into [provider-engine](https://github.com/metamask/provider-engine) to provide signing:
93+
94+
```js
95+
const WalletSubprovider = require('ethereumjs-wallet/provider-engine')
96+
97+
<engine>.addProvider(new WalletSubprovider(<wallet instance>))
98+
```
99+
100+
Note it only supports the basic wallet. With a HD Wallet, call `getWallet()` first.
101+
90102
### Remarks about `toV3`
91103

92104
The `options` is an optional object hash, where all the serialization parameters can be fine tuned:

provider-engine.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
const inherits = require('util').inherits
4+
const HookedWalletEthTxSubprovider = require('web3-provider-engine/subproviders/hooked-wallet-ethtx')
5+
6+
module.exports = WalletSubprovider
7+
8+
inherits(WalletSubprovider, HookedWalletEthTxSubprovider)
9+
10+
function WalletSubprovider (wallet, opts) {
11+
opts.getAccounts = function (cb) {
12+
cb(null, [ wallet.getAddressesString() ])
13+
}
14+
15+
opts.getPrivateKey = function (address, cb) {
16+
if (address !== wallet.getAddressString()) {
17+
return cb('Account not found')
18+
}
19+
20+
cb(null, wallet.getPrivateKey())
21+
}
22+
23+
WalletSubprovider.super_.call(this, opts)
24+
}

0 commit comments

Comments
 (0)