+ pub(super) fn get_payment_preimage(payment_hash: PaymentHash, payment_secret: PaymentSecret, keys: &ExpandedKey) -> Result<PaymentPreimage, APIError> {
+ let (iv_bytes, metadata_bytes) = decrypt_metadata(payment_secret, keys);
+
+ match Method::from_bits((metadata_bytes[0] & 0b1110_0000) >> METHOD_TYPE_OFFSET) {
+ Ok(Method::LdkPaymentHash) => {
+ derive_ldk_payment_preimage(payment_hash, &iv_bytes, &metadata_bytes, keys)
+ .map_err(|bad_preimage_bytes| APIError::APIMisuseError {
+ err: format!("Payment hash {} did not match decoded preimage {}", log_bytes!(payment_hash.0), log_bytes!(bad_preimage_bytes))
+ })
+ },
+ Ok(Method::UserPaymentHash) => Err(APIError::APIMisuseError {
+ err: "Expected payment type to be LdkPaymentHash, instead got UserPaymentHash".to_string()
+ }),
+ Err(other) => Err(APIError::APIMisuseError { err: format!("Unknown payment type: {}", other) }),
+ }
+ }
+