|
| 1 | +use crate::{hex_utils, rate_limiter::RateLimiter, types::DynStore}; |
| 2 | +use bitcoin::hashes::{sha256, Hash}; |
| 3 | +use lightning::{offers::static_invoice::StaticInvoice, util::ser::Writeable}; |
| 4 | +use std::{ |
| 5 | + sync::{Arc, Mutex}, |
| 6 | + time::Duration, |
| 7 | +}; |
| 8 | + |
| 9 | +pub(crate) struct StaticInvoiceStore { |
| 10 | + kv_store: Arc<DynStore>, |
| 11 | + request_rate_limiter: Mutex<RateLimiter>, |
| 12 | + persist_rate_limiter: Mutex<RateLimiter>, |
| 13 | +} |
| 14 | + |
| 15 | +impl StaticInvoiceStore { |
| 16 | + // Static invoices are stored at "static_invoices/<sha256(recipient_id)>/<invoice_slot>". |
| 17 | + // |
| 18 | + // Example: |
| 19 | + // static_invoices/039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81/001f |
| 20 | + const PRIMARY_NAMESPACE: &str = "static_invoices"; |
| 21 | + |
| 22 | + pub(crate) fn new(kv_store: Arc<DynStore>) -> Self { |
| 23 | + Self { |
| 24 | + kv_store, |
| 25 | + request_rate_limiter: Mutex::new(RateLimiter::new(Duration::from_millis(100))), |
| 26 | + persist_rate_limiter: Mutex::new(RateLimiter::new(Duration::from_millis(100))), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + fn check_rate_limit( |
| 31 | + limiter: &Mutex<RateLimiter>, recipient_id: &[u8], |
| 32 | + ) -> Result<(), lightning::io::Error> { |
| 33 | + let mut limiter = limiter.lock().unwrap(); |
| 34 | + if !limiter.allow(recipient_id) { |
| 35 | + Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, "Rate limit exceeded")) |
| 36 | + } else { |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + pub(crate) async fn handle_static_invoice_requested( |
| 42 | + &self, recipient_id: Vec<u8>, invoice_slot: u16, |
| 43 | + ) -> Result<Option<StaticInvoice>, lightning::io::Error> { |
| 44 | + Self::check_rate_limit(&self.request_rate_limiter, &recipient_id)?; |
| 45 | + |
| 46 | + let (secondary_namespace, key) = Self::get_storage_location(invoice_slot, recipient_id); |
| 47 | + |
| 48 | + self.kv_store.read(Self::PRIMARY_NAMESPACE, &secondary_namespace, &key).and_then(|data| { |
| 49 | + data.try_into().map(Some).map_err(|e| { |
| 50 | + lightning::io::Error::new( |
| 51 | + lightning::io::ErrorKind::InvalidData, |
| 52 | + format!("Failed to parse static invoice: {:?}", e), |
| 53 | + ) |
| 54 | + }) |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + pub(crate) async fn handle_persist_static_invoice( |
| 59 | + &self, invoice: StaticInvoice, invoice_slot: u16, recipient_id: Vec<u8>, |
| 60 | + ) -> Result<(), lightning::io::Error> { |
| 61 | + Self::check_rate_limit(&self.persist_rate_limiter, &recipient_id)?; |
| 62 | + |
| 63 | + let (secondary_namespace, key) = Self::get_storage_location(invoice_slot, recipient_id); |
| 64 | + |
| 65 | + let mut buf = Vec::new(); |
| 66 | + invoice.write(&mut buf)?; |
| 67 | + |
| 68 | + self.kv_store.write(Self::PRIMARY_NAMESPACE, &secondary_namespace, &key, buf) |
| 69 | + } |
| 70 | + |
| 71 | + fn get_storage_location(invoice_slot: u16, recipient_id: Vec<u8>) -> (String, String) { |
| 72 | + let hash = sha256::Hash::hash(&recipient_id).to_byte_array(); |
| 73 | + let secondary_namespace = hex_utils::to_string(&hash); |
| 74 | + |
| 75 | + let key = hex_utils::to_string(&invoice_slot.to_be_bytes()); |
| 76 | + (secondary_namespace, key) |
| 77 | + } |
| 78 | +} |
0 commit comments