Skip to content

Commit

Permalink
added importdescriptors command, updated createwallet command with de…
Browse files Browse the repository at this point in the history
…scriptors parameter and added bech32m address type
  • Loading branch information
sandipndev committed Sep 13, 2021
1 parent 99467e7 commit ff2e1f5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 12 deletions.
35 changes: 33 additions & 2 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,52 @@ pub trait RpcApi: Sized {
blank: Option<bool>,
passphrase: Option<&str>,
avoid_reuse: Option<bool>,
descriptors: Option<bool>,
) -> Result<json::LoadWalletResult> {
let mut args = [
wallet.into(),
opt_into_json(disable_private_keys)?,
opt_into_json(blank)?,
opt_into_json(passphrase)?,
opt_into_json(avoid_reuse)?,
opt_into_json(descriptors)?,
];
self.call(
"createwallet",
handle_defaults(&mut args, &[false.into(), false.into(), into_json("")?, false.into()]),
handle_defaults(
&mut args,
&[false.into(), false.into(), into_json("")?, false.into(), false.into()],
),
)
}

fn import_descriptors(
&self,
descriptor: &str,
change_descriptor: &str,
) -> Result<Vec<json::ImportDescriptorResult>> {
let ex_descriptor = json::ImportDescriptorRequest {
active: true,
descriptor: descriptor.to_string(),
range: [0, 100],
next_index: 0,
timestamp: "now".to_string(),
internal: false,
};

let in_descriptor = json::ImportDescriptorRequest {
active: true,
descriptor: change_descriptor.to_string(),
range: [0, 100],
next_index: 0,
timestamp: "now".to_string(),
internal: true,
};

let arg = into_json([ex_descriptor, in_descriptor])?;
self.call("importdescriptors", &[arg])
}

