diff --git a/Cargo.lock b/Cargo.lock index 22686ed31..83b287a1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,6 +72,15 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "bitcoin_hashes" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "006cc91e1a1d99819bc5b8214be3555c1f0611b169f527a1fdc54ed1f2b745b0" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -117,6 +126,7 @@ name = "cln-grpc" version = "0.0.1" dependencies = [ "anyhow", + "bitcoin_hashes", "cln-rpc", "hex", "log", @@ -164,6 +174,7 @@ name = "cln-rpc" version = "0.1.0" dependencies = [ "anyhow", + "bitcoin_hashes", "bytes", "env_logger", "futures-util", diff --git a/cln-grpc/Cargo.toml b/cln-grpc/Cargo.toml index 0d620dc0e..d76886919 100644 --- a/cln-grpc/Cargo.toml +++ b/cln-grpc/Cargo.toml @@ -10,6 +10,7 @@ cln-rpc = { path="../cln-rpc" } tonic = { version = "^0.5", features = ["tls", "transport"] } prost = "0.8" hex = "0.4.3" +bitcoin_hashes = { version = "0.10.0", features = [ "serde" ] } [dev-dependencies] serde_json = "1.0.72" diff --git a/cln-grpc/src/convert.rs b/cln-grpc/src/convert.rs index a8cfec26e..4543f4f1d 100644 --- a/cln-grpc/src/convert.rs +++ b/cln-grpc/src/convert.rs @@ -8,6 +8,8 @@ use std::convert::From; use cln_rpc::model::{responses,requests}; use crate::pb; use std::str::FromStr; +use bitcoin_hashes::sha256::Hash as Sha256; +use bitcoin_hashes::Hash; #[allow(unused_variables)] impl From for pb::GetinfoAddress { @@ -1020,7 +1022,7 @@ impl From for requests::SendpayRequest { fn from(c: pb::SendpayRequest) -> Self { Self { route: c.route.into_iter().map(|s| s.into()).collect(), // Rule #4 - payment_hash: c.payment_hash.try_into().unwrap(), // Rule #1 for type hash + payment_hash: Sha256::from_slice(&c.payment_hash).unwrap(), // Rule #1 for type hash label: c.label, // Rule #1 for type string? amount_msat: c.amount_msat.map(|a| a.into()), // Rule #1 for type msat? bolt11: c.bolt11, // Rule #1 for type string? @@ -1218,14 +1220,14 @@ impl From for requests::SendonionRequest { fn from(c: pb::SendonionRequest) -> Self { Self { onion: hex::encode(&c.onion), // Rule #1 for type hex - payment_hash: c.payment_hash.try_into().unwrap(), // Rule #1 for type hash + payment_hash: Sha256::from_slice(&c.payment_hash).unwrap(), // Rule #1 for type hash label: c.label, // Rule #1 for type string? shared_secrets: Some(c.shared_secrets.into_iter().map(|s| s.try_into().unwrap()).collect()), // Rule #4 partid: c.partid.map(|v| v as u16), // Rule #1 for type u16? bolt11: c.bolt11, // Rule #1 for type string? amount_msat: c.amount_msat.map(|a| a.into()), // Rule #1 for type msat? destination: c.destination.map(|v| cln_rpc::primitives::Pubkey::from_slice(&v[..]).unwrap()), // Rule #1 for type pubkey? - localofferid: c.localofferid.map(|v| v.try_into().unwrap()), // Rule #1 for type hash? + localofferid: c.localofferid.map(|v| Sha256::from_slice(&v).unwrap()), // Rule #1 for type hash? groupid: c.groupid, // Rule #1 for type u64? } } @@ -1236,7 +1238,7 @@ impl From for requests::ListsendpaysRequest { fn from(c: pb::ListsendpaysRequest) -> Self { Self { bolt11: c.bolt11, // Rule #1 for type string? - payment_hash: c.payment_hash.map(|v| v.try_into().unwrap()), // Rule #1 for type hash? + payment_hash: c.payment_hash.map(|v| Sha256::from_slice(&v).unwrap()), // Rule #1 for type hash? status: c.status.map(|v| v.try_into().unwrap()), } } @@ -1302,7 +1304,7 @@ impl From for requests::WaitinvoiceRequest { impl From for requests::WaitsendpayRequest { fn from(c: pb::WaitsendpayRequest) -> Self { Self { - payment_hash: c.payment_hash.try_into().unwrap(), // Rule #1 for type hash + payment_hash: Sha256::from_slice(&c.payment_hash).unwrap(), // Rule #1 for type hash timeout: c.timeout, // Rule #1 for type u32? partid: c.partid, // Rule #1 for type u64? groupid: c.groupid, // Rule #1 for type u64? @@ -1502,7 +1504,7 @@ impl From for requests::ListpaysRequest { fn from(c: pb::ListpaysRequest) -> Self { Self { bolt11: c.bolt11, // Rule #1 for type string? - payment_hash: c.payment_hash.map(|v| v.try_into().unwrap()), // Rule #1 for type hash? + payment_hash: c.payment_hash.map(|v| Sha256::from_slice(&v).unwrap()), // Rule #1 for type hash? status: c.status.map(|v| v.try_into().unwrap()), } } diff --git a/cln-grpc/src/pb.rs b/cln-grpc/src/pb.rs index 00291d198..734f91fc7 100644 --- a/cln-grpc/src/pb.rs +++ b/cln-grpc/src/pb.rs @@ -1,5 +1,6 @@ tonic::include_proto!("cln"); use std::str::FromStr; +use bitcoin_hashes::Hash; use cln_rpc::primitives::{ Amount as JAmount, AmountOrAll as JAmountOrAll, AmountOrAny as JAmountOrAny, @@ -21,7 +22,7 @@ impl From for JAmount { impl From for Outpoint { fn from(a: JOutpoint) -> Self { Outpoint { - txid: a.txid, + txid: a.txid.to_vec(), outnum: a.outnum, } } @@ -30,7 +31,7 @@ impl From for Outpoint { impl From for JOutpoint { fn from(a: Outpoint) -> Self { JOutpoint { - txid: a.txid, + txid: bitcoin_hashes::sha256::Hash::from_slice(&a.txid).unwrap(), outnum: a.outnum, } } diff --git a/cln-rpc/Cargo.toml b/cln-rpc/Cargo.toml index 6ba034df6..4346641b6 100644 --- a/cln-rpc/Cargo.toml +++ b/cln-rpc/Cargo.toml @@ -9,6 +9,7 @@ path = "examples/getinfo.rs" [dependencies] anyhow = "1.0.51" +bitcoin_hashes = { version = "0.10.0", features = [ "serde" ] } bytes = "1.1.0" log = "0.4.14" serde = { version = "1.0.131", features = ["derive"] } diff --git a/cln-rpc/src/model.rs b/cln-rpc/src/model.rs index dcad0497f..06f004aa7 100644 --- a/cln-rpc/src/model.rs +++ b/cln-rpc/src/model.rs @@ -116,16 +116,35 @@ pub enum Response { Stop(responses::StopResponse), } + +pub trait IntoRequest: Into { + type Response: TryFrom; +} + +#[derive(Debug)] +pub struct TryFromResponseError; + pub mod requests { #[allow(unused_imports)] use crate::primitives::*; #[allow(unused_imports)] use serde::{{Deserialize, Serialize}}; + use super::{IntoRequest, Request}; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct GetinfoRequest { } + impl From for Request { + fn from(r: GetinfoRequest) -> Self { + Request::Getinfo(r) + } + } + + impl IntoRequest for GetinfoRequest { + type Response = super::responses::GetinfoResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListpeersRequest { #[serde(alias = "id", skip_serializing_if = "Option::is_none")] @@ -134,12 +153,32 @@ pub mod requests { pub level: Option, } + impl From for Request { + fn from(r: ListpeersRequest) -> Self { + Request::ListPeers(r) + } + } + + impl IntoRequest for ListpeersRequest { + type Response = super::responses::ListpeersResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListfundsRequest { #[serde(alias = "spent", skip_serializing_if = "Option::is_none")] pub spent: Option, } + impl From for Request { + fn from(r: ListfundsRequest) -> Self { + Request::ListFunds(r) + } + } + + impl IntoRequest for ListfundsRequest { + type Response = super::responses::ListfundsResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SendpayRoute { #[serde(alias = "amount_msat")] @@ -174,6 +213,16 @@ pub mod requests { pub groupid: Option, } + impl From for Request { + fn from(r: SendpayRequest) -> Self { + Request::SendPay(r) + } + } + + impl IntoRequest for SendpayRequest { + type Response = super::responses::SendpayResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListchannelsRequest { #[serde(alias = "short_channel_id", skip_serializing_if = "Option::is_none")] @@ -184,12 +233,32 @@ pub mod requests { pub destination: Option, } + impl From for Request { + fn from(r: ListchannelsRequest) -> Self { + Request::ListChannels(r) + } + } + + impl IntoRequest for ListchannelsRequest { + type Response = super::responses::ListchannelsResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct AddgossipRequest { #[serde(alias = "message")] pub message: String, } + impl From for Request { + fn from(r: AddgossipRequest) -> Self { + Request::AddGossip(r) + } + } + + impl IntoRequest for AddgossipRequest { + type Response = super::responses::AddgossipResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct AutocleaninvoiceRequest { #[serde(alias = "expired_by", skip_serializing_if = "Option::is_none")] @@ -198,6 +267,16 @@ pub mod requests { pub cycle_seconds: Option, } + impl From for Request { + fn from(r: AutocleaninvoiceRequest) -> Self { + Request::AutoCleanInvoice(r) + } + } + + impl IntoRequest for AutocleaninvoiceRequest { + type Response = super::responses::AutocleaninvoiceResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct CheckmessageRequest { #[serde(alias = "message")] @@ -208,6 +287,16 @@ pub mod requests { pub pubkey: Option, } + impl From for Request { + fn from(r: CheckmessageRequest) -> Self { + Request::CheckMessage(r) + } + } + + impl IntoRequest for CheckmessageRequest { + type Response = super::responses::CheckmessageResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct CloseRequest { #[serde(alias = "id")] @@ -226,6 +315,16 @@ pub mod requests { pub feerange: Option>, } + impl From for Request { + fn from(r: CloseRequest) -> Self { + Request::Close(r) + } + } + + impl IntoRequest for CloseRequest { + type Response = super::responses::CloseResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ConnectRequest { #[serde(alias = "id")] @@ -236,6 +335,16 @@ pub mod requests { pub port: Option, } + impl From for Request { + fn from(r: ConnectRequest) -> Self { + Request::Connect(r) + } + } + + impl IntoRequest for ConnectRequest { + type Response = super::responses::ConnectResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct CreateinvoiceRequest { #[serde(alias = "invstring")] @@ -246,6 +355,16 @@ pub mod requests { pub preimage: String, } + impl From for Request { + fn from(r: CreateinvoiceRequest) -> Self { + Request::CreateInvoice(r) + } + } + + impl IntoRequest for CreateinvoiceRequest { + type Response = super::responses::CreateinvoiceResponse; + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum DatastoreMode { #[serde(rename = "must-create")] @@ -287,6 +406,16 @@ pub mod requests { pub generation: Option, } + impl From for Request { + fn from(r: DatastoreRequest) -> Self { + Request::Datastore(r) + } + } + + impl IntoRequest for DatastoreRequest { + type Response = super::responses::DatastoreResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct CreateonionHops { #[serde(alias = "pubkey")] @@ -307,6 +436,16 @@ pub mod requests { pub onion_size: Option, } + impl From for Request { + fn from(r: CreateonionRequest) -> Self { + Request::CreateOnion(r) + } + } + + impl IntoRequest for CreateonionRequest { + type Response = super::responses::CreateonionResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DeldatastoreRequest { #[serde(alias = "key")] @@ -315,12 +454,32 @@ pub mod requests { pub generation: Option, } + impl From for Request { + fn from(r: DeldatastoreRequest) -> Self { + Request::DelDatastore(r) + } + } + + impl IntoRequest for DeldatastoreRequest { + type Response = super::responses::DeldatastoreResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DelexpiredinvoiceRequest { #[serde(alias = "maxexpirytime", skip_serializing_if = "Option::is_none")] pub maxexpirytime: Option, } + impl From for Request { + fn from(r: DelexpiredinvoiceRequest) -> Self { + Request::DelExpiredInvoice(r) + } + } + + impl IntoRequest for DelexpiredinvoiceRequest { + type Response = super::responses::DelexpiredinvoiceResponse; + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum DelinvoiceStatus { #[serde(rename = "paid")] @@ -353,6 +512,16 @@ pub mod requests { pub desconly: Option, } + impl From for Request { + fn from(r: DelinvoiceRequest) -> Self { + Request::DelInvoice(r) + } + } + + impl IntoRequest for DelinvoiceRequest { + type Response = super::responses::DelinvoiceResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct InvoiceRequest { #[serde(alias = "amount_msat")] @@ -375,12 +544,32 @@ pub mod requests { pub deschashonly: Option, } + impl From for Request { + fn from(r: InvoiceRequest) -> Self { + Request::Invoice(r) + } + } + + impl IntoRequest for InvoiceRequest { + type Response = super::responses::InvoiceResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListdatastoreRequest { #[serde(alias = "key", skip_serializing_if = "crate::is_none_or_empty")] pub key: Option>, } + impl From for Request { + fn from(r: ListdatastoreRequest) -> Self { + Request::ListDatastore(r) + } + } + + impl IntoRequest for ListdatastoreRequest { + type Response = super::responses::ListdatastoreResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListinvoicesRequest { #[serde(alias = "label", skip_serializing_if = "Option::is_none")] @@ -393,6 +582,16 @@ pub mod requests { pub offer_id: Option, } + impl From for Request { + fn from(r: ListinvoicesRequest) -> Self { + Request::ListInvoices(r) + } + } + + impl IntoRequest for ListinvoicesRequest { + type Response = super::responses::ListinvoicesResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SendonionFirst_hop { #[serde(alias = "id")] @@ -427,6 +626,16 @@ pub mod requests { pub groupid: Option, } + impl From for Request { + fn from(r: SendonionRequest) -> Self { + Request::SendOnion(r) + } + } + + impl IntoRequest for SendonionRequest { + type Response = super::responses::SendonionResponse; + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListsendpaysStatus { #[serde(rename = "pending")] @@ -458,10 +667,30 @@ pub mod requests { pub status: Option, } + impl From for Request { + fn from(r: ListsendpaysRequest) -> Self { + Request::ListSendPays(r) + } + } + + impl IntoRequest for ListsendpaysRequest { + type Response = super::responses::ListsendpaysResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListtransactionsRequest { } + impl From for Request { + fn from(r: ListtransactionsRequest) -> Self { + Request::ListTransactions(r) + } + } + + impl IntoRequest for ListtransactionsRequest { + type Response = super::responses::ListtransactionsResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct PayRequest { #[serde(alias = "bolt11")] @@ -490,12 +719,32 @@ pub mod requests { pub description: Option, } + impl From for Request { + fn from(r: PayRequest) -> Self { + Request::Pay(r) + } + } + + impl IntoRequest for PayRequest { + type Response = super::responses::PayResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListnodesRequest { #[serde(alias = "id", skip_serializing_if = "Option::is_none")] pub id: Option, } + impl From for Request { + fn from(r: ListnodesRequest) -> Self { + Request::ListNodes(r) + } + } + + impl IntoRequest for ListnodesRequest { + type Response = super::responses::ListnodesResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct WaitanyinvoiceRequest { #[serde(alias = "lastpay_index", skip_serializing_if = "Option::is_none")] @@ -504,12 +753,32 @@ pub mod requests { pub timeout: Option, } + impl From for Request { + fn from(r: WaitanyinvoiceRequest) -> Self { + Request::WaitAnyInvoice(r) + } + } + + impl IntoRequest for WaitanyinvoiceRequest { + type Response = super::responses::WaitanyinvoiceResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct WaitinvoiceRequest { #[serde(alias = "label")] pub label: String, } + impl From for Request { + fn from(r: WaitinvoiceRequest) -> Self { + Request::WaitInvoice(r) + } + } + + impl IntoRequest for WaitinvoiceRequest { + type Response = super::responses::WaitinvoiceResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct WaitsendpayRequest { #[serde(alias = "payment_hash")] @@ -522,6 +791,16 @@ pub mod requests { pub groupid: Option, } + impl From for Request { + fn from(r: WaitsendpayRequest) -> Self { + Request::WaitSendPay(r) + } + } + + impl IntoRequest for WaitsendpayRequest { + type Response = super::responses::WaitsendpayResponse; + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum NewaddrAddresstype { #[serde(rename = "bech32")] @@ -549,6 +828,16 @@ pub mod requests { pub addresstype: Option, } + impl From for Request { + fn from(r: NewaddrRequest) -> Self { + Request::NewAddr(r) + } + } + + impl IntoRequest for NewaddrRequest { + type Response = super::responses::NewaddrResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct WithdrawRequest { #[serde(alias = "destination")] @@ -563,6 +852,16 @@ pub mod requests { pub utxos: Option>, } + impl From for Request { + fn from(r: WithdrawRequest) -> Self { + Request::Withdraw(r) + } + } + + impl IntoRequest for WithdrawRequest { + type Response = super::responses::WithdrawResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct KeysendExtratlvs { } @@ -587,6 +886,16 @@ pub mod requests { pub routehints: Option, } + impl From for Request { + fn from(r: KeysendRequest) -> Self { + Request::KeySend(r) + } + } + + impl IntoRequest for KeysendRequest { + type Response = super::responses::KeysendResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct FundpsbtRequest { #[serde(alias = "satoshi")] @@ -607,6 +916,16 @@ pub mod requests { pub excess_as_change: Option, } + impl From for Request { + fn from(r: FundpsbtRequest) -> Self { + Request::FundPsbt(r) + } + } + + impl IntoRequest for FundpsbtRequest { + type Response = super::responses::FundpsbtResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SendpsbtRequest { #[serde(alias = "psbt")] @@ -615,6 +934,16 @@ pub mod requests { pub reserve: Option, } + impl From for Request { + fn from(r: SendpsbtRequest) -> Self { + Request::SendPsbt(r) + } + } + + impl IntoRequest for SendpsbtRequest { + type Response = super::responses::SendpsbtResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SignpsbtRequest { #[serde(alias = "psbt")] @@ -623,6 +952,16 @@ pub mod requests { pub signonly: Option>, } + impl From for Request { + fn from(r: SignpsbtRequest) -> Self { + Request::SignPsbt(r) + } + } + + impl IntoRequest for SignpsbtRequest { + type Response = super::responses::SignpsbtResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct UtxopsbtRequest { #[serde(alias = "satoshi")] @@ -645,12 +984,32 @@ pub mod requests { pub excess_as_change: Option, } + impl From for Request { + fn from(r: UtxopsbtRequest) -> Self { + Request::UtxoPsbt(r) + } + } + + impl IntoRequest for UtxopsbtRequest { + type Response = super::responses::UtxopsbtResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TxdiscardRequest { #[serde(alias = "txid")] pub txid: String, } + impl From for Request { + fn from(r: TxdiscardRequest) -> Self { + Request::TxDiscard(r) + } + } + + impl IntoRequest for TxdiscardRequest { + type Response = super::responses::TxdiscardResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TxprepareRequest { #[serde(alias = "outputs")] @@ -663,12 +1022,32 @@ pub mod requests { pub utxos: Option>, } + impl From for Request { + fn from(r: TxprepareRequest) -> Self { + Request::TxPrepare(r) + } + } + + impl IntoRequest for TxprepareRequest { + type Response = super::responses::TxprepareResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TxsendRequest { #[serde(alias = "txid")] pub txid: String, } + impl From for Request { + fn from(r: TxsendRequest) -> Self { + Request::TxSend(r) + } + } + + impl IntoRequest for TxsendRequest { + type Response = super::responses::TxsendResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DisconnectRequest { #[serde(alias = "id")] @@ -677,6 +1056,16 @@ pub mod requests { pub force: Option, } + impl From for Request { + fn from(r: DisconnectRequest) -> Self { + Request::Disconnect(r) + } + } + + impl IntoRequest for DisconnectRequest { + type Response = super::responses::DisconnectResponse; + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum FeeratesStyle { #[serde(rename = "perkb")] @@ -702,6 +1091,16 @@ pub mod requests { pub style: FeeratesStyle, } + impl From for Request { + fn from(r: FeeratesRequest) -> Self { + Request::Feerates(r) + } + } + + impl IntoRequest for FeeratesRequest { + type Response = super::responses::FeeratesResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct FundchannelRequest { #[serde(alias = "id")] @@ -730,6 +1129,16 @@ pub mod requests { pub reserve: Option, } + impl From for Request { + fn from(r: FundchannelRequest) -> Self { + Request::FundChannel(r) + } + } + + impl IntoRequest for FundchannelRequest { + type Response = super::responses::FundchannelResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct GetrouteRequest { #[serde(alias = "id")] @@ -750,6 +1159,16 @@ pub mod requests { pub maxhops: Option, } + impl From for Request { + fn from(r: GetrouteRequest) -> Self { + Request::GetRoute(r) + } + } + + impl IntoRequest for GetrouteRequest { + type Response = super::responses::GetrouteResponse; + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListforwardsStatus { #[serde(rename = "offered")] @@ -784,6 +1203,16 @@ pub mod requests { pub out_channel: Option, } + impl From for Request { + fn from(r: ListforwardsRequest) -> Self { + Request::ListForwards(r) + } + } + + impl IntoRequest for ListforwardsRequest { + type Response = super::responses::ListforwardsResponse; + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListpaysStatus { #[serde(rename = "pending")] @@ -815,6 +1244,16 @@ pub mod requests { pub status: Option, } + impl From for Request { + fn from(r: ListpaysRequest) -> Self { + Request::ListPays(r) + } + } + + impl IntoRequest for ListpaysRequest { + type Response = super::responses::ListpaysResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct PingRequest { #[serde(alias = "id")] @@ -825,16 +1264,46 @@ pub mod requests { pub pongbytes: Option, } + impl From for Request { + fn from(r: PingRequest) -> Self { + Request::Ping(r) + } + } + + impl IntoRequest for PingRequest { + type Response = super::responses::PingResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SignmessageRequest { #[serde(alias = "message")] pub message: String, } + impl From for Request { + fn from(r: SignmessageRequest) -> Self { + Request::SignMessage(r) + } + } + + impl IntoRequest for SignmessageRequest { + type Response = super::responses::SignmessageResponse; + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct StopRequest { } + impl From for Request { + fn from(r: StopRequest) -> Self { + Request::Stop(r) + } + } + + impl IntoRequest for StopRequest { + type Response = super::responses::StopResponse; + } + } @@ -843,6 +1312,7 @@ pub mod responses { use crate::primitives::*; #[allow(unused_imports)] use serde::{{Deserialize, Serialize}}; + use super::{TryFromResponseError, Response}; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct GetinfoOur_features { @@ -975,6 +1445,17 @@ pub mod responses { pub warning_lightningd_sync: Option, } + impl TryFrom for GetinfoResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Getinfo(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListpeersPeersLogType { #[serde(rename = "SKIPPED")] @@ -1302,6 +1783,17 @@ pub mod responses { pub peers: Vec, } + impl TryFrom for ListpeersResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListPeers(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListfundsOutputsStatus { #[serde(rename = "unconfirmed")] @@ -1375,6 +1867,17 @@ pub mod responses { pub channels: Vec, } + impl TryFrom for ListfundsResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListFunds(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// status of the payment (could be complete if already sent previously) #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum SendpayStatus { @@ -1429,6 +1932,17 @@ pub mod responses { pub message: Option, } + impl TryFrom for SendpayResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::SendPay(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListchannelsChannels { #[serde(alias = "source")] @@ -1469,10 +1983,32 @@ pub mod responses { pub channels: Vec, } + impl TryFrom for ListchannelsResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListChannels(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct AddgossipResponse { } + impl TryFrom for AddgossipResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::AddGossip(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct AutocleaninvoiceResponse { #[serde(alias = "enabled")] @@ -1483,6 +2019,17 @@ pub mod responses { pub cycle_seconds: Option, } + impl TryFrom for AutocleaninvoiceResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::AutoCleanInvoice(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct CheckmessageResponse { #[serde(alias = "verified")] @@ -1491,6 +2038,17 @@ pub mod responses { pub pubkey: Pubkey, } + impl TryFrom for CheckmessageResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::CheckMessage(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// Whether we successfully negotiated a mutual close, closed without them, or discarded not-yet-opened channel #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum CloseType { @@ -1524,6 +2082,17 @@ pub mod responses { pub txid: Option, } + impl TryFrom for CloseResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Close(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// Whether they initiated connection or we did #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ConnectDirection { @@ -1595,6 +2164,17 @@ pub mod responses { pub direction: ConnectDirection, } + impl TryFrom for ConnectResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Connect(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// Whether it has been paid, or can no longer be paid #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum CreateinvoiceStatus { @@ -1650,6 +2230,17 @@ pub mod responses { pub payer_note: Option, } + impl TryFrom for CreateinvoiceResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::CreateInvoice(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DatastoreResponse { #[serde(alias = "key")] @@ -1662,6 +2253,17 @@ pub mod responses { pub string: Option, } + impl TryFrom for DatastoreResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Datastore(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct CreateonionResponse { #[serde(alias = "onion")] @@ -1670,6 +2272,17 @@ pub mod responses { pub shared_secrets: Vec, } + impl TryFrom for CreateonionResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::CreateOnion(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DeldatastoreResponse { #[serde(alias = "key")] @@ -1682,10 +2295,32 @@ pub mod responses { pub string: Option, } + impl TryFrom for DeldatastoreResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::DelDatastore(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DelexpiredinvoiceResponse { } + impl TryFrom for DelexpiredinvoiceResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::DelExpiredInvoice(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// State of invoice #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum DelinvoiceStatus { @@ -1733,6 +2368,17 @@ pub mod responses { pub payer_note: Option, } + impl TryFrom for DelinvoiceResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::DelInvoice(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct InvoiceResponse { #[serde(alias = "bolt11")] @@ -1755,6 +2401,17 @@ pub mod responses { pub warning_mpp: Option, } + impl TryFrom for InvoiceResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Invoice(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ListdatastoreDatastore { #[serde(alias = "key")] @@ -1773,6 +2430,17 @@ pub mod responses { pub datastore: Vec, } + impl TryFrom for ListdatastoreResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListDatastore(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// Whether it's paid, unpaid or unpayable #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListinvoicesInvoicesStatus { @@ -1834,6 +2502,17 @@ pub mod responses { pub invoices: Vec, } + impl TryFrom for ListinvoicesResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListInvoices(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// status of the payment (could be complete if already sent previously) #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum SendonionStatus { @@ -1884,6 +2563,17 @@ pub mod responses { pub message: Option, } + impl TryFrom for SendonionResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::SendOnion(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// status of the payment #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListsendpaysPaymentsStatus { @@ -1945,6 +2635,17 @@ pub mod responses { pub payments: Vec, } + impl TryFrom for ListsendpaysResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListSendPays(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// the purpose of this input (*EXPERIMENTAL_FEATURES* only) #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListtransactionsTransactionsInputsType { @@ -2093,6 +2794,17 @@ pub mod responses { pub transactions: Vec, } + impl TryFrom for ListtransactionsResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListTransactions(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// status of payment #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum PayStatus { @@ -2138,6 +2850,17 @@ pub mod responses { pub status: PayStatus, } + impl TryFrom for PayResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Pay(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// Type of connection #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListnodesNodesAddressesType { @@ -2202,6 +2925,17 @@ pub mod responses { pub nodes: Vec, } + impl TryFrom for ListnodesResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListNodes(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// Whether it's paid or expired #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum WaitanyinvoiceStatus { @@ -2250,6 +2984,17 @@ pub mod responses { pub payment_preimage: Option, } + impl TryFrom for WaitanyinvoiceResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::WaitAnyInvoice(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// Whether it's paid or expired #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum WaitinvoiceStatus { @@ -2298,6 +3043,17 @@ pub mod responses { pub payment_preimage: Option, } + impl TryFrom for WaitinvoiceResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::WaitInvoice(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// status of the payment #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum WaitsendpayStatus { @@ -2347,6 +3103,17 @@ pub mod responses { pub payment_preimage: Option, } + impl TryFrom for WaitsendpayResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::WaitSendPay(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct NewaddrResponse { #[serde(alias = "bech32", skip_serializing_if = "Option::is_none")] @@ -2355,6 +3122,17 @@ pub mod responses { pub p2sh_segwit: Option, } + impl TryFrom for NewaddrResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::NewAddr(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct WithdrawResponse { #[serde(alias = "tx")] @@ -2365,6 +3143,17 @@ pub mod responses { pub psbt: String, } + impl TryFrom for WithdrawResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Withdraw(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// status of payment #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum KeysendStatus { @@ -2404,6 +3193,17 @@ pub mod responses { pub status: KeysendStatus, } + impl TryFrom for KeysendResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::KeySend(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct FundpsbtReservations { #[serde(alias = "txid")] @@ -2434,6 +3234,17 @@ pub mod responses { pub reservations: Option>, } + impl TryFrom for FundpsbtResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::FundPsbt(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SendpsbtResponse { #[serde(alias = "tx")] @@ -2442,12 +3253,34 @@ pub mod responses { pub txid: String, } + impl TryFrom for SendpsbtResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::SendPsbt(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SignpsbtResponse { #[serde(alias = "signed_psbt")] pub signed_psbt: String, } + impl TryFrom for SignpsbtResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::SignPsbt(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct UtxopsbtReservations { #[serde(alias = "txid")] @@ -2478,6 +3311,17 @@ pub mod responses { pub reservations: Option>, } + impl TryFrom for UtxopsbtResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::UtxoPsbt(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TxdiscardResponse { #[serde(alias = "unsigned_tx")] @@ -2486,6 +3330,17 @@ pub mod responses { pub txid: String, } + impl TryFrom for TxdiscardResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::TxDiscard(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TxprepareResponse { #[serde(alias = "psbt")] @@ -2496,6 +3351,17 @@ pub mod responses { pub txid: String, } + impl TryFrom for TxprepareResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::TxPrepare(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TxsendResponse { #[serde(alias = "psbt")] @@ -2506,10 +3372,32 @@ pub mod responses { pub txid: String, } + impl TryFrom for TxsendResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::TxSend(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DisconnectResponse { } + impl TryFrom for DisconnectResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Disconnect(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct FeeratesPerkb { #[serde(alias = "min_acceptable")] @@ -2570,6 +3458,17 @@ pub mod responses { pub warning_missing_feerates: Option, } + impl TryFrom for FeeratesResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Feerates(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct FundchannelResponse { #[serde(alias = "tx")] @@ -2586,6 +3485,17 @@ pub mod responses { pub mindepth: Option, } + impl TryFrom for FundchannelResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::FundChannel(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// The features understood by the destination node #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum GetrouteRouteStyle { @@ -2625,6 +3535,17 @@ pub mod responses { pub route: Vec, } + impl TryFrom for GetrouteResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::GetRoute(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// still ongoing, completed, failed locally, or failed after forwarding #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListforwardsForwardsStatus { @@ -2700,6 +3621,17 @@ pub mod responses { pub forwards: Vec, } + impl TryFrom for ListforwardsResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListForwards(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + /// status of the payment #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum ListpaysPaysStatus { @@ -2757,12 +3689,34 @@ pub mod responses { pub pays: Vec, } + impl TryFrom for ListpaysResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::ListPays(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct PingResponse { #[serde(alias = "totlen")] pub totlen: u16, } + impl TryFrom for PingResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Ping(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SignmessageResponse { #[serde(alias = "signature")] @@ -2773,9 +3727,31 @@ pub mod responses { pub zbase: String, } + impl TryFrom for SignmessageResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::SignMessage(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct StopResponse { } + impl TryFrom for StopResponse { + type Error = super::TryFromResponseError; + + fn try_from(response: Response) -> Result { + match response { + Response::Stop(response) => Ok(response), + _ => Err(TryFromResponseError) + } + } + } + } diff --git a/cln-rpc/src/primitives.rs b/cln-rpc/src/primitives.rs index e8a5c504b..1dcc3e0ac 100644 --- a/cln-rpc/src/primitives.rs +++ b/cln-rpc/src/primitives.rs @@ -5,6 +5,9 @@ use serde::{Deserialize, Serialize}; use serde::{Deserializer, Serializer}; use std::str::FromStr; use std::string::ToString; +use bitcoin_hashes::Hash as BitcoinHash; + +pub use bitcoin_hashes::sha256::Hash as Sha256; #[derive(Copy, Clone, Serialize, Deserialize, Debug)] #[allow(non_camel_case_types)] @@ -224,51 +227,9 @@ impl Secret { } } -#[derive(Clone, Debug)] -pub struct Sha256([u8; 32]); -impl Sha256 { - pub fn to_vec(self) -> Vec { - self.0.to_vec() - } -} - -impl TryFrom> for Sha256 { - type Error = crate::Error; - fn try_from(v: Vec) -> Result { - if v.len() != 32 { - Err(anyhow!("Unexpected hash length: {}", hex::encode(v))) - } else { - Ok(Sha256(v.try_into().unwrap())) - } - } -} - -impl Serialize for Sha256 { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(&hex::encode(&self.0)) - } -} - -impl<'de> Deserialize<'de> for Sha256 { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - use serde::de::Error; - let s: String = Deserialize::deserialize(deserializer)?; - let h = hex::decode(s).map_err(|_| Error::custom("not a valid hex string"))?; - Ok(Sha256(h.try_into().map_err(|_| { - Error::custom("not a valid hex-encoded hash") - })?)) - } -} - #[derive(Clone, Debug, PartialEq)] pub struct Outpoint { - pub txid: Vec, + pub txid: Sha256, pub outnum: u32, } @@ -294,8 +255,11 @@ impl<'de> Deserialize<'de> for Outpoint { return Err(Error::custom("not a valid txid:output tuple")); } - let txid = + let txid_bytes = hex::decode(splits[0]).map_err(|_| Error::custom("not a valid hex encoded txid"))?; + + let txid= Sha256::from_slice(&txid_bytes).map_err(|e| Error::custom(format!("Invalid TxId: {}", e)))?; + let outnum: u32 = splits[1] .parse() .map_err(|e| Error::custom(format!("{} is not a valid number: {}", s, e)))?; diff --git a/contrib/msggen/msggen/gen/grpc.py b/contrib/msggen/msggen/gen/grpc.py index 074b435ce..def075955 100644 --- a/contrib/msggen/msggen/gen/grpc.py +++ b/contrib/msggen/msggen/gen/grpc.py @@ -349,6 +349,8 @@ class GrpcConverterGenerator(IGenerator): use cln_rpc::model::{responses,requests}; use crate::pb; use std::str::FromStr; + use bitcoin_hashes::sha256::Hash as Sha256; + use bitcoin_hashes::Hash; """) @@ -434,8 +436,8 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator): 'short_channel_id?': f'c.{name}.map(|v| cln_rpc::primitives::ShortChannelId::from_str(&v).unwrap())', 'secret': f'c.{name}.try_into().unwrap()', 'secret?': f'c.{name}.map(|v| v.try_into().unwrap())', - 'hash': f'c.{name}.try_into().unwrap()', - 'hash?': f'c.{name}.map(|v| v.try_into().unwrap())', + 'hash': f'Sha256::from_slice(&c.{name}).unwrap()', + 'hash?': f'c.{name}.map(|v| Sha256::from_slice(&v).unwrap())', 'txid': f'hex::encode(&c.{name})', }.get( typ,