|
1 | | -use std::{collections::HashMap, sync::Mutex}; |
2 | | - |
3 | | -use lightning::offers::static_invoice::StaticInvoice; |
| 1 | +use crate::{hex_utils, types::DynStore}; |
| 2 | +use bitcoin::hashes::{sha256, Hash}; |
| 3 | +use lightning::{offers::static_invoice::StaticInvoice, util::ser::Writeable}; |
| 4 | +use std::sync::Arc; |
4 | 5 |
|
5 | 6 | pub(crate) struct StaticInvoiceStore { |
6 | | - pub static_invoices: Mutex<HashMap<(Vec<u8>, u16), StaticInvoice>>, |
| 7 | + kv_store: Arc<DynStore>, |
7 | 8 | } |
8 | 9 |
|
9 | 10 | impl StaticInvoiceStore { |
10 | | - pub(crate) fn new() -> Self { |
11 | | - Self { static_invoices: Mutex::new(HashMap::new()) } |
| 11 | + // Static invoices are stored at "static_invoices/<sha256(recipient_id)>/<invoice_slot>". |
| 12 | + // |
| 13 | + // Example: |
| 14 | + // static_invoices/039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81/0000 |
| 15 | + const PRIMARY_NAMESPACE: &str = "static_invoices"; |
| 16 | + |
| 17 | + pub(crate) fn new(kv_store: Arc<DynStore>) -> Self { |
| 18 | + Self { kv_store } |
12 | 19 | } |
13 | 20 |
|
14 | 21 | pub(crate) async fn handle_static_invoice_requested( |
15 | 22 | &self, recipient_id: Vec<u8>, invoice_slot: u16, |
16 | | - ) -> Option<StaticInvoice> { |
17 | | - let map = self.static_invoices.lock().unwrap(); |
| 23 | + ) -> Result<Option<StaticInvoice>, lightning::io::Error> { |
| 24 | + let (secondary_namespace, key) = Self::get_storage_location(invoice_slot, recipient_id); |
18 | 25 |
|
19 | | - map.get(&(recipient_id.clone(), invoice_slot)).cloned() |
| 26 | + self.kv_store.read(Self::PRIMARY_NAMESPACE, &secondary_namespace, &key).and_then(|data| { |
| 27 | + data.try_into().map(Some).map_err(|e| { |
| 28 | + lightning::io::Error::new( |
| 29 | + lightning::io::ErrorKind::InvalidData, |
| 30 | + format!("Failed to parse static invoice: {:?}", e), |
| 31 | + ) |
| 32 | + }) |
| 33 | + }) |
20 | 34 | } |
21 | 35 |
|
22 | 36 | pub(crate) async fn handle_persist_static_invoice( |
23 | 37 | &self, invoice: StaticInvoice, invoice_slot: u16, recipient_id: Vec<u8>, |
24 | | - ) { |
25 | | - let mut map = self.static_invoices.lock().unwrap(); |
26 | | - map.insert((recipient_id, invoice_slot), invoice); |
| 38 | + ) -> Result<(), lightning::io::Error> { |
| 39 | + let (secondary_namespace, key) = Self::get_storage_location(invoice_slot, recipient_id); |
| 40 | + |
| 41 | + let mut buf = Vec::new(); |
| 42 | + invoice.write(&mut buf)?; |
| 43 | + |
| 44 | + self.kv_store.write(Self::PRIMARY_NAMESPACE, &secondary_namespace, &key, buf) |
| 45 | + } |
| 46 | + |
| 47 | + fn get_storage_location(invoice_slot: u16, recipient_id: Vec<u8>) -> (String, String) { |
| 48 | + let hash = sha256::Hash::hash(&recipient_id).to_byte_array(); |
| 49 | + let secondary_namespace = hex_utils::to_string(&hash); |
| 50 | + |
| 51 | + let key = hex_utils::to_string(&invoice_slot.to_be_bytes()); |
| 52 | + (secondary_namespace, key) |
27 | 53 | } |
28 | 54 | } |
0 commit comments