Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signing fixes back-ported from 2.x #3125

Merged
merged 4 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ Released with 1.0.0-beta.37 code base.
- HttpProvider: CORS issue with Firefox and Safari (#2978)
- Ensure the immutability of the `tx` object passed to function `signTransaction` (#2190)
- Gas check fixed (#2381)
- Signing issues #1998, #2033, and #1074 fixed (#3125)
121 changes: 121 additions & 0 deletions packages/web3-eth-accounts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/web3-eth-accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"web3-core": "1.2.1",
"web3-core-helpers": "1.2.1",
"web3-core-method": "1.2.1",
"web3-utils": "1.2.1"
"web3-utils": "1.2.1",
"ethereumjs-tx": "^2.1.1"
}
}
64 changes: 21 additions & 43 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,20 @@ var Method = require('web3-core-method');
var Promise = require('any-promise');
var Account = require("eth-lib/lib/account");
var Hash = require("eth-lib/lib/hash");
var RLP = require("eth-lib/lib/rlp");
var Nat = require("eth-lib/lib/nat");
var Bytes = require("eth-lib/lib/bytes");
var RLP = require("eth-lib/lib/rlp");// jshint ignore:line
var Bytes = require("eth-lib/lib/bytes");// jshint ignore:line
var cryp = (typeof global === 'undefined') ? require('crypto-browserify') : require('crypto');
var scrypt = require('scrypt-shim');
var uuid = require('uuid');
var utils = require('web3-utils');
var helpers = require('web3-core-helpers');
var Transaction = require('ethereumjs-tx').Transaction;


var isNot = function(value) {
return (_.isUndefined(value) || _.isNull(value));
};

var trimLeadingZero = function (hex) {
while (hex && hex.startsWith('0x0')) {
hex = '0x' + hex.slice(3);
}
return hex;
};

var makeEven = function (hex) {
if(hex.length % 2 === 1) {
hex = hex.replace('0x', '0x0');
}
return hex;
};


var Accounts = function Accounts() {
var _this = this;

Expand Down Expand Up @@ -168,36 +154,28 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca
transaction.value = transaction.value || '0x';
transaction.chainId = utils.numberToHex(transaction.chainId);

var rlpEncoded = RLP.encode([
Bytes.fromNat(transaction.nonce),
Bytes.fromNat(transaction.gasPrice),
Bytes.fromNat(transaction.gas),
transaction.to.toLowerCase(),
Bytes.fromNat(transaction.value),
transaction.data,
Bytes.fromNat(transaction.chainId || "0x1"),
"0x",
"0x"]);


var hash = Hash.keccak256(rlpEncoded);
if (privateKey.startsWith('0x')) {
privateKey = privateKey.substring(2);
}

var signature = Account.makeSigner(Nat.toNumber(transaction.chainId || "0x1") * 2 + 35)(Hash.keccak256(rlpEncoded), privateKey);
var ethTx = new Transaction(transaction);
ethTx.sign(Buffer.from(privateKey, 'hex'));

var rawTx = RLP.decode(rlpEncoded).slice(0, 6).concat(Account.decodeSignature(signature));
var validationResult = ethTx.validate(true);

rawTx[6] = makeEven(trimLeadingZero(rawTx[6]));
rawTx[7] = makeEven(trimLeadingZero(rawTx[7]));
rawTx[8] = makeEven(trimLeadingZero(rawTx[8]));
if (validationResult !== '') {
throw new Error('Signer Error: ' + validationResult);
}
nivida marked this conversation as resolved.
Show resolved Hide resolved

var rawTransaction = RLP.encode(rawTx);
var rlpEncoded = ethTx.serialize().toString('hex');
var rawTransaction = '0x' + rlpEncoded;
var transactionHash = utils.keccak256(rawTransaction);
nivida marked this conversation as resolved.
Show resolved Hide resolved

var values = RLP.decode(rawTransaction);
result = {
messageHash: hash,
v: trimLeadingZero(values[6]),
r: trimLeadingZero(values[7]),
s: trimLeadingZero(values[8]),
return {
messageHash: Buffer.from(ethTx.hash(false)).toString('hex'),
v: '0x' + Buffer.from(ethTx.v).toString('hex'),
r: '0x' + Buffer.from(ethTx.r).toString('hex'),
s: '0x' + Buffer.from(ethTx.s).toString('hex'),
rawTransaction: rawTransaction
};

Expand Down
4 changes: 2 additions & 2 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3026,7 +3026,7 @@ describe('typical usage', function() {
var expected = eth.accounts.wallet[0].signTransaction({
data: '0x1234567000000000000000000000000' + account.address.toLowerCase().replace('0x', '') + '00000000000000000000000000000000000000000000000000000000000000c8',
from: account.address.toLowerCase(),
gas: '0xc350',
gas: '0xd658',
gasPrice: '0xbb8',
chainId: '0x1',
nonce: '0x1',
Expand Down Expand Up @@ -3085,7 +3085,7 @@ describe('typical usage', function() {
arguments: [account.address, 200]
}).send({
from: account.address,
gas: 50000,
gas: 54872,
gasPrice: 3000,
chainId: 1,
nonce: 1,
Expand Down