-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
223880
committed
Apr 2, 2024
1 parent
e86f204
commit 252f13b
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import * as bitcoin from 'bitcoinjs-lib'; | ||
|
||
interface UTXO { | ||
txid: string; | ||
index: number; | ||
txout: bitcoin.TxOut; | ||
address: bitcoin.Address; | ||
} | ||
|
||
interface Wallet { | ||
utxos: UTXO[]; | ||
privateKeys: Record<string, bitcoin.ECPair.ECPairInterface>; | ||
} | ||
|
||
function addUTXO(wallet: Wallet, txid: string, index: number, txout: bitcoin.TxOut, address: bitcoin.Address) { | ||
wallet.utxos.push({ txid, index, txout, address }); | ||
} | ||
|
||
function removeUTXO(wallet: Wallet, txid: string) { | ||
wallet.utxos = wallet.utxos.filter(utxo => utxo.txid !== txid); | ||
} | ||
|
||
function selectUTXOs(wallet: Wallet, amount: number): UTXO[] { | ||
let selectedUTXOs: UTXO[] = []; | ||
let totalAmount = 0; | ||
|
||
for (const utxo of wallet.utxos) { | ||
totalAmount += utxo.txout.value; | ||
|
||
selectedUTXOs.push(utxo); | ||
|
||
if (totalAmount >= amount) { | ||
break; | ||
} | ||
} | ||
|
||
return selectedUTXOs; | ||
} | ||
|
||
function createTransaction(selectedUTXOs: UTXO[], outputs: { address: bitcoin.Address; amount: number }[]): bitcoin.Transaction { | ||
const txb = new bitcoin.TransactionBuilder(); | ||
for (const utxo of selectedUTXOs) { | ||
txb.addInput(utxo.txid, utxo.index); | ||
} | ||
|
||
for (const { address, amount } of outputs) { | ||
txb.addOutput(address, amount); | ||
} | ||
|
||
return txb.buildIncomplete(); | ||
} | ||
|
||
function signTransaction(wallet: Wallet, transaction: bitcoin.Transaction, selectedUTXOs: UTXO[]) { | ||
for (let i = 0; i < selectedUTXOs.length; i++) { | ||
const utxo = selectedUTXOs[i]; | ||
const key = wallet.privateKeys[utxo.address.toString()]; | ||
transaction.sign(i, key); | ||
} | ||
} | ||
|
||
// Example usage | ||
const wallet: Wallet = { | ||
utxos: [], | ||
privateKeys: {} | ||
}; | ||
|
||
// Add UTXOs to the wallet | ||
// addUTXO(wallet, ...); | ||
|
||
// Specify the amount and recipient address | ||
const amount = 10000000; // satoshis | ||
const recipientAddress = 'recipient_address'; | ||
|
||
// Select UTXOs for the transaction | ||
const selectedUTXOs = selectUTXOs(wallet, amount); | ||
|
||
// Create transaction outputs | ||
const outputs = [{ address: recipientAddress, amount }]; | ||
|
||
// Create the transaction | ||
const transaction = createTransaction(selectedUTXOs, outputs); | ||
|
||
// Sign the transaction | ||
signTransaction(wallet, transaction, selectedUTXOs); | ||
|
||
// Broadcast the transaction | ||
// broadcastTransaction(transaction); |
Empty file.