Skip to content

Commit 4c80f38

Browse files
committed
feat: add server.version, blockchain.silentpayments.subscribe and blockchain.silentpayments.unsubscribe
1 parent ed94df0 commit 4c80f38

4 files changed

Lines changed: 156 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tokio-util = { version = "0.7.15", features = ["compat"], optional = true }
2121
[features]
2222
default = ["tokio"]
2323
tokio = ["dep:tokio", "tokio-util"]
24+
frigate = []
2425

2526
[dev-dependencies]
2627
async-std = "1.13.0"

src/notification.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//!
66
//! - [`Notification::Header`] for `"blockchain.headers.subscribe"`
77
//! - [`Notification::ScriptHash`] for `"blockchain.scripthash.subscribe"`
8+
//! - [`Notification::SpSubscribe`] for `"blockchain.silentpayments.subscribe"`
89
//! - [`Notification::Unknown`] for unrecognized or unsupported methods
910
//!
1011
//! Each variant wraps a struct that contains the deserialized payload for that notification type.
@@ -32,6 +33,11 @@ pub enum Notification {
3233
/// status.
3334
ScriptHash(ScriptHashNotification),
3435

36+
/// A notification from `"blockchain.silentpayments.subscribe"` indicating a new history
37+
/// of transactions
38+
#[cfg(feature = "frigate")]
39+
SpSubscribe(SpNotification),
40+
3541
/// A catch-all for notifications with unrecognized methods.
3642
///
3743
/// The original [`RawNotification`] is preserved for downstream inspection.
@@ -52,6 +58,10 @@ impl Notification {
5258
"blockchain.scripthash.subscribe" => {
5359
ScriptHashNotification::deserialize(params).map(Notification::ScriptHash)
5460
}
61+
#[cfg(feature = "frigate")]
62+
"blockchain.silentpayments.subscribe" => {
63+
SpNotification::deserialize(params).map(Notification::SpSubscribe)
64+
}
5565
_ => Ok(Notification::Unknown(raw.clone())),
5666
}
5767
}
@@ -102,3 +112,30 @@ impl ScriptHashNotification {
102112
self.param_1
103113
}
104114
}
115+
116+
#[cfg(feature = "frigate")]
117+
#[derive(Debug, Clone, serde::Deserialize)]
118+
pub struct SpSubscription {
119+
pub address: String,
120+
pub labels: Vec<u32>,
121+
pub start_height: u32,
122+
}
123+
124+
#[cfg(feature = "frigate")]
125+
#[derive(Debug, Clone, serde::Deserialize)]
126+
pub struct TxTweak {
127+
pub height: u32,
128+
pub tx_hash: bitcoin::Txid,
129+
pub tweak_key: bitcoin::secp256k1::PublicKey,
130+
}
131+
132+
/// A notification indicating new confirmed transactions
133+
///
134+
/// Corresponds to `"blockchain.silentpayments.subscribe"` Frigate Electrum notification method
135+
#[cfg(feature = "frigate")]
136+
#[derive(Debug, Clone, serde::Deserialize)]
137+
pub struct SpNotification {
138+
pub subscription: SpSubscription,
139+
pub progress: f32,
140+
pub history: Vec<TxTweak>,
141+
}

src/pending_request.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ macro_rules! gen_pending_request_types {
8282
};
8383
}
8484

