Skip to content
This repository was archived by the owner on Apr 14, 2026. It is now read-only.

Repository files navigation

ecpair

Github CINPMcode style: prettier

A library for managing SECP256k1 keypairs written in TypeScript with transpiled JavaScript committed to git.

NoteECPair.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:

  1. Use a polyfill for crypto.getRandomValues()
  2. Use the --experimental-global-webcrypto flag when running node.js.
  3. Pass in a custom rng function to generate random values.

Example

TypeScript

import{Signer,SignerAsync,ECPairInterface,ECPairFactory,ECPairAPI,TinySecp256k1Interface}from'ecpair';import*ascryptofrom'crypto';// You need to provide the ECC library. The ECC library must implement // all the methods of the `TinySecp256k1Interface` interface.consttinysecp: TinySecp256k1Interface=require('tiny-secp256k1');constECPair: ECPairAPI=ECPairFactory(tinysecp);// You don't need to explicitly write ECPairInterface, but just to show// that the keyPair implements the interface this example includes it.// From WIFconstkeyPair1: ECPairInterface=ECPair.fromWIF('KynD8ZKdViVo5W82oyxvE18BbG6nZPVQ8Td8hYbwU94RmyUALUik');// Random private keyconstkeyPair2=ECPair.fromPrivateKey(crypto.randomBytes(32));// OR (uses randombytes library, compatible with browser)constkeyPair3=ECPair.makeRandom();// OR use your own custom random buffer generator BE CAREFUL!!!!constcustomRandomBufferFunc=(size: number): Buffer=>crypto.randomBytes(size);constkeyPair4=ECPair.makeRandom({rng: customRandomBufferFunc});// From pubkey (33 or 65 byte DER format public key)constkeyPair5=ECPair.fromPublicKey(keyPair1.publicKey);// Pass a custom networkconstnetwork={};// Your custom network object hereECPair.makeRandom({ network });ECPair.fromPrivateKey(crypto.randomBytes(32),{ network });ECPair.fromPublicKey(keyPair1.publicKey,{ network });// fromWIF will check the WIF version against the network you pass in// pass in multiple networks if you are not sureECPair.fromWIF('wif key...',network);constnetwork2={};// Your custom network object hereconstnetwork3={};// Your custom network object hereECPair.fromWIF('wif key...',[network,network2,network3]);

LICENSE MIT

Written and tested by bitcoinjs-lib contributors since 2014.

About

The ECPair module for bitcoinjs-lib

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages