j-bitcoin 2.0.0

J-Bitcoin

npm version License: ISC Node.js

A comprehensive JavaScript/TypeScript Bitcoin wallet library featuring HD wallets (BIP32/39/44/49/84/86), threshold signatures (TSS), Schnorr/ECDSA signatures, and Taproot support.

Features

Category Features
Wallets Custodial HD wallets, Non-custodial threshold wallets (TSS)
Standards BIP32, BIP39, BIP44, BIP49, BIP84, BIP86, BIP173, BIP322, BIP340, BIP350
Addresses Legacy P2PKH (1...), P2SH-P2WPKH (3...), Native SegWit (bc1q...), Taproot (bc1p...)
Signatures ECDSA, Schnorr (BIP340), Threshold signatures (TSS), BIP322 message signing
Networks Bitcoin Mainnet, Testnet

Installation

npm install j-bitcoin

Requirements: Node.js 16+ and npm 7+

Quick Start

Custodial HD Wallet

import { CustodialWallet } from 'j-bitcoin';

// Generate new wallet (12-word mnemonic by default, 256 for 24-word)
const { wallet, mnemonic } = CustodialWallet.createNew('main', 128);
console.log('Backup mnemonic:', mnemonic);

// Derive addresses (account, index, type)
const legacy = wallet.getReceivingAddress(0, 0, 'legacy');         // 1...
const wrapped = wallet.getReceivingAddress(0, 0, 'wrapped-segwit'); // 3...
const segwit = wallet.getReceivingAddress(0, 0, 'segwit');         // bc1q...
const taproot = wallet.getReceivingAddress(0, 0, 'taproot');       // bc1p...

console.log('Legacy:', legacy.address);
console.log('SegWit:', segwit.address);
console.log('Taproot:', taproot.address);

// Get change address
const change = wallet.getChangeAddress(0, 0, 'segwit');

// Batch generate addresses
const addresses = wallet.getAddresses(0, 'segwit', 20);

// Restore from mnemonic
const restored = CustodialWallet.fromMnemonic('main', mnemonic);

// Export/import WIF
const wif = wallet.exportWIF(0, 0, 0, 'segwit');
const wifWallet = CustodialWallet.fromWIF(wif);

Non-Custodial Threshold Wallet (TSS)

import { NonCustodialWallet } from 'j-bitcoin';

// Create TSS-only wallet (3 participants, threshold degree t=1)
// Signing requires 2t+1 = 3 participants
const { wallet, shares, config } = NonCustodialWallet.createNew('main', 3, 1);
console.log('TSS config:', config);

// Create HD + TSS wallet (combined mode)
const { wallet: hdWallet, mnemonic, shares: hdShares } = 
  NonCustodialWallet.createNewHD('main', 3, 1);

// Get TSS aggregate public key address
const tssAddress = wallet.getAddress('segwit');

// Get HD-derived addresses (same API as CustodialWallet)
const segwitAddr = hdWallet.getReceivingAddress(0, 0, 'segwit');

// Sign message hash with TSS
const messageHash = Buffer.alloc(32, 'test');
const signature = wallet.sign(messageHash);
const isValid = wallet.verify(messageHash, signature);

// Sign with HD-derived key
const hdSig = hdWallet.signMessageHD('Hello Bitcoin!', 0, 0, 'segwit');

BIP39 Mnemonic Operations

import { BIP39 } from 'j-bitcoin';

// Generate 12-word mnemonic (128-bit)
const { mnemonic } = BIP39.generateMnemonic(128);

// Generate 24-word mnemonic (256-bit)
const { mnemonic: mnemonic24 } = BIP39.generateMnemonic(256);

// Validate mnemonic
const isValid = BIP39.validateChecksum(mnemonic);

// Derive seed (with optional passphrase)
const seed = BIP39.deriveSeed(mnemonic, 'optional-passphrase');

Schnorr Signatures (BIP340)

import { Schnorr } from 'j-bitcoin';

const schnorr = new Schnorr();
const privateKey = Buffer.alloc(32, 'key');
const messageHash = Buffer.alloc(32, 'msg');

// Sign
const sig = await schnorr.sign(privateKey, messageHash);

// Verify
const publicKey = schnorr.getPublicKey(privateKey);
const isValid = await schnorr.verify(sig.signature, messageHash, publicKey);

