Bitcoin Cryptography: Signing and Verifying Messages with Node.js

Delve into Digital Signatures with ECDSA, Node.js and BitcoinJS

Abhishek Chauhan
Coinmonks
Published in
3 min readJun 20, 2023

--

Signing and Verifying Messages with ECDSA

Bitcoin has rapidly evolved into an Internet sensation since its inception in 2009. At the core of its framework lies robust cryptography methods, which ensure secure and private transactions within the decentralized network. In this post, we will dive deep into one such cryptography method — the Elliptic Curve Digital Signature Algorithm (ECDSA). We will explore how to sign a message with a Bitcoin private key and verify it with the corresponding public key using Node.js.

Understanding ECDSA in Bitcoin

ECDSA is an essential element of Bitcoin’s secure framework. It generates a unique private key, which is then used to derive a public key. A Bitcoin address is obtained from this public key using a hashing algorithm. When a Bitcoin transaction occurs, the sender uses their private key to sign the transaction, providing proof of ownership. The network verifies the signature using the sender’s public key, ensuring authenticity and preventing double-spending.

Generating a Bitcoin Key Pair

Before we can sign a message, we need to generate a Bitcoin key pair — a private key and a public key. We’ll use Node.js and a couple of Bitcoin-specific npm packages, namely bitcoinjs-lib, ecpair, and tiny.secp256k1.

Install the necessary packages if you haven’t done so already:

npm i bitcoinjs-lib ecpair tiny-secp256k1

Let’s generate a random key pair:

const bitcoin = require("bitcoinjs-lib");
const { ECPairFactory } = require("ecpair");
const ecc = require("tiny-secp256k1");
const ECPair = ECPairFactory(ecc);// Generate a random Bitcoin key pair
const keyPair = ECPair.makeRandom();
// Your private key in WIF
const privateKey = keyPair.toWIF();
console.log(`Private Key: ${privateKey}\n`);
// Your public key
const publicKey = keyPair.publicKey.toString("hex");
console.log(`Public Key: ${publicKey}\n`);

In this code snippet, we’re generating a random private key using the ECPair.makeRandom() function, and then obtaining the corresponding public key. Remember to keep your private key secure and private!

Signing and Verifying a Message

Now that we have our key pair, let’s proceed to sign a message:

// Create a message
const message = "Hello, Bitcoin!";
// Sign the message with the private key
const hash = bitcoin.crypto.sha256(message);
const signature = keyPair.sign(hash);
console.log(`Signature: ${signature.toString("base64")}\n`);
// Verify the message with the public key
const verified = keyPair.verify(hash, signature);
console.log(`Message Verified: ${verified}`);

In the above code, we are creating a message and signing it using our private key. We then verify the signature with our public key. If the code runs correctly, you will see the output ‘Message Verified: true’, indicating successful signing and verification.

Signing and Verifying a Message

Conclusion

Understanding Bitcoin’s underlying cryptography, such as the ECDSA, not only provides insight into Bitcoin’s secure and decentralized nature but also opens up possibilities for innovation. Learning to generate Bitcoin key pairs and use them to sign and verify messages can be the first step in a deeper exploration of Bitcoin and blockchain technology.

Whether you’re a seasoned Bitcoin enthusiast or a curious coder stepping into the world of blockchain, there’s always something new to learn and experiment with in this vibrant and ever-evolving field.

--

--

Abhishek Chauhan
Coinmonks

👨‍💻 Blockchain dev sharing insights on innovative solutions. Follow me on LinkedIn: https://www.linkedin.com/in/ac12644 🤝 GitHub: https://github.com/ac12644