ecpair

A library for managing SECP256k1 keypairs written in TypeScript with transpiled JavaScript committed to git.
Note ECPair.makeRandom() uses the crypto.getRandomValues if there is no custom rng function provided. This API currently is still an experimental feature as of Node.js 18.19.0. To work around this you can do one of the following:
- Use a polyfill for crypto.getRandomValues()
- Use the
--experimental-global-webcrypto flag when running node.js.
- Pass in a custom rng function to generate random values.
Example
TypeScript
import { Signer, SignerAsync, ECPairInterface, ECPairFactory, ECPairAPI, TinySecp256k1Interface } from 'ecpair';
import * as crypto from 'crypto';
const tinysecp: TinySecp256k1Interface = require('tiny-secp256k1');
const ECPair: ECPairAPI = ECPairFactory(tinysecp);
const keyPair1: ECPairInterface = ECPair.fromWIF('KynD8ZKdViVo5W82oyxvE18BbG6nZPVQ8Td8hYbwU94RmyUALUik');
const keyPair2 = ECPair.fromPrivateKey(crypto.randomBytes(32));
const keyPair3 = ECPair.makeRandom();
const customRandomBufferFunc = (size: number): Buffer => crypto.randomBytes(size);
const keyPair4 = ECPair.makeRandom({ rng: customRandomBufferFunc });
const keyPair5 = ECPair.fromPublicKey(keyPair1.publicKey);
const network = {};
ECPair.makeRandom({ network });
ECPair.fromPrivateKey(crypto.randomBytes(32), { network });
ECPair.fromPublicKey(keyPair1.publicKey, { network });
ECPair.fromWIF('wif key...', network);
const network2 = {};
const network3 = {};
ECPair.fromWIF('wif key...', [network, network2, network3]);
LICENSE MIT
Written and tested by bitcoinjs-lib contributors since 2014.