Skip to content

Commit e931dba

Browse files
authored
Merge branch 'RGB-Tools:master' into master
2 parents cb492da + 9181763 commit e931dba

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,9 @@ components:
23462346
payee_pubkey:
23472347
type: string
23482348
example: 03b79a4bc1ec365524b4fab9a39eb133753646babb5a1da5c4bc94c53110b7795d
2349+
preimage:
2350+
type: string
2351+
example: 89d28bd306aa9bb906fd0ac31092d04c37c919a171b343083167e2a3cdc60578
23492352
Peer:
23502353
type: object
23512354
required:

src/routes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ pub(crate) struct Payment {
863863
pub(crate) created_at: u64,
864864
pub(crate) updated_at: u64,
865865
pub(crate) payee_pubkey: String,
866+
pub(crate) preimage: Option<String>,
866867
}
867868

868869
#[derive(Clone, Deserialize, Serialize)]
@@ -1774,6 +1775,7 @@ pub(crate) async fn get_payment(
17741775
created_at: payment_info.created_at,
17751776
updated_at: payment_info.updated_at,
17761777
payee_pubkey: payment_info.payee_pubkey.to_string(),
1778+
preimage: payment_info.preimage.map(|p| hex_str(&p.0)),
17771779
},
17781780
}));
17791781
}
@@ -1803,6 +1805,7 @@ pub(crate) async fn get_payment(
18031805
created_at: payment_info.created_at,
18041806
updated_at: payment_info.updated_at,
18051807
payee_pubkey: payment_info.payee_pubkey.to_string(),
1808+
preimage: payment_info.preimage.map(|p| hex_str(&p.0)),
18061809
},
18071810
}));
18081811
}
@@ -2316,6 +2319,7 @@ pub(crate) async fn list_payments(
23162319
created_at: payment_info.created_at,
23172320
updated_at: payment_info.updated_at,
23182321
payee_pubkey: payment_info.payee_pubkey.to_string(),
2322+
preimage: payment_info.preimage.map(|p| hex_str(&p.0)),
23192323
});
23202324
}
23212325

@@ -2342,6 +2346,7 @@ pub(crate) async fn list_payments(
23422346
created_at: payment_info.created_at,
23432347
updated_at: payment_info.updated_at,
23442348
payee_pubkey: payment_info.payee_pubkey.to_string(),
2349+
preimage: payment_info.preimage.map(|p| hex_str(&p.0)),
23452350
});
23462351
}
23472352

src/test/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use amplify::s;
22
use biscuit_auth::{builder::date, macros::*, KeyPair};
3+
use bitcoin::hashes::sha256::Hash as Sha256;
4+
use bitcoin::hashes::Hash;
35
use chrono::{DateTime, Local, Utc};
46
use electrum_client::ElectrumApi;
57
use lazy_static::lazy_static;
@@ -41,7 +43,7 @@ use crate::routes::{
4143
SendPaymentResponse, SendRgbRequest, SendRgbResponse, Swap, SwapStatus, TakerRequest,
4244
Transaction, Transfer, UnlockRequest, Unspent, WitnessData,
4345
};
44-
use crate::utils::{hex_str_to_vec, ELECTRUM_URL_REGTEST, PROXY_ENDPOINT_LOCAL};
46+
use crate::utils::{hex_str, hex_str_to_vec, ELECTRUM_URL_REGTEST, PROXY_ENDPOINT_LOCAL};
4547

4648
use super::*;
4749

@@ -83,6 +85,13 @@ fn _bitcoin_cli() -> [String; 7] {
8385
]
8486
}
8587

88+
fn check_preimage_matches_hash(payment: &Payment, expected_payment_hash: &str) {
89+
let payment_preimage = payment.preimage.as_ref().unwrap();
90+
let payment_preimage_hash =
91+
hex_str(&Sha256::hash(&hex_str_to_vec(payment_preimage).unwrap()).to_byte_array());
92+
assert_eq!(payment_preimage_hash, expected_payment_hash);
93+
}
94+
8695
async fn _check_response_is_ok(res: Response) -> Response {
8796
if res.status() != reqwest::StatusCode::OK {
8897
panic!("reqwest response is not OK: {:?}", res.text().await);

src/test/payment.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,24 @@ async fn success() {
6464
assert_eq!(payment.asset_id, Some(asset_id.clone()));
6565
assert_eq!(payment.asset_amount, asset_amount);
6666
assert_eq!(payment.status, HTLCStatus::Succeeded);
67+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
6768
let payment = get_payment(node2_addr, &decoded.payment_hash).await;
6869
assert_eq!(payment.asset_id, Some(asset_id.clone()));
6970
assert_eq!(payment.asset_amount, asset_amount);
7071
assert_eq!(payment.status, HTLCStatus::Succeeded);
72+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
73+
let payment = list_payments(node1_addr)
74+
.await
75+
.into_iter()
76+
.find(|payment| payment.payment_hash == decoded.payment_hash)
77+
.unwrap();
78+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
79+
let payment = list_payments(node2_addr)
80+
.await
81+
.into_iter()
82+
.find(|payment| payment.payment_hash == decoded.payment_hash)
83+
.unwrap();
84+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
7185

7286
let asset_amount = Some(50);
7387
let LNInvoiceResponse { invoice } =
@@ -86,10 +100,12 @@ async fn success() {
86100
assert_eq!(payment.asset_id, Some(asset_id.clone()));
87101
assert_eq!(payment.asset_amount, asset_amount);
88102
assert_eq!(payment.status, HTLCStatus::Succeeded);
103+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
89104
let payment = get_payment(node2_addr, &decoded.payment_hash).await;
90105
assert_eq!(payment.asset_id, Some(asset_id.clone()));
91106
assert_eq!(payment.asset_amount, asset_amount);
92107
assert_eq!(payment.status, HTLCStatus::Succeeded);
108+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
93109

94110
let LNInvoiceResponse { invoice } =
95111
ln_invoice(node2_addr, None, Some(&asset_id), asset_amount, 900).await;
@@ -100,10 +116,12 @@ async fn success() {
100116
assert_eq!(payment.asset_id, Some(asset_id.clone()));
101117
assert_eq!(payment.asset_amount, asset_amount);
102118
assert_eq!(payment.status, HTLCStatus::Succeeded);
119+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
103120
let payment = get_payment(node2_addr, &decoded.payment_hash).await;
104121
assert_eq!(payment.asset_id, Some(asset_id.clone()));
105122
assert_eq!(payment.asset_amount, asset_amount);
106123
assert_eq!(payment.status, HTLCStatus::Succeeded);
124+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
107125

108126
let LNInvoiceResponse { invoice } =
109127
ln_invoice(node1_addr, None, Some(&asset_id), asset_amount, 900).await;
@@ -114,10 +132,12 @@ async fn success() {
114132
assert_eq!(payment.asset_id, Some(asset_id.clone()));
115133
assert_eq!(payment.asset_amount, asset_amount);
116134
assert_eq!(payment.status, HTLCStatus::Succeeded);
135+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
117136
let payment = get_payment(node2_addr, &decoded.payment_hash).await;
118137
assert_eq!(payment.asset_id, Some(asset_id.clone()));
119138
assert_eq!(payment.asset_amount, asset_amount);
120139
assert_eq!(payment.status, HTLCStatus::Succeeded);
140+
check_preimage_matches_hash(&payment, &decoded.payment_hash);
121141

122142
let channels_1 = list_channels(node1_addr).await;
123143
let channels_2 = list_channels(node2_addr).await;

0 commit comments

Comments
 (0)