Skip to content

Commit f92e8ca

Browse files
committed
tmp
1 parent cb49166 commit f92e8ca

6 files changed

Lines changed: 83 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bitcoin/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ light-client = []
1313

1414
[dependencies]
1515
thiserror = "1.0"
16-
bitcoincore-rpc = { git = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc", rev = "7bd815f1e1ae721404719ee8e6867064b7c68494" }
16+
bitcoincore-rpc = { path = "/home/sander/workspace/rust-bitcoincore-rpc/client/" }
1717
hex = "0.4.2"
1818
async-trait = "0.1.40"
1919
tokio = { version = "1.0", features = ["full"] }
@@ -36,9 +36,9 @@ serde_json = "1.0.82"
3636

3737
# Substrate dependencies
3838
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
39+
regex = "1.4.3"
3940

4041
[dev-dependencies]
4142
mockall = "0.8.1"
42-
regex = "1.4.3"
4343
rand = "0.7"
4444
serial_test = "*"

bitcoin/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ pub enum Error {
6161
TransactionSigningError,
6262
#[error("Failed to obtain public key")]
6363
MissingPublicKey,
64+
#[error("getaddressinfo did not return a derivation path")]
65+
MissingDerivationPath,
6466
#[error("Failed to connect")]
6567
ConnectionRefused,
6668
#[error("Wallet not found")]

bitcoin/src/lib.rs

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ mod iter;
1313

1414
use async_trait::async_trait;
1515
use backoff::{backoff::Backoff, future::retry, ExponentialBackoff};
16-
use bitcoincore_rpc::{
17-
bitcoin::consensus::encode::serialize_hex,
18-
bitcoincore_rpc_json::{GetDescriptorInfoResult, ImportDescriptors, ScanningDetails, Timestamp},
19-
};
2016
pub use bitcoincore_rpc::{
2117
bitcoin as bitcoin_primitives,
2218
bitcoin::{
@@ -47,7 +43,10 @@ pub use bitcoincore_rpc::{
4743
jsonrpc::{self, error::RpcError, Error as JsonRpcError},
4844
Auth, Client, Error as BitcoinError, RpcApi,
4945
};
50-
use bitcoincore_rpc::{bitcoin::consensus::encode::serialize_hex, bitcoincore_rpc_json::ScanningDetails};
46+
use bitcoincore_rpc::{
47+
bitcoin::consensus::encode::serialize_hex,
48+
bitcoincore_rpc_json::{GetDescriptorInfoResult, ImportDescriptors, ScanningDetails, Timestamp},
49+
};
5150
pub use electrs::{ElectrsClient, Error as ElectrsError};
5251
pub use error::{BitcoinRpcError, ConversionError, Error};
5352
pub use iter::{reverse_stream_transactions, stream_blocks, stream_in_chain_transactions};
@@ -832,21 +831,84 @@ impl BitcoinCoreApi for BitcoinCore {
832831
}
833832

834833
fn dump_private_key(&self, address: &Address) -> Result<PrivateKey, Error> {
835-
Ok(self.rpc.dump_private_key(address)?)
834+
let address_info = self.rpc.get_address_info(&address).unwrap();
835+
dbg!(&address_info);
836+
let derivation_path = address_info.hd_key_path.ok_or(Error::MissingDerivationPath)?;
837+
let result = self.rpc.list_descriptors(true)?;
838+
839+
for descriptor in result.descriptors {
840+
let range = descriptor
841+
.range
842+
.map(|[start, end]| [start, end.max(descriptor.next.unwrap_or(end))]);
843+
844+
for generated_address in self.rpc.derive_addresses(&descriptor.desc, range)? {
845+
if address == &generated_address.assume_checked() {
846+
info!("Found private address! {}", descriptor.desc);
847+
848+
let re = regex::Regex::new(r".*[(]([tx]prv[a-zA-Z0-9]+)").expect("Regex is known good");
849+
if let Some(caps) = re.captures(&descriptor.desc) {
850+
println!("private key = {}", &caps[1]);
851+
// assert_eq!("", &caps[1]);
852+
let key = bitcoincore_rpc::bitcoin::bip32::ExtendedPrivKey::from_str(&caps[1]).unwrap();
853+
let secp = bitcoincore_rpc::bitcoin::key::Secp256k1::new();
854+
let private_key = key.derive_priv(&secp, &derivation_path).unwrap().to_priv();
855+
warn!(
856+
"wif: {}",
857+
private_key.to_wif()
858+
);
859+
860+
return Ok(private_key);
861+
}
862+
863+
864+
865+
// wif
866+
let re = regex::Regex::new(r".*[(]([LKc][a-zA-Z0-9]{51})[)]").expect("Regex is known good");
867+
if let Some(caps) = re.captures(&descriptor.desc) {
868+
println!("wif = {}", &caps[1]);
869+
assert!(derivation_path.is_master());
870+
let private_key = PrivateKey::from_wif(&caps[1]).unwrap();
871+
return Ok(private_key);
872+
}
873+
}
874+
}
875+
}
876+
877+
// let wallet_pubkey = address_info.pubkey.ok_or(Error::MissingPublicKey)?;
878+
879+
panic!()
836880
}
837881

838882
fn import_private_key(&self, private_key: &PrivateKey, is_derivation_key: bool) -> Result<(), Error> {
839-
Ok(self.rpc.import_private_key(
840-
private_key,
841-
is_derivation_key.then_some(DERIVATION_KEY_LABEL),
842-
Some(false),
843-
)?)
883+
let wif = private_key.to_wif();
884+
let info = self.rpc.get_descriptor_info(&format!("wpkh({wif})"))?;
885+
let checksum = info.checksum.unwrap();
886+
let req = ImportDescriptors {
887+
descriptor: format!("wpkh({wif})#{checksum}"),
888+
timestamp: Timestamp::Now,
889+
active: Some(false),
890+
range: None,
891+
next_index: None,
892+
internal: None,
893+
label: is_derivation_key.then_some(DERIVATION_KEY_LABEL.to_owned()),
894+
};
895+
let result = self.rpc.import_descriptors(req)?;
896+
assert_eq!(result.len(), 1);
897+
assert!(result[0].success);
898+
899+
Ok(())
900+
// assert!(resu
901+
// Ok(self.rpc.import_private_key(
902+
// private_key,
903+
// is_derivation_key.then_some(DERIVATION_KEY_LABEL),
904+
// Some(false),
905+
// )?)
844906
}
845907

846908
/// Derive and import the private key for the master public key and public secret
847909
async fn add_new_deposit_key(&self, public_key: PublicKey, secret_key: Vec<u8>) -> Result<(), Error> {
848910
let address = Address::p2wpkh(&public_key, self.network).map_err(ConversionError::from)?;
849-
let private_key = self.rpc.dump_private_key(&address)?;
911+
let private_key = self.dump_private_key(&address)?;
850912
let deposit_secret_key =
851913
addr::calculate_deposit_secret_key(private_key.inner, SecretKey::from_slice(&secret_key)?)?;
852914
self.import_private_key(

runtime/src/rpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ compile_error!("Tests are only supported for the kintsugi runtime");
5555

5656
cfg_if::cfg_if! {
5757
if #[cfg(feature = "parachain-metadata-interlay")] {
58-
const DEFAULT_SPEC_VERSION: Range<u32> = 1025000..1026000;
58+
const DEFAULT_SPEC_VERSION: Range<u32> = 1024000..1025000;
5959
pub const DEFAULT_SPEC_NAME: &str = "interlay-parachain";
6060
pub const SS58_PREFIX: u16 = 2032;
6161
} else if #[cfg(feature = "parachain-metadata-kintsugi")] {

vault/src/system.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use runtime::{
2323
PrettyPrint, RegisterVaultEvent, StoreMainChainHeaderEvent, TryFromSymbol, UpdateActiveBlockEvent, UtilFuncs,
2424
VaultCurrencyPair, VaultId, VaultRegistryPallet,
2525
};
26-
use std::{collections::HashMap, pin::Pin, sync::Arc, time::Duration};
26+
use std::{collections::HashMap, pin::Pin, sync::Arc, time::Duration, str::FromStr};
2727
use tokio::{sync::RwLock, time::sleep};
2828

2929
pub const VERSION: &str = git_version!(args = ["--tags"]);
@@ -651,6 +651,9 @@ impl VaultService {
651651
)
652652
.await?;
653653

654+
// let address = bitcoin::bitcoin_primitives::Address::from_str("bcrt1qseu4xznuehrclpqn9r40ghgs092tfv6j2428dh").unwrap().assume_checked();
655+
// self.btc_rpc_master_wallet.dump_private_key(&address).unwrap();
656+
654657
let startup_height = self.await_parachain_block().await?;
655658

656659
let open_request_executor = execute_open_requests(

0 commit comments

Comments
 (0)