Skip to content

Commit 79efa7f

Browse files
authored
Merge pull request #105 from tcharding/03-12-raw-transactions
Implement raw transaction methods
2 parents 9fb50b1 + 102a42e commit 79efa7f

File tree

37 files changed

+921
-247
lines changed

37 files changed

+921
-247
lines changed

client/src/client_sync/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ use std::fs::File;
2020
use std::io::{BufRead, BufReader};
2121
use std::path::PathBuf;
2222

23+
use bitcoin::Txid;
24+
use serde::{Deserialize, Serialize};
25+
2326
pub use crate::client_sync::error::Error;
2427

2528
/// Crate-specific Result type.
@@ -220,3 +223,21 @@ fn log_response(method: &str, resp: &Result<jsonrpc::Response>) {
220223
}
221224
}
222225
}
226+
227+
/// Input used as parameter to `create_raw_transaction`.
228+
#[derive(Debug, Serialize)]
229+
pub struct Input {
230+
/// The txid of the transaction that contains the UTXO.
231+
pub txid: bitcoin::Txid,
232+
/// The vout for the UTXO.
233+
pub vout: u64,
234+
/// Sequence number if needed.
235+
pub sequence: Option<bitcoin::Sequence>,
236+
}
237+
238+
/// An element in the `inputs` argument of method `walletcreatefundedpsbt`.
239+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
240+
pub struct WalletCreateFundedPsbtInput {
241+
txid: Txid,
242+
vout: u32,
243+
}

client/src/client_sync/v17/mod.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,8 @@ use serde::{Deserialize, Serialize};
2121
use crate::client_sync::into_json;
2222
use crate::types::v17::*;
2323

24-
/// An element in the `inputs` argument of method `walletcreatefundedpsbt`.
25-
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
26-
pub struct WalletCreateFundedPsbtInput {
27-
txid: Txid,
28-
vout: u32,
29-
}
30-
31-
/// Argument to the `Client::get_new_address_with_type` function.
32-
///
33-
/// For Core versions 0.17 through to v22. For Core v23 and onwards use `v23::AddressType`.
34-
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
35-
#[serde(rename_all = "kebab-case")]
36-
pub enum AddressType {
37-
Legacy,
38-
P2shSegwit,
39-
Bech32,
40-
}
41-
42-
impl fmt::Display for AddressType {
43-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44-
use AddressType::*;
45-
46-
let s = match *self {
47-
Legacy => "legacy",
48-
P2shSegwit => "p2sh-segwit",
49-
Bech32 => "bech32",
50-
};
51-
fmt::Display::fmt(s, f)
52-
}
53-
}
24+
#[rustfmt::skip] // Keep public re-exports separate.
25+
pub use crate::client_sync::WalletCreateFundedPsbtInput;
5426

5527
crate::define_jsonrpc_minreq_client!("v17");
5628
crate::impl_client_check_expected_server_version!({ [170100] });
@@ -96,6 +68,8 @@ crate::impl_client_v17__getnetworkinfo!();
9668
crate::impl_client_v17__getpeerinfo!();
9769

9870
// == Rawtransactions ==
71+
crate::impl_client_v17__createrawtransaction!();
72+
crate::impl_client_v17__fundrawtransaction!();
9973
crate::impl_client_v17__sendrawtransaction!();
10074

10175
// == Wallet ==
@@ -129,3 +103,27 @@ crate::impl_client_v17__signmessage!();
129103
crate::impl_client_v17__signrawtransactionwithwallet!();
130104
crate::impl_client_v17__walletcreatefundedpsbt!();
131105
crate::impl_client_v17__walletprocesspsbt!();
106+
107+
/// Argument to the `Client::get_new_address_with_type` function.
108+
///
109+
/// For Core versions 0.17 through to v22. For Core v23 and onwards use `v23::AddressType`.
110+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
111+
#[serde(rename_all = "kebab-case")]
112+
pub enum AddressType {
113+
Legacy,
114+
P2shSegwit,
115+
Bech32,
116+
}
117+
118+
impl fmt::Display for AddressType {
119+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120+
use AddressType::*;
121+
122+
let s = match *self {
123+
Legacy => "legacy",
124+
P2shSegwit => "p2sh-segwit",
125+
Bech32 => "bech32",
126+
};
127+
fmt::Display::fmt(s, f)
128+
}
129+
}

