JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<head>
<title>Web3 connection with no Wallet</title>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
</head>
<body>
<h2>
Hello world!
</h2>
<input id="web3info" type="button" value="Click me" onclick="f1();" /> asdfasdfasd
<input type="text" value="" name="senderAccount" id="recacc">
<button class="button">Send Transaction</button>
</body>
</html>
JavaScript
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://xxx.xxx.xxx.xxx:8540'));
var senderAccount = '0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var senderPrivateKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var recac = document.getElementById("recacc"); //input for receiver account
receiverAccount = recac.value;
var amount = parseInt(web3.toWei(0.1, "ether"));
var gasLimit = web3.toHex(35000);
var privateKeyHex = new Buffer(senderPrivateKey, 'hex');
var gasPrice = parseInt(web3.eth.gasPrice);
var gasPriceHex = web3.toHex(gasPrice);
var nonce = web3.eth.getTransactionCount(senderAccount);
var nonceHex = web3.toHex(nonce);
var recAcc = web3.eth.sendTransaction(web3.toHex(receiverAccount)); //how use this method correctly?
const rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimit,
to: recAcc,
value: web3.toHex(amount),
data: '0x00',
chainId: web3.toHex(web3.version.network)
};
console.log("rawTx", rawTx)
var tx = new EthTx(rawTx);
tx.sign(privateKeyHex);
var serializedTx = tx.serialize();
document.querySelector('button').addEventListener('click', function() {
alert(recacc.value);
web3.eth.sendRawTransaction("0x" + serializedTx.toString('hex'), function(error, transactionHash) {
if (!error)
console.log("Transaction has been succesfully SENT, transaction hash: ", transactionHash);
else
console.log(error)
})
}, false);