@@ -126,21 +126,12 @@ async fn fund_new_account(endpoint: BitcoindHttpEndpoint) -> anyhow::Result<Acco
126
126
pub async fn mine_a_block ( endpoint : BitcoindHttpEndpoint ) -> anyhow:: Result < ( ) > {
127
127
let client = reqwest:: Client :: new ( ) ;
128
128
129
- let new_address: NewAddressResponse = client
130
- . post ( & endpoint. to_string ( ) )
131
- . basic_auth ( USERNAME , Some ( PASSWORD ) )
132
- . json ( & NewAddressRequest :: new ( "bech32" ) )
133
- . send ( )
134
- . await
135
- . context ( "failed to create new address" ) ?
136
- . json :: < NewAddressResponse > ( )
137
- . await ?;
138
- assert ! ( new_address. error. is_none( ) ) ;
129
+ let new_address = new_address ( & endpoint. to_string ( ) ) . await ?;
139
130
140
131
let _ = client
141
132
. post ( & endpoint. to_string ( ) )
142
133
. basic_auth ( USERNAME , Some ( PASSWORD ) )
143
- . json ( & GenerateToAddressRequest :: new ( 1 , new_address. result ) )
134
+ . json ( & GenerateToAddressRequest :: new ( 1 , new_address) )
144
135
. send ( )
145
136
. await ?;
146
137
@@ -248,6 +239,13 @@ impl NewAddressRequest {
248
239
}
249
240
}
250
241
242
+ #[ derive( Debug , serde:: Deserialize ) ]
243
+ struct NewAddressResponse {
244
+ result : Option < Address > ,
245
+ error : Option < JsonRpcError > ,
246
+ id : String ,
247
+ }
248
+
251
249
#[ derive( Debug , serde:: Serialize ) ]
252
250
pub struct GenerateToAddressRequest {
253
251
jsonrpc : String ,
@@ -304,13 +302,6 @@ struct FundResponse {
304
302
id : String ,
305
303
}
306
304
307
- #[ derive( Debug , serde:: Deserialize ) ]
308
- struct NewAddressResponse {
309
- result : Address ,
310
- error : Option < JsonRpcError > ,
311
- id : String ,
312
- }
313
-
314
305
#[ derive( Debug , serde:: Deserialize , thiserror:: Error ) ]
315
306
#[ error( "JSON-RPC request failed with code {code}: {message}" ) ]
316
307
pub struct JsonRpcError {
@@ -321,21 +312,12 @@ pub struct JsonRpcError {
321
312
async fn fund ( endpoint : & str , address : Address , amount : Amount ) -> anyhow:: Result < sha256d:: Hash > {
322
313
let client = reqwest:: Client :: new ( ) ;
323
314
324
- let new_address = client
325
- . post ( endpoint)
326
- . basic_auth ( USERNAME , Some ( PASSWORD ) )
327
- . json ( & NewAddressRequest :: new ( "bech32" ) )
328
- . send ( )
329
- . await
330
- . context ( "failed to create new address" ) ?
331
- . json :: < NewAddressResponse > ( )
332
- . await ?;
333
- assert ! ( new_address. error. is_none( ) ) ;
315
+ let new_address = new_address ( endpoint) . await ?;
334
316
335
317
let _ = client
336
318
. post ( endpoint)
337
319
. basic_auth ( USERNAME , Some ( PASSWORD ) )
338
- . json ( & GenerateToAddressRequest :: new ( 101 , new_address. result ) )
320
+ . json ( & GenerateToAddressRequest :: new ( 101 , new_address) )
339
321
. send ( )
340
322
. await
341
323
. context ( "failed to generate blocks" ) ?;
@@ -360,6 +342,30 @@ async fn fund(endpoint: &str, address: Address, amount: Amount) -> anyhow::Resul
360
342
}
361
343
}
362
344
345
+ async fn new_address ( endpoint : & str ) -> anyhow:: Result < Address > {
346
+ let client = reqwest:: Client :: new ( ) ;
347
+
348
+ let response = client
349
+ . post ( endpoint)
350
+ . basic_auth ( USERNAME , Some ( PASSWORD ) )
351
+ . json ( & NewAddressRequest :: new ( "bech32" ) )
352
+ . send ( )
353
+ . await
354
+ . context ( "failed to create new address" ) ?
355
+ . json :: < NewAddressResponse > ( )
356
+ . await ?;
357
+
358
+ match response. error {
359
+ None => match response. result {
360
+ None => Err ( anyhow:: Error :: msg (
361
+ "no address returned without yielding error" ,
362
+ ) ) ,
363
+ Some ( address) => Ok ( address) ,
364
+ } ,
365
+ Some ( error) => Err ( anyhow:: Error :: new ( error) ) ,
366
+ }
367
+ }
368
+
363
369
fn derive_address ( secret_key : secp256k1:: SecretKey ) -> Address {
364
370
let public_key =
365
371
secp256k1:: PublicKey :: from_secret_key ( & secp256k1:: Secp256k1 :: new ( ) , & secret_key) ;
0 commit comments