1- use crate :: { hex_utils, rate_limiter:: RateLimiter , types:: DynStore } ;
2- use bitcoin:: hashes:: { sha256, Hash } ;
1+ use crate :: hex_utils;
2+ use crate :: io:: STATIC_INVOICES_PRIMARY_NAMESPACE ;
3+ use crate :: payment:: rate_limiter:: RateLimiter ;
4+ use crate :: types:: DynStore ;
5+
6+ use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
7+ use bitcoin:: hashes:: Hash ;
8+
39use lightning:: { offers:: static_invoice:: StaticInvoice , util:: ser:: Writeable } ;
4- use std:: {
5- sync:: { Arc , Mutex } ,
6- time:: Duration ,
7- } ;
10+
11+ use std:: sync:: { Arc , Mutex } ;
12+ use std:: time:: Duration ;
813
914pub ( crate ) struct StaticInvoiceStore {
1015 kv_store : Arc < DynStore > ,
@@ -13,12 +18,6 @@ pub(crate) struct StaticInvoiceStore {
1318}
1419
1520impl 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-
2221 const RATE_LIMITER_BUCKET_CAPACITY : u32 = 5 ;
2322 const RATE_LIMITER_REFILL_INTERVAL : Duration = Duration :: from_millis ( 100 ) ;
2423 const RATE_LIMITER_MAX_IDLE : Duration = Duration :: from_secs ( 600 ) ;
@@ -57,14 +56,16 @@ impl StaticInvoiceStore {
5756
5857 let ( secondary_namespace, key) = Self :: get_storage_location ( invoice_slot, recipient_id) ;
5958
60- self . kv_store . read ( Self :: PRIMARY_NAMESPACE , & secondary_namespace, & key) . and_then ( |data| {
61- data. try_into ( ) . map ( Some ) . map_err ( |e| {
62- lightning:: io:: Error :: new (
63- lightning:: io:: ErrorKind :: InvalidData ,
64- format ! ( "Failed to parse static invoice: {:?}" , e) ,
65- )
66- } )
67- } )
59+ self . kv_store . read ( STATIC_INVOICES_PRIMARY_NAMESPACE , & secondary_namespace, & key) . and_then (
60+ |data| {
61+ data. try_into ( ) . map ( Some ) . map_err ( |e| {
62+ lightning:: io:: Error :: new (
63+ lightning:: io:: ErrorKind :: InvalidData ,
64+ format ! ( "Failed to parse static invoice: {:?}" , e) ,
65+ )
66+ } )
67+ } ,
68+ )
6869 }
6970
7071 pub ( crate ) async fn handle_persist_static_invoice (
@@ -77,14 +78,14 @@ impl StaticInvoiceStore {
7778 let mut buf = Vec :: new ( ) ;
7879 invoice. write ( & mut buf) ?;
7980
80- self . kv_store . write ( Self :: PRIMARY_NAMESPACE , & secondary_namespace, & key, buf)
81+ self . kv_store . write ( STATIC_INVOICES_PRIMARY_NAMESPACE , & secondary_namespace, & key, buf)
8182 }
8283
8384 fn get_storage_location ( invoice_slot : u16 , recipient_id : Vec < u8 > ) -> ( String , String ) {
84- let hash = sha256 :: Hash :: hash ( & recipient_id) . to_byte_array ( ) ;
85+ let hash = Sha256 :: hash ( & recipient_id) . to_byte_array ( ) ;
8586 let secondary_namespace = hex_utils:: to_string ( & hash) ;
8687
87- let key = hex_utils :: to_string ( & invoice_slot. to_be_bytes ( ) ) ;
88+ let key = format ! ( "{:05}" , invoice_slot) ;
8889 ( secondary_namespace, key)
8990 }
9091}
0 commit comments