mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-19 05:44:12 +01:00
cln-rpc: Add OutputDesc
for {addr: amt}
style arguments
This is likely inherited from bitcoind, and a bit awkward for us, so we parse it into a classic struct, but serialize it back into the bitcoind format when talking to the RPC.
This commit is contained in:
parent
eb2aa8c51c
commit
0354a7fdb1
@ -39,4 +39,9 @@ message Feerate {
|
||||
uint32 perkb = 4;
|
||||
uint32 perkw = 5;
|
||||
}
|
||||
}
|
||||
|
||||
message OutputDesc {
|
||||
string address = 1;
|
||||
Amount amount = 2;
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
tonic::include_proto!("cln");
|
||||
|
||||
use cln_rpc::primitives::{Amount as JAmount, Utxo as JUtxo};
|
||||
use cln_rpc::primitives::{
|
||||
Amount as JAmount, Feerate as JFeerate, OutputDesc as JOutputDesc, Utxo as JUtxo,
|
||||
};
|
||||
|
||||
impl From<JAmount> for Amount {
|
||||
fn from(a: JAmount) -> Self {
|
||||
@ -34,7 +36,6 @@ impl From<&Utxo> for JUtxo {
|
||||
|
||||
impl From<&Feerate> for cln_rpc::primitives::Feerate {
|
||||
fn from(f: &Feerate) -> cln_rpc::primitives::Feerate {
|
||||
use cln_rpc::primitives::Feerate as JFeerate;
|
||||
use feerate::Style;
|
||||
match f.style.clone().unwrap() {
|
||||
Style::Slow(_) => JFeerate::Slow,
|
||||
@ -45,3 +46,12 @@ impl From<&Feerate> for cln_rpc::primitives::Feerate {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&OutputDesc> for JOutputDesc {
|
||||
fn from(od: &OutputDesc) -> JOutputDesc {
|
||||
JOutputDesc {
|
||||
address: od.address.clone(),
|
||||
amount: od.amount.as_ref().unwrap().into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -368,8 +368,60 @@ mod test {
|
||||
for (input, output) in tests.into_iter() {
|
||||
let parsed: Feerate = input.try_into().unwrap();
|
||||
assert_eq!(parsed, output);
|
||||
let serialized: String = (&parsed).into();
|
||||
assert_eq!(serialized, input);
|
||||
let serialized: String = (&parsed).into();
|
||||
assert_eq!(serialized, input);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_output_desc() {
|
||||
let a = r#"{"address":"1234msat"}"#;
|
||||
let od = serde_json::from_str(a).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
OutputDesc {
|
||||
address: "address".to_string(),
|
||||
amount: Amount { msat: 1234 }
|
||||
},
|
||||
od
|
||||
);
|
||||
let serialized: String = serde_json::to_string(&od).unwrap();
|
||||
assert_eq!(a, serialized);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct OutputDesc {
|
||||
pub address: String,
|
||||
pub amount: Amount,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for OutputDesc {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let map: std::collections::HashMap<String, Amount> =
|
||||
Deserialize::deserialize(deserializer)?;
|
||||
|
||||
let (address, amount) = map.iter().next().unwrap();
|
||||
|
||||
Ok(OutputDesc {
|
||||
address: address.to_string(),
|
||||
amount: *amount,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for OutputDesc {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
use serde::ser::SerializeMap;
|
||||
let mut map = serializer.serialize_map(Some(1))?;
|
||||
map.serialize_key(&self.address)?;
|
||||
map.serialize_value(&self.amount)?;
|
||||
map.end()
|
||||
}
|
||||
}
|
||||
|
@ -232,6 +232,7 @@ class PrimitiveField(Field):
|
||||
"number",
|
||||
"feerate",
|
||||
"utxo", # A string representing the tuple (txid, outnum)
|
||||
"OutputDesc", # A dict that maps an address to an amount (bitcoind style)
|
||||
]
|
||||
|
||||
def __init__(self, typename, path, description):
|
||||
|
Loading…
Reference in New Issue
Block a user