Merge pull request #2432 from jkczyz/2023-07-bolt12-node-signer
[rust-lightning] / lightning / src / util / test_utils.rs
index bf5aa02bb41e310f92d06253487ca39d6c2ed8df..e7e29600dab619e877c01c6a066de8db55da0a8a 100644 (file)
@@ -24,6 +24,8 @@ use crate::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
 use crate::ln::{msgs, wire};
 use crate::ln::msgs::LightningError;
 use crate::ln::script::ShutdownScript;
+use crate::offers::invoice::UnsignedBolt12Invoice;
+use crate::offers::invoice_request::UnsignedInvoiceRequest;
 use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId};
 use crate::routing::utxo::{UtxoLookup, UtxoLookupError, UtxoResult};
 use crate::routing::router::{find_route, InFlightHtlcs, Path, Route, RouteParameters, Router, ScorerAccountingForInFlightHtlcs};
@@ -44,9 +46,10 @@ use bitcoin::network::constants::Network;
 use bitcoin::hash_types::{BlockHash, Txid};
 use bitcoin::util::sighash::SighashCache;
 
-use bitcoin::secp256k1::{SecretKey, PublicKey, Secp256k1, ecdsa::Signature, Scalar};
+use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey};
 use bitcoin::secp256k1::ecdh::SharedSecret;
-use bitcoin::secp256k1::ecdsa::RecoverableSignature;
+use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
+use bitcoin::secp256k1::schnorr;
 
 #[cfg(any(test, feature = "_test_utils"))]
 use regex;
@@ -112,13 +115,13 @@ impl<'a> TestRouter<'a> {
 impl<'a> Router for TestRouter<'a> {
        fn find_route(
                &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&channelmanager::ChannelDetails]>,
-               inflight_htlcs: &InFlightHtlcs
+               inflight_htlcs: InFlightHtlcs
        ) -> Result<Route, msgs::LightningError> {
                if let Some((find_route_query, find_route_res)) = self.next_routes.lock().unwrap().pop_front() {
                        assert_eq!(find_route_query, *params);
                        if let Ok(ref route) = find_route_res {
                                let mut binding = self.scorer.lock().unwrap();
-                               let scorer = ScorerAccountingForInFlightHtlcs::new(binding.deref_mut(), inflight_htlcs);
+                               let scorer = ScorerAccountingForInFlightHtlcs::new(binding.deref_mut(), &inflight_htlcs);
                                for path in &route.paths {
                                        let mut aggregate_msat = 0u64;
                                        for (idx, hop) in path.hops.iter().rev().enumerate() {
@@ -800,6 +803,18 @@ impl NodeSigner for TestNodeSigner {
                unreachable!()
        }
 
+       fn sign_bolt12_invoice_request(
+               &self, _invoice_request: &UnsignedInvoiceRequest
+       ) -> Result<schnorr::Signature, ()> {
+               unreachable!()
+       }
+
+       fn sign_bolt12_invoice(
+               &self, _invoice: &UnsignedBolt12Invoice,
+       ) -> Result<schnorr::Signature, ()> {
+               unreachable!()
+       }
+
        fn sign_gossip_message(&self, _msg: msgs::UnsignedGossipMessage) -> Result<Signature, ()> {
                unreachable!()
        }
@@ -840,6 +855,18 @@ impl NodeSigner for TestKeysInterface {
                self.backing.sign_invoice(hrp_bytes, invoice_data, recipient)
        }
 
+       fn sign_bolt12_invoice_request(
+               &self, invoice_request: &UnsignedInvoiceRequest
+       ) -> Result<schnorr::Signature, ()> {
+               self.backing.sign_bolt12_invoice_request(invoice_request)
+       }
+
+       fn sign_bolt12_invoice(
+               &self, invoice: &UnsignedBolt12Invoice,
+       ) -> Result<schnorr::Signature, ()> {
+               self.backing.sign_bolt12_invoice(invoice)
+       }
+
        fn sign_gossip_message(&self, msg: msgs::UnsignedGossipMessage) -> Result<Signature, ()> {
                self.backing.sign_gossip_message(msg)
        }
@@ -1105,20 +1132,20 @@ impl TestWalletSource {
 }
 
 impl WalletSource for TestWalletSource {
-    fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
+       fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
                Ok(self.utxos.borrow().clone())
-    }
+       }
 
-    fn get_change_script(&self) -> Result<Script, ()> {
+       fn get_change_script(&self) -> Result<Script, ()> {
                let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
                Ok(Script::new_p2pkh(&public_key.pubkey_hash()))
-    }
+       }
 
-    fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
+       fn sign_tx(&self, mut tx: Transaction) -> Result<Transaction, ()> {
                let utxos = self.utxos.borrow();
                for i in 0..tx.input.len() {
                        if let Some(utxo) = utxos.iter().find(|utxo| utxo.outpoint == tx.input[i].previous_output) {
-                               let sighash = SighashCache::new(&*tx)
+                               let sighash = SighashCache::new(&tx)
                                        .legacy_signature_hash(i, &utxo.output.script_pubkey, EcdsaSighashType::All as u32)
                                        .map_err(|_| ())?;
                                let sig = self.secp.sign_ecdsa(&sighash.as_hash().into(), &self.secret_key);
@@ -1129,6 +1156,6 @@ impl WalletSource for TestWalletSource {
                                        .into_script();
                        }
                }
-               Ok(())
-    }
+               Ok(tx)
+       }
 }