Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 08e8900

Browse files
authored
Merge pull request #4 from tcharding/06-06-miniscript
Add support for rust-miniscript
2 parents 139109e + f319abf commit 08e8900

File tree

45 files changed

+644
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+644
-56
lines changed
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
// All features uses 26_0
22
#[cfg(feature = "26_0")]
33
#[allow(unused_imports)] // Not all users need the json types.
4-
pub use bitcoind_json_rpc_client::{client_sync::v26::Client, json::v26 as json};
4+
pub use bitcoind_json_rpc_client::{client_sync::v26::{Client, AddressType}, json::v26 as json};
55

66
#[cfg(all(feature = "25_2", not(feature = "26_0")))]
77
#[allow(unused_imports)] // Not all users need the json types.
8-
pub use bitcoind_json_rpc_client::{client_sync::v25::Client, json::v25 as json};
8+
pub use bitcoind_json_rpc_client::{client_sync::v25::{Client, AddressType}, json::v25 as json};
99

1010
#[cfg(all(feature = "25_1", not(feature = "25_2")))]
1111
#[allow(unused_imports)] // Not all users need the json types.
12-
pub use bitcoind_json_rpc_client::{client_sync::v25::Client, json::v25 as json};
12+
pub use bitcoind_json_rpc_client::{client_sync::v25::{Client, AddressType}, json::v25 as json};
1313

1414
#[cfg(all(feature = "25_0", not(feature = "25_1")))]
1515
#[allow(unused_imports)] // Not all users need the json types.
16-
pub use bitcoind_json_rpc_client::{client_sync::v25::Client, json::v25 as json};
16+
pub use bitcoind_json_rpc_client::{client_sync::v25::{Client, AddressType}, json::v25 as json};
1717

1818
#[cfg(all(feature = "24_2", not(feature = "25_0")))]
1919
#[allow(unused_imports)] // Not all users need the json types.
20-
pub use bitcoind_json_rpc_client::{client_sync::v24::Client, json::v24 as json};
20+
pub use bitcoind_json_rpc_client::{client_sync::v24::{Client, AddressType}, json::v24 as json};
2121

2222
#[cfg(all(feature = "24_1", not(feature = "24_2")))]
2323
#[allow(unused_imports)] // Not all users need the json types.
24-
pub use bitcoind_json_rpc_client::{client_sync::v24::Client, json::v24 as json};
24+
pub use bitcoind_json_rpc_client::{client_sync::v24::{Client, AddressType}, json::v24 as json};
2525

2626
#[cfg(all(feature = "24_0_1", not(feature = "24_1")))]
2727
#[allow(unused_imports)] // Not all users need the json types.
28-
pub use bitcoind_json_rpc_client::{client_sync::v24::Client, json::v24 as json};
28+
pub use bitcoind_json_rpc_client::{client_sync::v24::{Client, AddressType}, json::v24 as json};
2929

3030
#[cfg(all(feature = "23_2", not(feature = "24_0_1")))]
3131
#[allow(unused_imports)] // Not all users need the json types.
32-
pub use bitcoind_json_rpc_client::{client_sync::v23::Client, json::v23 as json};
32+
pub use bitcoind_json_rpc_client::{client_sync::v23::{Client, AddressType}, json::v23 as json};
3333

3434
#[cfg(all(feature = "23_1", not(feature = "23_2")))]
3535
#[allow(unused_imports)] // Not all users need the json types.
36-
pub use bitcoind_json_rpc_client::{client_sync::v23::Client, json::v23 as json};
36+
pub use bitcoind_json_rpc_client::{client_sync::v23::{Client, AddressType}, json::v23 as json};
3737

3838
#[cfg(all(feature = "23_0", not(feature = "23_1")))]
3939
#[allow(unused_imports)] // Not all users need the json types.
40-
pub use bitcoind_json_rpc_client::{client_sync::v23::Client, json::v23 as json};
40+
pub use bitcoind_json_rpc_client::{client_sync::v23::{Client, AddressType}, json::v23 as json};
4141

4242
#[cfg(all(feature = "22_1", not(feature = "23_0")))]
4343
#[allow(unused_imports)] // Not all users need the json types.
44-
pub use bitcoind_json_rpc_client::{client_sync::v22::Client, json::v22 as json};
44+
pub use bitcoind_json_rpc_client::{client_sync::v22::{Client, AddressType}, json::v22 as json};
4545

