Skip to content

Commit

Permalink
Less strict parsing for loose providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 27, 2017
1 parent fb65772 commit e4c455b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 18 deletions.
2 changes: 1 addition & 1 deletion providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethers-providers",
"version": "2.1.4",
"version": "2.1.5",
"description": "Service provider for Ethereum wallet library.",
"bugs": {
"url": "http://github.com/ethers-io/ethers.js/issues",
Expand Down
40 changes: 23 additions & 17 deletions providers/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ var formatTransaction = {
nonce: checkNumber,
data: utils.hexlify,

r: checkUint256,
s: checkUint256,
v: checkNumber,
r: allowNull(checkUint256),
s: allowNull(checkUint256),
v: allowNull(checkNumber),

creates: allowNull(utils.getAddress, null),

raw: utils.hexlify,
raw: allowNull(utils.hexlify),
};

function checkTransaction(transaction) {
Expand All @@ -199,19 +199,23 @@ function checkTransaction(transaction) {
}

if (!transaction.raw) {
var raw = [
utils.hexlify(transaction.nonce),
utils.hexlify(transaction.gasPrice),
utils.hexlify(transaction.gasLimit),
(transaction.to || "0x"),
utils.hexlify(transaction.value || '0x'),
utils.hexlify(transaction.data || '0x'),
utils.hexlify(transaction.v || '0x'),
utils.hexlify(transaction.r),
utils.hexlify(transaction.s),
];

transaction.raw = utils.RLP.encode(raw);
// Very loose providers (e.g. TestRPC) don't provide a signature or raw
if (transaction.v && transaction.r && transaction.s) {
var raw = [
utils.hexlify(transaction.nonce),
utils.hexlify(transaction.gasPrice),
utils.hexlify(transaction.gasLimit),
(transaction.to || "0x"),
utils.hexlify(transaction.value || '0x'),
utils.hexlify(transaction.data || '0x'),
utils.hexlify(transaction.v || '0x'),
utils.hexlify(transaction.r),
utils.hexlify(transaction.s),
];

transaction.raw = utils.RLP.encode(raw);
}
}


Expand All @@ -223,12 +227,14 @@ function checkTransaction(transaction) {
networkId = utils.bigNumberify(networkId).toNumber();
}

if (typeof(networkId) !== 'number') {
if (typeof(networkId) !== 'number' && result.v != null) {
networkId = (result.v - 35) / 2;
if (networkId < 0) { networkId = 0; }
networkId = parseInt(networkId);
}

if (typeof(networkId) !== 'number') { networkId = 0; }

result.networkId = networkId;

// 0x0000... should actually be null
Expand Down
48 changes: 48 additions & 0 deletions tests/run-testrpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

// Temporary test case for TestRPC woes; this will go away in the future and
// is meant to help test loosely compliant backends
//
// See: https://github.com/ethers-io/ethers.js/issues/45
//
// Please run `npm install ethereumjs-testrpc` before using this testcase

var TestRPC = require("ethereumjs-testrpc");

var providers = require('../providers');
var Wallet = require('../wallet/wallet');

var port = 18545;

var privateKey = '0x0123456789012345678901234567890123456789012345678901234567890123';

var server = TestRPC.server({
accounts: [ { secretKey: privateKey } ]
});

server.listen(port, function(err, blockchain) {

var provider = new providers.JsonRpcProvider('http://localhost:' + port, 'unspecified');

var wallet = new Wallet(privateKey, provider);

var sendPromise = wallet.sendTransaction({
to: wallet.address,
value: 1,
gasLimit: 21000,
gasPrice: 100000000000
});

sendPromise.then(function(tx) {
console.log('Send', tx);
return provider.getTransaction(tx.hash).then(function(tx) {
console.log('Get', tx);
});
}).catch(function (error) {
console.log(error);

}).then(function() {
server.close();
});
});

0 comments on commit e4c455b

Please sign in to comment.