Skip to content

Commit c3aca16

Browse files
committed
Add Node fn for sending an onion message
1 parent 740c40a commit c3aca16

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,6 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
722722
scorer,
723723
peer_store,
724724
payment_store,
725-
_onion_messenger: onion_messenger,
725+
onion_messenger: onion_messenger,
726726
})
727727
}

src/lib.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ use lightning::chain::keysinterface::EntropySource;
130130
use lightning::chain::Confirm;
131131
use lightning::ln::channelmanager::{self, PaymentId, RecipientOnionFields, Retry};
132132
use lightning::ln::{PaymentHash, PaymentPreimage};
133+
use lightning::onion_message::{CustomOnionMessageContents, Destination, OnionMessageContents};
133134

135+
use lightning::util::ser::{Writeable, Writer};
134136
use lightning::util::config::{ChannelConfig, ChannelHandshakeConfig, UserConfig};
135137
pub use lightning::util::logger::Level as LogLevel;
136138

@@ -195,6 +197,23 @@ const WALLET_SYNC_INTERVAL_MINIMUM_SECS: u64 = 10;
195197
// The length in bytes of our wallets' keys seed.
196198
const WALLET_KEYS_SEED_LEN: usize = 64;
197199

200+
struct UserOnionMessageContents {
201+
tlv_type: u64,
202+
data: Vec<u8>,
203+
}
204+
205+
impl CustomOnionMessageContents for UserOnionMessageContents {
206+
fn tlv_type(&self) -> u64 {
207+
self.tlv_type
208+
}
209+
}
210+
211+
impl Writeable for UserOnionMessageContents {
212+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), std::io::Error> {
213+
w.write_all(&self.data)
214+
}
215+
}
216+
198217
#[derive(Debug, Clone)]
199218
/// Represents the configuration of an [`Node`] instance.
200219
///
@@ -283,7 +302,7 @@ pub struct Node<K: KVStore + Sync + Send + 'static> {
283302
scorer: Arc<Mutex<Scorer>>,
284303
peer_store: Arc<PeerStore<K, Arc<FilesystemLogger>>>,
285304
payment_store: Arc<PaymentStore<K, Arc<FilesystemLogger>>>,
286-
_onion_messenger: Arc<OnionMessenger>,
305+
onion_messenger: Arc<OnionMessenger>,
287306
}
288307

289308
impl<K: KVStore + Sync + Send + 'static> Node<K> {
@@ -810,6 +829,21 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
810829
self.wallet.send_to_address(address, None)
811830
}
812831

832+
/// Send an onion message to the following address.
833+
pub fn send_onion_message(
834+
&self, node_pks: Vec<PublicKey>, destination_pk: PublicKey, tlv_type: u64, data: Vec<u8>,
835+
) {
836+
match self.onion_messenger.send_onion_message(
837+
&node_pks,
838+
Destination::Node(destination_pk),
839+
OnionMessageContents::Custom(UserOnionMessageContents { tlv_type, data }),
840+
None,
841+
) {
842+
Ok(()) => println!("SUCCESS: forwarded onion message to first hop"),
843+
Err(e) => println!("ERROR: failed to send onion message: {:?}", e),
844+
}
845+
}
846+
813847
/// Retrieve a list of known channels.
814848
pub fn list_channels(&self) -> Vec<ChannelDetails> {
815849
self.channel_manager.list_channels().into_iter().map(|c| c.into()).collect()

0 commit comments

Comments
 (0)