ECDSA Signatures

import { ECDSA } from 'j-bitcoin';

const privateKey = Buffer.alloc(32, 'key');
const messageHash = Buffer.alloc(32, 'msg');

// Sign
const sig = ECDSA.sign(privateKey, messageHash);
console.log('DER:', sig.der.toString('hex'));
console.log('Recovery ID:', sig.recovery);

// Verify
const publicKey = ECDSA.getPublicKey(privateKey);
const isValid = ECDSA.verify(sig, messageHash, publicKey);

Bech32 Address Encoding

import { BECH32 } from 'j-bitcoin';

// Encode public key to SegWit address
const address = BECH32.to_P2WPKH(publicKeyHex, 'main');

// Encode to Taproot address  
const taprootAddr = BECH32.to_P2TR(xOnlyPubKeyHex, 'main');

// Decode address
const { program, version, type } = BECH32.decode(address);

Transaction Building

import { CustodialWallet } from 'j-bitcoin';

const wallet = CustodialWallet.fromMnemonic('main', mnemonic);

// Create transaction builder
const builder = wallet.createTransaction();

// Add inputs and outputs
builder.addInput(txid, vout, sequence);
builder.addOutput(address, amount);
builder.setFeeRate(10); // sat/vB

// Sign with wallet keys
await wallet.signTransaction(builder, [
  { account: 0, change: 0, index: 0, type: 'segwit' }
]);

// Get raw transaction hex
const rawTx = builder.build().toHex();

Project Structure

src/
├── wallet/                    # Wallet implementations
│   ├── custodial.js          # HD wallet (BIP32/39/44/49/84/86)
│   └── non-custodial.js      # Threshold signature + HD wallet
├── bip/                       # BIP standards
│   ├── BIP173-BIP350.js      # Bech32/Bech32m encoding
│   ├── bip32/                # HD key derivation
│   ├── bip39/                # Mnemonic generation
│   └── bip49.js              # P2SH-P2WPKH support
├── core/
│   ├── constants.js          # Network/crypto constants
│   ├── crypto/
│   │   ├── hash/             # RIPEMD160, HASH160
│   │   └── signatures/       # ECDSA, Schnorr, Threshold
│   └── taproot/              # Taproot support
├── encoding/
│   ├── base58.js             # Base58Check
│   ├── base32.js             # Bech32/Bech32m
│   └── address/              # Address encode/decode
├── transaction/
│   ├── builder.js            # Transaction construction
│   ├── psbt.js               # PSBT support
│   ├── message-signing.js    # BIP322 message signing
│   └── script-builder.js     # Script creation
└── utils/
    ├── validation.js         # Input validation
    └── address-helpers.js    # Address utilities

test/
├── testnet-data/             # Wallet state for testnet testing
├── testnet-test.js           # Main testnet testing script
├── test-all-features.js      # Comprehensive feature tests (43 tests)
├── test-chain.js             # Chain transaction tests
├── custodial-test.js         # BTC standards compliance tests
└── non-custodial-test.js     # TSS compliance tests

API Reference

Wallet Classes

Class Description
CustodialWallet Full HD wallet with BIP32/39/44/49/84/86 support
NonCustodialWallet Threshold (TSS) + HD hybrid wallet

Wallet Methods

Method Description
createNew(network, strength) Create new wallet with mnemonic
fromMnemonic(network, mnemonic) Restore from BIP39 phrase
fromWIF(wif) Import from WIF private key
fromExtendedKey(network, xprv/xpub) Import from extended key
getReceivingAddress(account, index, type) Get external address
getChangeAddress(account, index, type) Get internal address
getAddresses(account, type, count) Batch generate addresses
signMessage(message, account, index, type) Sign message
createTransaction() Create transaction builder
signTransaction(builder, inputInfo) Sign transaction
exportWIF(account, change, index, type) Export key as WIF

Cryptographic Modules

Module Description
BIP39 Mnemonic generation (12-24 words), validation, seed derivation
ECDSA Standard Bitcoin signatures with recovery
Schnorr BIP340 Schnorr signatures
BECH32 Bech32/Bech32m address encoding (BIP173/BIP350)
BIP322 Generic message signing for all address types
b58encode / b58decode Base58Check encoding

