@@ -58,6 +58,8 @@ pub const BITCOIN_NETWORK_FNAME: &str = "bitcoin_network";
5858pub const ELECTRUM_URL_FNAME : & str = "electrum_url" ;
5959/// Name of the file containing the wallet fingerprint
6060pub const WALLET_FINGERPRINT_FNAME : & str = "wallet_fingerprint" ;
61+ const INBOUND_EXT : & str = "inbound" ;
62+ const OUTBOUND_EXT : & str = "outbound" ;
6163
6264/// RGB channel info
6365#[ derive( Debug , Clone , Deserialize , Serialize ) ]
@@ -82,7 +84,7 @@ pub struct RgbPaymentInfo {
8284 /// RGB remote amount
8385 pub remote_rgb_amount : u64 ,
8486 /// Whether the RGB amount in route should be overridden
85- pub override_route_amount : bool ,
87+ pub swap_payment : bool ,
8688 /// Whether the payment is inbound
8789 pub inbound : bool ,
8890}
@@ -166,15 +168,29 @@ pub(crate) fn color_commitment<SP: Deref>(channel_context: &ChannelContext<SP>,
166168 . assignments_type ( & FieldName :: from ( "beneficiary" ) ) . expect ( "valid assignment" ) ;
167169
168170 for htlc in commitment_tx. htlcs ( ) {
171+ if htlc. amount_rgb . unwrap_or ( 0 ) == 0 {
172+ continue ;
173+ }
174+ let htlc_amount_rgb = htlc. amount_rgb . expect ( "this HTLC has RGB assets" ) ;
175+
169176 let htlc_vout = htlc. transaction_output_index . unwrap ( ) ;
170177
178+ let inbound = htlc. offered == counterparty;
179+
171180 let htlc_payment_hash = hex:: encode ( htlc. payment_hash . 0 ) ;
172181 let htlc_proxy_id = format ! ( "{chan_id}{htlc_payment_hash}" ) ;
173- let rgb_payment_info_proxy_id_path = ldk_data_dir. join ( htlc_proxy_id) ;
174-
182+ let mut rgb_payment_info_proxy_id_path = ldk_data_dir. join ( htlc_proxy_id) ;
175183 let rgb_payment_info_path = ldk_data_dir. join ( htlc_payment_hash) ;
176- let mut rgb_payment_info_tmp_path = rgb_payment_info_path. clone ( ) ;
177- rgb_payment_info_tmp_path. set_extension ( "pending" ) ;
184+ let mut rgb_payment_info_path = rgb_payment_info_path. clone ( ) ;
185+ if inbound {
186+ rgb_payment_info_proxy_id_path. set_extension ( INBOUND_EXT ) ;
187+ rgb_payment_info_path. set_extension ( INBOUND_EXT ) ;
188+ } else {
189+ rgb_payment_info_proxy_id_path. set_extension ( OUTBOUND_EXT ) ;
190+ rgb_payment_info_path. set_extension ( OUTBOUND_EXT ) ;
191+ }
192+ let rgb_payment_info_tmp_path = append_pending_extension ( & rgb_payment_info_path) ;
193+
178194 if rgb_payment_info_tmp_path. exists ( ) {
179195 let mut rgb_payment_info = parse_rgb_payment_info ( & rgb_payment_info_tmp_path) ;
180196 rgb_payment_info. local_rgb_amount = rgb_info. local_rgb_amount ;
@@ -189,19 +205,19 @@ pub(crate) fn color_commitment<SP: Deref>(channel_context: &ChannelContext<SP>,
189205 } else {
190206 let rgb_payment_info = RgbPaymentInfo {
191207 contract_id : rgb_info. contract_id ,
192- amount : htlc . amount_rgb . unwrap ( ) ,
208+ amount : htlc_amount_rgb ,
193209 local_rgb_amount : rgb_info. local_rgb_amount ,
194210 remote_rgb_amount : rgb_info. remote_rgb_amount ,
195- override_route_amount : false ,
196- inbound : htlc . offered == counterparty ,
211+ swap_payment : false ,
212+ inbound,
197213 } ;
198214 let serialized_info = serde_json:: to_string ( & rgb_payment_info) . expect ( "valid rgb payment info" ) ;
199215 fs:: write ( rgb_payment_info_proxy_id_path, serialized_info. clone ( ) ) . expect ( "able to write rgb payment info file" ) ;
200216 fs:: write ( rgb_payment_info_path, serialized_info) . expect ( "able to write rgb payment info file" ) ;
201217 rgb_payment_info
202218 } ;
203219
204- if htlc . offered == counterparty {
220+ if inbound {
205221 rgb_received_htlc += rgb_payment_info. amount
206222 } else {
207223 rgb_offered_htlc += rgb_payment_info. amount
@@ -328,10 +344,10 @@ pub(crate) fn color_commitment<SP: Deref>(channel_context: &ChannelContext<SP>,
328344
329345/// Color HTLC transaction
330346pub ( crate ) fn color_htlc ( htlc_tx : & mut Transaction , htlc : & HTLCOutputInCommitment , ldk_data_dir : & Path ) -> Result < ( ) , ChannelError > {
331- let htlc_amount_rgb = htlc. amount_rgb . expect ( "this is a rgb channel" ) ;
332- if htlc_amount_rgb == 0 {
347+ if htlc. amount_rgb . unwrap_or ( 0 ) == 0 {
333348 return Ok ( ( ) )
334349 }
350+ let htlc_amount_rgb = htlc. amount_rgb . expect ( "this HTLC has RGB assets" ) ;
335351
336352 htlc_tx. output . push ( TxOut { value : 0 , script_pubkey : Script :: new_op_return ( & [ 1 ] ) } ) ;
337353 let psbt = PartiallySignedTransaction :: from_unsigned_tx ( htlc_tx. clone ( ) ) . expect ( "valid transaction" ) ;
@@ -511,8 +527,10 @@ pub(crate) fn color_closing(channel_id: &ChannelId, funding_outpoint: &OutPoint,
511527}
512528
513529/// Get RgbPaymentInfo file path
514- pub fn get_rgb_payment_info_path ( payment_hash : & PaymentHash , ldk_data_dir : & Path ) -> PathBuf {
515- ldk_data_dir. join ( hex:: encode ( payment_hash. 0 ) )
530+ pub fn get_rgb_payment_info_path ( payment_hash : & PaymentHash , ldk_data_dir : & Path , inbound : bool ) -> PathBuf {
531+ let mut path = ldk_data_dir. join ( hex:: encode ( payment_hash. 0 ) ) ;
532+ path. set_extension ( if inbound { INBOUND_EXT } else { OUTBOUND_EXT } ) ;
533+ path
516534}
517535
518536/// Parse RgbPaymentInfo
@@ -559,17 +577,22 @@ pub fn write_rgb_channel_info(path: &PathBuf, rgb_info: &RgbInfo) {
559577 fs:: write ( path, serialized_info) . expect ( "able to write" )
560578}
561579
580+ fn append_pending_extension ( path : & PathBuf ) -> PathBuf {
581+ let mut new_path = path. clone ( ) ;
582+ new_path. set_extension ( format ! ( "{}_pending" , new_path. extension( ) . unwrap( ) . to_string_lossy( ) ) ) ;
583+ new_path
584+ }
585+
562586/// Write RGB payment info to file
563- pub fn write_rgb_payment_info_file ( ldk_data_dir : & Path , payment_hash : & PaymentHash , contract_id : ContractId , amount_rgb : u64 , override_route_amount : bool , inbound : bool ) {
564- let rgb_payment_info_path = get_rgb_payment_info_path ( payment_hash, ldk_data_dir) ;
565- let mut rgb_payment_info_tmp_path = rgb_payment_info_path. clone ( ) ;
566- rgb_payment_info_tmp_path. set_extension ( "pending" ) ;
587+ pub fn write_rgb_payment_info_file ( ldk_data_dir : & Path , payment_hash : & PaymentHash , contract_id : ContractId , amount_rgb : u64 , swap_payment : bool , inbound : bool ) {
588+ let rgb_payment_info_path = get_rgb_payment_info_path ( payment_hash, ldk_data_dir, inbound) ;
589+ let rgb_payment_info_tmp_path = append_pending_extension ( & rgb_payment_info_path) ;
567590 let rgb_payment_info = RgbPaymentInfo {
568591 contract_id,
569592 amount : amount_rgb,
570593 local_rgb_amount : 0 ,
571594 remote_rgb_amount : 0 ,
572- override_route_amount ,
595+ swap_payment ,
573596 inbound,
574597 } ;
575598 let serialized_info = serde_json:: to_string ( & rgb_payment_info) . expect ( "valid rgb payment info" ) ;
@@ -716,12 +739,13 @@ pub(crate) fn update_rgb_channel_amount_pending(channel_id: &ChannelId, rgb_offe
716739
717740/// Whether the payment is colored
718741pub ( crate ) fn is_payment_rgb ( ldk_data_dir : & Path , payment_hash : & PaymentHash ) -> bool {
719- ldk_data_dir. join ( hex:: encode ( payment_hash. 0 ) ) . exists ( )
742+ get_rgb_payment_info_path ( payment_hash, ldk_data_dir, false ) . exists ( ) ||
743+ get_rgb_payment_info_path ( payment_hash, ldk_data_dir, true ) . exists ( )
720744}
721745
722746/// Filter first_hops for contract_id
723747pub ( crate ) fn filter_first_hops ( ldk_data_dir : & Path , payment_hash : & PaymentHash , first_hops : & mut Vec < & ChannelDetails > ) -> ContractId {
724- let rgb_payment_info_path = ldk_data_dir . join ( hex :: encode ( payment_hash. 0 ) ) ;
748+ let rgb_payment_info_path = get_rgb_payment_info_path ( payment_hash, ldk_data_dir , false ) ;
725749 let serialized_info = fs:: read_to_string ( rgb_payment_info_path) . expect ( "valid rgb payment info file" ) ;
726750 let rgb_payment_info: RgbPaymentInfo = serde_json:: from_str ( & serialized_info) . expect ( "valid rgb payment info file" ) ;
727751 let contract_id = rgb_payment_info. contract_id ;
0 commit comments