4646
#[cfg(all(feature = "22_0", not(feature = "22_1")))]
4747
#[allow(unused_imports)] // Not all users need the json types.
48-
pub use bitcoind_json_rpc_client::{client_sync::v22::Client, json::v22 as json};
48+
pub use bitcoind_json_rpc_client::{client_sync::v22::{Client, AddressType}, json::v22 as json};
4949

5050
#[cfg(all(feature = "0_21_2", not(feature = "22_0")))]
5151
#[allow(unused_imports)] // Not all users need the json types.
52-
pub use bitcoind_json_rpc_client::{client_sync::v21::Client, json::v21 as json};
52+
pub use bitcoind_json_rpc_client::{client_sync::v21::{Client, AddressType}, json::v21 as json};
5353

5454
#[cfg(all(feature = "0_20_2", not(feature = "0_21_2")))]
5555
#[allow(unused_imports)] // Not all users need the json types.
56-
pub use bitcoind_json_rpc_client::{client_sync::v20::Client, json::v20 as json};
56+
pub use bitcoind_json_rpc_client::{client_sync::v20::{Client, AddressType}, json::v20 as json};
5757

5858
#[cfg(all(feature = "0_19_1", not(feature = "0_20_2")))]
5959
#[allow(unused_imports)] // Not all users need the json types.
60-
pub use bitcoind_json_rpc_client::{client_sync::v19::Client, json::v19 as json};
60+
pub use bitcoind_json_rpc_client::{client_sync::v19::{Client, AddressType}, json::v19 as json};
6161

6262
#[cfg(all(feature = "0_18_1", not(feature = "0_19_1")))]
6363
#[allow(unused_imports)] // Not all users need the json types.
64-
pub use bitcoind_json_rpc_client::{client_sync::v18::Client, json::v18 as json};
64+
pub use bitcoind_json_rpc_client::{client_sync::v18::{Client, AddressType}, json::v18 as json};
6565

6666
#[cfg(all(feature = "0_17_2", not(feature = "0_18_1")))]
6767
#[allow(unused_imports)] // Not all users need the json types.
68-
pub use bitcoind_json_rpc_client::{client_sync::v17::Client, json::v17 as json};
68+
pub use bitcoind_json_rpc_client::{client_sync::v17::{Client, AddressType}, json::v17 as json};
6969

7070
// To make --no-default-features work we have to enable some feature, use most recent version same as for default.
7171
#[cfg(all(not(feature = "26_0"), not(feature = "25_2"), not(feature = "25_1"), not(feature = "25_0"), not(feature = "24_2"),not(feadure = "24_1"), not(feature = "24_0_1"), not(feature = "23_2"), not(feature = "23_1"), not(feature = "23_0"), not(feature = "22_1"), not(feature = "22_0"), not(feature = "0_21_2"), not(feature = "0_20_2"), not(feature = "0_19_1"), not(feature = "0_18_1"), not(feature = "0_17_2")))]
7272
#[allow(unused_imports)] // Not all users need the json types.
73-
pub use bitcoind_json_rpc_client::{client_sync::v26::Client, json::v26 as json};
73+
pub use bitcoind_json_rpc_client::{client_sync::v26::{Client, AddressType}, json::v26 as json};

bitcoind/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
22
#![cfg_attr(feature = "doc", cfg_attr(all(), doc = include_str!("../README.md")))]
33

4+
pub extern crate bitcoind_json_rpc_client as client;
5+
46
#[rustfmt::skip]
5-
mod client;
7+
mod client_versions;
68
mod versions;
79

810
use std::ffi::OsStr;
@@ -18,9 +20,11 @@ use log::{debug, error, warn};
1820
use tempfile::TempDir;
1921
pub use {anyhow, tempfile, which};
2022

21-
use self::client::Client;
22-
#[allow(unused_imports)] // for --no-default-features
23-
use self::versions::VERSION;
23+
#[rustfmt::skip] // Keep pubic re-exports separate.
24+
pub use self::{
25+
client_versions::{json, Client, AddressType},
26+
versions::VERSION,
27+
};
2428

