X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fconvert.rs;h=7f1bf108828753df82a0a754e82c22bc332a8664;hb=8b3567dd1aca31e46837dca423c209cf8c33eb70;hp=84b033e3a0820fc911f5d4dec42a32f0f8ba05df;hpb=f790560dadcf9910ff8d2e12118a0cc1126e2157;p=ldk-sample diff --git a/src/convert.rs b/src/convert.rs index 84b033e..7f1bf10 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,7 +1,8 @@ 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, @@ -116,3 +117,33 @@ impl TryInto for JsonResponse { }) } } + +pub struct ListUnspentUtxo { + pub txid: Txid, + pub vout: u32, + pub amount: u64, + pub address: Address, +} + +pub struct ListUnspentResponse(pub Vec); + +impl TryInto for JsonResponse { + type Error = std::io::Error; + fn try_into(self) -> Result { + 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(), + }) + .collect(); + Ok(ListUnspentResponse(utxos)) + } +}