Closed 0xwhatly closed 6 years ago
@MrTibbles .encodeABI() does not allow you to supply a third parameter (The transaction object in getData
so how do you go about getting information to put into a raw transaction when you need to pass in the from address?
contract.transfer.getData(to, amount, { from })
We have to use raw transactions, as we're managing transactions on an API for our erc-20 token and manage our users private keys, so we're constantly signing transactions from different addresses.
Use this utility library - https://github.com/ethereumjs/ethereumjs-tx The documentation should explain all :)
@MrTibbles the package is used to create raw transactions, not to extract data from a contract's function in the way @ChristianTucker asked.
This is still an issue btw
+1
so was there an answer for this? I want to pass the data parameter in the rawTx, but not sure how to get the encoded data. web3's getData does not seem to be working
try
let data = myContract.methods.transfer(toAddress, value).encodeABI();
"web3": "^1.0.0-beta.24"
It's working now. Sorry for spamming but got another question. Can you unsign a transaction and read the content?
Sorry, I don't quite understand what you mean
@greatyaqi .. so here's what I want to do. 1- Generate tx with data (done) 2- Sign tx (not working) 3- Send signed tx to someone else (not started) 4- get another signature 5- send tx to ethereum
let web3 = new Web3('http://localhost:7545') // using ganache
let signed = await web3.eth.signTransaction(data,
second line is throwing this error: Error: Method eth_signTransaction not supported.
@rawadrifai Signing method(recommended use):
const Tx = require('ethereumjs-tx');
const Web3 = require('web3');
web3 = new Web3(web3.currentProvider);
let myContract = new web3.eth.Contract(abi, contractAddress);
let data = myContract.methods.transfer(toAddress, value).encodeABI();
let rawTx = {
"nonce": web3.utils.toHex(nonce),
"gasPrice": "0x3b9aca00",
"gasLimit": web3.utils.toHex(gasLimit),
"to": contractAddress,
"value": "0x00",
"data": data,
}
const tx = new Tx(rawTx)
tx.sign(privateKey)
let serializedTx = "0x" + tx.serialize().toString('hex');
web3.eth.sendSignedTransaction(serializedTx).on('transactionHash', function (txHash) {
}).on('receipt', function (receipt) {
console.log("receipt:" + receipt);
}).on('confirmation', function (confirmationNumber, receipt) {
//console.log("confirmationNumber:" + confirmationNumber + " receipt:" + receipt);
}).on('error', function (error) {
});
Signing method: The doc: https://web3js.readthedocs.io/en/1.0/web3-eth.html#sendtransaction
@greatyaqi signing with ethereumjs-tx requires knowing the private key. The idea is that the function would tie into the web3 provider (metamask for example), which would authorize the signature.
web3.eth.signTransaction seems to be a function provided by web3, so why not use it?
@rawadrifai First the private key signature is done locally, so it's safe, but when you use the signTransaction method, you first want to unlock your account. In the process, anyone can operate your account. This is very Dangerous behavior
@greatyaqi what do you think of using web3.eth.sign instead? This method does not require private key. I agree with you local usage is safe-r, but still it will require the end user to go retrieve their private key, say from metamask, and then input it somewhere for usage.
@rawadrifai
web3.eth.sign(dataToSign, address [, callback])
address
parameter can also be an address or index from the web3.eth.accounts.wallet. It will then sign locally using the private key of this account.
No matter what method you use to sign, don't use the method to unlock the account.
https://ethereum.stackexchange.com/questions/32427/how-can-i-safely-unlock-an-account-and-send-transactions-from-a-web-application
web3js version: ^1.0.0-beta.24
without "getData" method .