Git Product home page Git Product logo

hooked-web3-provider's Introduction

DEPRECATED

for rough equivalent see https://github.com/ethjs/ethjs-provider-signer

for advanced configuration see https://github.com/metamask/provider-engine

Hooked Web3 Provider

The Hooked Web3 Provider is a normal HTTP Web3 provider that allows you to "hook in" an external transaction signer that can be used to sign transactions for accounts not managed by the Ethereum client (i.e., aren't managed by geth). An example transaction signer might be an external program or device that manages its own accounts and private keys. Together, the transaction signer and the web3 provider will use the Ethereum client to place signed transactions on the network.

The goal of this library is to successfully modularize all the moving parts related to external transaction signing, to improve usability and encourage a separation of concerns.

Install

npm install hooked-web3-provider

Use

First, include the package. In Node:

var HookedWeb3Provider = require("hooked-web3-provider");

Or the Browser:

<script type="text/javascript" src="./build/hooked-web3-provider.js"></script>

Then, instantiate the provider:

var provider = new HookedWeb3Provider({
  host: "http://localhost:8545",
  transaction_signer: { 
    // Can be any object that implements the following methods:
    hasAddress: function(address, callback) {...},
    signTransaction: function(tx_params, callback) {...}
  }
});

Transaction Signer Interface

Transaction signers are bound by a simple interface. The object must contain two methods, described below.

hasAddress(address, callback)

Asynchronous method used to determine if the transaction signer manages the address passed in. We recommend the transaction signer account for addresses that start with "0x" as well as those that do not.

######Arguments:

  • string - Address that will be checked.
  • function(error, boolean) - callback when finished. If an error occured, error will be non-null; otherwise it's null. Upon a successful request, the boolean value will represent whether or not the address is managed by the transaction signer.
signTransaction(tx_params, callback)

Asynchronous method used to sign a transaction. This method will only be called if hasAddress provides a value of true for a specific address. This method should sign a transaction based on the parameters.

######Arguments

  • object - An object containing the transaction parameters. Example below.
  • function(error, string) - callback when finished signing. If an error occured, error will be non-null; otherwise it's null. If the transaction was successfully signed, the string reterned via the callback will be the raw, signed hash of the transaction. Example below.

Example Transaction Parameters: tx_params

Note that proper transaction parameters are those used by web3 in its RPC interface. All values are hexadecimal, and are prefixed with "0x".

{
  from: "0x985095ef977ba75fb2bb79cd5c4b84c81392dff6",
  gas: "0x2fefd8",
  gasPrice: "0xba43b7400",
  nonce: "0x21",
  to: "0x54450450e24286143a35686ad77a7c851ada01a0",
  value: "0xde0b6b3a7640000",
  data: "0x..."
}

Example Raw Transaction Response

The above example transaction parameters, when signed, would produce a raw, signed transaction like the following:

0xf86d21850ba43b7400832fefd89454450450e24286143a35686ad77a7c851ada01a0880de0b6b3a7640000801ba0c36fdbf8043a64a6096ee81da4de7f04def477b9a3210a18967fad07f72112b2a04aedfd1d9d9085256373b40ef02bc3da0a95054f40075de340086c9512707b29

Example Use

For an example of this in action, see ./build/index.html for a test application that uses the hooked provider in conjunction with ethereumjs-accounts.

Develop / Contribute

To develop and contribute to this project, first install Truffle:

$ npm install -g truffle

Then edit ./app/hooked-web3-provider.es6, and run:

cd ./checkout/of/repository
$ truffle build

You can have Truffle build automatically as you make changes via:

$ truffle watch

License

MIT

hooked-web3-provider's People

Contributors

kumavis avatar tcoulter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hooked-web3-provider's Issues

Provider is invalid

Hello,

Code

var web3Provider = new HookedWeb3Provider({
host: "https://ropsten.infura.io/v3/",
transaction_signer: global_keystore
});
web3.setProvider(web3Provider);

Send Transaction code

var fromAddr = '0x669dd6157cc2f15ba0b5d0524fe8abd335f5413e';
var toAddr = '0x15007ed8ef6916b2180fdb55d8d45e4f9993f3b2';
var valueEth = '0.01';
var value = parseFloat(valueEth) * 1.0e18
var gasPrice = 18000000000
var gas = 50000
web3.eth.sendTransaction({ from: fromAddr, to: toAddr, value: value, gasPrice: gasPrice, gas: gas }, function(err, txhash) {
console.log('error: ' + err)
console.log('txhash: ' + txhash)
})

We have used this code with web3 and using sendTransaction function getting this type of error..

Error

TypeError: Cannot read property 'hasAddress' of undefined
at HookedWeb3Provider.rewritePayloads (D:\wamp64\www\web3js\node_modules\hooked-web3-provider\build\hooked-web3-provider.js:128:33)
at HookedWeb3Provider.sendAsync (D:\wamp64\www\web3js\node_modules\hooked-web3-provider\build\hooked-web3-provider.js:96:14)
at RequestManager.send (D:\wamp64\www\web3js\node_modules\web3-core-requestmanager\src\index.js:132:66)
at sendRequest (D:\wamp64\www\web3js\node_modules\web3-core-method\src\index.js:562:42)
at Eth.send [as sendTransaction] (D:\wamp64\www\web3js\node_modules\web3-core-method\src\index.js:583:13)
at D:\wamp64\www\web3js\server.js:132:14
at Layer.handle [as handle_request] (D:\wamp64\www\web3js\node_modules\express\lib\router\layer.js:95:5)
at next (D:\wamp64\www\web3js\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\wamp64\www\web3js\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\wamp64\www\web3js\node_modules\express\lib\router\layer.js:95:5)
at D:\wamp64\www\web3js\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\wamp64\www\web3js\node_modules\express\lib\router\index.js:335:12)
at next (D:\wamp64\www\web3js\node_modules\express\lib\router\index.js:275:10)
at module.exports (D:\wamp64\www\web3js\node_modules\express-ejs-layouts\lib\express-layouts.js:116:3)
at Layer.handle [as handle_request] (D:\wamp64\www\web3js\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\wamp64\www\web3js\node_modules\express\lib\router\index.js:317:13)
at D:\wamp64\www\web3js\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\wamp64\www\web3js\node_modules\express\lib\router\index.js:335:12)
at next (D:\wamp64\www\web3js\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (D:\wamp64\www\web3js\node_modules\body-parser\lib\types\urlencoded.js:91:7)
at Layer.handle [as handle_request] (D:\wamp64\www\web3js\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\wamp64\www\web3js\node_modules\express\lib\router\index.js:317:13)

Please Help me..

Symbol unvailable: suggest for replacement

We are using hooked provider and lightwallet libs for our mobile wallet LETH (https://github.com/inzhoop-co/LETH) and we find some issues using Symbol on android devices.

In hooked-web3-provider.js 
row 49: ... requests[Symbol.iterator]() ...

Symbol is unvailable for more devices so we suggest a fix with a replacement using forEach, like this :

requests.forEach(function(request) {
   if (request.method == "eth_sendTransaction") {
       throw new Error("HookedWeb3Provider does not support synchronous transactions. Please provide a callback.");
}

Look at our version on github for complete file:
https://github.com/inzhoop-co/LETH/blob/master/www/lib/thirdparty/hooked-web3-provider.js

Great work!

Support for TestRPC.provider()

I'd like to run my tests using an eth-lightwallet keystore and hooked-web3-provider.

This is working great with TestRPC if the transactions are sent over HTTP, but using TestRPC.provider() fails if I try to implement HookedWeb3provider (which seems to assume a HTTP host).

I was hoping to do something generic like this, that supports both HTTP hosts and TestRPC.provider()

  // create the provider for web3 transactions using ks
  const hookedProvider = new HookedWeb3Provider({
    ...web3.currentProvider,
    transaction_signer: keystore,
  });
  // update the web3 instance to use our new provider
  web3.setProvider(hookedProvider);

However, it fails with:

Error: Invalid JSON RPC response: Error: connect ECONNREFUSED 127.0.0.1:8545

Happy to help with a PR this is within the scope of hooked-web3-provider. Is it?

MaxListenersExceededWarning: Possible EventEmitter memory leak detected

I'm not sure where to file this issue -- here or in the infura git.

I run this script in NodeJS. While everything seems to work just fine I get a warning sometimes.

Here's the warning:

(node:18895) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 unlock listeners added. Use emitter.setMaxListeners() to increase limit

Here's the script:

const Web3 = require('web3');
const ProviderEngine = require('web3-provider-engine/index.js');
const ZeroClientProvider = require('web3-provider-engine/zero.js');

const engine = ZeroClientProvider({
  getAccounts: function(){},
  rpcUrl: 'https://ropsten.infura.io/jD3JIY48JscUvTqJVMF',
})
const web3 = new Web3(engine);

const contractAddress = '0xc740ab099da657612143c34c5f7593a3af5ee6e8';
const contractAbi = [{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"emit","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"x","type":"uint256"}],"name":"myEvent","type":"event"}];
const contract = web3.eth.contract(contractAbi).at(contractAddress);

const event = contract.myEvent({x: null})
event.watch(function(error, result){
  console.log(error, result.args.x.toNumber());
});

If you need more information I will gladly provide. Btw, will events fire consistently with this setup?

session_nonces increment issue

I saw that session_nonce and global nonce for a Sender X was incremented even if you enter a wrong password or if you cancel the transaction. This causes that your next transaction will be always queued.

I MOVED the row:

 // Update caches.
 session_nonces[sender] = final_nonce + 1;
 _this3.global_nonces[sender] = final_nonce + 1;

AFTER (row 169):

_this3.transaction_signer.signTransaction(tx_params, function (err, raw_tx) {
              if (err != null) {
                return next(err);
 }

and it solves for me.

Look at our version on github for complete file:
https://github.com/inzhoop-co/LETH/blob/master/www/lib/thirdparty/hooked-web3-provider.js

Morden?

I can't seem to find how to connect to the Ethereum testnet, Morden. Any advice?

Question about transaction signer

What's the difference between Transaction Signer Interface and LightWallet's transaction singer?

I don't see really see where lightwallet then comes into place.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.