85+
#[cfg(not(feature = "frigate"))]
8586
gen_pending_request_types! {
8687
Header,
8788
HeaderWithProof,
@@ -106,6 +107,33 @@ gen_pending_request_types! {
106107
Custom
107108
}
108109

110+
#[cfg(feature = "frigate")]
111+
gen_pending_request_types! {
112+
Header,
113+
HeaderWithProof,
114+
Headers,
115+
HeadersWithCheckpoint,
116+
EstimateFee,
117+
HeadersSubscribe,
118+
RelayFee,
119+
GetBalance,
120+
GetHistory,
121+
GetMempool,
122+
ListUnspent,
123+
ScriptHashSubscribe,
124+
ScriptHashUnsubscribe,
125+
BroadcastTx,
126+
GetTx,
127+
GetTxMerkle,
128+
GetTxidFromPos,
129+
GetFeeHistogram,
130+
Banner,
131+
Ping,
132+
Version,
133+
SpSubscribe,
134+
SpUnSubscribe
135+
}
136+
109137
type Handler =
110138
Box<dyn FnOnce(Result<Value, Value>) -> Result<Option<Event>, serde_json::Error> + Send + Sync>;
111139

src/request.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,93 @@ impl Request for Ping {
656656
("server.ping".into(), vec![])
657657
}
658658
}
659+
660+
/// A request to establish connection with Frigate Electrum client
661+
///
662+
/// This corresponds to the `"server.version"` Frigate Electrum RPC method
663+
///
664+
/// See: https://github.com/sparrowwallet/frigate
665+
#[cfg(feature = "frigate")]
666+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
667+
pub struct Version {
668+
pub client_name: CowStr,
669+
pub version: CowStr,
670+
}
671+
672+
#[cfg(feature = "frigate")]
673+
impl Request for Version {
674+
type Response = Vec<String>;
675+
676+
fn to_method_and_params(&self) -> MethodAndParams {
677+
(
678+
"server.version".into(),
679+
vec![self.client_name.clone().into(), self.version.clone().into()],
680+
)
681+
}
682+
}
683+
684+
/// A request to subscribe to payment outputs belonging to the provided keys
685+
///
686+
/// This corresponds to the `"blockchain.silentpayments.subscribe"` Frigate Electrum RPC method.
687+
/// It returns The silent payment address that has been subscribed.
688+
///
689+
/// See: https://github.com/sparrowwallet/frigate#blockchainsilentpaymentssubscribe
690+
#[cfg(feature = "frigate")]
691+
#[derive(Debug, Clone, PartialEq, Eq)]
692+
pub struct SpSubscribe {
693+
pub scan_priv_key: bitcoin::secp256k1::SecretKey,
694+
pub scan_pub_key: bitcoin::secp256k1::PublicKey,
695+
pub start_height: Option<u32>,
696+
pub labels: Option<Vec<u32>>,
697+
}
698+
699+
#[cfg(feature = "frigate")]
700+
impl Request for SpSubscribe {
701+
type Response = String;
702+
703+
fn to_method_and_params(&self) -> MethodAndParams {
704+
let mut params = vec![
705+
serde_json::json!(self.scan_priv_key),
706+
serde_json::json!(self.scan_pub_key),
707+
];
708+
709+
if let Some(start_height) = self.start_height {
710+
params.push(start_height.into());
711+
}
712+
713+
if let Some(labels) = &self.labels {
714+
params.push(labels.clone().into());
715+
}
716+
717+
("blockchain.silentpayments.subscribe".into(), params)
718+
}
719+
}
720+
721+
/// A request to unsubscribe to payment outputs belonging to the provided keys
722+
///
723+
/// This corresponds to the `"blockchain.silentpayments.unsubscribe"` Frigate Electrum RPC method.
724+
/// It returns The silent payment address that has been subscribed.This should cancel any scans that
725+
/// may be currently running for this address.
726+
///
727+
/// See: https://github.com/sparrowwallet/frigate#blockchainsilentpaymentsunsubscribe
728+
#[cfg(feature = "frigate")]
729+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
730+
pub struct SpUnSubscribe {
731+
pub scan_priv_key: bitcoin::secp256k1::SecretKey,
732+
pub scan_pub_key: bitcoin::secp256k1::PublicKey,
733+
}
734+
735+
#[cfg(feature = "frigate")]
736+
impl Request for SpUnSubscribe {
737+
type Response = String;
738+
739+
fn to_method_and_params(&self) -> MethodAndParams {
740+
(
741+
"blockchain.silentpayments.unsubscribe".into(),
742+
vec![
743+
serde_json::json!(self.scan_priv_key),
744+
serde_json::json!(self.scan_pub_key),
745+
],
746+
)
747+
}
748+
}

0 commit comments

Comments
 (0)