@@ -19,8 +19,8 @@ use common::{
19
19
use ldk_node:: config:: EsploraSyncConfig ;
20
20
use ldk_node:: liquidity:: LSPS2ServiceConfig ;
21
21
use ldk_node:: payment:: {
22
- ConfirmationStatus , PaymentDirection , PaymentKind , PaymentStatus , QrPaymentResult ,
23
- SendingParameters ,
22
+ ConfirmationStatus , PaymentDetails , PaymentDirection , PaymentKind , PaymentStatus ,
23
+ QrPaymentResult , SendingParameters ,
24
24
} ;
25
25
use ldk_node:: { Builder , Event , NodeError } ;
26
26
@@ -29,8 +29,10 @@ use lightning::routing::gossip::{NodeAlias, NodeId};
29
29
use lightning:: util:: persist:: KVStore ;
30
30
31
31
use lightning_invoice:: { Bolt11InvoiceDescription , Description } ;
32
+ use lightning_types:: payment:: PaymentPreimage ;
32
33
33
34
use bitcoin:: address:: NetworkUnchecked ;
35
+ use bitcoin:: hashes:: sha256:: Hash as Sha256Hash ;
34
36
use bitcoin:: hashes:: Hash ;
35
37
use bitcoin:: Address ;
36
38
use bitcoin:: Amount ;
@@ -817,7 +819,7 @@ fn simple_bolt12_send_receive() {
817
819
let node_a_payments =
818
820
node_a. list_payments_with_filter ( |p| matches ! ( p. kind, PaymentKind :: Bolt12Offer { .. } ) ) ;
819
821
assert_eq ! ( node_a_payments. len( ) , 1 ) ;
820
- match node_a_payments. first ( ) . unwrap ( ) . kind {
822
+ match & node_a_payments. first ( ) . unwrap ( ) . kind {
821
823
PaymentKind :: Bolt12Offer {
822
824
hash,
823
825
preimage,
@@ -828,7 +830,7 @@ fn simple_bolt12_send_receive() {
828
830
} => {
829
831
assert ! ( hash. is_some( ) ) ;
830
832
assert ! ( preimage. is_some( ) ) ;
831
- assert_eq ! ( offer_id, offer. id( ) ) ;
833
+ assert_eq ! ( offer_id, & offer. id( ) ) ;
832
834
assert_eq ! ( & expected_quantity, qty) ;
833
835
assert_eq ! ( expected_payer_note. unwrap( ) , note. clone( ) . unwrap( ) . 0 ) ;
834
836
//TODO: We should eventually set and assert the secret sender-side, too, but the BOLT12
@@ -844,12 +846,12 @@ fn simple_bolt12_send_receive() {
844
846
let node_b_payments =
845
847
node_b. list_payments_with_filter ( |p| matches ! ( p. kind, PaymentKind :: Bolt12Offer { .. } ) ) ;
846
848
assert_eq ! ( node_b_payments. len( ) , 1 ) ;
847
- match node_b_payments. first ( ) . unwrap ( ) . kind {
849
+ match & node_b_payments. first ( ) . unwrap ( ) . kind {
848
850
PaymentKind :: Bolt12Offer { hash, preimage, secret, offer_id, .. } => {
849
851
assert ! ( hash. is_some( ) ) ;
850
852
assert ! ( preimage. is_some( ) ) ;
851
853
assert ! ( secret. is_some( ) ) ;
852
- assert_eq ! ( offer_id, offer. id( ) ) ;
854
+ assert_eq ! ( offer_id, & offer. id( ) ) ;
853
855
} ,
854
856
_ => {
855
857
panic ! ( "Unexpected payment kind" ) ;
@@ -883,7 +885,7 @@ fn simple_bolt12_send_receive() {
883
885
matches ! ( p. kind, PaymentKind :: Bolt12Offer { .. } ) && p. id == payment_id
884
886
} ) ;
885
887
assert_eq ! ( node_a_payments. len( ) , 1 ) ;
886
- let payment_hash = match node_a_payments. first ( ) . unwrap ( ) . kind {
888
+ let payment_hash = match & node_a_payments. first ( ) . unwrap ( ) . kind {
887
889
PaymentKind :: Bolt12Offer {
888
890
hash,
889
891
preimage,
@@ -894,7 +896,7 @@ fn simple_bolt12_send_receive() {
894
896
} => {
895
897
assert ! ( hash. is_some( ) ) ;
896
898
assert ! ( preimage. is_some( ) ) ;
897
- assert_eq ! ( offer_id, offer. id( ) ) ;
899
+ assert_eq ! ( offer_id, & offer. id( ) ) ;
898
900
assert_eq ! ( & expected_quantity, qty) ;
899
901
assert_eq ! ( expected_payer_note. unwrap( ) , note. clone( ) . unwrap( ) . 0 ) ;
900
902
//TODO: We should eventually set and assert the secret sender-side, too, but the BOLT12
@@ -913,12 +915,12 @@ fn simple_bolt12_send_receive() {
913
915
matches ! ( p. kind, PaymentKind :: Bolt12Offer { .. } ) && p. id == node_b_payment_id
914
916
} ) ;
915
917
assert_eq ! ( node_b_payments. len( ) , 1 ) ;
916
- match node_b_payments. first ( ) . unwrap ( ) . kind {
918
+ match & node_b_payments. first ( ) . unwrap ( ) . kind {
917
919
PaymentKind :: Bolt12Offer { hash, preimage, secret, offer_id, .. } => {
918
920
assert ! ( hash. is_some( ) ) ;
919
921
assert ! ( preimage. is_some( ) ) ;
920
922
assert ! ( secret. is_some( ) ) ;
921
- assert_eq ! ( offer_id, offer. id( ) ) ;
923
+ assert_eq ! ( offer_id, & offer. id( ) ) ;
922
924
} ,
923
925
_ => {
924
926
panic ! ( "Unexpected payment kind" ) ;
@@ -951,7 +953,7 @@ fn simple_bolt12_send_receive() {
951
953
matches ! ( p. kind, PaymentKind :: Bolt12Refund { .. } ) && p. id == node_b_payment_id
952
954
} ) ;
953
955
assert_eq ! ( node_b_payments. len( ) , 1 ) ;
954
- match node_b_payments. first ( ) . unwrap ( ) . kind {
956
+ match & node_b_payments. first ( ) . unwrap ( ) . kind {
955
957
PaymentKind :: Bolt12Refund {
956
958
hash,
957
959
preimage,
@@ -977,7 +979,7 @@ fn simple_bolt12_send_receive() {
977
979
matches ! ( p. kind, PaymentKind :: Bolt12Refund { .. } ) && p. id == node_a_payment_id
978
980
} ) ;
979
981
assert_eq ! ( node_a_payments. len( ) , 1 ) ;
980
- match node_a_payments. first ( ) . unwrap ( ) . kind {
982
+ match & node_a_payments. first ( ) . unwrap ( ) . kind {
981
983
PaymentKind :: Bolt12Refund { hash, preimage, secret, .. } => {
982
984
assert ! ( hash. is_some( ) ) ;
983
985
assert ! ( preimage. is_some( ) ) ;
@@ -1389,3 +1391,69 @@ fn facade_logging() {
1389
1391
validate_log_entry ( entry) ;
1390
1392
}
1391
1393
}
1394
+
1395
+ #[ test]
1396
+ fn spontaneous_send_with_custom_preimage ( ) {
1397
+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
1398
+ let chain_source = TestChainSource :: Esplora ( & electrsd) ;
1399
+ let ( node_a, node_b) = setup_two_nodes ( & chain_source, false , true , false ) ;
1400
+
1401
+ let address_a = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
1402
+ let premine_sat = 1_000_000 ;
1403
+ premine_and_distribute_funds (
1404
+ & bitcoind. client ,
1405
+ & electrsd. client ,
1406
+ vec ! [ address_a] ,
1407
+ Amount :: from_sat ( premine_sat) ,
1408
+ ) ;
1409
+ node_a. sync_wallets ( ) . unwrap ( ) ;
1410
+ node_b. sync_wallets ( ) . unwrap ( ) ;
1411
+ open_channel ( & node_a, & node_b, 500_000 , true , & electrsd) ;
1412
+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
1413
+ node_a. sync_wallets ( ) . unwrap ( ) ;
1414
+ node_b. sync_wallets ( ) . unwrap ( ) ;
1415
+ expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
1416
+ expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
1417
+
1418
+ let seed = b"test_payment_preimage" ;
1419
+ let bytes: Sha256Hash = Sha256Hash :: hash ( seed) ;
1420
+ let custom_bytes = bytes. to_byte_array ( ) ;
1421
+ let custom_preimage = PaymentPreimage ( custom_bytes) ;
1422
+
1423
+ let amount_msat = 100_000 ;
1424
+ let payment_id = node_a
1425
+ . spontaneous_payment ( )
1426
+ . send_with_preimage ( amount_msat, node_b. node_id ( ) , custom_preimage. clone ( ) . into ( ) , None )
1427
+ . unwrap ( ) ;
1428
+
1429
+ // check payment status and verify stored preimage
1430
+ expect_payment_successful_event ! ( node_a, Some ( payment_id) , None ) ;
1431
+ let details: PaymentDetails =
1432
+ node_a. list_payments_with_filter ( |p| p. id == payment_id) . first ( ) . unwrap ( ) . clone ( ) ;
1433
+ assert_eq ! ( details. status, PaymentStatus :: Succeeded ) ;
1434
+ if let PaymentKind :: Spontaneous { preimage : Some ( pi) , .. } = details. kind {
1435
+ assert_eq ! ( pi, custom_preimage. into( ) ) ;
1436
+ } else {
1437
+ panic ! ( "Expected a spontaneous PaymentKind with a preimage" ) ;
1438
+ }
1439
+
1440
+ // Verify receiver side (node_b)
1441
+ expect_payment_received_event ! ( node_b, amount_msat) ;
1442
+ let receiver_payments: Vec < PaymentDetails > = node_b. list_payments_with_filter ( |p| {
1443
+ p. direction == PaymentDirection :: Inbound
1444
+ && matches ! ( p. kind, PaymentKind :: Spontaneous { .. } )
1445
+ } ) ;
1446
+
1447
+ assert_eq ! ( receiver_payments. len( ) , 1 ) ;
1448
+ let receiver_details = & receiver_payments[ 0 ] ;
1449
+ assert_eq ! ( receiver_details. status, PaymentStatus :: Succeeded ) ;
1450
+ assert_eq ! ( receiver_details. amount_msat, Some ( amount_msat) ) ;
1451
+ assert_eq ! ( receiver_details. direction, PaymentDirection :: Inbound ) ;
1452
+
1453
+ // Verify receiver also has the same preimage
1454
+ if let PaymentKind :: Spontaneous { preimage : Some ( pi) , .. } = & receiver_details. kind {
1455
+ assert_eq ! ( pi, & custom_preimage. into( ) ) ;
1456
+ } else {
1457
+ panic ! ( "Expected receiver to have spontaneous PaymentKind with preimage" ) ;
1458
+ }
1459
+ }
0 commit comments