Update to LDK 0.0.121
[ldk-sample] / src / convert.rs
index 84b033e3a0820fc911f5d4dec42a32f0f8ba05df..686761fffdc97b138361c5b251298e1e628f2596 100644 (file)
@@ -1,7 +1,7 @@
-use bitcoin::hashes::hex::FromHex;
-use bitcoin::BlockHash;
+use bitcoin::{Address, BlockHash, Txid};
 use lightning_block_sync::http::JsonResponse;
 use std::convert::TryInto;
+use std::str::FromStr;
 
 pub struct FundedTx {
        pub changepos: i64,
@@ -110,9 +110,41 @@ impl TryInto<BlockchainInfo> for JsonResponse {
        fn try_into(self) -> std::io::Result<BlockchainInfo> {
                Ok(BlockchainInfo {
                        latest_height: self.0["blocks"].as_u64().unwrap() as usize,
-                       latest_blockhash: BlockHash::from_hex(self.0["bestblockhash"].as_str().unwrap())
+                       latest_blockhash: BlockHash::from_str(self.0["bestblockhash"].as_str().unwrap())
                                .unwrap(),
                        chain: self.0["chain"].as_str().unwrap().to_string(),
                })
        }
 }
+
+pub struct ListUnspentUtxo {
+       pub txid: Txid,
+       pub vout: u32,
+       pub amount: u64,
+       pub address: Address,
+}
+
+pub struct ListUnspentResponse(pub Vec<ListUnspentUtxo>);
+
+impl TryInto<ListUnspentResponse> for JsonResponse {
+       type Error = std::io::Error;
+       fn try_into(self) -> Result<ListUnspentResponse, Self::Error> {
+               let utxos = self
+                       .0
+                       .as_array()
+                       .unwrap()
+                       .iter()
+                       .map(|utxo| ListUnspentUtxo {
+                               txid: Txid::from_str(&utxo["txid"].as_str().unwrap().to_string()).unwrap(),
+                               vout: utxo["vout"].as_u64().unwrap() as u32,
+                               amount: bitcoin::Amount::from_btc(utxo["amount"].as_f64().unwrap())
+                                       .unwrap()
+                                       .to_sat(),
+                               address: Address::from_str(&utxo["address"].as_str().unwrap().to_string())
+                                       .unwrap()
+                                       .assume_checked(), // the expected network is not known at this point
+                       })
+                       .collect();
+               Ok(ListUnspentResponse(utxos))
+       }
+}