Skip to content

Commit 8715d36

Browse files
authored
Merge pull request #6329 from fdefelici/refactor/bitcoin-rpc
refactor: bitcoin rpc porting
2 parents 495fe91 + cff3c9b commit 8715d36

File tree

13 files changed

+3530
-5
lines changed

13 files changed

+3530
-5
lines changed

stacks-common/src/deps_common/bitcoin/network/serialize.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use std::{error, fmt, io};
2525
use crate::address;
2626
use crate::deps_common::bitcoin::network::encodable::{ConsensusDecodable, ConsensusEncodable};
2727
use crate::deps_common::bitcoin::util::hash::Sha256dHash;
28-
use crate::util::hash::to_hex as hex_encode;
28+
use crate::util::hash::{hex_bytes, to_hex as hex_encode};
29+
use crate::util::HexError;
2930

3031
/// Serialization error
3132
#[derive(Debug)]
@@ -67,6 +68,8 @@ pub enum Error {
6768
UnrecognizedNetworkCommand(String),
6869
/// Unexpected hex digit
6970
UnexpectedHexDigit(char),
71+
/// Invalid hex input
72+
InvalidHex(HexError),
7073
}
7174

7275
impl fmt::Display for Error {
@@ -106,6 +109,7 @@ impl fmt::Display for Error {
106109
write!(f, "unrecognized network command: {nwcmd}")
107110
}
108111
Error::UnexpectedHexDigit(ref d) => write!(f, "unexpected hex digit: {d}"),
112+
Error::InvalidHex(ref e) => fmt::Display::fmt(e, f),
109113
}
110114
}
111115
}
@@ -123,7 +127,8 @@ impl error::Error for Error {
123127
| Error::UnsupportedWitnessVersion(..)
124128
| Error::UnsupportedSegwitFlag(..)
125129
| Error::UnrecognizedNetworkCommand(..)
126-
| Error::UnexpectedHexDigit(..) => None,
130+
| Error::UnexpectedHexDigit(..)
131+
| Error::InvalidHex(..) => None,
127132
}
128133
}
129134
}
@@ -142,6 +147,13 @@ impl From<io::Error> for Error {
142147
}
143148
}
144149

150+
#[doc(hidden)]
151+
impl From<HexError> for Error {
152+
fn from(error: HexError) -> Self {
153+
Error::InvalidHex(error)
154+
}
155+
}
156+
145157
/// Objects which are referred to by hash
146158
pub trait BitcoinHash {
147159
/// Produces a Sha256dHash which can be used to refer to the object
@@ -193,6 +205,15 @@ where
193205
}
194206
}
195207

208+
/// Deserialize an object from a hex-encoded string
209+
pub fn deserialize_hex<T>(data: &str) -> Result<T, Error>
210+
where
211+
for<'a> T: ConsensusDecodable<RawDecoder<Cursor<&'a [u8]>>>,
212+
{
213+
let bytes = hex_bytes(data)?;
214+
deserialize(&bytes)
215+
}
216+
196217
/// An encoder for raw binary data
197218
pub struct RawEncoder<W> {
198219
writer: W,

stacks-node/src/burnchains/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod bitcoin_regtest_controller;
22
pub mod mocknet_controller;
3+
pub mod rpc;
34

45
use std::time::Instant;
56

0 commit comments

Comments
 (0)