mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 07:34:24 +01:00
Use bitcoin_hashes for Sha256
This commit is contained in:
committed by
Christian Decker
parent
10917743fe
commit
e272c93a88
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -72,6 +72,15 @@ version = "0.13.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
|
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]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
@@ -117,6 +126,7 @@ name = "cln-grpc"
|
|||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"bitcoin_hashes",
|
||||||
"cln-rpc",
|
"cln-rpc",
|
||||||
"hex",
|
"hex",
|
||||||
"log",
|
"log",
|
||||||
@@ -164,6 +174,7 @@ name = "cln-rpc"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"bitcoin_hashes",
|
||||||
"bytes",
|
"bytes",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ cln-rpc = { path="../cln-rpc" }
|
|||||||
tonic = { version = "^0.5", features = ["tls", "transport"] }
|
tonic = { version = "^0.5", features = ["tls", "transport"] }
|
||||||
prost = "0.8"
|
prost = "0.8"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
|
bitcoin_hashes = { version = "0.10.0", features = [ "serde" ] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde_json = "1.0.72"
|
serde_json = "1.0.72"
|
||||||
|
|||||||
14
cln-grpc/src/convert.rs
generated
14
cln-grpc/src/convert.rs
generated
@@ -8,6 +8,8 @@ use std::convert::From;
|
|||||||
use cln_rpc::model::{responses,requests};
|
use cln_rpc::model::{responses,requests};
|
||||||
use crate::pb;
|
use crate::pb;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use bitcoin_hashes::sha256::Hash as Sha256;
|
||||||
|
use bitcoin_hashes::Hash;
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
impl From<responses::GetinfoAddress> for pb::GetinfoAddress {
|
impl From<responses::GetinfoAddress> for pb::GetinfoAddress {
|
||||||
@@ -1020,7 +1022,7 @@ impl From<pb::SendpayRequest> for requests::SendpayRequest {
|
|||||||
fn from(c: pb::SendpayRequest) -> Self {
|
fn from(c: pb::SendpayRequest) -> Self {
|
||||||
Self {
|
Self {
|
||||||
route: c.route.into_iter().map(|s| s.into()).collect(), // Rule #4
|
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?
|
label: c.label, // Rule #1 for type string?
|
||||||
amount_msat: c.amount_msat.map(|a| a.into()), // Rule #1 for type msat?
|
amount_msat: c.amount_msat.map(|a| a.into()), // Rule #1 for type msat?
|
||||||
bolt11: c.bolt11, // Rule #1 for type string?
|
bolt11: c.bolt11, // Rule #1 for type string?
|
||||||
@@ -1218,14 +1220,14 @@ impl From<pb::SendonionRequest> for requests::SendonionRequest {
|
|||||||
fn from(c: pb::SendonionRequest) -> Self {
|
fn from(c: pb::SendonionRequest) -> Self {
|
||||||
Self {
|
Self {
|
||||||
onion: hex::encode(&c.onion), // Rule #1 for type hex
|
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?
|
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
|
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?
|
partid: c.partid.map(|v| v as u16), // Rule #1 for type u16?
|
||||||
bolt11: c.bolt11, // Rule #1 for type string?
|
bolt11: c.bolt11, // Rule #1 for type string?
|
||||||
amount_msat: c.amount_msat.map(|a| a.into()), // Rule #1 for type msat?
|
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?
|
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?
|
groupid: c.groupid, // Rule #1 for type u64?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1236,7 +1238,7 @@ impl From<pb::ListsendpaysRequest> for requests::ListsendpaysRequest {
|
|||||||
fn from(c: pb::ListsendpaysRequest) -> Self {
|
fn from(c: pb::ListsendpaysRequest) -> Self {
|
||||||
Self {
|
Self {
|
||||||
bolt11: c.bolt11, // Rule #1 for type string?
|
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()),
|
status: c.status.map(|v| v.try_into().unwrap()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1302,7 +1304,7 @@ impl From<pb::WaitinvoiceRequest> for requests::WaitinvoiceRequest {
|
|||||||
impl From<pb::WaitsendpayRequest> for requests::WaitsendpayRequest {
|
impl From<pb::WaitsendpayRequest> for requests::WaitsendpayRequest {
|
||||||
fn from(c: pb::WaitsendpayRequest) -> Self {
|
fn from(c: pb::WaitsendpayRequest) -> Self {
|
||||||
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?
|
timeout: c.timeout, // Rule #1 for type u32?
|
||||||
partid: c.partid, // Rule #1 for type u64?
|
partid: c.partid, // Rule #1 for type u64?
|
||||||
groupid: c.groupid, // Rule #1 for type u64?
|
groupid: c.groupid, // Rule #1 for type u64?
|
||||||
@@ -1502,7 +1504,7 @@ impl From<pb::ListpaysRequest> for requests::ListpaysRequest {
|
|||||||
fn from(c: pb::ListpaysRequest) -> Self {
|
fn from(c: pb::ListpaysRequest) -> Self {
|
||||||
Self {
|
Self {
|
||||||
bolt11: c.bolt11, // Rule #1 for type string?
|
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()),
|
status: c.status.map(|v| v.try_into().unwrap()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
tonic::include_proto!("cln");
|
tonic::include_proto!("cln");
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use bitcoin_hashes::Hash;
|
||||||
|
|
||||||
use cln_rpc::primitives::{
|
use cln_rpc::primitives::{
|
||||||
Amount as JAmount, AmountOrAll as JAmountOrAll, AmountOrAny as JAmountOrAny,
|
Amount as JAmount, AmountOrAll as JAmountOrAll, AmountOrAny as JAmountOrAny,
|
||||||
@@ -21,7 +22,7 @@ impl From<Amount> for JAmount {
|
|||||||
impl From<JOutpoint> for Outpoint {
|
impl From<JOutpoint> for Outpoint {
|
||||||
fn from(a: JOutpoint) -> Self {
|
fn from(a: JOutpoint) -> Self {
|
||||||
Outpoint {
|
Outpoint {
|
||||||
txid: a.txid,
|
txid: a.txid.to_vec(),
|
||||||
outnum: a.outnum,
|
outnum: a.outnum,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,7 +31,7 @@ impl From<JOutpoint> for Outpoint {
|
|||||||
impl From<Outpoint> for JOutpoint {
|
impl From<Outpoint> for JOutpoint {
|
||||||
fn from(a: Outpoint) -> Self {
|
fn from(a: Outpoint) -> Self {
|
||||||
JOutpoint {
|
JOutpoint {
|
||||||
txid: a.txid,
|
txid: bitcoin_hashes::sha256::Hash::from_slice(&a.txid).unwrap(),
|
||||||
outnum: a.outnum,
|
outnum: a.outnum,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ path = "examples/getinfo.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.51"
|
anyhow = "1.0.51"
|
||||||
|
bitcoin_hashes = { version = "0.10.0", features = [ "serde" ] }
|
||||||
bytes = "1.1.0"
|
bytes = "1.1.0"
|
||||||
log = "0.4.14"
|
log = "0.4.14"
|
||||||
serde = { version = "1.0.131", features = ["derive"] }
|
serde = { version = "1.0.131", features = ["derive"] }
|
||||||
|
|||||||
976
cln-rpc/src/model.rs
generated
976
cln-rpc/src/model.rs
generated
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,9 @@ use serde::{Deserialize, Serialize};
|
|||||||
use serde::{Deserializer, Serializer};
|
use serde::{Deserializer, Serializer};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
|
use bitcoin_hashes::Hash as BitcoinHash;
|
||||||
|
|
||||||
|
pub use bitcoin_hashes::sha256::Hash as Sha256;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
|
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
|
||||||
#[allow(non_camel_case_types)]
|
#[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<u8> {
|
|
||||||
self.0.to_vec()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<Vec<u8>> for Sha256 {
|
|
||||||
type Error = crate::Error;
|
|
||||||
fn try_from(v: Vec<u8>) -> Result<Self, crate::Error> {
|
|
||||||
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
serializer.serialize_str(&hex::encode(&self.0))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for Sha256 {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
||||||
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)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Outpoint {
|
pub struct Outpoint {
|
||||||
pub txid: Vec<u8>,
|
pub txid: Sha256,
|
||||||
pub outnum: u32,
|
pub outnum: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,8 +255,11 @@ impl<'de> Deserialize<'de> for Outpoint {
|
|||||||
return Err(Error::custom("not a valid txid:output tuple"));
|
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"))?;
|
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]
|
let outnum: u32 = splits[1]
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(|e| Error::custom(format!("{} is not a valid number: {}", s, e)))?;
|
.map_err(|e| Error::custom(format!("{} is not a valid number: {}", s, e)))?;
|
||||||
|
|||||||
@@ -349,6 +349,8 @@ class GrpcConverterGenerator(IGenerator):
|
|||||||
use cln_rpc::model::{responses,requests};
|
use cln_rpc::model::{responses,requests};
|
||||||
use crate::pb;
|
use crate::pb;
|
||||||
use std::str::FromStr;
|
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())',
|
'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}.try_into().unwrap()',
|
||||||
'secret?': f'c.{name}.map(|v| v.try_into().unwrap())',
|
'secret?': f'c.{name}.map(|v| v.try_into().unwrap())',
|
||||||
'hash': f'c.{name}.try_into().unwrap()',
|
'hash': f'Sha256::from_slice(&c.{name}).unwrap()',
|
||||||
'hash?': f'c.{name}.map(|v| v.try_into().unwrap())',
|
'hash?': f'c.{name}.map(|v| Sha256::from_slice(&v).unwrap())',
|
||||||
'txid': f'hex::encode(&c.{name})',
|
'txid': f'hex::encode(&c.{name})',
|
||||||
}.get(
|
}.get(
|
||||||
typ,
|
typ,
|
||||||
|
|||||||
Reference in New Issue
Block a user