Less strict parsing for loose providers.

This commit is contained in:
Richard Moore 2017-10-27 17:03:26 -04:00
parent fb65772dd6
commit e4c455bdb6
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
4 changed files with 114 additions and 18 deletions

@ -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",

@ -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) {
@ -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);
}
}
@ -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

48
tests/run-testrpc.js Normal file

@ -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();
});
});

42
tests/test-contract.json Normal file

File diff suppressed because one or more lines are too long