Skip to content

Server signed blockchain transactions utilizing web3.js - Ethereum JavaScript API.

License

Notifications You must be signed in to change notification settings

AleRapchan/server-signed-transactions

Repository files navigation

Server Signed Transactions

Server signed blockchain transactions utilizing web3.js - Ethereum JavaScript API

Logo

Goals

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

Problem

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

Solution

Sign the transaction in the server side using web3js library.

Tech Stack

Client: React

Server: Node, Express

Run Locally

Create Express app

mkdir project-folder
cd project-folder
npx express-generator
npm install
npm start

Check out the app in the browser

http://localhost:3000/

Connect to Web3

  • 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);

Get contract object

  • 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);

Create transaction

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 and send

  • 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);
});

Author

Support

For support, email [email protected] or join our Slack channel.

Appendix

About

Server signed blockchain transactions utilizing web3.js - Ethereum JavaScript API.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published