I want to create and sign a transaction in browser using web3. In order to create a raw transaction, I think I have to do something like this (hopefully you can fill the gaps of my knowledge)
var pk = '0x6ba33b3f7997c2bf63d82f3baa1a8069014a59fa1f554af3266aa85afee9d0a9';
pk = new Buffer(pk,'hex');
var address = '0xFb4d271F3056aAF8Bcf8aeB00b5cb4B6C02c7368';
var myContractsAddress = '0x0cb4edc28d17c43a75797bf5effc141fd5da8715';
var rawTx = {
nonce: web3.toHex(web3.eth.getTransactionCount(acct1)),
to: myContractsAddress,
gasPrice: web3.toHex(20000000000),
gasLimit: web3.toHex(200000),
value: ***Here I am not sure, am I supposed to encode my list of variables? and do I encode everything to hex, or just integers? And how would that list look like?***
data: *This field is irrelevant for now, and just for documentationpurpose, right?*
}
This should give me my raw transaction (the part about the datafield I unfortunately don't know and would love to get help with!
Lets say my raw transaction is valid and correct at this point, I would have to sign it with the private key now. The solution I've seen used a node.js library called ethereumjs-tx. Is there a solution with web3 only, or do I have to port this library somehow into my browser?
ethereumjs-tx
doesn't need to be "ported" to the browser, since it already works fine there. Do you have the ABI for your contract?value
is the amount of ether you're sending (perhaps 0 if you're just calling a contract function).data
is what tells the contract what function you're calling (via a hash called a "function selector") and with what parameters (ABI-encoded). If you have the ABI, web3.js can compute thedata
field for you.