July 15, 2026

Crypto Cheque! Introduction to Message Signing in Ethereum

Crypto Cheque! Introduction to Message Signing in Ethereum

Lines 1–2 imports Web3 and creates an instance using our private Ganache blockchain. This allows us to use the accounts on Ganache to sign messages and call functions. Web3 also contains utility functions, one of which we will use to sign our message. On line 2, localhost:8545 refers to the address and port Ganache is using, which can be viewed under RPC SERVER. (localhost = 127.0.0.1)

Lines 5 retrieves the JSON contract representation created by Truffle upon migration using the NodeJs fs (filesystem) module. We’ll cover this in detail soon.

Line 6 parses the JSON so it is readable by our program.

Line 7 extracts the contract address from the JSON and stores it as contractAddress.

Line 9 declares the payment signing function signPayment. It takes in the recipient (payee) address along with the agreed amount. It’s marked async so we can take advantage of Javascript’s async/await pattern for retrieving data asynchronously.

Line 10 uses web3 to retrieve all the accounts associated with the given blockchain (Ganache) and stores them in a list as accounts

Line 11 assigns the first account in the list to payer.

Line 12 retrieves the transaction count for the given address (payer). We’ll be using this as our nonce to embed within our message.

Line 13 uses web3’s soliditySha3() hashing function to create a hash of our message. Our message contains the recipient (payee), amount, txCount (nonce) and the contract’s address (contractAddress). This particular hashing function mimics the behavior of the keccak256() function in solidity.

Lines 15 & 18 are try/catch blocks. The code will attempt to execute the contents of the try block, falling back to the catch block if it fails. This syntax is useful for the async/await pattern because it doesn’t have inherent error catching when compared to promises.

Line 16 signs the message hash we created using the private key from one of the Ganache accounts. Replace <GANACHE_PRIVATE_KEY> with the actual private key from the first account in Ganache. Retrieve it by clicking the key icon on the right side of the account box.

**IMPORTANT: Make sure to prepend the private key with 0x after pasting it in the code, otherwise the program won’t recognize it as a valid key. Example: 0x6efc5ffc3ec7609736a2299a4a80e8f4377039b947c310ffa6f9ddf7a1fd5398

It’s worth noting that web3.eth.accounts.sign automatically prefixes the message with x19Ethereum Signed Message:n before signing. We’d have to manually add the prefix if we were using an alternative signing method such as web3.eth.sign. Check the docs for more information on the difference between these methods.

The output value of web3.eth.accounts.sign is an object that contains the signature which we will explore momentarily.

Line 17 outputs the amount, txCount & sigObject to the console so we can use them when we interact with the contract.

Line 23 calls signPayment with the 2nd address on the Ganache list (the payee) and the send amount. Simply copy + paste the address from ganache. The second parameter is the amount (10e18). Remember, 10e18 Wei = 1 Ether, same as we wrote in the contract.

In the terminal, run:

node index.js

The output should resemble the following:

100000000000000 4 

The first 2 numbers are the amount and the nonce (transaction count), those are important for verifying the message signer.

The signature object not only includes the signature at the bottom which we’ll use, but some other interesting data as well. Remember in our smart contract how we split the signature into the v, r, and s components? These are the same components outputted above. We can also see the message in a different format and the messageHash.

Now that we’ve created the cheque contract and signed a message as the payer to approve a withdrawal, let’s deploy the contract and interact with it to claim the payment as the payee.

First off, open truffle-config.js located in the project’s root. Delete the contents of the file and replace it with the following:

This configuration is necessary so that Truffle knows which blockchain network to use when we deploy smart contracts. Here, we’ve set up a network called development, although the name is trivial. The host and port come directly from Ganache. Since this is the only network we’re working with, Truffle will use it by default when we deploy and interact with smart contracts.

Next, create a new file titled 2_cheque.js under the migrations directory.

cd migrations
touch cheque.js
cd ..

Open it and type:

Line 1 imports the smart contract using the Truffle Artifactor. This converts the raw solidity to a JSON representation that Truffle can read.

Lines 3–5 deploy the contract instance to Ganache using the Truffle Deployer, which handles everything behind the scenes. represents metadata that’s passed in as a parameter to the deployment. In this case, it’s the amount of Ether we’re sending to the contract in the form of Wei.

Recall how we marked the constructor function as payable in the contract. This permits Ether to be sent to the contract upon deployment when the constructor is called.

constructor() public payable 

Now that we’ve set up our config and deployment settings, it’s time to play with our new toy. In the console, type the following:

truffle migrate 

This command tells Truffle to accomplish 2 things:

  1. Compile the raw solidity into a JSON readable representation of the contract. Use truffle compile as an alias.
  2. Deploy the JSON abstraction to the network based on the configuration we’ve provided. At present, Truffle will deploy this contract on our development (Ganache) network using the first address in the list by default, with 1e18 Ether. Use truffle deploy as an alias.

After running this command, open Ganache. Notice how the first account in the list has dropped in Ether balance. The difference is the deployment cost + the amount we sent to the contract.

You’ll also notice the new directory build created in the project’s root containing the JSON files.

truffle console

opens up the Truffle console for the default network (development). This is a javascript runtime environment complete with the modules used by Truffle.

let app;
Cheque.deployed().then((instance) => )

Here, we’re assigning app to our deployed instance of Cheque.

let accounts;
web3.eth.getAccounts().then((result) => )

This retrieves the accounts from Ganache using web3 as an array and assigns them to accounts. Try running accounts in the console, you’ll see the list as the output:

[ '0x2642269FCDDbB58f76124b7f4a98Df93E99ae803',
'0xd09D4cF222ef0B4D5623815aE9a01682a1d17E88',
'0x66e0249709b4dD1A30Ab3E18cBcb6F944a95d48b',
'0xe15bcb2964339dF76341cAF10977F84CBd036ef1',
'0xa485b07b9C149757acA6c0B29F051fAAe84364f0',
'0x5bC383FB28F52a4aE42c5b4caD682163c0FE0654',
'0x8a8da609576A3Bd1ab6018285Ef1F020467F9ECB',
'0xebe47e36F5343e733917bd652d156B0005d2eb50',
'0x6A3C002BC49A1fE1a895a152CeF3c29831E8d46b',
'0xae9086C3A251D55bd5491E1Af1898608a788F263' ]

Finally, we’ll claim our payment as the rightful payee:

app.claimPayment(1, NONCE, SIGNATURE, )

Since we stored the deployed contract instance in app, we can use it to call its functions. Here, we’re calling claimPayment as the payee. The 1 parameter refers to the amount in Ether. It is represented in Ether here because the smart contract is responsible for converting it back to Wei. Replace NONCE with the txCount output andSIGNATURE with the sig output, both from running index.js. tells the program to use the second address in our list of accounts as the function’s caller (payee!). You should see an output like this:

,
logs: [] }

Now if we look at Ganache, the first account is deducted 1.02 ETH (cost of deployment + Ether sent) and the second account is credited 1 ETH for claiming the payment.

Congratulations! If you’ve stuck it out this far, you’ve just learned how to create a smart cheque on Ethereum!

?? Stay tuned for more educational articles to come. Full length video courses on how to program the Ethereum Blockchain are also coming out soon! ??

Published at Tue, 30 Jul 2019 04:27:02 +0000

{flickr|100|campaign}

Previous Article

Proof of Uptime Job Announcement – EverLife.AI

Next Article

Proof of Uptime Job Announcement – EverLife.AI

You might be interested in …