-
Notifications
You must be signed in to change notification settings - Fork 90
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
Add/implement HRN (Human-Readable Name) support using a sub-module #437
Open
Dhanraj30
wants to merge
1
commit into
lightningdevkit:main
Choose a base branch
from
Dhanraj30:Add/support-for-human-readable-names-in-payments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// src/payment/hrn.rs | ||
|
||
use crate::error::Error; | ||
use crate::logger::{log_error, log_info, Logger}; | ||
use crate::payment::store::{PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus, PaymentStore}; | ||
use crate::types::ChannelManager; | ||
|
||
use lightning::ln::channelmanager::{PaymentId, Retry}; | ||
use lightning::offers::offer::{Amount, Offer, Quantity}; | ||
use lightning::offers::parse::Bolt12SemanticError; | ||
use lightning::util::string::UntrustedString; | ||
|
||
use std::sync::{Arc, RwLock}; | ||
|
||
/// A payment handler for sending payments to Human-Readable Names (HRNs). | ||
pub struct HrnPayment { | ||
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, | ||
channel_manager: Arc<ChannelManager>, | ||
payment_store: Arc<PaymentStore<Arc<Logger>>>, | ||
logger: Arc<Logger>, | ||
} | ||
|
||
impl HrnPayment { | ||
pub(crate) fn new( | ||
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, | ||
channel_manager: Arc<ChannelManager>, | ||
payment_store: Arc<PaymentStore<Arc<Logger>>>, | ||
logger: Arc<Logger>, | ||
) -> Self { | ||
Self { runtime, channel_manager, payment_store, logger } | ||
} | ||
|
||
/// Send a payment to a Human-Readable Name (HRN). | ||
/// | ||
/// This method resolves the HRN to an offer and sends the payment. | ||
/// | ||
/// If `payer_note` is `Some`, it will be seen by the recipient and reflected back in the invoice. | ||
/// If `quantity` is `Some`, it represents the number of items requested. | ||
pub fn send_to_hrn( | ||
&self, hrn: &str, quantity: Option<u64>, payer_note: Option<String>, | ||
) -> Result<PaymentId, Error> { | ||
let rt_lock = self.runtime.read().unwrap(); | ||
if rt_lock.is_none() { | ||
return Err(Error::NotRunning); | ||
} | ||
|
||
// Resolve the HRN to an offer | ||
let offer = self.resolve_hrn_to_offer(hrn)?; | ||
|
||
// Use the existing payment logic to send the payment | ||
let mut random_bytes = [0u8; 32]; | ||
rand::thread_rng().fill_bytes(&mut random_bytes); | ||
let payment_id = PaymentId(random_bytes); | ||
let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT); | ||
let max_total_routing_fee_msat = None; | ||
|
||
match self.channel_manager.pay_for_offer( | ||
&offer, | ||
quantity, | ||
None, | ||
payer_note.clone(), | ||
payment_id, | ||
retry_strategy, | ||
max_total_routing_fee_msat, | ||
) { | ||
Ok(()) => { | ||
let payee_pubkey = offer.issuer_signing_pubkey(); | ||
log_info!( | ||
self.logger, | ||
"Initiated sending payment to HRN: {} (payee: {:?})", | ||
hrn, | ||
payee_pubkey | ||
); | ||
|
||
let kind = PaymentKind::Bolt12Offer { | ||
hash: None, | ||
preimage: None, | ||
secret: None, | ||
offer_id: offer.id(), | ||
payer_note: payer_note.map(UntrustedString), | ||
quantity, | ||
}; | ||
let payment = PaymentDetails::new( | ||
payment_id, | ||
kind, | ||
None, // Amount will be set by the offer | ||
PaymentDirection::Outbound, | ||
PaymentStatus::Pending, | ||
); | ||
self.payment_store.insert(payment)?; | ||
|
||
Ok(payment_id) | ||
} | ||
Err(e) => { | ||
log_error!(self.logger, "Failed to send payment to HRN: {:?}", e); | ||
match e { | ||
Bolt12SemanticError::DuplicatePaymentId => Err(Error::DuplicatePayment), | ||
_ => Err(Error::PaymentSendingFailed), | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Resolves a Human-Readable Name (HRN) to an offer. | ||
/// | ||
/// This is a placeholder for actual HRN resolution logic. | ||
fn resolve_hrn_to_offer(&self, hrn: &str) -> Result<Offer, Error> { | ||
// Placeholder logic for resolving HRN to an offer | ||
log_info!(self.logger, "Resolving HRN: {}", hrn); | ||
|
||
// For now, return a mock offer | ||
let offer_builder = self.channel_manager.create_offer_builder(None).map_err(|e| { | ||
log_error!(self.logger, "Failed to create offer builder: {:?}", e); | ||
Error::OfferCreationFailed | ||
})?; | ||
|
||
let offer = offer_builder | ||
.amount_msats(1000) // Example amount | ||
.description(hrn.to_string()) | ||
.build() | ||
.map_err(|e| { | ||
log_error!(self.logger, "Failed to create offer: {:?}", e); | ||
Error::OfferCreationFailed | ||
})?; | ||
|
||
Ok(offer) | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::logger::TestLogger; | ||
use crate::types::TestChannelManager; | ||
|
||
#[test] | ||
fn test_send_to_hrn() { | ||
let runtime = Arc::new(RwLock::new(Some(Arc::new(tokio::runtime::Runtime::new().unwrap())))); | ||
let channel_manager = Arc::new(TestChannelManager::new()); | ||
let payment_store = Arc::new(PaymentStore::new(Vec::new(), Arc::new(TestStore::new(false)), Arc::new(TestLogger::new()))); | ||
let logger = Arc::new(TestLogger::new()); | ||
|
||
let hrn_payment = HrnPayment::new(runtime, channel_manager, payment_store, logger); | ||
let result = hrn_payment.send_to_hrn("example.hrn", None, None); | ||
assert!(result.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_resolve_hrn_to_offer() { | ||
let runtime = Arc::new(RwLock::new(Some(Arc::new(tokio::runtime::Runtime::new().unwrap())))); | ||
let channel_manager = Arc::new(TestChannelManager::new()); | ||
let payment_store = Arc::new(PaymentStore::new(Vec::new(), Arc::new(TestStore::new(false)), Arc::new(TestLogger::new()))); | ||
let logger = Arc::new(TestLogger::new()); | ||
|
||
let hrn_payment = HrnPayment::new(runtime, channel_manager, payment_store, logger); | ||
let result = hrn_payment.resolve_hrn_to_offer("example.hrn"); | ||
assert!(result.is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're building an offer which we can use to receive payment to ourselves and then trying to pay it, paying ourselves. Instead, you need to look at the
lightning-dns-resolver
crate and its contained resolver to resolve the HRN to a URI.