loading...
Ethereum

Signing and making transactions on Ethereum using web3.js

I’ve been working with various blockchain platforms for a while now. If you want to reach out, drop me a mail at aaron [at] aaroncolaco.com

“Palm tree on the hill overlooking ocean bay surrounded by cliffs” by Chor Hung Tsang on Unsplash

A recent use case forced me to make transactions on the Ethereum blockchain without running my own peer, and without using any wallet or extension like Mist or Metamask.

I’m going to outline how I went about doing this using a keystore file, and web3.js.

I connected to Infura’s peers to test this out. It’s free and you should give it a try if you don’t want to run your own peer.

Infura – Scalable Blockchain Infrastructure

I’m going to assume you already know how to create a keystore file. If you don’t, take a look at the docs (using geth).

I used node v8.9.3, and web3 v1.0.0-beta.34

  1. Import web3 (duh!)
const Web3 = require('web3');

2. Connect to Infura’s peers

(Or you could connect to your own peer)

const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/<TOKEN>"));

3. Read contents of keystore file

(I’m going to copy the contents for the sake of simplicity)

const keystore = "Contents of keystore file";

4. Decrypt keystore file

const decryptedAccount = web3.eth.accounts.decrypt(keystore, 'PASSWORD');

5. Create a transaction object

var rawTransaction = {
  "from": "Keystore account id",
  "to": "Account you want to transfer to",
  "value": web3.utils.toHex(web3.utils.toWei("0.001", "ether")),
  "gas": 200000,
  "chainId": 3
};

6. Sign and send the transaction

decryptedAccount.signTransaction(rawTransaction)
  .then(signedTx => web3.eth.sendSignedTransaction(signedTx.rawTransaction))
  .then(receipt => console.log("Transaction receipt: ", receipt))
  .catch(err => console.error(err));

You can also sign the transaction using the private key from the decrypted keystore file:

web3.eth.accounts.signTransaction(rawTransaction, decryptedAccount.privateKey)
  .then(console.log);

Complete code:

const Web3 = require('web3');
// connect to any peer; using infura here
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/<TOKEN>"));
// contents of keystore file, can do a fs read
const keystore = "Contents of keystore file";
const decryptedAccount = web3.eth.accounts.decrypt(keystore, 'PASSWORD');
const rawTransaction = {
  "from": "Keystore account id",
  "to": "Account you want to transfer to",
  "value": web3.utils.toHex(web3.utils.toWei("0.001", "ether")),
  "gas": 2000,
  "chainId": 3
};
decryptedAccount.signTransaction(rawTransaction)
  .then(signedTx => web3.eth.sendSignedTransaction(signedTx.rawTransaction))
  .then(receipt => console.log("Transaction receipt: ", receipt))
  .catch(err => console.error(err));
// Or sign using private key from decrypted keystore file
/*
web3.eth.accounts.signTransaction(rawTransaction, decryptedAccount.privateKey)
  .then(console.log);
*/

This is approach will work with mobile apps as well and it’s better than having to run your own peer and sign all your transactions on it.




Source link
Show More
loading...

Leave a Reply

Pin It on Pinterest

Share This

Share this post with your friends!

Close
Close