Skip to content

Commit a1a308e

Browse files
committed
Merge branch 'master' of github.com:GideonBature/corepc into pruneblockchain
2 parents 4ea1381 + e64104b commit a1a308e

Some content is hidden

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

77 files changed

+6106
-640
lines changed

.github/workflows/rust.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ jobs:
211211
feature:
212212
[
213213
"28_0",
214+
"27_2",
214215
"27_1",
215216
"27_0",
216217
"26_2",

client/src/client_sync/error.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::{error, fmt, io};
44

5-
use bitcoin::{hex, secp256k1};
5+
use bitcoin::hex;
66

77
/// The error type for errors produced in this library.
88
#[derive(Debug)]
@@ -12,9 +12,7 @@ pub enum Error {
1212
HexToBytes(hex::HexToBytesError),
1313
Json(serde_json::error::Error),
1414
BitcoinSerialization(bitcoin::consensus::encode::FromHexError),
15-
Secp256k1(secp256k1::Error),
1615
Io(io::Error),
17-
InvalidAmount(bitcoin::amount::ParseAmountError),
1816
InvalidCookieFile,
1917
/// The JSON result had an unexpected structure.
2018
UnexpectedStructure,
@@ -46,18 +44,10 @@ impl From<bitcoin::consensus::encode::FromHexError> for Error {
4644
fn from(e: bitcoin::consensus::encode::FromHexError) -> Error { Error::BitcoinSerialization(e) }
4745
}
4846

49-
impl From<secp256k1::Error> for Error {
50-
fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) }
51-
}
52-
5347
impl From<io::Error> for Error {
5448
fn from(e: io::Error) -> Error { Error::Io(e) }
5549
}
5650

