Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix messageHash, decoding bug
  • Loading branch information
GregTheGreek committed Jun 3, 2021
commit 018e4a827feed1d4ad9878b2c287c5acf24867e9
32 changes: 17 additions & 15 deletions package-lock.json

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

6 changes: 3 additions & 3 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.

22 changes: 22 additions & 0 deletions packages/web3-eth-accounts/src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var RLP = require("eth-lib/lib/rlp"); // jshint ignore:line

function decodeUnknownTxType(rawTx, txOptions = {}) {
const stripped = rawTx.slice(2);
const data = Buffer.from(stripped, "hex")
if (data[0] <= 0x7f) {
// It is an EIP-2718 Typed Transaction
return {
values: RLP.decode("0x" + stripped.slice(2)),
isTyped: true
};
} else {
return {
values: RLP.decode(rawTx),
isTyped: false
};
}
}

module.exports = {
decodeUnknownTxType,
}
20 changes: 14 additions & 6 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ var Method = require('web3-core-method');
var Account = require('eth-lib/lib/account');
var Hash = require('eth-lib/lib/hash');
var RLP = require("eth-lib/lib/rlp"); // jshint ignore:line
var {decodeUnknownTxType} = require("./helpers");
var Bytes = require('eth-lib/lib/bytes');// jshint ignore:line
var cryp = (typeof global === 'undefined') ? require('crypto-browserify') : require('crypto');
var scrypt = require('scrypt-js');
var uuid = require('uuid');
var utils = require('web3-utils');
var helpers = require('web3-core-helpers');
var {TransactionFactory} = require('@ethereumjs/tx');
var {TransactionFactory, Transaction, FeeMarketEIP1559Transaction, AccessListEIP2930Transaction} = require('@ethereumjs/tx');
var Common = require('@ethereumjs/common').default;


Expand Down Expand Up @@ -159,8 +160,12 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca
var transaction = helpers.formatters.inputCallFormatter(_.clone(tx));
transaction.data = transaction.data || '0x';
transaction.value = transaction.value || '0x';
transaction.chainId = transaction.chainId;
transaction.gasLimit = transaction.gasLimit || transaction.gas;
transaction.type = "0x0"; // default to legacy
if (transaction.accessList) {
// EIP-2930
transaction.type = "0x01"
}

// Because tx has no @ethereumjs/tx signing options we use fetched vals.
if (!hasTxSigningOptions) {
Expand Down Expand Up @@ -221,7 +226,7 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca
var transactionHash = utils.keccak256(rawTransaction);

var result = {
messageHash: '0x' + Buffer.from(signedTx.hash(false)).toString('hex'),
messageHash: '0x' + Buffer.from(signedTx.getMessageToSign(true)).toString('hex'),
v: '0x' + Buffer.from(signedTx.v).toString('hex'),
r: '0x' + Buffer.from(signedTx.r).toString('hex'),
s: '0x' + Buffer.from(signedTx.s).toString('hex'),
Expand Down Expand Up @@ -286,10 +291,13 @@ function _validateTransactionForSigning(tx) {
return;
}


/* jshint ignore:start */
Accounts.prototype.recoverTransaction = function recoverTransaction(rawTx) {
var values = RLP.decode(rawTx);
Accounts.prototype.recoverTransaction = function recoverTransaction(rawTx, txOptions = {}) {
var {values, isTyped} = decodeUnknownTxType(rawTx);
if (isTyped) {
delete values[0]; // tx type
delete values[6]; //
}
var signature = Account.encodeSignature(values.slice(6, 9));
var recovery = Bytes.toNumber(values[6]);
var extraData = recovery < 35 ? [] : [Bytes.fromNumber((recovery - 35) >> 1), '0x', '0x'];
Expand Down
Loading