2529
#[derive(Debug)]
2630
/// Struct representing the bitcoind process with related information
@@ -687,8 +691,6 @@ mod test {
687691
fn test_multi_wallet() {
688692
use bitcoind_json_rpc_client::bitcoin::Amount;
689693

690-
use crate::client::json;
691-
692694
let exe = init();
693695
let bitcoind = BitcoinD::new(exe).unwrap();
694696
let alice = bitcoind.create_wallet("alice").unwrap();

client/src/client_sync/v17/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ pub mod blockchain;
88
pub mod control;
99
pub mod generating;
1010
pub mod network;
11+
pub mod raw_transactions;
1112
pub mod wallet;
1213

1314
use bitcoin::address::{Address, NetworkChecked};
1415
use bitcoin::{Amount, Block, BlockHash, Txid};
16+
use serde::{Deserialize, Serialize};
1517

1618
use crate::client_sync::{handle_defaults, into_json};
1719
use crate::json::v17::*;
@@ -34,10 +36,35 @@ crate::impl_client_v17__generatetoaddress!();
3436
crate::impl_client_v17__getnetworkinfo!();
3537
crate::impl_client_check_expected_server_version!({ [170200] });
3638

39+
// == Rawtransactions ==
40+
crate::impl_client_v17__sendrawtransaction!();
41+
3742
// == Wallet ==
3843
crate::impl_client_v17__createwallet!();
3944
crate::impl_client_v17__unloadwallet!();
4045
crate::impl_client_v17__loadwallet!();
4146
crate::impl_client_v17__getnewaddress!();
4247
crate::impl_client_v17__getbalance!();
4348
crate::impl_client_v17__sendtoaddress!();
49+
crate::impl_client_v17__gettransaction!();
50+
51+
/// Argument to the `Client::get_new_address_with_type` function.
52+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
53+
pub enum AddressType {
54+
Legacy,
55+
P2ShSegwit,
56+
Bech32,
57+
}
58+
59+
impl fmt::Display for AddressType {
60+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61+
use AddressType::*;
62+
63+
let s = match *self {
64+
Legacy => "legacy",
65+
P2ShSegwit => "p2sh-segwit",
66+
Bech32 => "bech32",
67+
};
68+
fmt::Display::fmt(s, f)
69+
}
70+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Macros for implementing JSON-RPC methods on a client.
4+
//!
5+
//! Specifically this is methods found under the `== Rawtransactions ==` section of the
6+
//! API docs of `bitcoind v0.17.1`.
7+
//!
8+
//! All macros require `Client` to be in scope.
9+
//!
10+
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
11+
12+
/// Implements bitcoind JSON-RPC API method `sendrawtransaction`
13+
#[macro_export]
14+
macro_rules! impl_client_v17__sendrawtransaction {
15+
() => {
16+
impl Client {
17+
pub fn send_raw_transaction(
18+
&self,
19+
tx: &bitcoin::Transaction,
20+
) -> Result<SendRawTransaction> {
21+
self.call("sendrawtransaction", &[into_json(tx)?])
22+
}
23+
}
24+
};
25+
}

client/src/client_sync/v17/wallet.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,24 @@ macro_rules! impl_client_v17__getnewaddress {
7171
Ok(address)
7272
}
7373

74+
/// Gets a new address from `bitcoind` and parses it assuming its correct.
75+
pub fn new_address_with_type(&self, ty: AddressType) -> Result<bitcoin::Address> {
76+
use core::str::FromStr;
77+
78+
let json = self.get_new_address_with_type(ty)?;
79+
let address = bitcoin::Address::from_str(&json.0)
80+
.expect("assume the address is valid")
81+
.assume_checked(); // Assume bitcoind will return an invalid address for the network its on.
82+
Ok(address)
83+
}
84+
7485
pub fn get_new_address(&self) -> Result<GetNewAddress> {
7586
self.call("getnewaddress", &[])
7687
}
88+
89+
pub fn get_new_address_with_type(&self, ty: AddressType) -> Result<GetNewAddress> {
90+
self.call("getnewaddress", &["".into(), into_json(ty)?])
91+
}
7792
}
7893
};
7994
}
@@ -94,3 +109,15 @@ macro_rules! impl_client_v17__sendtoaddress {
94109
}
95110
};
96111
}
112+
113+
/// Implements bitcoind JSON-RPC API method `gettransaction`
114+
#[macro_export]
115+
macro_rules! impl_client_v17__gettransaction {
116+
() => {
117+
impl Client {
118+
pub fn get_transaction(&self, txid: Txid) -> Result<GetTransaction> {
119+
self.call("gettransaction", &[into_json(txid)?])
120+
}
121+
}
122+
};
123+
}

client/src/client_sync/v18.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ crate::impl_client_v17__generatetoaddress!();
2828
crate::impl_client_v17__getnetworkinfo!();
2929
crate::impl_client_check_expected_server_version!({ [180100] });
3030

