@@ -58,6 +58,8 @@ pub const BITCOIN_NETWORK_FNAME: &str = "bitcoin_network";
58
58
pub const ELECTRUM_URL_FNAME : & str = "electrum_url" ;
59
59
/// Name of the file containing the wallet fingerprint
60
60
pub const WALLET_FINGERPRINT_FNAME : & str = "wallet_fingerprint" ;
61
+ const INBOUND_EXT : & str = "inbound" ;
62
+ const OUTBOUND_EXT : & str = "outbound" ;
61
63
62
64
/// RGB channel info
63
65
#[ derive( Debug , Clone , Deserialize , Serialize ) ]
@@ -82,7 +84,7 @@ pub struct RgbPaymentInfo {
82
84
/// RGB remote amount
83
85
pub remote_rgb_amount : u64 ,
84
86
/// Whether the RGB amount in route should be overridden
85
- pub override_route_amount : bool ,
87
+ pub swap_payment : bool ,
86
88
/// Whether the payment is inbound
87
89
pub inbound : bool ,
88
90
}
@@ -166,15 +168,29 @@ pub(crate) fn color_commitment<SP: Deref>(channel_context: &ChannelContext<SP>,
166
168
. assignments_type ( & FieldName :: from ( "beneficiary" ) ) . expect ( "valid assignment" ) ;
167
169
168
170
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
+
169
176
let htlc_vout = htlc. transaction_output_index . unwrap ( ) ;
170
177
178
+ let inbound = htlc. offered == counterparty;
179
+
171
180
let htlc_payment_hash = hex:: encode ( htlc. payment_hash . 0 ) ;
172
181
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) ;
175
183
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
+
178
194
if rgb_payment_info_tmp_path. exists ( ) {
179
195
let mut rgb_payment_info = parse_rgb_payment_info ( & rgb_payment_info_tmp_path) ;
180
196
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>,
189
205
} else {
190
206
let rgb_payment_info = RgbPaymentInfo {
191
207
contract_id : rgb_info. contract_id ,
192
- amount : htlc . amount_rgb . unwrap ( ) ,
208
+ amount : htlc_amount_rgb ,
193
209
local_rgb_amount : rgb_info. local_rgb_amount ,
194
210
remote_rgb_amount : rgb_info. remote_rgb_amount ,
195
- override_route_amount : false ,
196
- inbound : htlc . offered == counterparty ,
211
+ swap_payment : false ,
212
+ inbound,
197
213
} ;
198
214
let serialized_info = serde_json:: to_string ( & rgb_payment_info) . expect ( "valid rgb payment info" ) ;
199
215
fs:: write ( rgb_payment_info_proxy_id_path, serialized_info. clone ( ) ) . expect ( "able to write rgb payment info file" ) ;
200
216
fs:: write ( rgb_payment_info_path, serialized_info) . expect ( "able to write rgb payment info file" ) ;
201
217
rgb_payment_info
202
218
} ;
203
219
204
- if htlc . offered == counterparty {
220
+ if inbound {
205
221
rgb_received_htlc += rgb_payment_info. amount
206
222
} else {
207
223
rgb_offered_htlc += rgb_payment_info. amount
@@ -328,10 +344,10 @@ pub(crate) fn color_commitment<SP: Deref>(channel_context: &ChannelContext<SP>,
328
344
329
345
/// Color HTLC transaction
330
346
pub ( 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 {
333
348
return Ok ( ( ) )
334
349
}
350
+ let htlc_amount_rgb = htlc. amount_rgb . expect ( "this HTLC has RGB assets" ) ;
335
351
336
352
htlc_tx. output . push ( TxOut { value : 0 , script_pubkey : Script :: new_op_return ( & [ 1 ] ) } ) ;
337
353
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,
511
527
}
512
528
513
529
/// 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
516
534
}
517
535
518
536
/// Parse RgbPaymentInfo
@@ -559,17 +577,22 @@ pub fn write_rgb_channel_info(path: &PathBuf, rgb_info: &RgbInfo) {
559
577
fs:: write ( path, serialized_info) . expect ( "able to write" )
560
578
}
561
579
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
+
562
586
/// 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) ;
567
590
let rgb_payment_info = RgbPaymentInfo {
568
591
contract_id,
569
592
amount : amount_rgb,
570
593
local_rgb_amount : 0 ,
571
594
remote_rgb_amount : 0 ,
572
- override_route_amount ,
595
+ swap_payment ,
573
596
inbound,
574
597
} ;
575
598
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
716
739
717
740
/// Whether the payment is colored
718
741
pub ( 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 ( )
720
744
}
721
745
722
746
/// Filter first_hops for contract_id
723
747
pub ( 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 ) ;
725
749
let serialized_info = fs:: read_to_string ( rgb_payment_info_path) . expect ( "valid rgb payment info file" ) ;
726
750
let rgb_payment_info: RgbPaymentInfo = serde_json:: from_str ( & serialized_info) . expect ( "valid rgb payment info file" ) ;
727
751
let contract_id = rgb_payment_info. contract_id ;
0 commit comments