Get started with bitcoinjs-lib CDN
MIT licensed
BitcoinJS-lib is a popular library for working with Bitcoin transactions and blockchain data.
Tags:- bitcoin
- browser
- client
- library
Stable version
Copied!
How to start using bitcoinjs-lib CDN
// Include the CDN script
const script = document.createElement('script');
script.src = 'https://cdn.cdnhub.io/bitcoinjs-lib/0.2.0-1/bitcoinjs-min.js';
document.head.appendChild(script);
// Wait for the library to load
window.onBitcoinjsLoaded = () => {
// Create a new Bitcoin wallet
const mnemonic = "treat dwarf stitch apple"; // 12-word mnemonic
const network = new bitcoin.networks.BitcoinCash();
const hdNode = bitcoin.HDNode.fromMnemonic(mnemonic, network);
const receivingAddress = hdNode.getAddressAtPath("m/44'/0'/0'/0/0");
// Create a new transaction
const tx = new bitcoin.TransactionBuilder(network);
const changeAddress = hdNode.getAddressAtPath("m/44'/0'/0'/0/1");
const changeAmount = 1000; // Satoshis
// Add an input with the receiving address and a test amount
tx.addInput(new bitcoin.TransactionInput({
prevOut: null,
script: bitcoin.script.empty(),
sequence: 0,
address: receivingAddress.toString(),
satoshis: 5000 // Test amount in Satoshis
}));
// Set the output with the change address and the change amount
tx.addOutput(new bitcoin.output.PayToPubKeyHash({
address: changeAddress.toString(),
value: changeAmount
}));
// Sign the transaction with the private key
const privateKey = hdNode.derivePath("m/44'/0'/0'/0/0").privateKey;
tx.sign(0, privateKey);
// Send the transaction to the Bitcoin Cash network
const broadcastTransaction = new bitcoin.RPCClient('http://localhost:8332'); // Replace with your RPC endpoint
broadcastTransaction.sendTransaction(tx.toHex(), (err, txid) => {
if (err) {
console.error(err);
} else {
console.log("Transaction broadcasted with ID: " + txid);
}
});
};
Copied!