Skip to content

Commit cdc8352

Browse files
committed
Correctly match spark ln sends on their id
1 parent 8957b94 commit cdc8352

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

orange-sdk/src/trusted_wallet/spark.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,14 @@ impl Spark {
473473
// Process transfers in this batch
474474
for transfer in &transfers_response {
475475
// Create a payment record
476+
let id = get_id_from_wallet_transfer(transfer)?;
476477
let store_tx: StoreTransaction = StoreTransaction::try_from(transfer)?;
478+
477479
// Insert payment into storage
478480
if let Err(err) = store.write(
479481
SPARK_PRIMARY_NAMESPACE,
480482
SPARK_PAYMENTS_NAMESPACE,
481-
transfer.id.to_string().as_str(),
483+
id.as_str(),
482484
&store_tx.encode(),
483485
) {
484486
log_error!(logger, "Failed to insert payment: {err:?}");
@@ -653,6 +655,33 @@ impl TryFrom<&SspUserRequest> for PaymentType {
653655
}
654656
}
655657

658+
fn get_id_from_wallet_transfer(transfer: &WalletTransfer) -> Result<String, TrustedError> {
659+
match &transfer.user_request {
660+
// Some(SspUserRequest::LeavesSwapRequest(_)) => PaymentType::TrustedInternal {},
661+
// Some(SspUserRequest::LightningReceiveRequest(_)) => PaymentType::IncomingLightning {},
662+
Some(SspUserRequest::LightningSendRequest(request)) => {
663+
// Spark uses UUIDs for payment IDs, so we need to convert them
664+
// to our format. Spark uses a UUID in the format `SparkLightningSendRequest:<uuid>`
665+
// We only need the UUID part, so we split by ':' and take the last part.
666+
// If the format is invalid, we return an error.
667+
if let Some(id) = request.id.split(':').next_back() {
668+
let uuid = Uuid::from_str(id).map_err(|_| {
669+
TrustedError::Other(format!("Failed to parse payment id: {id}"))
670+
})?;
671+
Ok(uuid.to_string())
672+
} else {
673+
// if it's not in the expected format, try to parse the whole thing as a uuid
674+
let uuid = Uuid::from_str(&request.id).map_err(|_| {
675+
TrustedError::Other(format!("Failed to parse payment id: {}", request.id))
676+
})?;
677+
Ok(uuid.to_string())
678+
}
679+
},
680+
None => Ok(transfer.id.to_string()),
681+
_ => Ok(transfer.id.to_string()), // todo do we need to handle other types differently?
682+
}
683+
}
684+
656685
impl From<TransferStatus> for TxStatus {
657686
fn from(o: TransferStatus) -> TxStatus {
658687
match o {

0 commit comments

Comments
 (0)