@@ -60,34 +60,6 @@ function selectUTXOs(wallet: Wallet, amount: number): UTXO[] {
60
60
return selectedUTXOs ;
61
61
}
62
62
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
-
91
63
// Sign the transaction
92
64
function signTransaction (
93
65
wallet : Wallet ,
@@ -139,15 +111,79 @@ try {
139
111
const outputs = [ { address : recipientAddress , value : amount } ] ;
140
112
141
113
// Create and sign transaction
142
- const psbt = createTransaction ( selectedUTXOs , outputs ) ;
114
+ const psbt = createTransaction ( selectedUTXOs , outputs , address ! ) ;
143
115
const signedTx = signTransaction ( wallet , psbt , selectedUTXOs ) ;
144
116
145
117
// Get the transaction hex
146
118
const txHex = signedTx . toHex ( ) ;
147
119
console . log ( 'Transaction hex:' , txHex ) ;
148
120
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
+
149
185
// Note: To broadcast, you'd need to use a Bitcoin node API or service
150
186
// await broadcastTransaction(txHex);
151
187
} catch ( error ) {
152
188
console . error ( 'Transaction failed:' , error ) ;
153
- }
189
+ } console . error ( 'Transaction failed:' , Error ) ;
0 commit comments