ECPair: SECP256k1 Keypair Management for Bitcoin

3.0.1 · active · verified Tue Apr 21

ecpair is a JavaScript/TypeScript library designed for managing SECP256k1 keypairs, primarily used within the BitcoinJS ecosystem for client-side applications. Currently at stable version 3.0.1, it provides core functionalities for generating new keypairs, importing them from various formats like WIF (Wallet Import Format) or raw private/public keys, and deriving public keys. Releases are generally stable, driven by updates within the broader BitcoinJS ecosystem or critical bug fixes. A key differentiator is its modular architecture, which externalizes all elliptic curve cryptography operations to a separate ECC library (e.g., `tiny-secp256k1`), enhancing flexibility and allowing for specific backend optimizations or compliance. The library is written in TypeScript and ships with comprehensive type definitions, promoting robust development practices.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `ecpair` with an ECC library, generate random keypairs, and import keys from WIF, private keys, and public keys, including custom network and RNG function usage.

import { ECPairFactory, TinySecp256k1Interface, ECPairInterface } from 'ecpair';
import * as crypto from 'crypto';

// You need to provide an ECC library that implements TinySecp256k1Interface
// This is typically 'tiny-secp256k1'
const tinysecp: TinySecp256k1Interface = require('tiny-secp256k1');
const ECPair = ECPairFactory(tinysecp);

// Generate a random key pair
const keyPair1: ECPairInterface = ECPair.makeRandom();
console.log('Random Private Key (HEX):', keyPair1.privateKey?.toString('hex'));
console.log('Random Public Key (HEX):', keyPair1.publicKey.toString('hex'));

// Import from WIF
const wifKey = 'L45P2H58kPq611cRjP5H3rXfRjP5H3rXfRjP5H3rXfRjP5H3rXfRjP5H3rXfRjP5H3rXfRjP5H3rXf'; // Example WIF (DO NOT USE FOR REAL ASSETS)
const keyPair2: ECPairInterface = ECPair.fromWIF(wifKey);
console.log('WIF Public Key (HEX):', keyPair2.publicKey.toString('hex'));

// Import from a private key buffer
const privateKeyBuffer = crypto.randomBytes(32);
const keyPair3: ECPairInterface = ECPair.fromPrivateKey(privateKeyBuffer);
console.log('Imported Private Key (HEX):', keyPair3.privateKey?.toString('hex'));

// Import from a public key buffer
const keyPair4: ECPairInterface = ECPair.fromPublicKey(keyPair1.publicKey);
console.log('Imported Public Key (HEX):', keyPair4.publicKey.toString('hex'));

// Demonstrate custom network and RNG
const customNetwork = { messagePrefix: '\x18Bitcoin Signed Message:\n', bip32: { public: 0x0488b21e, private: 0x0488ade4 }, pubKeyHash: 0x00, scriptHash: 0x05, wif: 0x80 };
const customRng = (size: number): Buffer => crypto.randomBytes(size);
const keyPair5 = ECPair.makeRandom({ network: customNetwork, rng: customRng });
console.log('Custom Network Public Key (HEX):', keyPair5.publicKey.toString('hex'));

view raw JSON →