diff --git a/src/app/CoinCointrol.ts b/src/app/CoinCointrol.ts new file mode 100644 index 00000000..1b000868 --- /dev/null +++ b/src/app/CoinCointrol.ts @@ -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; +} + +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); diff --git a/src/app/bitcoin b/src/app/bitcoin new file mode 100644 index 00000000..e69de29b