Send Ether without a Browser (Metamask)wallet.

Pranshu Rastogi
Zubi.io
Published in
3 min readJul 8, 2019
Sign a transaction and sent it to Ethereum.

In the current era, Blockchain is growing up with the enormous speed and Ethereum is one of the significant, open-source, public blockchain-based distributed computing platform and operating system featuring smart contract functionality.

There are plenty of wallets available to transfer ether from one account to another. But what, if you wish to transfer ether on your own without using any wallet.

This article will help you to simply sign a transaction and send ether on your own and eventually in the upcoming post I will teach you how to make your ethereum wallet.

First of all, we will start by installing some dependencies. Make sure you have Node installed, open up your terminal and install.

npm i --save ethereumjs-txnpm i --save web3

Add this to your JS file

const Tx = require('ethereumjs-tx').Transaction
const Web3 = require('web3')

Now we need to create an object of web3, we will be using Infura to create a blockchain node.
Infura: It is a hosted Ethereum node cluster that lets you run your application without requiring them to set up their own Ethereum node or wallet.

You can create your node from here.

const web3 = new Web3('https://mainnet.infura.io/v3/7xxxxxxx')

Now we need two accounts to transfer ether, you can get that from Metamask or create your own by using.

web3.eth.accounts.create()

Now we have to initialize two address and their private keys. Save your Private Keys to .env file for security purpose.

const account1 = "0x1c47ab2738e07114450F8a1B58a5787ed1c01026"
const account2 = "0x909c183ce8F35634e94b899C6Po96243cc3fD976"
const privateKey1 = Buffer.from(
process.env.PRIV_KEY1,
'hex'
)
const privateKey2 = Buffer.from(
process.env.PRIV_KEY1,
'hex'
)

Transaction structure :

So we will be creating a Tx object and the signing with that a private key to create a raw transaction that can be sent on blockchain for confirmation so that we can transfer ether from one account to another.

web3.eth.getTransactionCount(account1,(err,txCount) =>{
// Creating a transaction object
const txObject = {
nonce:web3.utils.toHex(txCount),
to:account2,
value:web3.utils.toHex(web3.utils.toWei('0.002','ether')),
gasLimit:web3.utils.toHex(21000),
gasPrice:web3.utils.toHex(web3.utils.toWei('10','gwei'))
}
console.log("Transaction Object",txObject)
// change Ropsten to Mainnet, if you wish to send on mainnet.
const tx = new Tx(txObject, { chain: 'ropsten', hardfork: 'petersburg' },)
tx.sign(privateKey1)
const serializedTransaction = tx.serialize()
console.log("serialized Transaction",serializedTransaction)
const raw = '0x'+ serializedTransaction.toString('hex')
console.log("Raw Transaction",raw)
web3.eth.sendSignedTransaction(raw,(err,txHash)=>{
console.log("Error",err)
console.log('txHash:',txHash)
})
})

In the end, we will get a Transaction hash.

txHash: 0xdf7b63fd97d39675179bad34e07d5a913e3db62793b8f7f2874c582a7501aea5

You can also check the balance of your account :

web3.eth.getBalance(account1,(err,result) =>{
console.log("Error",err)
console.log("Balance of ",account1, result)
})

So, in this tutorial we have learned to create a transaction, sign a transaction and then send a transaction to Blockchain for the confirmation.

For, full source code click here.

Follow Pranshu on Twitter, Linkedin, Github.

About Zubi

Zubi is a platform for emerging technologies that provide opportunities from learning to building a career. It bestows one with personalised opportunities in the space. Zubi started with building a blockchain ecosystem and is working on mass adoption of the technology.

--

--

Pranshu Rastogi
Zubi.io

Blockchain || ZKP || Queerly curious 🧐|| Empathy resonates 💗 || Learn & Grow together