fn list_wallets(&self) -> Result<Vec<String>> {
self.call("listwallets", &[])
}
Expand Down Expand Up @@ -1136,7 +1168,6 @@ impl RpcApi for Client {
if log_enabled!(Debug) {
debug!(target: "bitcoincore_rpc", "JSON-RPC request: {} {}", cmd, serde_json::Value::from(args));
}

let resp = self.client.send_request(req).map_err(Error::from);
log_response(cmd, &resp);
Ok(resp?.result()?)
Expand Down
4 changes: 2 additions & 2 deletions integration_test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ PID1=$!
sleep 3

BLOCKFILTERARG=""
if bitcoind -version | grep -q "v0\.\(19\|2\)"; then
if bitcoind -version | grep -q "v\(0\.19\|0\.2\|2\)"; then
BLOCKFILTERARG="-blockfilterindex=1"
fi

FALLBACKFEEARG=""
if bitcoind -version | grep -q "v0\.2"; then
if bitcoind -version | grep -q "v\(0\.2\|2\)"; then
FALLBACKFEEARG="-fallbackfee=0.00001000"
fi

Expand Down
43 changes: 36 additions & 7 deletions integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern crate lazy_static;
extern crate log;

use std::collections::HashMap;
use std::str::FromStr;

use bitcoincore_rpc::json;
use bitcoincore_rpc::jsonrpc::error::Error as JsonRpcError;
Expand All @@ -30,9 +31,7 @@ use bitcoin::{
Address, Amount, Network, OutPoint, PrivateKey, Script, SigHashType, SignedAmount, Transaction,
TxIn, TxOut, Txid,
};
use bitcoincore_rpc::bitcoincore_rpc_json::{
GetBlockTemplateModes, GetBlockTemplateRules, ScanTxOutRequest,
};
use bitcoincore_rpc::bitcoincore_rpc_json::{GetBlockTemplateModes, GetBlockTemplateRules, ScanTxOutRequest};

lazy_static! {
static ref SECP: secp256k1::Secp256k1<secp256k1::All> = secp256k1::Secp256k1::new();
Expand Down Expand Up @@ -125,15 +124,13 @@ fn main() {
log::set_logger(&LOGGER).map(|()| log::set_max_level(log::LevelFilter::max())).unwrap();

let rpc_url = format!("{}/wallet/testwallet", get_rpc_url());
let auth = get_auth();

let cl = Client::new(&rpc_url, auth).unwrap();
let cl = Client::new(&rpc_url, get_auth()).unwrap();

test_get_network_info(&cl);
unsafe { VERSION = cl.version().unwrap() };
println!("Version: {}", version());

cl.create_wallet("testwallet", None, None, None, None).unwrap();
cl.create_wallet("testwallet", None, None, None, None, None).unwrap();

test_get_mining_info(&cl);
test_get_blockchain_info(&cl);
Expand Down Expand Up @@ -203,6 +200,11 @@ fn main() {
//TODO load_wallet(&self, wallet: &str) -> Result<json::LoadWalletResult> {
//TODO unload_wallet(&self, wallet: Option<&str>) -> Result<()> {
//TODO backup_wallet(&self, destination: Option<&str>) -> Result<()> {

let rpc_url = format!("{}/wallet/testdescriptorwallet", get_rpc_url());
let desc_cl = Client::new(&rpc_url, get_auth()).unwrap();

test_descriptor_wallet(&desc_cl);
test_stop(cl);
}

Expand Down Expand Up @@ -905,6 +907,7 @@ fn test_create_wallet(cl: &Client) {
blank: Option<bool>,
passphrase: Option<&'a str>,
avoid_reuse: Option<bool>,
descriptors: Option<bool>,
}

let mut wallet_params = vec![
Expand All @@ -914,20 +917,23 @@ fn test_create_wallet(cl: &Client) {
blank: None,
passphrase: None,
avoid_reuse: None,
descriptors: None,
},
WalletParams {
name: wallet_names[1],
disable_private_keys: Some(true),
blank: None,
passphrase: None,
avoid_reuse: None,
descriptors: None,
},
WalletParams {
name: wallet_names[2],
disable_private_keys: None,
blank: Some(true),
passphrase: None,
avoid_reuse: None,
descriptors: None,
},
];

Expand All @@ -938,13 +944,15 @@ fn test_create_wallet(cl: &Client) {
blank: None,
passphrase: Some("pass"),
avoid_reuse: None,
descriptors: None,
});
wallet_params.push(WalletParams {
name: wallet_names[4],
disable_private_keys: None,
blank: None,
passphrase: None,
avoid_reuse: Some(true),
descriptors: None,
});
}

Expand All @@ -956,6 +964,7 @@ fn test_create_wallet(cl: &Client) {
wallet_param.blank,
wallet_param.passphrase,
wallet_param.avoid_reuse,
wallet_param.descriptors,
)
.unwrap();

Expand Down Expand Up @@ -1050,3 +1059,23 @@ fn test_getblocktemplate(cl: &Client) {
fn test_stop(cl: Client) {
println!("Stopping: '{}'", cl.stop().unwrap());
}

fn test_descriptor_wallet(cl: &Client) {
cl.create_wallet(
"testdescriptorwallet",
Some(false),
Some(true),
Some(""),
Some(false),
Some(true),
)
.unwrap();

cl.import_descriptors(
"wpkh(tprv8ZgxMBicQKsPeRBCAfUGsZhyHy9dwWyPqhSJmaMnMJQWWtt8L2SkTeHaiF82CUCGtiTiHAs3cMkjdKckGKiCWeYtvMPF1jDTWYTryRMicpx/86h/1h/0h/0/*)#ymr4jlz6",
"wpkh(tprv8ZgxMBicQKsPeRBCAfUGsZhyHy9dwWyPqhSJmaMnMJQWWtt8L2SkTeHaiF82CUCGtiTiHAs3cMkjdKckGKiCWeYtvMPF1jDTWYTryRMicpx/86h/1h/0h/1/*)#40x502jz",
).unwrap();

let add = cl.get_new_address(None, Some(json::AddressType::Bech32)).unwrap();
assert_eq!(add, Address::from_str("bcrt1q7crcza94drr00skmu5x0n00rhmwnthde2frhwk").unwrap());
}
25 changes: 24 additions & 1 deletion json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,11 +1066,13 @@ pub struct GetPeerInfoResult {
}

#[derive(Copy, Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
#[serde(rename_all = "lowercase")]
#[serde(rename_all = "snake_case")]
pub enum GetPeerInfoResultNetwork {
Ipv4,
Ipv6,
Onion,
I2p,
NotPubliclyRoutable,
// this is undocumented upstream
Unroutable,
}
Expand Down Expand Up @@ -1607,6 +1609,7 @@ pub enum AddressType {
Legacy,
P2shSegwit,
Bech32,
Bech32m,
}

/// Used to represent arguments that can either be an address or a public key.
Expand Down Expand Up @@ -1686,3 +1689,23 @@ where
}
Ok(Some(res))
}

/// Import Descriptor Request
#[derive(Serialize, Clone, PartialEq, Eq, Debug)]
pub struct ImportDescriptorRequest {
pub active: bool,
#[serde(rename = "desc")]
pub descriptor: String,
pub range: [i64; 2],
pub next_index: i64,
pub timestamp: String,
pub internal: bool,
}

/// Imported Descriptor Result
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
pub struct ImportDescriptorResult {
pub success: bool,
pub warnings: Option<Vec<String>>,
pub error: Option<String>,
}

0 comments on commit ff2e1f5

Please sign in to comment.