Server signed blockchain transactions utilizing web3.js - Ethereum JavaScript API
A system that sign transactions in the server side, without needing to use apps like Metamask for it.
- When to server sign
- Create express app
- Connect to web3
- Get contract object
- Create transaction
- Sign and send
When to server sign:
- For automated calls to change state in blockchain
- Enterprise system access to smart contracts
- CRON job connection
- Automated oracle
- Generated payments
- Exchange transactions
- Avoid custodial model
- Requires server app to have access to private key
- May require message queue to handle large volumes
Sign the transaction in the server side using web3js library.
Client: React
Server: Node, Express
mkdir project-folder
cd project-folder
npx express-generator
npm install
npm start
Check out the app in the browser
http://localhost:3000/
- Run ganache
- Install npm module web3
npm install web3
- Add to app.js
const Web3 = require('web3');
var Tx = require('ethereumjs-tx').Transaction;
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
web3.eth.getAccounts(console.log);
- Need the contract address and ABI - add to app.js
const contractAddress = 'YOUR_CONTRACT_ADD';
const ABI = require('YOUR_ABI_FILE');
var TestContract = web3.eth.contract([YOUR_ABI], contractAddress);
Need the contract address, ABI, account, private key and nonce
const contractAddress = 'YOUR_CONTRACT_ADD';
const ABI = require('YOUR_ABI_FILE');
const account = '0xACCOUNT_ADDRESS';
const privateKey = Buffer.from('YOUR_PRIVATE_KEY', 'hex');
const newAddress = '0x5aB5E52245Fd4974499aa625709EE1F5A81c8157';
var TestContract = new web3.eth.Contract([YOUR_ABI], contractAddress);
const _data = TestContract.methods.setOwner(_newAddress).encodeABI();
web3.eth.getTransactionCount(account)
.then(nonce => {
var rawTx = {
nonce: nonce,
gasPrice: '0x20000000000',
gasLimit: '0x27511’,
to: contractAddress,
value: 0,
data: _data }
- Sign the transaction
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);
});
For support, email [email protected] or join our Slack channel.
- Web3.js: https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#contract-events
- Bootstrap 5: https://getbootstrap.com/docs/5.0/getting-started/introduction/
- Metamask: https://docs.metamask.io/guide/
- Remix: https://remix-ide.readthedocs.io/en/latest/
- React: https://reactjs.org/docs/getting-started.html
- Solidity: https://docs.soliditylang.org/en/v0.4.24/
- Ganache: https://www.trufflesuite.com/docs/ganache/overview