Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit 55cb9eb

Browse files
committed
Extract getnewaddress into separate function
1 parent cefcf20 commit 55cb9eb

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

scripts/src/docker/bitcoin.rs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,12 @@ async fn fund_new_account(endpoint: BitcoindHttpEndpoint) -> anyhow::Result<Acco
126126
pub async fn mine_a_block(endpoint: BitcoindHttpEndpoint) -> anyhow::Result<()> {
127127
let client = reqwest::Client::new();
128128

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?;
139130

140131
let _ = client
141132
.post(&endpoint.to_string())
142133
.basic_auth(USERNAME, Some(PASSWORD))
143-
.json(&GenerateToAddressRequest::new(1, new_address.result))
134+
.json(&GenerateToAddressRequest::new(1, new_address))
144135
.send()
145136
.await?;
146137

@@ -248,6 +239,13 @@ impl NewAddressRequest {
248239
}
249240
}
250241

242+
#[derive(Debug, serde::Deserialize)]
243+
struct NewAddressResponse {
244+
result: Option<Address>,
245+
error: Option<JsonRpcError>,
246+
id: String,
247+
}
248+
251249
#[derive(Debug, serde::Serialize)]
252250
pub struct GenerateToAddressRequest {
253251
jsonrpc: String,
@@ -304,13 +302,6 @@ struct FundResponse {
304302
id: String,
305303
}
306304

307-
#[derive(Debug, serde::Deserialize)]
308-
struct NewAddressResponse {
309-
result: Address,
310-
error: Option<JsonRpcError>,
311-
id: String,
312-
}
313-
314305
#[derive(Debug, serde::Deserialize, thiserror::Error)]
315306
#[error("JSON-RPC request failed with code {code}: {message}")]
316307
pub struct JsonRpcError {
@@ -321,21 +312,12 @@ pub struct JsonRpcError {
321312
async fn fund(endpoint: &str, address: Address, amount: Amount) -> anyhow::Result<sha256d::Hash> {
322313
let client = reqwest::Client::new();
323314

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?;
334316

335317
let _ = client
336318
.post(endpoint)
337319
.basic_auth(USERNAME, Some(PASSWORD))
338-
.json(&GenerateToAddressRequest::new(101, new_address.result))
320+
.json(&GenerateToAddressRequest::new(101, new_address))
339321
.send()
340322
.await
341323
.context("failed to generate blocks")?;
@@ -360,6 +342,30 @@ async fn fund(endpoint: &str, address: Address, amount: Amount) -> anyhow::Resul
360342
}
361343
}
362344

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+
363369
fn derive_address(secret_key: secp256k1::SecretKey) -> Address {
364370
let public_key =
365371
secp256k1::PublicKey::from_secret_key(&secp256k1::Secp256k1::new(), &secret_key);

0 commit comments

Comments
 (0)