client/src/client_sync/v17/raw_transactions.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,37 @@
99
//!
1010
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
1111
12+
/// Implements Bitcoin Core JSON-RPC API method `createrawtransaction`
13+
#[macro_export]
14+
macro_rules! impl_client_v17__createrawtransaction {
15+
() => {
16+
impl Client {
17+
pub fn create_raw_transaction(
18+
&self,
19+
inputs: &[$crate::client_sync::Input],
20+
outputs: &std::collections::BTreeMap<String, f64>, // Map of address to amount.
21+
) -> Result<CreateRawTransaction> {
22+
self.call("createrawtransaction", &[into_json(inputs)?, into_json(outputs)?])
23+
}
24+
}
25+
};
26+
}
27+
28+
/// Implements Bitcoin Core JSON-RPC API method `fundrawtransaction`
29+
#[macro_export]
30+
macro_rules! impl_client_v17__fundrawtransaction {
31+
() => {
32+
impl Client {
33+
pub fn fund_raw_transaction(
34+
&self,
35+
hex: &str, // Hex encoded transaction.
36+
) -> Result<FundRawTransaction> {
37+
self.call("fundrawtransaction", &[into_json(hex)?])
38+
}
39+
}
40+
};
41+
}
42+
1243
/// Implements Bitcoin Core JSON-RPC API method `sendrawtransaction`
1344
#[macro_export]
1445
macro_rules! impl_client_v17__sendrawtransaction {

client/src/client_sync/v17/wallet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ macro_rules! impl_client_v17__walletcreatefundedpsbt {
439439
impl Client {
440440
pub fn wallet_create_funded_psbt(
441441
&self,
442-
inputs: Vec<WalletCreateFundedPsbtInput>,
442+
inputs: Vec<$crate::client_sync::WalletCreateFundedPsbtInput>,
443443
outputs: Vec<BTreeMap<Address, Amount>>,
444444
) -> Result<WalletCreateFundedPsbt> {
445445
self.call("walletcreatefundedpsbt", &[into_json(inputs)?, into_json(outputs)?])

client/src/client_sync/v18/mod.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
pub mod control;
88

9+
use std::collections::BTreeMap;
910
use std::path::Path;
1011

1112
use bitcoin::address::{Address, NetworkChecked};
@@ -15,7 +16,7 @@ use crate::client_sync::into_json;
1516
use crate::types::v18::*;
1617

1718
#[rustfmt::skip] // Keep public re-exports separate.
18-
pub use crate::client_sync::v17::AddressType;
19+
pub use crate::client_sync::{v17::AddressType, WalletCreateFundedPsbtInput};
1920

2021
crate::define_jsonrpc_minreq_client!("v18");
2122
crate::impl_client_check_expected_server_version!({ [180100] });
@@ -62,6 +63,8 @@ crate::impl_client_v17__getnetworkinfo!();
6263
crate::impl_client_v17__getpeerinfo!();
6364

6465
// == Rawtransactions ==
66+
crate::impl_client_v17__createrawtransaction!();
67+
crate::impl_client_v17__fundrawtransaction!();
6568
crate::impl_client_v17__sendrawtransaction!();
6669

6770
// == Wallet ==
@@ -77,9 +80,21 @@ crate::impl_client_v17__getnewaddress!();
7780
crate::impl_client_v17__getrawchangeaddress!();
7881
crate::impl_client_v17__getreceivedbyaddress!();
7982
crate::impl_client_v17__gettransaction!();
80-
81-
// Upto here
82-
83-
// crate::impl_client_v17__unloadwallet!();
83+
crate::impl_client_v17__getunconfirmedbalance!();
84+
crate::impl_client_v17__getwalletinfo!();
85+
crate::impl_client_v17__listaddressgroupings!();
86+
crate::impl_client_v17__listlabels!();
87+
crate::impl_client_v17__listlockunspent!();
88+
crate::impl_client_v17__listreceivedbyaddress!();
89+
crate::impl_client_v17__listsinceblock!();
90+
crate::impl_client_v17__listtransactions!();
91+
crate::impl_client_v17__listunspent!();
92+
crate::impl_client_v17__listwallets!();
8493
crate::impl_client_v17__loadwallet!();
94+
crate::impl_client_v17__rescanblockchain!();
95+
crate::impl_client_v17__sendmany!();
8596
crate::impl_client_v17__sendtoaddress!();
97+
crate::impl_client_v17__signmessage!();
98+
crate::impl_client_v17__signrawtransactionwithwallet!();
99+
crate::impl_client_v17__walletcreatefundedpsbt!();
100+
crate::impl_client_v17__walletprocesspsbt!();

client/src/client_sync/v19/mod.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
pub mod blockchain;
88
pub mod wallet;
99

10+
use std::collections::BTreeMap;
11+
use std::path::Path;
12+
1013
use bitcoin::address::{Address, NetworkChecked};
11-
use bitcoin::{Amount, Block, BlockHash, Txid};
14+
use bitcoin::{Amount, Block, BlockHash, PublicKey, Txid};
1215

1316
use crate::client_sync::into_json;
1417
use crate::types::v19::*;
1518

19+
#[rustfmt::skip] // Keep public re-exports separate.
20+
pub use crate::client_sync::{v17::AddressType, WalletCreateFundedPsbtInput};
21+
1622
crate::define_jsonrpc_minreq_client!("v19");
1723

1824
// == Blockchain ==
@@ -55,16 +61,38 @@ crate::impl_client_v17__getnetworkinfo!();
5561
crate::impl_client_check_expected_server_version!({ [190100] });
5662

5763
// == Rawtransactions ==
64+
crate::impl_client_v17__createrawtransaction!();
65+
crate::impl_client_v17__fundrawtransaction!();
5866
crate::impl_client_v17__sendrawtransaction!();
5967

6068
// == Wallet ==
69+
crate::impl_client_v17__addmultisigaddress!();
70+
crate::impl_client_v17__bumpfee!();
6171
crate::impl_client_v17__createwallet!();
62-
//crate::impl_client_v17__unloadwallet!();
63-
crate::impl_client_v17__loadwallet!();
64-
crate::impl_client_v17__getnewaddress!();
72+
crate::impl_client_v17__dumpprivkey!();
73+
crate::impl_client_v17__dumpwallet!();
74+
crate::impl_client_v17__getaddressesbylabel!();
75+
crate::impl_client_v17__getaddressinfo!();
6576
crate::impl_client_v17__getbalance!();
66-
crate::impl_client_v19__getbalances!();
67-
crate::impl_client_v17__sendtoaddress!();
77+
crate::impl_client_v17__getnewaddress!();
78+
crate::impl_client_v17__getrawchangeaddress!();
79+
crate::impl_client_v17__getreceivedbyaddress!();
6880
crate::impl_client_v17__gettransaction!();
69-
70-
pub use crate::client_sync::v17::AddressType;
81+
crate::impl_client_v17__getunconfirmedbalance!();
82+
crate::impl_client_v17__getwalletinfo!();
83+
crate::impl_client_v17__listaddressgroupings!();
84+
crate::impl_client_v17__listlabels!();
85+
crate::impl_client_v17__listlockunspent!();
86+
crate::impl_client_v17__listreceivedbyaddress!();
87+
crate::impl_client_v17__listsinceblock!();
88+
crate::impl_client_v17__listtransactions!();
89+
crate::impl_client_v17__listunspent!();
90+
crate::impl_client_v17__listwallets!();
91+
crate::impl_client_v17__loadwallet!();
92+
crate::impl_client_v17__rescanblockchain!();
93+
crate::impl_client_v17__sendmany!();
94+
crate::impl_client_v17__sendtoaddress!();
95+
crate::impl_client_v17__signmessage!();
96+
crate::impl_client_v17__signrawtransactionwithwallet!();
97+
crate::impl_client_v17__walletcreatefundedpsbt!();
98+
crate::impl_client_v17__walletprocesspsbt!();

client/src/client_sync/v20.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
//!
55
//! We ignore option arguments unless they effect the shape of the returned JSON data.
66
7+
use std::collections::BTreeMap;
8+
use std::path::Path;
9+
710
use bitcoin::address::{Address, NetworkChecked};
8-
use bitcoin::{Amount, Block, BlockHash, Txid};
11+
use bitcoin::{Amount, Block, BlockHash, PublicKey, Txid};
912

1013
use crate::client_sync::into_json;
1114
use crate::types::v20::*;
1215

1316
#[rustfmt::skip] // Keep public re-exports separate.
14-
pub use crate::client_sync::v17::AddressType;
17+
pub use crate::client_sync::{v17::AddressType, WalletCreateFundedPsbtInput};
1518

1619
crate::define_jsonrpc_minreq_client!("v20");
1720

@@ -55,14 +58,39 @@ crate::impl_client_v17__getnetworkinfo!();
5558
crate::impl_client_check_expected_server_version!({ [200200] });
5659

5760
// == Rawtransactions ==
61+
crate::impl_client_v17__createrawtransaction!();
62+
crate::impl_client_v17__fundrawtransaction!();
5863
crate::impl_client_v17__sendrawtransaction!();
5964

6065
// == Wallet ==
66+
crate::impl_client_v17__addmultisigaddress!();
67+
crate::impl_client_v17__bumpfee!();
6168
crate::impl_client_v17__createwallet!();
62-
//crate::impl_client_v17__unloadwallet!();
63-
crate::impl_client_v17__loadwallet!();
64-
crate::impl_client_v17__getnewaddress!();
69+
crate::impl_client_v17__dumpprivkey!();
70+
crate::impl_client_v17__dumpwallet!();
71+
crate::impl_client_v17__getaddressesbylabel!();
72+
crate::impl_client_v17__getaddressinfo!();
6573
crate::impl_client_v17__getbalance!();
6674
crate::impl_client_v19__getbalances!();
67-
crate::impl_client_v17__sendtoaddress!();
75+
crate::impl_client_v17__getnewaddress!();
76+
crate::impl_client_v17__getrawchangeaddress!();
77+
crate::impl_client_v17__getreceivedbyaddress!();
6878
crate::impl_client_v17__gettransaction!();
79+
crate::impl_client_v17__getunconfirmedbalance!();
80+
crate::impl_client_v17__getwalletinfo!();
81+
crate::impl_client_v17__listaddressgroupings!();
82+
crate::impl_client_v17__listlabels!();
83+
crate::impl_client_v17__listlockunspent!();
84+
crate::impl_client_v17__listreceivedbyaddress!();
85+
crate::impl_client_v17__listsinceblock!();
86+
crate::impl_client_v17__listtransactions!();
87+
crate::impl_client_v17__listunspent!();
88+
crate::impl_client_v17__listwallets!();
89+
crate::impl_client_v17__loadwallet!();
90+
crate::impl_client_v17__rescanblockchain!();
91+
crate::impl_client_v17__sendmany!();
92+
crate::impl_client_v17__sendtoaddress!();
93+
crate::impl_client_v17__signmessage!();
94+
crate::impl_client_v17__signrawtransactionwithwallet!();
95+
crate::impl_client_v17__walletcreatefundedpsbt!();
96+
crate::impl_client_v17__walletprocesspsbt!();

client/src/client_sync/v21.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
//!
55
//! We ignore option arguments unless they effect the shape of the returned JSON data.
66
7+
use std::collections::BTreeMap;
8+
use std::path::Path;
9+
710
use bitcoin::address::{Address, NetworkChecked};
8-
use bitcoin::{Amount, Block, BlockHash, Txid};
11+
use bitcoin::{Amount, Block, BlockHash, PublicKey, Txid};
912

1013
use crate::client_sync::into_json;
1114
use crate::types::v21::*;
1215

1316
#[rustfmt::skip] // Keep public re-exports separate.
14-
pub use crate::client_sync::v17::AddressType;
17+
pub use crate::client_sync::{v17::AddressType, WalletCreateFundedPsbtInput};
1518

1619
crate::define_jsonrpc_minreq_client!("v21");
1720

@@ -55,13 +58,39 @@ crate::impl_client_v17__getnetworkinfo!();
5558
crate::impl_client_check_expected_server_version!({ [210200] });
5659

5760
// == Rawtransactions ==
61+
crate::impl_client_v17__createrawtransaction!();
62+
crate::impl_client_v17__fundrawtransaction!();
5863
crate::impl_client_v17__sendrawtransaction!();
5964

6065
// == Wallet ==
66+
crate::impl_client_v17__addmultisigaddress!();
67+
crate::impl_client_v17__bumpfee!();
6168
crate::impl_client_v17__createwallet!();
62-
crate::impl_client_v17__loadwallet!();
63-
crate::impl_client_v17__getnewaddress!();
69+
crate::impl_client_v17__dumpprivkey!();
70+
crate::impl_client_v17__dumpwallet!();
71+
crate::impl_client_v17__getaddressesbylabel!();
72+
crate::impl_client_v17__getaddressinfo!();
6473
crate::impl_client_v17__getbalance!();
6574
crate::impl_client_v19__getbalances!();
66-
crate::impl_client_v17__sendtoaddress!();
75+
crate::impl_client_v17__getnewaddress!();
76+
crate::impl_client_v17__getrawchangeaddress!();
77+
crate::impl_client_v17__getreceivedbyaddress!();
6778
crate::impl_client_v17__gettransaction!();
79+
crate::impl_client_v17__getunconfirmedbalance!();
80+
crate::impl_client_v17__getwalletinfo!();
81+
crate::impl_client_v17__listaddressgroupings!();
82+
crate::impl_client_v17__listlabels!();
83+
crate::impl_client_v17__listlockunspent!();
84+
crate::impl_client_v17__listreceivedbyaddress!();
85+
crate::impl_client_v17__listsinceblock!();
86+
crate::impl_client_v17__listtransactions!();
87+
crate::impl_client_v17__listunspent!();
88+
crate::impl_client_v17__listwallets!();
89+
crate::impl_client_v17__loadwallet!();
90+
crate::impl_client_v17__rescanblockchain!();
91+
crate::impl_client_v17__sendmany!();
92+
crate::impl_client_v17__sendtoaddress!();
93+
crate::impl_client_v17__signmessage!();
94+
crate::impl_client_v17__signrawtransactionwithwallet!();
95+
crate::impl_client_v17__walletcreatefundedpsbt!();
96+
crate::impl_client_v17__walletprocesspsbt!();

0 commit comments

Comments
 (0)