Wallet Creation

Wallet Creation

The wallet creation process involves generating a new key pair, which gives us both a public address and a private key. Here’s the code and a breakdown of how it works:

let key, address, fullAddress, privateKey;

do {
    key = ec.genKeyPair();  // Generate a new key pair
    fullAddress = key.getPublic('hex');  // Get the full public address in hexadecimal
    address = key.getPublic().getX();  // Get the 'X' coordinate of the public key as the main address
    privateKey = key.getPrivate('hex');  // Get the private key in hexadecimal format
} while (!address.toString(16).startsWith('f'));  // Ensure the address starts with 'f'

How It Works

  • Generate Key Pair: The ec.genKeyPair() function creates a new elliptic curve key pair. This includes a public key (for the address) and a private key.

  • Get Public Address: getPublic('hex') provides the full public key in hexadecimal format, which serves as the unique wallet address.

  • Address Validation: To maintain consistency, we ensure each address starts with the letter ‘f’. If the generated address does not begin with ‘f’, we discard it and generate a new one. This is done using a do...while loop.

  • Get Private Key: Finally, getPrivate('hex') retrieves the private key in hexadecimal format. This key is essential for wallet recovery and must be kept secure.

Example Creation

{
    "fullAddress": "048fcd... (full address here)",
    "address": "f8b7a...",
    "privateKey": "d1b1c... (private key here)"
}

Last updated