0
0
BlockchainComparisonBeginner · 4 min read

Public Key vs Private Key Blockchain: Key Differences and Usage

In blockchain, a public key is a cryptographic code shared openly to receive transactions, while a private key is a secret code used to sign and authorize transactions securely. The public key ensures transparency, and the private key ensures control and security over assets.
⚖️

Quick Comparison

This table summarizes the main differences between public key and private key in blockchain.

FactorPublic KeyPrivate Key
VisibilityShared openly on the networkKept secret by the owner
PurposeReceive funds and verify signaturesSign transactions and prove ownership
Security RoleUsed to verify authenticityUsed to authorize and control assets
LengthUsually longer (e.g., 256 bits)Same length as public key but kept private
Example UseWallet address derived from public keyUnlock wallet and sign transactions
⚖️

Key Differences

The public key is like your email address that you can share with anyone to receive messages (or funds). It is derived from the private key using a one-way mathematical function, so it is safe to share publicly without revealing the private key.

The private key is like your password or signature. It must be kept secret because it allows you to sign transactions and prove ownership of your blockchain assets. If someone else gets your private key, they can control your funds.

In Solidity and blockchain, the public key is often used to generate wallet addresses, while the private key is used off-chain to sign transactions before sending them to the network for verification.

⚖️

Code Comparison

This Solidity example shows how a public key (address) is used to verify ownership by checking the signer of a message.

solidity
pragma solidity ^0.8.0;

contract VerifySignature {
    // This function verifies that a message was signed by the owner of the given address (public key)
    function verify(address signer, bytes32 messageHash, bytes memory signature) public pure returns (bool) {
        // Recover the address that signed the message
        address recovered = recoverSigner(messageHash, signature);
        // Check if recovered address matches the signer (public key)
        return recovered == signer;
    }

    function recoverSigner(bytes32 messageHash, bytes memory signature) internal pure returns (address) {
        // Split signature into r, s, v variables
        require(signature.length == 65, "Invalid signature length");
        bytes32 r;
        bytes32 s;
        uint8 v;
        assembly {
            r := mload(add(signature, 32))
            s := mload(add(signature, 64))
            v := byte(0, mload(add(signature, 96)))
        }
        // Recover signer address using ecrecover
        return ecrecover(messageHash, v, r, s);
    }
}
↔️

Private Key Equivalent

Private keys are not used directly in Solidity because they must remain secret and are used off-chain to sign transactions. Here is a simple JavaScript example using ethers.js to sign a message with a private key.

javascript
import { ethers } from "ethers";

async function signMessage() {
  const privateKey = "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
  const wallet = new ethers.Wallet(privateKey);
  const message = "Hello, blockchain!";
  const signature = await wallet.signMessage(message);
  console.log("Signature:", signature);
}

signMessage();
Output
Signature: 0x... (a long hex string representing the signed message)
🎯

When to Use Which

Choose public key when you want to share your address to receive funds or verify signatures on-chain. It is safe and necessary for transparency.

Choose private key when you need to sign transactions or prove ownership off-chain. Keep it secret and never expose it in smart contracts or public code.

In practice, use the private key in your wallet software or backend to sign transactions, and use the public key/address in Solidity contracts to verify or identify users.

Key Takeaways

Public keys are shared openly to receive funds and verify signatures.
Private keys are secret and used to sign transactions securely off-chain.
Never expose private keys in Solidity or public code to avoid theft.
Use public keys (addresses) in smart contracts to identify users.
Sign transactions with private keys using wallet software or libraries like ethers.js.