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

feat(lightning): Add Lightning as transport #85

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion teos-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ bitcoin = { version = "0.28.0", features = [ "use-serde" ] }
lightning = "0.0.108"

[build-dependencies]
tonic-build = "0.6"
tonic-build = "0.6"

[dev-dependencies]
lightning = { version = "0.0.108", features = ["_test_utils"] }
1 change: 1 addition & 0 deletions teos-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod constants;
pub mod cryptography;
pub mod dbm;
pub mod errors;
pub mod lightning;
pub mod net;
pub mod receipts;
pub mod ser;
Expand Down
145 changes: 145 additions & 0 deletions teos-common/src/lightning/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//! A module that implements useful gRPC messages to lightning message conversions.

use super::messages::*;
use crate::appointment::Locator;
use crate::constants::ENCRYPTED_BLOB_MAX_SIZE;
use crate::protos as msgs;

use bitcoin::hashes::Hash;
use bitcoin::Txid;

/// Conversions from individual messages to tower messages.
mod msg_to_tower_msg {
use super::*;
macro_rules! impl_from_msg {
($msg: ident) => {
impl From<$msg> for TowerMessage {
fn from(m: $msg) -> TowerMessage {
TowerMessage::$msg(m)
}
}
};
}

impl_from_msg!(Register);
impl_from_msg!(SubscriptionDetails);
impl_from_msg!(AddUpdateAppointment);
impl_from_msg!(AppointmentAccepted);
impl_from_msg!(AppointmentRejected);
impl_from_msg!(GetAppointment);
impl_from_msg!(AppointmentData);
impl_from_msg!(TrackerData);
impl_from_msg!(AppointmentNotFound);
impl_from_msg!(GetSubscriptionInfo);
impl_from_msg!(SubscriptionInfo);
}

// FIXME: There are a lot of `unwrap()`s in these conversions. We assume that the gRPC interface won't send invalid data.
// If the conversion panics this would crash the lightning server.

/// Conversion from user requests to gRPC requests.
/// These are used by the tower when routing lightning requests to its internal gRPC server.
mod msg_to_grpc {
use super::*;
impl From<Register> for msgs::RegisterRequest {
fn from(r: Register) -> Self {
msgs::RegisterRequest {
user_id: r.pubkey.to_vec(),
}
}
}

impl From<AddUpdateAppointment> for msgs::AddAppointmentRequest {
fn from(r: AddUpdateAppointment) -> Self {
let appointment = msgs::Appointment {
locator: r.locator.to_vec(),
encrypted_blob: r.encrypted_blob,
to_self_delay: r.to_self_delay.unwrap_or(42),
};

msgs::AddAppointmentRequest {
appointment: Some(appointment),
signature: r.signature,
}
}
}

impl From<GetAppointment> for msgs::GetAppointmentRequest {
fn from(r: GetAppointment) -> Self {
msgs::GetAppointmentRequest {
locator: r.locator.to_vec(),
signature: r.signature,
}
}
}

impl From<GetSubscriptionInfo> for msgs::GetSubscriptionInfoRequest {
fn from(r: GetSubscriptionInfo) -> Self {
msgs::GetSubscriptionInfoRequest {
signature: r.signature,
}
}
}
}

/// Conversion from gRPC responses to tower responses.
/// These are used by the tower when parsing internal gRPC server's responses.
mod grpc_to_tower_msg {
use super::*;
impl From<msgs::RegisterResponse> for TowerMessage {
fn from(r: msgs::RegisterResponse) -> Self {
SubscriptionDetails {
appointment_max_size: ENCRYPTED_BLOB_MAX_SIZE as u16,
start_block: r.subscription_start,
amount_msat: None,
invoice: None,
signature: Some(r.subscription_signature),
}
.into()
}
}

impl From<msgs::AddAppointmentResponse> for TowerMessage {
fn from(r: msgs::AddAppointmentResponse) -> Self {
AppointmentAccepted {
locator: Locator::from_slice(&r.locator).unwrap(),
start_block: r.start_block,
receipt_signature: Some(r.signature),
}
.into()
}
}

impl From<msgs::GetAppointmentResponse> for TowerMessage {
fn from(r: msgs::GetAppointmentResponse) -> Self {
match r.appointment_data.unwrap().appointment_data.unwrap() {
msgs::appointment_data::AppointmentData::Appointment(a) => AppointmentData {
locator: Locator::from_slice(&a.locator).unwrap(),
encrypted_blob: a.encrypted_blob,
}
.into(),
msgs::appointment_data::AppointmentData::Tracker(t) => TrackerData {
dispute_txid: Txid::from_slice(&t.dispute_txid).unwrap(),
penalty_txid: Txid::from_slice(&t.penalty_txid).unwrap(),
penalty_rawtx: t.penalty_rawtx,
}
.into(),
}
}
}

impl From<msgs::GetSubscriptionInfoResponse> for TowerMessage {
fn from(r: msgs::GetSubscriptionInfoResponse) -> Self {
SubscriptionInfo {
available_slots: r.available_slots,
subscription_expiry: r.subscription_expiry,
locators: r
.locators
.into_iter()
.map(|l| Locator::from_slice(&l).unwrap())
.collect(),
}
.into()
}
}
}
Loading