Skip to content

Commit

Permalink
cln-rpc: Add OutputDesc for {addr: amt} style arguments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cdecker authored and rustyrussell committed Apr 1, 2022
1 parent eb2aa8c commit 0354a7f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cln-grpc/proto/primitives.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ message Feerate {
uint32 perkb = 4;
uint32 perkw = 5;
}
}

message OutputDesc {
string address = 1;
Amount amount = 2;
}
14 changes: 12 additions & 2 deletions cln-grpc/src/pb.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
}
}
}
56 changes: 54 additions & 2 deletions cln-rpc/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
1 change: 1 addition & 0 deletions contrib/msggen/msggen/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 0354a7f

Please sign in to comment.