|
| 1 | +use bitcoin::{key::TweakedPublicKey, secp256k1::SecretKey, PublicKey}; |
| 2 | +use jsonrpc::simple_http::{self, SimpleHttpTransport}; |
| 3 | +use jsonrpc::Client; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +use serde_json::value::to_raw_value; |
| 6 | + |
| 7 | +#[derive(Debug)] |
| 8 | +pub enum FrigateError { |
| 9 | + JsonRpc(jsonrpc::Error), |
| 10 | + ParseUrl(url::ParseError), |
| 11 | + Serde(serde_json::Error), |
| 12 | +} |
| 13 | + |
| 14 | +impl From<serde_json::Error> for FrigateError { |
| 15 | + fn from(value: serde_json::Error) -> Self { |
| 16 | + FrigateError::Serde(value) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl From<url::ParseError> for FrigateError { |
| 21 | + fn from(value: url::ParseError) -> Self { |
| 22 | + Self::ParseUrl(value) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl From<jsonrpc::Error> for FrigateError { |
| 27 | + fn from(value: jsonrpc::Error) -> Self { |
| 28 | + Self::JsonRpc(value) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +pub struct FrigateClient { |
| 33 | + pub host_url: String, |
| 34 | + client: Client, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Serialize, Deserialize)] |
| 38 | +pub struct History { |
| 39 | + height: u64, |
| 40 | + tx_hash: bitcoin::BlockHash, |
| 41 | + tweak_key: TweakedPublicKey, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Serialize, Deserialize)] |
| 45 | +pub struct NotifPayload { |
| 46 | + scan_private_key: SecretKey, |
| 47 | + spend_public_key: PublicKey, |
| 48 | + address: String, |
| 49 | + labels: Option<Vec<u32>>, |
| 50 | + start_height: u64, |
| 51 | + progress: f32, |
| 52 | + history: Vec<History>, |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Serialize, Deserialize)] |
| 56 | +pub struct SubscribeRequest { |
| 57 | + scan_privkey: SecretKey, |
| 58 | + spend_pubkey: PublicKey, |
| 59 | + start_height: u64, |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Serialize, Deserialize)] |
| 63 | +pub struct UnsubscribeRequest { |
| 64 | + scan_privkey: SecretKey, |
| 65 | + spend_pubkey: PublicKey, |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Serialize, Deserialize)] |
| 69 | +pub struct GetRequest { |
| 70 | + tx_hash: bitcoin::BlockHash, |
| 71 | +} |
| 72 | + |
| 73 | +const SUBSCRIBE_RPC_METHOD: &str = "blockchain.silentpayments.subscribe"; |
| 74 | +const UNSUBSCRIBE_RPC_METHOD: &str = "blockchain.silentpayments.unsubscribe"; |
| 75 | + |
| 76 | +impl FrigateClient { |
| 77 | + pub fn new( |
| 78 | + host_url: &str, |
| 79 | + user: Option<&str>, |
| 80 | + password: Option<&str>, |
| 81 | + ) -> Result<Self, simple_http::Error> { |
| 82 | + let transport = SimpleHttpTransport::builder() |
| 83 | + .url(host_url)? |
| 84 | + .auth(user.unwrap_or(""), password) |
| 85 | + .build(); |
| 86 | + |
| 87 | + Ok(Self { |
| 88 | + host_url: host_url.to_string(), |
| 89 | + client: Client::with_transport(transport), |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + pub fn subscribe(&self, req: &SubscribeRequest) -> Result<String, FrigateError> { |
| 94 | + let params = to_raw_value(&serde_json::json!(req))?; |
| 95 | + let req = self |
| 96 | + .client |
| 97 | + .build_request(SUBSCRIBE_RPC_METHOD, Some(¶ms)); |
| 98 | + let res = self.client.send_request(req)?; |
| 99 | + Ok(res.result()?) |
| 100 | + } |
| 101 | + |
| 102 | + pub fn unsubscribe(&self, req: &UnsubscribeRequest) -> Result<String, FrigateError> { |
| 103 | + let params = to_raw_value(&serde_json::json!(req))?; |
| 104 | + let req = self |
| 105 | + .client |
| 106 | + .build_request(UNSUBSCRIBE_RPC_METHOD, Some(¶ms)); |
| 107 | + let res = self.client.send_request(req)?; |
| 108 | + Ok(res.result()?) |
| 109 | + } |
| 110 | +} |
0 commit comments