diff --git a/cln-integration/Cargo.lock b/cln-integration/Cargo.lock index 627e3e6..b28ee2b 100644 --- a/cln-integration/Cargo.lock +++ b/cln-integration/Cargo.lock @@ -169,6 +169,15 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bytes" version = "1.5.0" @@ -228,10 +237,12 @@ dependencies = [ "anyhow", "clightning-testing", "env_logger", + "hex", "log", "ntest", "serde", "serde_json", + "sha2", "tokio", ] @@ -241,6 +252,35 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "env_filter" version = "0.1.0" @@ -286,6 +326,16 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -315,6 +365,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "379dada1584ad501b383485dd706b8afb7a70fcbc7f4da7d780638a5a6124a60" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "hex_lit" version = "0.1.1" @@ -641,6 +697,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -729,6 +796,12 @@ dependencies = [ "winnow", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -741,6 +814,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/cln-integration/Cargo.toml b/cln-integration/Cargo.toml index 298cab6..1cc87b1 100644 --- a/cln-integration/Cargo.toml +++ b/cln-integration/Cargo.toml @@ -12,3 +12,5 @@ tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] } serde = "1.0" serde_json = "1.0.1" ntest = "0.9.0" +sha2 = "0.10" +hex = "0.4" diff --git a/cln-integration/src/main.rs b/cln-integration/src/main.rs index d8b1a6e..917ac95 100644 --- a/cln-integration/src/main.rs +++ b/cln-integration/src/main.rs @@ -94,5 +94,51 @@ async fn test_ocean_simple_payout() -> anyhow::Result<()> { } let payout = payout.unwrap(); log::info!("ocean-pay response {}", serde_json::to_string_pretty(&payout)?); + + #[derive(Deserialize)] + struct OceanPayout { + // The invoice decode command result + invoice_decode: DecodeInvoice, + // The information from the pay command + pay: PayResult, + } + + #[derive(Deserialize)] + struct DecodeInvoice { + // The node that we paid, in our case the miner node + invoice_node_id: String, + // The invoice payment hash + invoice_payment_hash: String, + // The public key of the node that ask for the invoice + // in our case the ocean lightning node. + invreq_payer_id: String, + } + + #[derive(Deserialize)] + struct PayResult { + /// Primage: The proof that the miner + /// give to us when the payment is received + payment_preimage: String, + /// The hash of the preimage that is inside the invoice + payment_hash: String, + } + + let payout = serde_json::from_value::(payout)?; + // Time for proof + // proof that the invoice is paied + assert_eq!(payout.invoice_decode.invoice_payment_hash, payout.pay.payment_hash); + + let calculate_payout = calculate_payment_hash(&payout.pay.payment_preimage); + assert_eq!(payout.pay.payment_hash, calculate_payout); + + // We can check that we pay the miner because we are able to + // derive the invreq_payer_id frm the invreq_metadata but + // we need to implement this code or expose the code from CLN + // FIXME: See https://github.com/ElementsProject/lightning/blob/master/lightningd/offer.c#L434-L463 + + // And we can check also that we pay the miner + let info = miner_1.rpc().getinfo()?; + let id = info.id; + assert_eq!(id, payout.invoice_decode.invoice_node_id); Ok(()) } diff --git a/cln-integration/src/utils.rs b/cln-integration/src/utils.rs index a42b795..04bcac3 100644 --- a/cln-integration/src/utils.rs +++ b/cln-integration/src/utils.rs @@ -169,3 +169,20 @@ pub fn wait_for_funds(cln: &cln::Node) -> anyhow::Result<()> { ); Ok(()) } + +pub fn calculate_payment_hash(preimage: &str) -> String { + use sha2::{Sha256, Digest}; + use hex::decode as hex_decode; + + // Decode the hex-encoded preimage string to bytes + let preimage_bytes = hex_decode(preimage).expect("Decoding failed"); + + // Create a Sha256 object + let mut hasher = Sha256::new(); + + // Write input message + hasher.update(preimage_bytes); + + // Read hash digest and convert it to a hexadecimal string + format!("{:x}", hasher.finalize()) +}