31+
// == Rawtransactions ==
32+
crate::impl_client_v17__sendrawtransaction!();
33+
3134
// == Wallet ==
3235
crate::impl_client_v17__createwallet!();
3336
crate::impl_client_v17__unloadwallet!();
3437
crate::impl_client_v17__loadwallet!();
3538
crate::impl_client_v17__getnewaddress!();
3639
crate::impl_client_v17__getbalance!();
3740
crate::impl_client_v17__sendtoaddress!();
41+
crate::impl_client_v17__gettransaction!();
42+
43+
pub use crate::client_sync::v17::AddressType;

client/src/client_sync/v19/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ crate::impl_client_v17__generatetoaddress!();
3030
crate::impl_client_v17__getnetworkinfo!();
3131
crate::impl_client_check_expected_server_version!({ [190100] });
3232

33+
// == Rawtransactions ==
34+
crate::impl_client_v17__sendrawtransaction!();
35+
3336
// == Wallet ==
3437
crate::impl_client_v17__createwallet!();
3538
crate::impl_client_v17__unloadwallet!();
@@ -38,3 +41,6 @@ crate::impl_client_v17__getnewaddress!();
3841
crate::impl_client_v17__getbalance!();
3942
crate::impl_client_v19__getbalances!();
4043
crate::impl_client_v17__sendtoaddress!();
44+
crate::impl_client_v17__gettransaction!();
45+
46+
pub use crate::client_sync::v17::AddressType;

client/src/client_sync/v20.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ crate::impl_client_v17__generatetoaddress!();
2828
crate::impl_client_v17__getnetworkinfo!();
2929
crate::impl_client_check_expected_server_version!({ [200200] });
3030

31+
// == Rawtransactions ==
32+
crate::impl_client_v17__sendrawtransaction!();
33+
3134
// == Wallet ==
3235
crate::impl_client_v17__createwallet!();
3336
crate::impl_client_v17__unloadwallet!();
@@ -36,3 +39,6 @@ crate::impl_client_v17__getnewaddress!();
3639
crate::impl_client_v17__getbalance!();
3740
crate::impl_client_v19__getbalances!();
3841
crate::impl_client_v17__sendtoaddress!();
42+
crate::impl_client_v17__gettransaction!();
43+
44+
pub use crate::client_sync::v17::AddressType;

client/src/client_sync/v21.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ crate::impl_client_v17__getbestblockhash!();
1818
crate::impl_client_v17__getblock!();
1919
crate::impl_client_v17__gettxout!();
2020

21-
// == Network ==
22-
crate::impl_client_v17__getnetworkinfo!();
23-
crate::impl_client_check_expected_server_version!({ [210200] });
24-
2521
// == Control ==
2622
crate::impl_client_v17__stop!();
2723

2824
// == Generating ==
2925
crate::impl_client_v17__generatetoaddress!();
3026

27+
// == Network ==
28+
crate::impl_client_v17__getnetworkinfo!();
29+
crate::impl_client_check_expected_server_version!({ [210200] });
30+
31+
// == Rawtransactions ==
32+
crate::impl_client_v17__sendrawtransaction!();
33+
3134
// == Wallet ==
3235
crate::impl_client_v17__createwallet!();
3336
crate::impl_client_v17__unloadwallet!();
@@ -36,3 +39,6 @@ crate::impl_client_v17__getnewaddress!();
3639
crate::impl_client_v17__getbalance!();
3740
crate::impl_client_v19__getbalances!();
3841
crate::impl_client_v17__sendtoaddress!();
42+
crate::impl_client_v17__gettransaction!();
43+
44+
pub use crate::client_sync::v17::AddressType;

client/src/client_sync/v22/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ crate::impl_client_v17__generatetoaddress!();
3030
crate::impl_client_v17__getnetworkinfo!();
3131
crate::impl_client_check_expected_server_version!({ [220000, 220100] });
3232

33+
// == Rawtransactions ==
34+
crate::impl_client_v17__sendrawtransaction!();
35+
3336
// == Wallet ==
3437
crate::impl_client_v17__createwallet!();
3538
crate::impl_client_v22__unloadwallet!();
@@ -38,3 +41,6 @@ crate::impl_client_v17__getbalance!();
3841
crate::impl_client_v19__getbalances!();
3942
crate::impl_client_v17__getnewaddress!();
4043
crate::impl_client_v17__sendtoaddress!();
44+
crate::impl_client_v17__gettransaction!();
45+
46+
pub use crate::client_sync::v17::AddressType;

0 commit comments

Comments
 (0)