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

Commit cefcf20

Browse files
committed
Print wallet descriptor to env file
1 parent e837d8d commit cefcf20

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

scripts/src/docker/bitcoin.rs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,14 @@ pub async fn mine_a_block(endpoint: BitcoindHttpEndpoint) -> anyhow::Result<()>
147147
Ok(())
148148
}
149149

150-
pub struct DerivationPath {
151-
path: Vec<ChildNumber>,
152-
}
150+
#[derive(Debug, Clone)]
151+
pub struct DerivationPath(Vec<ChildNumber>);
153152

154153
impl Display for DerivationPath {
155154
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
156-
for i in 0..self.path.len() {
157-
let elem = self.path.get(i).unwrap();
158-
159-
let separator = if i == self.path.len() - 1 { "" } else { "/" };
160-
161-
match elem {
162-
ChildNumber::Normal { index } => {
163-
write!(f, "{:?}{:0}", index, separator)?;
164-
}
165-
ChildNumber::Hardened { index } => {
166-
write!(f, "{:?}h{:0}", index, separator)?;
167-
}
168-
}
155+
for i in &self.0 {
156+
write!(f, "/")?;
157+
fmt::Display::fmt(i, f)?;
169158
}
170159

171160
Ok(())
@@ -174,22 +163,21 @@ impl Display for DerivationPath {
174163

175164
impl DerivationPath {
176165
pub fn bip44_bitcoin_testnet() -> anyhow::Result<Self> {
177-
Ok(Self {
178-
path: vec![
179-
ChildNumber::from_hardened_idx(44)?,
180-
ChildNumber::from_hardened_idx(1)?,
181-
ChildNumber::from_hardened_idx(0)?,
182-
ChildNumber::from_normal_idx(0)?,
183-
ChildNumber::from_normal_idx(0)?,
184-
],
185-
})
166+
Ok(Self(vec![
167+
ChildNumber::from_hardened_idx(44)?,
168+
ChildNumber::from_hardened_idx(1)?,
169+
ChildNumber::from_hardened_idx(0)?,
170+
ChildNumber::from_normal_idx(0)?,
171+
ChildNumber::from_normal_idx(0)?,
172+
]))
186173
}
187174
}
188175

189-
#[derive(Clone)]
176+
#[derive(Debug, Clone)]
190177
pub struct Account {
191178
pub master: ExtendedPrivKey,
192179
first_account: rust_bitcoin::util::key::PrivateKey,
180+
derivation_path: DerivationPath,
193181
}
194182

195183
impl Account {
@@ -206,13 +194,14 @@ impl Account {
206194

207195
// derive a private key from the master key
208196
let priv_key = master
209-
.derive_priv(&Secp256k1::new(), &derivation_path.path)
197+
.derive_priv(&Secp256k1::new(), &derivation_path.0)
210198
.map(|secret_key| secret_key.private_key)?;
211199

212200
// it is not great to store derived data in here but since the derivation can fail, it is better to fail early instead of later
213201
Ok(Self {
214202
master,
215203
first_account: priv_key,
204+
derivation_path,
216205
})
217206
}
218207

@@ -224,6 +213,17 @@ impl Account {
224213
}
225214
}
226215

216+
impl Display for Account {
217+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
218+
write!(f, "wpkh(")?;
219+
fmt::Display::fmt(&self.master, f)?;
220+
fmt::Display::fmt(&self.derivation_path, f)?;
221+
write!(f, ")")?;
222+
223+
Ok(())
224+
}
225+
}
226+
227227
#[derive(Debug, serde::Serialize)]
228228
pub struct NewAddressRequest {
229229
jsonrpc: String,
@@ -386,7 +386,23 @@ mod tests {
386386
let derivation_path = DerivationPath::bip44_bitcoin_testnet().unwrap();
387387

388388
let to_string = derivation_path.to_string();
389-
assert_eq!(to_string, "44h/1h/0h/0/0")
389+
assert_eq!(to_string, "/44'/1'/0'/0/0")
390+
}
391+
392+
#[test]
393+
fn format_account() {
394+
let account = Account::new_random().unwrap();
395+
let master = account.master;
396+
397+
let to_string = account.to_string();
398+
assert_eq!(
399+
to_string,
400+
format!(
401+
"wpkh({}{})",
402+
master,
403+
DerivationPath::bip44_bitcoin_testnet().unwrap()
404+
)
405+
)
390406
}
391407

392408
#[test]

scripts/src/env/start.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Context;
22
use envfile::EnvFile;
33

4-
use crate::docker::bitcoin::{DerivationPath, PASSWORD, USERNAME};
4+
use crate::docker::bitcoin::{PASSWORD, USERNAME};
55
use crate::{
66
docker::{
77
self,
@@ -68,24 +68,12 @@ pub async fn execute() -> anyhow::Result<Environment> {
6868
);
6969
envfile.update("ETHEREUM_NODE_HTTP_URL", &parity.http_endpoint.to_string());
7070

71-
envfile.update(
72-
"BITCOIN_HD_KEY_0",
73-
&format!("{}", bitcoind.account_0.master),
74-
);
75-
envfile.update(
76-
"BITCOIN_HD_KEY_1",
77-
&format!("{}", bitcoind.account_1.master),
78-
);
71+
envfile.update("BITCOIN_WALLET_0", &bitcoind.account_0.to_string());
72+
envfile.update("BITCOIN_WALLET_1", &bitcoind.account_1.to_string());
7973
envfile.update("BITCOIN_P2P_URI", &bitcoind.p2p_uri.to_string());
8074
envfile.update("BITCOIN_HTTP_URI", &bitcoind.http_endpoint.to_string());
8175
envfile.update("BITCOIN_USERNAME", USERNAME);
8276
envfile.update("BITCOIN_PASSWORD", PASSWORD);
83-
envfile.update(
84-
"BITCOIN_DERIVATION_PATH",
85-
&*DerivationPath::bip44_bitcoin_testnet()
86-
.expect("can create derivation path")
87-
.to_string(),
88-
);
8977

9078
envfile.update("HTTP_URL_CND_0", &cnd_0.http_endpoint.to_string());
9179
envfile.update("HTTP_URL_CND_1", &cnd_1.http_endpoint.to_string());

0 commit comments

Comments
 (0)