Key Derivation

Function Description
generateMasterKey(seed, network) Generate BIP32 master key from seed
derive(path, extendedKey) Derive child key at BIP32 path

Development

# Install dependencies
npm install

# Run tests
npm test
npm run test:coverage

# Run wallet compliance tests
node test/custodial-test.js
node test/non-custodial-test.js

# Run comprehensive feature tests (43 tests)
node test/test-all-features.js

# Run testnet testing (requires funding)
node test/testnet-test.js

# Lint and format
npm run lint
npm run format

Testnet Verification

This library has been fully verified on Bitcoin Testnet4 with real transactions:

Address Type Custodial Non-Custodial (TSS)
Legacy (P2PKH)
Wrapped SegWit (P2SH-P2WPKH)
Native SegWit (P2WPKH)
Taproot (P2TR)

All signing algorithms tested: ECDSA, Schnorr (BIP340), BIP143, BIP341

Security

⚠️ Important: This library handles private keys and cryptographic material.

  • Never share private keys, mnemonics, or threshold shares
  • Test on testnet before mainnet deployment
  • Store securely - use encrypted offline storage for mnemonics
  • Validate inputs - always validate addresses and signatures
  • Clear sensitive data - call wallet.destroy() when done

Dependencies

License

ISC License - see LICENSE


Made with ❤️ for the Bitcoin community

index.js

Description:
  • Enterprise-grade Bitcoin library with HD wallets, threshold signatures, and full BIP compliance for secure cryptocurrency operations.

Source:
Version:
  • 2.0.0
Author:
  • yfbsei
License:
  • ISC

Enterprise-grade Bitcoin library with HD wallets, threshold signatures, and full BIP compliance for secure cryptocurrency operations.

src/bip/BIP173-BIP350.js

Description:
  • BIP173/BIP350 Bech32 address encoding for Bitcoin

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

BIP173/BIP350 Bech32 address encoding for Bitcoin

src/bip/bip32/derive.js

Description:
  • BIP32 child key derivation

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

BIP32 child key derivation

src/bip/bip32/master-key.js

Description:
  • BIP32 master key generation from seed

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

BIP32 master key generation from seed

src/bip/bip39/mnemonic.js

Description:
  • BIP39 mnemonic phrase generation and seed derivation

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

BIP39 mnemonic phrase generation and seed derivation

src/bip/bip49.js

Description:
  • P2SH-P2WPKH (3... addresses) derivation and address generation

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

P2SH-P2WPKH (3... addresses) derivation and address generation

src/core/constants.js

Description:
  • Core constants and configuration for J-Bitcoin library

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Core constants and configuration for J-Bitcoin library

src/core/crypto/hash/ripemd160.js

Description:
  • RIPEMD160 cryptographic hash function implementation

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

RIPEMD160 cryptographic hash function implementation

src/core/crypto/signatures/ecdsa.js

src/core/crypto/signatures/schnorr-BIP340.js

src/core/crypto/signatures/threshold/index.js

Description:
  • Exports all threshold signature components for use in the J-Bitcoin library. Implements the nChain Threshold Signatures whitepaper protocol.

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Exports all threshold signature components for use in the J-Bitcoin library. Implements the nChain Threshold Signatures whitepaper protocol.

src/core/crypto/signatures/threshold/jvrss.js

Description:
  • Implements the JVRSS protocol from Section 2.1 of the nChain whitepaper for distributed generation of shared secrets with Feldman verification.

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC
See:
  • Threshold-Signatures-whitepaper-nchain.pdf Section 2.1

Implements the JVRSS protocol from Section 2.1 of the nChain whitepaper for distributed generation of shared secrets with Feldman verification.

src/core/crypto/signatures/threshold/mpc-operations.js

Description:
  • Implements ADDSS, PROSS, and INVSS from Sections 2.2-2.4 of the nChain whitepaper. These operations allow computation on shared secrets without revealing them.

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC
See:
  • Threshold-Signatures-whitepaper-nchain.pdf Sections 2.2, 2.3, 2.4

Implements ADDSS, PROSS, and INVSS from Sections 2.2-2.4 of the nChain whitepaper. These operations allow computation on shared secrets without revealing them.

src/core/crypto/signatures/threshold/participant.js

