Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offers: wait for offers invoice #84

Closed
wants to merge 8 commits into from
33 changes: 31 additions & 2 deletions src/onion_messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use bitcoin::secp256k1::PublicKey;
use core::ops::Deref;
use lightning::ln::features::InitFeatures;
use lightning::ln::msgs::{Init, OnionMessage, OnionMessageHandler};
use lightning::offers::invoice::Bolt12Invoice;
use lightning::onion_message::{
CustomOnionMessageHandler, MessageRouter, OffersMessageHandler, OnionMessenger,
CustomOnionMessageHandler, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessenger,
PendingOnionMessage,
};
use lightning::sign::EntropySource;
use lightning::sign::NodeSigner;
Expand All @@ -19,12 +21,13 @@ use log::{debug, error, info, trace, warn};
use rand_chacha::ChaCha20Rng;
use rand_core::{RngCore, SeedableRng};
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::{HashMap, VecDeque};
use std::error::Error;
use std::fmt;
use std::io::Cursor;
use std::marker::Copy;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::{select, time, time::Duration, time::Interval};
use tonic_lnd::{
Expand Down Expand Up @@ -699,6 +702,32 @@ pub(crate) async fn relay_outgoing_msg_event(
}
}

// A simple offers message handler.
#[derive(Clone)]
pub struct InvoiceHandler {
pub invoices: Arc<Mutex<VecDeque<Bolt12Invoice>>>,
}

impl OffersMessageHandler for InvoiceHandler {
fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage> {
match message {
OffersMessage::Invoice(invoice) => {
println!("Received an invoice offers message.");
self.invoices.lock().unwrap().push_back(invoice);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we track that we actually sent an invoice request for the offer, and discard the invoice if not?

None
}
_ => {
println!("Received an unsupported offers message.");
None
}
}
}

fn release_pending_messages(&self) -> Vec<PendingOnionMessage<OffersMessage>> {
vec![]
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down