Skip to content

Commit 33be159

Browse files
committed
CC to production
1 parent c4cacda commit 33be159

File tree

1 file changed

+66
-30
lines changed

1 file changed

+66
-30
lines changed

src/app/bitcoin/CoinCointrol.ts

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,6 @@ function selectUTXOs(wallet: Wallet, amount: number): UTXO[] {
6060
return selectedUTXOs;
6161
}
6262

63-
// Create a transaction
64-
function createTransaction(
65-
selectedUTXOs: UTXO[],
66-
outputs: { address: string; value: number }[],
67-
network: bitcoin.Network = bitcoin.networks.bitcoin
68-
): bitcoin.Psbt {
69-
const psbt = new bitcoin.Psbt({ network });
70-
71-
// Add inputs
72-
for (const utxo of selectedUTXOs) {
73-
psbt.addInput({
74-
hash: utxo.txid,
75-
index: utxo.vout,
76-
nonWitnessUtxo: Buffer.from(utxo.scriptPubKey), // Simplified for example
77-
});
78-
}
79-
80-
// Add outputs
81-
for (const output of outputs) {
82-
psbt.addOutput({
83-
address: output.address,
84-
value: output.value,
85-
});
86-
}
87-
88-
return psbt;
89-
}
90-
9163
// Sign the transaction
9264
function signTransaction(
9365
wallet: Wallet,
@@ -139,15 +111,79 @@ try {
139111
const outputs = [{ address: recipientAddress, value: amount }];
140112

141113
// Create and sign transaction
142-
const psbt = createTransaction(selectedUTXOs, outputs);
114+
const psbt = createTransaction(selectedUTXOs, outputs, address!);
143115
const signedTx = signTransaction(wallet, psbt, selectedUTXOs);
144116

145117
// Get the transaction hex
146118
const txHex = signedTx.toHex();
147119
console.log('Transaction hex:', txHex);
148120

121+
// Add this function to estimate fees (very basic example)
122+
function estimateFee(inputs: number, outputs: number): number {
123+
const byteSize = (inputs * 148) + (outputs * 34) + 10; // Rough estimate
124+
const satPerByte = 20; // Adjust based on network conditions
125+
return byteSize * satPerByte;
126+
}
127+
128+
// Modified createTransaction function with fees and change
129+
function createTransaction(
130+
selectedUTXOs: UTXO[],
131+
outputs: { address: string; value: number }[],
132+
changeAddress: string,
133+
network: bitcoin.Network = bitcoin.networks.bitcoin
134+
): bitcoin.Psbt {
135+
const psbt = new bitcoin.Psbt({ network });
136+
137+
// Add inputs
138+
for (const utxo of selectedUTXOs) {
139+
psbt.addInput({
140+
hash: utxo.txid,
141+
index: utxo.vout,
142+
nonWitnessUtxo: Buffer.from(utxo.scriptPubKey),
143+
});
144+
}
145+
146+
// Calculate total input amount
147+
const totalInput = selectedUTXOs.reduce((sum, utxo) => sum + utxo.value, 0);
148+
const totalOutput = outputs.reduce((sum, output) => sum + output.value, 0);
149+
const fee = estimateFee(selectedUTXOs.length, outputs.length + 1); // +1 for change
150+
151+
// Add outputs
152+
for (const output of outputs) {
153+
psbt.addOutput({
154+
address: output.address,
155+
value: output.value,
156+
});
157+
}
158+
159+
// Add change output if necessary
160+
const change = totalInput - totalOutput - fee;
161+
if (change > 0) {
162+
psbt.addOutput({
163+
address: changeAddress,
164+
value: change,
165+
});
166+
} else if (change < 0) {
167+
throw new Error('Insufficient funds including fees');
168+
}
169+
170+
return psbt;
171+
}
172+
173+
// Updated example usage
174+
try {
175+
const selectedUTXOs = selectUTXOs(wallet, amount + estimateFee(1, 2));
176+
const outputs = [{ address: recipientAddress, value: amount }];
177+
const changeAddress = address!; // Using the same address for simplicity
178+
const psbt = createTransaction(selectedUTXOs, outputs, changeAddress);
179+
const signedTx = signTransaction(wallet, psbt, selectedUTXOs);
180+
console.log('Transaction hex:', signedTx.toHex());
181+
} catch (error) {
182+
console.error('Transaction failed:', error);
183+
}
184+
149185
// Note: To broadcast, you'd need to use a Bitcoin node API or service
150186
// await broadcastTransaction(txHex);
151187
} catch (error) {
152188
console.error('Transaction failed:', error);
153-
}
189+
} console.error('Transaction failed:', Error);

0 commit comments

Comments
 (0)