57-
impl From<bitcoin::amount::ParseAmountError> for Error {
58-
fn from(e: bitcoin::amount::ParseAmountError) -> Error { Error::InvalidAmount(e) }
59-
}
60-
6151
impl fmt::Display for Error {
6252
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6353
use Error::*;
@@ -68,9 +58,7 @@ impl fmt::Display for Error {
6858
HexToBytes(ref e) => write!(f, "hex to bytes decode error: {}", e),
6959
Json(ref e) => write!(f, "JSON error: {}", e),
7060
BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
71-
Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e),
7261
Io(ref e) => write!(f, "I/O error: {}", e),
73-
InvalidAmount(ref e) => write!(f, "invalid amount: {}", e),
7462
InvalidCookieFile => write!(f, "invalid cookie file"),
7563
UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
7664
Returned(ref s) => write!(f, "the daemon returned an error string: {}", s),
@@ -90,9 +78,7 @@ impl error::Error for Error {
9078
HexToBytes(ref e) => Some(e),
9179
Json(ref e) => Some(e),
9280
BitcoinSerialization(ref e) => Some(e),
93-
Secp256k1(ref e) => Some(e),
9481
Io(ref e) => Some(e),
95-
InvalidAmount(ref e) => Some(e),
9682
ServerVersion(ref e) => Some(e),
9783
InvalidCookieFile | UnexpectedStructure | Returned(_) | MissingUserPassword => None,
9884
}

client/src/client_sync/mod.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ pub mod v26;
1616
pub mod v27;
1717
pub mod v28;
1818

19+
use std::collections::HashMap;
1920
use std::fs::File;
2021
use std::io::{BufRead, BufReader};
2122
use std::path::PathBuf;
2223

23-
use bitcoin::Txid;
24+
use bitcoin::{Address, Amount, Txid};
2425
use serde::{Deserialize, Serialize};
2526

2627
pub use crate::client_sync::error::Error;
@@ -235,6 +236,23 @@ pub struct Input {
235236
pub sequence: Option<bitcoin::Sequence>,
236237
}
237238

239+
/// Output used as parameter to `create_raw_transaction`.
240+
// Abuse `HashMap` so we can derive serialize to get the correct JSON object.
241+
#[derive(Debug, Serialize)]
242+
pub struct Output(
243+
/// Map of address to value. Always only has a single item in it.
244+
HashMap<String, f64>,
245+
);
246+
247+
impl Output {
248+
/// Creates a single output that serializes as Core expects.
249+
pub fn new(addr: Address, value: Amount) -> Self {
250+
let mut map = HashMap::new();
251+
map.insert(addr.to_string(), value.to_btc());
252+
Output(map)
253+
}
254+
}
255+
238256
/// An element in the `inputs` argument of method `walletcreatefundedpsbt`.
239257
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
240258
pub struct WalletCreateFundedPsbtInput {

client/src/client_sync/v17/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,21 @@ crate::impl_client_v17__getnetworkinfo!();
7777
crate::impl_client_v17__getpeerinfo!();
7878

7979
// == Rawtransactions ==
80+
crate::impl_client_v17__combinepsbt!();
81+
crate::impl_client_v17__combinerawtransaction!();
82+
crate::impl_client_v17__converttopsbt!();
83+
crate::impl_client_v17__createpsbt!();
8084
crate::impl_client_v17__createrawtransaction!();
85+
crate::impl_client_v17__decodepsbt!();
86+
crate::impl_client_v17__decoderawtransaction!();
87+
crate::impl_client_v17__decodescript!();
88+
crate::impl_client_v17__finalizepsbt!();
8189
crate::impl_client_v17__fundrawtransaction!();
90+
crate::impl_client_v17__getrawtransaction!();
8291
crate::impl_client_v17__sendrawtransaction!();
92+
crate::impl_client_v17__signrawtransaction!();
93+
crate::impl_client_v17__signrawtransactionwithkey!();
94+
crate::impl_client_v17__testmempoolaccept!();
8395

8496
// == Wallet ==
8597
crate::impl_client_v17__addmultisigaddress!();

client/src/client_sync/v17/raw_transactions.rs

+191-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,67 @@
99
//!
1010
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
1111
12+
/// Implements Bitcoin Core JSON-RPC API method `combinepsbt`
13+
#[macro_export]
14+
macro_rules! impl_client_v17__combinepsbt {
15+
() => {
16+
impl Client {
17+
pub fn combine_psbt(&self, txs: &[bitcoin::Psbt]) -> Result<CombinePsbt> {
18+
let txs = txs.iter().map(|psbt| format!("{}", psbt)).collect::<Vec<String>>();
19+
self.call("combinepsbt", &[txs.into()])
20+
}
21+
}
22+
};
23+
}
24+
25+
/// Implements Bitcoin Core JSON-RPC API method `combinerawtransaction`
26+
#[macro_export]
27+
macro_rules! impl_client_v17__combinerawtransaction {
28+
() => {
29+
impl Client {
30+
pub fn combine_raw_transaction(
31+
&self,
32+
txs: &[bitcoin::Transaction],
33+
) -> Result<CombineRawTransaction> {
34+
let encoded = txs
35+
.iter()
36+
.map(|tx| bitcoin::consensus::encode::serialize_hex(tx))
37+
.collect::<Vec<String>>();
38+
self.call("combinerawtransaction", &[into_json(encoded)?])
39+
}
40+
}
41+
};
42+
}
43+
44+
/// Implements Bitcoin Core JSON-RPC API method `converttopsbt`
45+
#[macro_export]
46+
macro_rules! impl_client_v17__converttopsbt {
47+
() => {
48+
impl Client {
49+
pub fn convert_to_psbt(&self, tx: &bitcoin::Transaction) -> Result<ConvertToPsbt> {
50+
let hex = bitcoin::consensus::encode::serialize_hex(tx);
51+
self.call("converttopsbt", &[hex.into()])
52+
}
53+
}
54+
};
55+
}
56+
57+
/// Implements Bitcoin Core JSON-RPC API method `createpsbt`
58+
#[macro_export]
59+
macro_rules! impl_client_v17__createpsbt {
60+
() => {
61+
impl Client {
62+
pub fn create_psbt(
63+
&self,
64+
inputs: &[$crate::client_sync::Input],
65+
outputs: &[$crate::client_sync::Output],
66+
) -> Result<CreatePsbt> {
67+
self.call("createpsbt", &[into_json(inputs)?, into_json(outputs)?])
68+
}
69+
}
70+
};
71+
}
72+
1273
/// Implements Bitcoin Core JSON-RPC API method `createrawtransaction`
1374
#[macro_export]
1475
macro_rules! impl_client_v17__createrawtransaction {
@@ -17,24 +78,98 @@ macro_rules! impl_client_v17__createrawtransaction {
1778
pub fn create_raw_transaction(
1879
&self,
1980
inputs: &[$crate::client_sync::Input],
20-
outputs: &std::collections::BTreeMap<String, f64>, // Map of address to amount.
81+
outputs: &[$crate::client_sync::Output],
2182
) -> Result<CreateRawTransaction> {
2283
self.call("createrawtransaction", &[into_json(inputs)?, into_json(outputs)?])
2384
}
2485
}
2586
};
2687
}
2788

89+
/// Implements Bitcoin Core JSON-RPC API method `decodepsbt`
90+
#[macro_export]
91+
macro_rules! impl_client_v17__decodepsbt {
92+
() => {
93+
impl Client {
94+
pub fn decode_psbt(&self, psbt: &str) -> Result<DecodePsbt> {
95+
self.call("decodepsbt", &[psbt.into()])
96+
}
97+
}
98+
};
99+
}
100+
101+
/// Implements Bitcoin Core JSON-RPC API method `finalizepsbt`
102+
#[macro_export]
103+
macro_rules! impl_client_v17__finalizepsbt {
104+
() => {
105+
impl Client {
106+
pub fn finalize_psbt(&self, psbt: &bitcoin::Psbt) -> Result<FinalizePsbt> {
107+
let psbt = format!("{}", psbt);
108+
self.call("finalizepsbt", &[psbt.into()])
109+
}
110+
}
111+
};
112+
}
113+
114+
/// Implements Bitcoin Core JSON-RPC API method `decoderawtransaction`
115+
#[macro_export]
116+
macro_rules! impl_client_v17__decoderawtransaction {
117+
() => {
118+
impl Client {
119+
pub fn decode_raw_transaction(
120+
&self,
121+
tx: &bitcoin::Transaction,
122+
) -> Result<DecodeRawTransaction> {
123+
let hex = bitcoin::consensus::encode::serialize_hex(tx);
124+
self.call("decoderawtransaction", &[hex.into()])
125+
}
126+
}
127+
};
128+
}
129+
130+
/// Implements Bitcoin Core JSON-RPC API method `decodescript`
131+
#[macro_export]
132+
macro_rules! impl_client_v17__decodescript {
133+
() => {
134+
impl Client {
135+
// Arg is the hex encoded script we want to decode.
136+
pub fn decode_script(&self, script: &str) -> Result<DecodeScript> {
137+
self.call("decodescript", &[script.into()])
138+
}
139+
}
140+
};
141+
}
142+
28143
/// Implements Bitcoin Core JSON-RPC API method `fundrawtransaction`
29144
#[macro_export]
30145
macro_rules! impl_client_v17__fundrawtransaction {
31146
() => {
32147
impl Client {
33148
pub fn fund_raw_transaction(
34149
&self,
35-
hex: &str, // Hex encoded transaction.
150+
tx: &bitcoin::Transaction,
36151
) -> Result<FundRawTransaction> {
37-
self.call("fundrawtransaction", &[into_json(hex)?])
152+
let hex = bitcoin::consensus::encode::serialize_hex(tx);
153+
self.call("fundrawtransaction", &[hex.into()])
154+
}
155+
}
156+
};
157+
}
158+
159+
/// Implements Bitcoin Core JSON-RPC API method `getrawtransaction`
160+
#[macro_export]
161+
macro_rules! impl_client_v17__getrawtransaction {
162+
() => {
163+
impl Client {
164+
pub fn get_raw_transaction(&self, txid: bitcoin::Txid) -> Result<GetRawTransaction> {
165+
self.call("getrawtransaction", &[into_json(&txid)?, false.into()])
166+
}
167+
168+
pub fn get_raw_transaction_verbose(
169+
&self,
170+
txid: Txid,
171+
) -> Result<GetRawTransactionVerbose> {
172+
self.call("getrawtransaction", &[into_json(&txid)?, true.into()])
38173
}
39174
}
40175
};
@@ -55,3 +190,56 @@ macro_rules! impl_client_v17__sendrawtransaction {
55190
}
56191
};
57192
}
193+
194+
/// Implements Bitcoin Core JSON-RPC API method `signrawtransaction`
195+
#[macro_export]
196+
macro_rules! impl_client_v17__signrawtransaction {
197+
() => {
198+
impl Client {
199+
pub fn sign_raw_transaction(
200+
&self,
201+
tx: &bitcoin::Transaction,
202+
) -> Result<SignRawTransaction> {
203+
let hex = bitcoin::consensus::encode::serialize_hex(tx);
204+
self.call("signrawtransaction", &[hex.into()])
205+
}
206+
}
207+
};
208+
}
209+
210+
/// Implements Bitcoin Core JSON-RPC API method `signrawtransactionwithkey`
211+
#[macro_export]
212+
macro_rules! impl_client_v17__signrawtransactionwithkey {
213+
() => {
214+
impl Client {
215+
pub fn sign_raw_transaction_with_key(
216+
&self,
217+
tx: &bitcoin::Transaction,
218+
keys: &[bitcoin::PrivateKey],
219+
) -> Result<SignRawTransaction> {
220+
let hex = bitcoin::consensus::encode::serialize_hex(tx);
221+
let keys = keys.iter().map(|k| format!("{}", k)).collect::<Vec<String>>();
222+
self.call("signrawtransactionwithkey", &[hex.into(), into_json(keys)?])
223+
}
224+
}
225+
};
226+
}
227+
228+
/// Implements Bitcoin Core JSON-RPC API method `testmempoolaccept`
229+
#[macro_export]
230+
macro_rules! impl_client_v17__testmempoolaccept {
231+
() => {
232+
impl Client {
233+
pub fn test_mempool_accept(
234+
&self,
235+
txs: &[bitcoin::Transaction],
236+
) -> Result<TestMempoolAccept> {
237+
let encoded = txs
238+
.iter()
239+
.map(|tx| bitcoin::consensus::encode::serialize_hex(tx))
240+
.collect::<Vec<String>>();
241+
self.call("testmempoolaccept", &[into_json(encoded)?])
242+
}
243+
}
244+
};
245+
}

