This repository provides Bitgesell blockchain support for the BitcoinJS library.
No need to fork or modify bitcoinjs-lib
β just install and use this bitgesell-bitcoinjs package!
- Node.js >= 14
- bitcoinjs-lib
6.Ρ
version - ecpair
- bip32
- bip39
- @bitcoinerlab/secp256k1
Install dependencies:
npm install bitgesell-bitcoinjs bitcoinjs-lib@^6 ecpair bip32 bip39 @bitcoinerlab/secp256k1
IMPORTANT! Always import the Bitgesell Mainnet network configuration from this bitgesell-bitcoinjs package and pass it as the network parameter where required.
import { BITGESELL_MAINNET } from 'bitgesell-bitcoinjs';
- Generate random wallet
- Import wallet from WIF
- Generate wallet from mnemonic (BIP39 + BIP84)
- Validate address
- Sign a SegWit transaction (P2WPKH)
!!! ALL EXAMPLES ARE IN "examples" DIRECTORY !!!
To run examples:
npm ci
node examples
import { BITGESELL_MAINNET } from 'bitgesell-bitcoinjs';
import { payments as Payments, address as Address, Psbt } from 'bitcoinjs-lib';
import ECPairFactory from 'ecpair';
import ecc from '@bitcoinerlab/secp256k1';
import { randomBytes } from 'crypto';
import BIP32Factory from 'bip32';
import * as bip39 from 'bip39';
const ECPair = ECPairFactory(ecc);
const rng = (size) => randomBytes(size);
const keyPair = ECPair.makeRandom({ network: BITGESELL_MAINNET, rng });
const paymentsRandom = Payments.p2wpkh({
network: BITGESELL_MAINNET,
pubkey: Buffer.from(keyPair.publicKey),
});
console.info('public address:', paymentsRandom.address);
const ECPair = ECPairFactory(ecc);
const keyPair2 = ECPair.fromWIF('L2ScZnHCser7xN1FsJtc4eR1icDtmwJfqgK3q2XUwb3nmbMEDYkw', BITGESELL_MAINNET);
const paymentFromWif = Payments.p2wpkh({
network: BITGESELL_MAINNET,
pubkey: Buffer.from(keyPair2.publicKey),
});
console.info('public address from wif:', paymentFromWif.address);
const mnemonic = bip39.generateMnemonic(256);
console.info('seed:', mnemonic);
const seed = bip39.mnemonicToSeedSync(mnemonic);
const bip32 = BIP32Factory(ecc);
const root = bip32.fromSeed(seed, BITGESELL_MAINNET);
const path = 'm/84\'/0\'/0\'/0/0';
const child = root.derivePath(path);
const paymentsFromSeed = Payments.p2wpkh({
network: BITGESELL_MAINNET,
pubkey: Buffer.from(child.publicKey),
});
console.info('public address from seed:', paymentsFromSeed.address);
const isValidAddress = (address) => {
try {
Address.toOutputScript(address, BITGESELL_MAINNET);
return true;
} catch (_) {
return false;
}
}
isValidAddress(paymentsFromSeed.address) ? console.info('valid address') : console.info('invalid address');
const psbt = new Psbt({ network: BITGESELL_MAINNET });
const utxoValue = 100000000;
psbt.addInput({
hash: '3d2e4113a0a2bb94aed0eef1e7a21a8d6dbda399b048330cb55853befe82a8ed',
index: 0,
witnessUtxo: {
script: paymentFromWif.output,
value: utxoValue,
},
});
psbt.addOutput({
address: 'bgl1q8j64v07nhzgs4rwyrv664zd4vx70xk7fe25npf',
value: utxoValue - 10000,
});
psbt.signInput(0, keyPair2);
psbt.finalizeAllInputs();
const rawTx = psbt.extractTransaction().toHex();
console.info('signed transaction hex:', rawTx);