Skip to content

Commit 8bc4de4

Browse files
committed
Fix rpc
1 parent e982480 commit 8bc4de4

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

examples/psbt_signer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ fn main() -> Result<(), Box<dyn Error>> {
3333
let internal_secret_xkey = DescriptorSecretKey::from_str("[e9824965/84'/1'/0']tprv8fvem7qWxY3SGCQczQpRpqTKg455wf1zgixn6MZ4ze8gRfHjov5gXBQTadNfDgqs9ERbZZ3Bi1PNYrCCusFLucT39K525MWLpeURjHwUsfX/1/*").unwrap();
3434

3535
let secp = Secp256k1::new();
36-
let external_public_xkey = external_secret_xkey.as_public(&secp).unwrap();
37-
let internal_public_xkey = internal_secret_xkey.as_public(&secp).unwrap();
36+
let external_public_xkey = external_secret_xkey.to_public(&secp).unwrap();
37+
let internal_public_xkey = internal_secret_xkey.to_public(&secp).unwrap();
3838

3939
let signing_external_descriptor = descriptor!(wpkh(external_secret_xkey)).unwrap();
4040
let signing_internal_descriptor = descriptor!(wpkh(internal_secret_xkey)).unwrap();

src/blockchain/rpc.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Deref for RpcBlockchain {
7777
}
7878

7979
/// RpcBlockchain configuration options
80-
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
80+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
8181
pub struct RpcConfig {
8282
/// The bitcoin node url
8383
pub url: String,
@@ -96,7 +96,7 @@ pub struct RpcConfig {
9696
/// In general, BDK tries to sync `scriptPubKey`s cached in [`crate::database::Database`] with
9797
/// `scriptPubKey`s imported in the Bitcoin Core Wallet. These parameters are used for determining
9898
/// how the `importdescriptors` RPC calls are to be made.
99-
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
99+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
100100
pub struct RpcSyncParams {
101101
/// The minimum number of scripts to scan for on initial sync.
102102
pub start_script_count: usize,
@@ -167,7 +167,7 @@ impl Blockchain for RpcBlockchain {
167167
.estimate_smart_fee(target as u16, None)?
168168
.fee_rate
169169
.ok_or(Error::FeeRateUnavailable)?
170-
.as_sat() as f64;
170+
.to_sat() as f64;
171171

172172
Ok(FeeRate::from_sat_per_vb((sat_per_kb / 1000f64) as f32))
173173
}
@@ -410,7 +410,12 @@ impl<'a, D: BatchDatabase> DbState<'a, D> {
410410
updated = true;
411411
TransactionDetails {
412412
txid: tx_res.info.txid,
413-
..Default::default()
413+
transaction: None,
414+
415+
received: 0,
416+
sent: 0,
417+
fee: None,
418+
confirmation_time: None,
414419
}
415420
});
416421

@@ -430,7 +435,7 @@ impl<'a, D: BatchDatabase> DbState<'a, D> {
430435
// update fee (if needed)
431436
if let (None, Some(new_fee)) = (db_tx.fee, tx_res.detail.fee) {
432437
updated = true;
433-
db_tx.fee = Some(new_fee.as_sat().unsigned_abs());
438+
db_tx.fee = Some(new_fee.to_sat().unsigned_abs());
434439
}
435440

436441
// update confirmation time (if needed)
@@ -603,7 +608,7 @@ impl<'a, D: BatchDatabase> DbState<'a, D> {
603608
LocalUtxo {
604609
outpoint: OutPoint::new(entry.txid, entry.vout),
605610
txout: TxOut {
606-
value: entry.amount.as_sat(),
611+
value: entry.amount.to_sat(),
607612
script_pubkey: entry.script_pub_key,
608613
},
609614
keychain,
@@ -873,15 +878,13 @@ impl BlockchainFactory for RpcBlockchainFactory {
873878
mod test {
874879
use super::*;
875880
use crate::{
876-
descriptor::{into_wallet_descriptor_checked, AsDerived},
877-
testutils::blockchain_tests::TestClient,
881+
descriptor::into_wallet_descriptor_checked, testutils::blockchain_tests::TestClient,
878882
wallet::utils::SecpCtx,
879883
};
880884

881885
use bitcoin::{Address, Network};
882886
use bitcoincore_rpc::RpcApi;
883887
use log::LevelFilter;
884-
use miniscript::DescriptorTrait;
885888

886889
crate::bdk_blockchain_tests! {
887890
fn test_instance(test_client: &TestClient) -> RpcBlockchain {
@@ -958,7 +961,7 @@ mod test {
958961

959962
// generate scripts (1 tx per script)
960963
let scripts = (0..TX_COUNT)
961-
.map(|index| desc.as_derived(index, &secp).script_pubkey())
964+
.map(|index| desc.at_derivation_index(index).script_pubkey())
962965
.collect::<Vec<_>>();
963966

964967
// import scripts and wait

src/testutils/blockchain_tests.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
// Bitcoin Dev Kit
2+
//
3+
// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
4+
//
5+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
6+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
8+
// You may not use this file except in accordance with one or both of these
9+
// licenses.
10+
111
use crate::testutils::TestIncomingTx;
212
use bitcoin::consensus::encode::{deserialize, serialize};
313
use bitcoin::hashes::hex::{FromHex, ToHex};
414
use bitcoin::hashes::sha256d;
5-
use bitcoin::{Address, Amount, Script, Transaction, Txid, Witness};
15+
use bitcoin::{Address, Amount, PackedLockTime, Script, Sequence, Transaction, Txid, Witness};
616
pub use bitcoincore_rpc::bitcoincore_rpc_json::AddressType;
717
pub use bitcoincore_rpc::{Auth, Client as RpcClient, RpcApi};
818
use core::str::FromStr;
@@ -110,7 +120,7 @@ impl TestClient {
110120
if let Some(true) = meta_tx.replaceable {
111121
// for some reason core doesn't set this field right
112122
for input in &mut tx.input {
113-
input.sequence = 0xFFFFFFFD;
123+
input.sequence = Sequence(0xFFFFFFFD);
114124
}
115125
}
116126

@@ -164,6 +174,7 @@ impl TestClient {
164174
use bitcoin::blockdata::script::Builder;
165175
use bitcoin::blockdata::transaction::{OutPoint, TxIn, TxOut};
166176
use bitcoin::hash_types::{BlockHash, TxMerkleNode};
177+
use bitcoin::hashes::Hash;
167178

168179
let block_template: serde_json::Value = self
169180
.call("getblocktemplate", &[json!({"rules": ["segwit"]})])
@@ -176,23 +187,23 @@ impl TestClient {
176187
block_template["previousblockhash"].as_str().unwrap(),
177188
)
178189
.unwrap(),
179-
merkle_root: TxMerkleNode::default(),
190+
merkle_root: TxMerkleNode::all_zeros(),
180191
time: block_template["curtime"].as_u64().unwrap() as u32,
181192
bits: u32::from_str_radix(block_template["bits"].as_str().unwrap(), 16).unwrap(),
182193
nonce: 0,
183194
};
184195
debug!("header: {:#?}", header);
185196

186197
let height = block_template["height"].as_u64().unwrap() as i64;
187-
let witness_reserved_value: Vec<u8> = sha256d::Hash::default().as_ref().into();
198+
let witness_reserved_value: Vec<u8> = sha256d::Hash::all_zeros().as_ref().into();
188199
// burn block subsidy and fees, not a big deal
189200
let mut coinbase_tx = Transaction {
190201
version: 1,
191-
lock_time: 0,
202+
lock_time: PackedLockTime(0),
192203
input: vec![TxIn {
193204
previous_output: OutPoint::null(),
194205
script_sig: Builder::new().push_int(height).into_script(),
195-
sequence: 0xFFFFFFFF,
206+
sequence: Sequence(0xFFFFFFFF),
196207
witness: Witness::from_vec(vec![witness_reserved_value]),
197208
}],
198209
output: vec![],
@@ -1184,7 +1195,7 @@ macro_rules! bdk_blockchain_tests {
11841195
// 5. Verify 25_000 sats are received by test bitcoind node taproot wallet
11851196

11861197
let taproot_balance = taproot_wallet_client.get_balance(None, None).unwrap();
1187-
assert_eq!(taproot_balance.as_sat(), 25_000, "node has incorrect taproot wallet balance");
1198+
assert_eq!(taproot_balance.to_sat(), 25_000, "node has incorrect taproot wallet balance");
11881199
}
11891200

11901201
#[test]

src/testutils/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,18 @@ macro_rules! testutils {
187187
use $crate::miniscript::descriptor::Descriptor;
188188
use $crate::miniscript::TranslatePk;
189189

190-
struct Translator(HashMap<&'static str, (String, Option<String>, Option<String>)>);
190+
struct Translator {
191+
keys: HashMap<&'static str, (String, Option<String>, Option<String>)>,
192+
is_internal: bool,
193+
}
191194

192195
impl $crate::miniscript::Translator<String, String, Infallible> for Translator {
193196
fn pk(&mut self, pk: &String) -> Result<String, Infallible> {
194-
match self.0.get(pk.as_str()) {
195-
Some((key, ext_path, _)) => Ok(format!("{}{}", key, ext_path.clone().unwrap_or_default())),
197+
match self.keys.get(pk.as_str()) {
198+
Some((key, ext_path, int_path)) => {
199+
let path = if self.is_internal { int_path } else { ext_path };
200+
Ok(format!("{}{}", key, path.clone().unwrap_or_default()))
201+
}
196202
None => Ok(pk.clone()),
197203
}
198204
}
@@ -208,12 +214,14 @@ macro_rules! testutils {
208214
keys = testutils!{ @keys $( $keys )* };
209215
)*
210216

211-
let mut translator = Translator(keys);
217+
let mut translator = Translator { keys, is_internal: false };
212218

213219
let external: Descriptor<String> = FromStr::from_str($external_descriptor).unwrap();
214220
let external = external.translate_pk(&mut translator).expect("Infallible conversion");
215221
let external = external.to_string();
216222

223+
translator.is_internal = true;
224+
217225
let internal = None::<String>$(.or({
218226
let internal: Descriptor<String> = FromStr::from_str($internal_descriptor).unwrap();
219227
let internal = internal.translate_pk(&mut translator).expect("Infallible conversion");

0 commit comments

Comments
 (0)