client/src/client_sync/v17/wallet.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,9 @@ macro_rules! impl_client_v17__signrawtransactionwithwallet {
414414
// `hexstring`: The transaction hex string.
415415
pub fn sign_raw_transaction_with_wallet(
416416
&self,
417-
hex: &str,
418-
) -> Result<SignRawTransactionWithWallet> {
417+
tx: &bitcoin::Transaction,
418+
) -> Result<SignRawTransaction> {
419+
let hex = bitcoin::consensus::encode::serialize_hex(tx);
419420
self.call("signrawtransactionwithwallet", &[into_json(hex)?])
420421
}
421422
}

client/src/client_sync/v18/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! We ignore option arguments unless they effect the shape of the returned JSON data.
66
77
pub mod control;
8+
pub mod raw_transactions;
89

910
use std::collections::BTreeMap;
1011
use std::path::Path;
@@ -71,9 +72,24 @@ crate::impl_client_v17__getnetworkinfo!();
7172
crate::impl_client_v17__getpeerinfo!();
7273

7374
// == Rawtransactions ==
75+
crate::impl_client_v18__analyzepsbt!();
76+
crate::impl_client_v17__combinepsbt!();
77+
crate::impl_client_v17__combinerawtransaction!();
78+
crate::impl_client_v17__converttopsbt!();
79+
crate::impl_client_v17__createpsbt!();
7480
crate::impl_client_v17__createrawtransaction!();
81+
crate::impl_client_v17__decodepsbt!();
82+
crate::impl_client_v17__decoderawtransaction!();
83+
crate::impl_client_v17__decodescript!();
84+
crate::impl_client_v17__finalizepsbt!();
7585
crate::impl_client_v17__fundrawtransaction!();
86+
crate::impl_client_v17__getrawtransaction!();
87+
crate::impl_client_v18__joinpsbts!();
7688
crate::impl_client_v17__sendrawtransaction!();
89+
crate::impl_client_v17__signrawtransaction!();
90+
crate::impl_client_v17__signrawtransactionwithkey!();
91+
crate::impl_client_v17__testmempoolaccept!();
92+
crate::impl_client_v18__utxoupdatepsbt!();
7793

7894
// == Wallet ==
7995
crate::impl_client_v17__addmultisigaddress!();

0 commit comments

Comments
 (0)