Description:
  • Represents an individual participant in the TSS protocol, managing their private polynomial, key shares, and signature generation.

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC
See:
  • Threshold-Signatures-whitepaper-nchain.pdf Sections 2.1, 4.3

Represents an individual participant in the TSS protocol, managing their private polynomial, key shares, and signature generation.

src/core/crypto/signatures/threshold/polynomial.js

Description:
  • Implements polynomial arithmetic over finite fields for Shamir's Secret Sharing and Lagrange interpolation as specified in the nChain TSS whitepaper.

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC
See:
  • Threshold-Signatures-whitepaper-nchain.pdf Section 2.1

Implements polynomial arithmetic over finite fields for Shamir's Secret Sharing and Lagrange interpolation as specified in the nChain TSS whitepaper.

src/core/crypto/signatures/threshold/threshold-signature.js

Description:
  • Full implementation of the nChain Threshold Signature protocol as specified in Section 4 of the whitepaper.

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC
See:
  • Threshold-Signatures-whitepaper-nchain.pdf Section 4

Full implementation of the nChain Threshold Signature protocol as specified in Section 4 of the whitepaper.

src/core/taproot/control-block.js

Description:
  • Taproot control block implementation following BIP341

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Taproot control block implementation following BIP341

src/core/taproot/merkle-tree.js

Description:
  • Taproot merkle tree implementation for script path spending

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Taproot merkle tree implementation for script path spending

src/core/taproot/tapscript-interpreter.js

src/encoding/address/decode.js

Description:
  • Bitcoin address and key decoding utilities

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Bitcoin address and key decoding utilities

src/encoding/address/encode.js

Description:
  • Bitcoin address and key encoding utilities

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Bitcoin address and key encoding utilities

src/encoding/base32.js

Description:
  • Bech32/Bech32m encoding for Bitcoin SegWit and Taproot addresses

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Bech32/Bech32m encoding for Bitcoin SegWit and Taproot addresses

src/encoding/base58.js

Description:
  • Base58Check encoding implementation for Bitcoin

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Base58Check encoding implementation for Bitcoin

src/transaction/builder.js

Description:
  • Build and sign Bitcoin transactions (Legacy, SegWit, Taproot)

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Build and sign Bitcoin transactions (Legacy, SegWit, Taproot)

src/transaction/message-signing.js

Description:
  • Implements BIP322 message signing for all address types

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Implements BIP322 message signing for all address types

src/transaction/parser.js

Description:
  • Parse raw Bitcoin transaction hex back into structured objects

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Parse raw Bitcoin transaction hex back into structured objects

src/transaction/psbt.js

Description:
  • PSBT (Partially Signed Bitcoin Transaction) implementation

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

PSBT (Partially Signed Bitcoin Transaction) implementation

src/transaction/script-builder.js

Description:
  • Build and parse Bitcoin scripts for all address types

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Build and parse Bitcoin scripts for all address types

src/transaction/sighash.js

Description:
  • Implements BIP143 (SegWit) and BIP341 (Taproot) sighash algorithms

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Implements BIP143 (SegWit) and BIP341 (Taproot) sighash algorithms

src/transaction/utxo-manager.js

Description:
  • UTXO management with selection strategies

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

UTXO management with selection strategies

src/transaction/witness-builder.js

Description:
  • Build witness stacks for P2WPKH, P2WSH, P2TR key-path and script-path

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Build witness stacks for P2WPKH, P2WSH, P2TR key-path and script-path

src/utils/address-helpers.js

Description:
  • Address helper functions and utilities

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Address helper functions and utilities

src/utils/validation.js

Description:
  • Comprehensive validation utilities for Bitcoin operations

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Comprehensive validation utilities for Bitcoin operations

src/wallet/custodial.js

Description:
  • Full-featured HD wallet with all address types and transaction signing

Source:
Version:
  • 2.0.0
Author:
  • yfbsei
License:
  • ISC

Full-featured HD wallet with all address types and transaction signing

src/wallet/non-custodial.js

Description:
  • Implements a non-custodial wallet using the nChain TSS protocol for distributed key management and threshold signing.

Source:
Version:
  • 1.0.0
Author:
  • yfbsei
License:
  • ISC

Implements a non-custodial wallet using the nChain TSS protocol for distributed key management and threshold signing.