1 use bitcoin::hashes::hex::FromHex;
2 use bitcoin::BlockHash;
3 use lightning_block_sync::http::JsonResponse;
4 use std::convert::TryInto;
11 impl TryInto<FundedTx> for JsonResponse {
12 type Error = std::io::Error;
13 fn try_into(self) -> std::io::Result<FundedTx> {
15 changepos: self.0["changepos"].as_i64().unwrap(),
16 hex: self.0["hex"].as_str().unwrap().to_string(),
21 pub struct RawTx(pub String);
23 impl TryInto<RawTx> for JsonResponse {
24 type Error = std::io::Error;
25 fn try_into(self) -> std::io::Result<RawTx> {
26 Ok(RawTx(self.0.as_str().unwrap().to_string()))
35 impl TryInto<SignedTx> for JsonResponse {
36 type Error = std::io::Error;
37 fn try_into(self) -> std::io::Result<SignedTx> {
39 hex: self.0["hex"].as_str().unwrap().to_string(),
40 complete: self.0["complete"].as_bool().unwrap(),
45 pub struct NewAddress(pub String);
46 impl TryInto<NewAddress> for JsonResponse {
47 type Error = std::io::Error;
48 fn try_into(self) -> std::io::Result<NewAddress> {
49 Ok(NewAddress(self.0.as_str().unwrap().to_string()))
53 pub struct FeeResponse {
54 pub feerate: Option<u32>,
55 // pub errors: Array<String>,
59 impl TryInto<FeeResponse> for JsonResponse {
60 type Error = std::io::Error;
61 fn try_into(self) -> std::io::Result<FeeResponse> {
62 let errored = !self.0["errors"].is_null();
65 feerate: match self.0["feerate"].as_f64() {
66 Some(fee) => Some((fee * 100_000_000.0).round() as u32),
70 // // The feerate from bitcoind is in BTC/kb, and we want satoshis/kb.
71 // false => Some((self.0["feerate"].as_f64().unwrap() * 100_000_000.0).round() as u32),
76 pub struct BlockchainInfo {
77 pub latest_height: usize,
78 pub latest_blockhash: BlockHash,
81 impl TryInto<BlockchainInfo> for JsonResponse {
82 type Error = std::io::Error;
83 fn try_into(self) -> std::io::Result<BlockchainInfo> {
85 latest_height: self.0["blocks"].as_u64().unwrap() as usize,
86 latest_blockhash: BlockHash::from_hex(self.0["bestblockhash"].as_str().unwrap())