Bitcore Library
raw JSON → 10.10.7 verified Sat Apr 25 auth: no javascript
A pure and powerful JavaScript Bitcoin library providing core Bitcoin primitives such as addresses, transactions, HD keys (BIP32), and message signing. Current stable version is 10.10.7. Maintained as part of the Bitcore ecosystem by BitPay/Bitcore, it is primarily used for building Bitcoin-related Node.js applications. It offers a comprehensive API for creating and parsing transactions, managing keys, and interacting with the Bitcoin network. Key differentiators include full support for BIP21, BIP32, BIP37, and BIP69, and its integration with the Bitcore full node and Insight block explorer.
Common errors
error Cannot find module 'bitcore-lib' ↓
cause Package not installed or not in node_modules.
fix
Run npm install bitcore-lib
error TypeError: Right-hand side of 'instanceof' is not an object ↓
cause Multiple instances of bitcore-lib loaded (e.g., via peer dependencies).
fix
Ensure only one version of bitcore-lib is installed; use npm dedupe or check package.json.
error Error: Invalid hex string ↓
cause Hex string passed to Transaction.from() is malformed or has wrong length.
fix
Provide a valid hex string (e.g., raw transaction) or use Buffer.from(hex, 'hex').
error Error: Transaction input satoshis do not add up to the outputs ↓
cause UTXO satoshis provided do not equal output satoshis plus fee.
fix
Ensure total(inputs) = total(outputs) + fee. Check utxo.satoshis and .to() amounts.
Warnings
breaking v8.0.0 removed the 'bitcore' namespace; methods are now top-level exports. ↓
fix Use named imports like import { PrivateKey } from 'bitcore-lib' instead of bitcore.PrivateKey.
breaking v10.0.0 changed default ESM module; require() no longer works without .default. ↓
fix Use import bitcore from 'bitcore-lib' or const bitcore = require('bitcore-lib').default.
deprecated bitcore.HDPublicKey and bitcore.HDPrivateKey are deprecated in favor of bitcore.HDKey. ↓
fix Use bitcore.HDKey.fromExtendedKey() instead.
gotcha Transaction signing mutates the Transaction object; always sign last. ↓
fix Call sign() only after all inputs/outputs are set.
Install
npm install bitcore-lib yarn add bitcore-lib pnpm add bitcore-lib Imports
- bitcore wrong
const bitcore = require('bitcore-lib')correctimport bitcore from 'bitcore-lib' - PrivateKey wrong
import PrivateKey from 'bitcore-lib/PrivateKey'correctimport { PrivateKey } from 'bitcore-lib' - Transaction wrong
const Transaction = require('bitcore-lib').Transactioncorrectimport { Transaction } from 'bitcore-lib' - Address wrong
import Address from 'bitcore-lib/lib/address'correctimport { Address } from 'bitcore-lib'
Quickstart
import bitcore from 'bitcore-lib';
// Generate a new private key
const privateKey = new bitcore.PrivateKey();
// Derive the public key and address
const publicKey = privateKey.toPublicKey();
const address = publicKey.toAddress();
console.log('Private Key:', privateKey.toString());
console.log('Address:', address.toString());
// Create a simple transaction (unsigned)
const utxo = {
address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
txId: 'a477af6b2667c29670467e4e0728b685ee07b240235771b3181b3c3c0f3f3a0b',
outputIndex: 0,
script: '76a914cbc20a7664f2f69e5355aa427045bc15e7c6c77288ac',
satoshis: 10000
};
const tx = new bitcore.Transaction()
.from(utxo)
.to('1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2', 9000)
.change(address)
.fee(1000)
.sign(privateKey);
console.log('Raw transaction:', tx.toString());