Basically, I'm accepting ERC20 token payments in an online store, and in order to associate particular payments with particular customers, I want customers to send their transaction hash to the online store before they broadcast the transaction to the Ethereum network. This way, when I monitor incoming transactions I can reliably match payments with my customers.
The simplest way to send transactions with web3 is to call into web3.eth.sendTransaction like this, from our website:
web3.eth.getTransactionCount(web.eth3.accounts[0], function (err, count) {
var txObj = {
nonce: web3.toHex(count),
to: contractAddress,
from: web3.eth.accounts[0],
value: '0x0',
gasPrice: web3.toHex(gasPrice),
gas: web3.toHex(gasLimit),
data: contract.transfer.getData(toAddress, amount, {from: web3.eth.accounts[0]}),
};
web3.eth.sendTransaction(txObj, function (err, txHash) {
// Send txHash to our servers...
});
});
But the problem with this is the transaction is broadcast to the Ethereum network at the same time that it gives me the hash, so an attacker could monitor the unconfirmed transaction pool and claim the transaction was theirs, when it is actually our customer's. What I need to do is code this in such a way that the hash can be successfully sent to our servers before it is ever broadcast to the network. I've also tried this kind of approach:
web3.eth.getTransactionCount(web.eth3.accounts[0], function (err, count) {
var txObj = {
nonce: web3.toHex(count),
to: contractAddress,
value: '0x0',
gasPrice: web3.toHex(gasPrice),
gasLimit: web3.toHex(gasLimit),
data: contract.transfer.getData(toAddress, amount, {from: web3.eth.accounts[0]}),
};
var txData = /* format txObj for use with web3.eth.sign... */;
web3.eth.sign(web3.eth.accounts[0], txData, function (err, signedTx) {
// Send txHash to our servers, call web3.eth.sendRawTransaction
});
});
But the 2 problems with this approach are 1) I am not sure how to get txObj into a format that can be passed to sign (I tried var txData = '0x' + new ethereumjs.Tx(txObj).serialize().toString('hex'); but that didn't work (I got errors saying the message length was invalid)) and 2) web3.eth.sign is apparently in the process of being deprecated and replaced with a personal_sign method. The personal_sign method takes as a parameter the account's password, and I don't have access to my users' account passwords through the code on my web page, so that appears to be a no-go.
Last, I tried web3.eth.signTransaction. It's not documented, I wasn't able to figure out how to use it correctly, and even if I could it probably wouldn't be wise to use an undocumented function.
Which version of web3 you use?
I use 1.0.0@beta-34 and there is a problem with "web3.eth.accounts.signTransaction" function.
I get "Account.makeSigner is not a function" error.
Same version. Since I first posted, I located this documentation which does detail how to use the web3.eth.signTransaction method. I never got an Account.makeSigner is not a function error, however I did get a different error due to MetaMask's not supporting web3.eth.signTransaction. I'm not sure just how clear that issue report is, but I managed to get MetaMask compiled so that I could see what was going on in the non-minified code, and sure enough a critical signTransaction javascript function internal to the MetaMask plugin was undefined, making it impossible to use web3.eth.signTransaction. It's possible that web3.js signTransaction function would work against a different provider than MetaMask's - I have not tried.
Not sure what to do with this issue - I would say close it unless there is provably a problem with web3.js's signTransaction function.
(BTW, I found a different solution to my problem of accepting ERC20 token payments. If you'll indulge me - we found that appending arbitrary data onto the end of the DATA section of the eth transaction does not affect the smart contract's ability to execute it, so we use encodeABI to create the hex data of the method call, and then append the order number onto the end of it. This allows us to accept ERC20 payments and associate them securely with orders as stored in our database.)
I'm sorry, the "Account.makeSigner is not a function" error occurs while calling web3.eth.accounts.signTransaction. I edited my above comment.
Any idea about this issue? I also have this error when signing a tx. Web3 signTransaction was working days ago but not anymore. The weird part is that I didn't update Web3 since.
I know where the issue comes from, I'll trye to be concise as possible.
I use the package [email protected] which installs as dependency [email protected]. But, web3-eth-accounts also require to use the dependency [email protected] (a newer version). web3-eth-accounts is the package for managing accounts using web3.eth.accounts so it is the one executing signTransaction. eth-lib defines the methods of the object Account which is used to manipulate accounts. The older version eth-lib doesn't have the method makeSigner thus the error.
Normally NPM is nesting dependencies when both packages use different version of the same dependency, and it actually does this (NPM installs [email protected] for web3-eth-accounts right inside web3-eth-accounts/node_modules), but for some reasons web3-eth-accounts preferes to use the older version installed in the root of the project node_modules/eth-lib instead of node_modules/web3-eth-accounts/node_modules/eth-lib.
I have this issue with Ionic, I suspect that this issue is due to Webpack, but I'm really not sure.
For now I did a ugly workaround :
I updated this line https://github.com/ethereum/web3.js/blob/1.0/packages/web3-eth-accounts/src/index.js#L29 to
var Account = require("web3-eth-accounts/node_modules/eth-lib/lib/account");
@Slaals , I also got the same error and I'm using ionic just like you.
as your suggestion I just run npm install [email protected] -D , after this seems works now.
Thank you.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions
Most helpful comment
@Slaals , I also got the same error and I'm using ionic just like you.
as your suggestion I just run
npm install [email protected] -D, after this seems works now.Thank you.