Skip to content

Latest commit

 

History

History
127 lines (90 loc) · 4.17 KB

readme.md

File metadata and controls

127 lines (90 loc) · 4.17 KB

Bitcoin Multisig with bitcoinjs-lib

This repository provides an implementation of Bitcoin Multisignature (Multisig) using the bitcoinjs-lib library. Multisig allows multiple parties to create a Bitcoin wallet where more than one signature is required to authorize a transaction.

Features

  • Multisig Wallet Creation: Easily create a Bitcoin multisig wallet.
  • Transaction Creation and Signing: Create and sign multisig transactions.
  • Compatibility: Compatible with Bitcoin mainnet, testnet, and custom networks.

Requirements

  • Node.js: Version 12 or later.
  • bitcoinjs-lib: A JavaScript library for Bitcoin.

Installation

To get started, clone this repository and install the required dependencies.

git clone https://github.com/yourusername/bitcoin-multisig-private.git
cd bitcoin-multisig-private
npm install

How to Use

1. Install Dependencies

Make sure you have bitcoinjs-lib installed:

npm install bitcoinjs-lib

2. Create a Multisig Address

To create a multisig address, you will need to provide the public keys and the number of signatures required. Here's an example of creating a 2-of-3 multisig address.

const bitcoin = require('bitcoinjs-lib');

// Replace with actual public keys
const pubKeys = [
  'yourPublicKey1',
  'yourPublicKey2',
  'yourPublicKey3'
].map((key) => Buffer.from(key, 'hex'));

// 2-of-3 Multisig
const { address } = bitcoin.payments.p2ms({
  m: 2, // Minimum number of signatures required
  pubkeys: pubKeys,
});

console.log(`Multisig Address: ${address}`);

3. Create a Multisig Transaction

Once the multisig address is created, you can create a transaction by providing inputs and outputs. Here’s an example of creating and signing a transaction for a multisig address:

const bitcoin = require('bitcoinjs-lib');

// Define your multisig wallet details
const pubKeys = [
  'yourPublicKey1',
  'yourPublicKey2',
  'yourPublicKey3'
].map((key) => Buffer.from(key, 'hex'));

// Define the private keys for signing the transaction
const privateKeys = [
  'yourPrivateKey1',
  'yourPrivateKey2',
  'yourPrivateKey3'
].map((key) => bitcoin.ECPair.fromWIF(key));

// Example of an unspent output (UTXO) to spend
const txb = new bitcoin.TransactionBuilder();
txb.addInput('txid', 0); // Replace with actual txid and index
txb.addOutput('destinationAddress', 100000); // Replace with the actual output address and amount

// Sign the transaction with the private keys
privateKeys.forEach((key, index) => {
  txb.sign(0, key); // Replace 0 with the correct index if necessary
});

const tx = txb.build();
console.log(`Signed Transaction: ${tx.toHex()}`);

4. Broadcast the Transaction

After signing the transaction, you can broadcast it to the Bitcoin network using a service like Blockstream's Esplora or any other Bitcoin node.

# Using Blockstream's Esplora API
curl -X POST -d '{"tx":"yourSignedTxHex"}' https://blockstream.info/api/tx

Testing

To test this locally, you can use the Bitcoin Testnet instead of the main Bitcoin network. Just change the network in bitcoinjs-lib as follows:

const network = bitcoin.networks.testnet; // Use testnet instead of mainnet

Contributing

We welcome contributions! If you'd like to contribute to this project, please fork the repository and submit a pull request.

  • Fork this repository
  • Create a branch (git checkout -b feature-xyz)
  • Make your changes
  • Commit your changes (git commit -am 'Add new feature')
  • Push to the branch (git push origin feature-xyz)
  • Open a pull request

Connect With Me:

Mail Badge Telegram Badge Skype Badge Discord Badge