Skip to content

Commit d2e4f1d

Browse files
TheBlueMatt-macbookMatt Corallo
andauthored
Return PaymentId from pay and make it useful as a hashtable key in bindings (#30)
Co-authored-by: Matt Corallo <git+macbook@bluematt.me>
1 parent 69defd3 commit d2e4f1d

4 files changed

Lines changed: 36 additions & 45 deletions

File tree

orange-sdk/src/ffi/orange/mod.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Transaction {
6262
/// A PaymentId is a unique identifier for a payment. It can be either a Lightning payment or a
6363
/// Trusted payment. It is used to track the state of a payment and to provide information about
6464
/// the payment to the user.
65-
#[derive(Debug, Clone, Hash, PartialEq, Eq, uniffi::Object)]
65+
#[derive(Debug, Clone, Hash, PartialEq, Eq, uniffi::Enum)]
6666
pub enum PaymentId {
6767
Lightning(String),
6868
Trusted(String),
@@ -77,14 +77,6 @@ impl From<OrangePaymentId> for PaymentId {
7777
}
7878
}
7979

80-
#[uniffi::export]
81-
impl PaymentId {
82-
/// Gets a string representation of this PaymentId
83-
pub fn to_string(&self) -> String {
84-
format!("{self:?}")
85-
}
86-
}
87-
8880
/// The status of a transaction. This is used to track the state of a transaction
8981
#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
9082
pub enum TxStatus {
@@ -173,7 +165,7 @@ pub enum Event {
173165
/// An outgoing payment was successful.
174166
PaymentSuccessful {
175167
/// A local identifier used to track the payment.
176-
payment_id: Arc<PaymentId>,
168+
payment_id: PaymentId,
177169
/// The hash of the payment.
178170
payment_hash: Vec<u8>,
179171
/// The preimage to the `payment_hash`.
@@ -186,7 +178,7 @@ pub enum Event {
186178
/// An outgoing payment has failed.
187179
PaymentFailed {
188180
/// A local identifier used to track the payment.
189-
payment_id: Arc<PaymentId>,
181+
payment_id: PaymentId,
190182
/// The hash of the payment.
191183
///
192184
/// This will be `None` if the payment failed before receiving an invoice when paying a
@@ -202,7 +194,7 @@ pub enum Event {
202194
/// A payment has been received.
203195
PaymentReceived {
204196
/// A local identifier used to track the payment.
205-
payment_id: Arc<PaymentId>,
197+
payment_id: PaymentId,
206198
/// The hash of the payment.
207199
payment_hash: Vec<u8>,
208200
/// The value, in msats, that has been received.
@@ -216,7 +208,7 @@ pub enum Event {
216208
/// A payment has been received.
217209
OnchainPaymentReceived {
218210
/// A local identifier used to track the payment.
219-
payment_id: Arc<PaymentId>,
211+
payment_id: PaymentId,
220212
/// The transaction ID.
221213
txid: Vec<u8>,
222214
/// The value, in sats, that has been received.
@@ -252,7 +244,7 @@ pub enum Event {
252244
/// A rebalance from our trusted wallet has been initiated.
253245
RebalanceInitiated {
254246
/// The `payment_id` of the transaction that triggered the rebalance.
255-
trigger_payment_id: Arc<PaymentId>,
247+
trigger_payment_id: PaymentId,
256248
/// The `payment_id` of the rebalance payment sent from the trusted wallet.
257249
trusted_rebalance_payment_id: Vec<u8>,
258250
/// The amount, in msats, of the rebalance payment.
@@ -261,7 +253,7 @@ pub enum Event {
261253
/// A rebalance from our trusted wallet was successful.
262254
RebalanceSuccessful {
263255
/// The `payment_id` of the transaction that triggered the rebalance.
264-
trigger_payment_id: Arc<PaymentId>,
256+
trigger_payment_id: PaymentId,
265257
/// The `payment_id` of the rebalance payment sent from the trusted wallet.
266258
trusted_rebalance_payment_id: Vec<u8>,
267259
/// The `payment_id` of the rebalance payment sent to the LN wallet.
@@ -293,14 +285,14 @@ impl From<OrangeEvent> for Event {
293285
payment_preimage,
294286
fee_paid_msat,
295287
} => Event::PaymentSuccessful {
296-
payment_id: Arc::new(payment_id.into()),
288+
payment_id: payment_id.into(),
297289
payment_hash: payment_hash.0.to_vec(),
298290
payment_preimage: payment_preimage.0.to_vec(),
299291
fee_paid_msat,
300292
},
301293
OrangeEvent::PaymentFailed { payment_id, payment_hash, reason } => {
302294
Event::PaymentFailed {
303-
payment_id: Arc::new(payment_id.into()),
295+
payment_id: payment_id.into(),
304296
payment_hash: payment_hash.map(|h| h.0.to_vec()),
305297
reason: reason.map(|r| format!("{:?}", r)),
306298
}
@@ -312,15 +304,15 @@ impl From<OrangeEvent> for Event {
312304
custom_records,
313305
lsp_fee_msats,
314306
} => Event::PaymentReceived {
315-
payment_id: Arc::new(payment_id.into()),
307+
payment_id: payment_id.into(),
316308
payment_hash: payment_hash.0.to_vec(),
317309
amount_msat,
318310
custom_records: custom_records.into_iter().map(|r| r.value).collect(),
319311
lsp_fee_msats,
320312
},
321313
OrangeEvent::OnchainPaymentReceived { payment_id, txid, amount_sat, status: _ } => {
322314
Event::OnchainPaymentReceived {
323-
payment_id: Arc::new(payment_id.into()),
315+
payment_id: payment_id.into(),
324316
txid: txid.to_byte_array().to_vec(),
325317
amount_sat,
326318
}
@@ -352,7 +344,7 @@ impl From<OrangeEvent> for Event {
352344
trusted_rebalance_payment_id,
353345
amount_msat,
354346
} => Event::RebalanceInitiated {
355-
trigger_payment_id: Arc::new(trigger_payment_id.into()),
347+
trigger_payment_id: trigger_payment_id.into(),
356348
trusted_rebalance_payment_id: trusted_rebalance_payment_id.to_vec(),
357349
amount_msat,
358350
},
@@ -363,7 +355,7 @@ impl From<OrangeEvent> for Event {
363355
amount_msat,
364356
fee_msat,
365357
} => Event::RebalanceSuccessful {
366-
trigger_payment_id: Arc::new(trigger_payment_id.into()),
358+
trigger_payment_id: trigger_payment_id.into(),
367359
trusted_rebalance_payment_id: trusted_rebalance_payment_id.to_vec(),
368360
ln_rebalance_payment_id: ln_rebalance_payment_id.to_vec(),
369361
amount_msat,

orange-sdk/src/ffi/orange/wallet.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ impl Wallet {
156156
///
157157
/// Returns once the payment is pending. This does not mean the payment has completed -
158158
/// it may still fail. Use event handlers or transaction listing to monitor payment status.
159-
pub async fn pay(&self, payment_info: Arc<PaymentInfo>) -> Result<(), WalletError> {
160-
self.inner.pay(&payment_info.0).await?;
161-
Ok(())
159+
pub async fn pay(&self, payment_info: Arc<PaymentInfo>) -> Result<super::PaymentId, WalletError> {
160+
let id = self.inner.pay(&payment_info.0).await?;
161+
Ok(id.into())
162162
}
163163

164164
/// Estimates the fees required to pay using the provided PaymentInfo

orange-sdk/src/lib.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,9 @@ impl Wallet {
10561056
/// If applicable, this will also initiate a rebalance from the trusted wallet to the
10571057
/// lightning wallet based on the resulting balance and configured tunables.
10581058
///
1059-
/// Returns true once the payment is pending, however, this does not mean that the
1059+
/// Returns once the payment is pending, however, this does not mean that the
10601060
/// payment has been completed. The payment may still fail.
1061-
pub async fn pay(&self, instructions: &PaymentInfo) -> Result<(), WalletError> {
1061+
pub async fn pay(&self, instructions: &PaymentInfo) -> Result<PaymentId, WalletError> {
10621062
let trusted_balance = self.inner.trusted.get_balance().await?;
10631063
let ln_balance = self.inner.ln_wallet.get_balance();
10641064

@@ -1104,7 +1104,7 @@ impl Wallet {
11041104
.unwrap(),
11051105
},
11061106
);
1107-
return Ok(());
1107+
return Ok(PaymentId::Trusted(id));
11081108
},
11091109
Err(e) => {
11101110
log_debug!(self.inner.logger, "Trusted payment failed with {e:?}");
@@ -1149,7 +1149,7 @@ impl Wallet {
11491149
self.inner.runtime.spawn_cancellable_background_task(async move {
11501150
inner_ref.rebalancer.do_rebalance_if_needed().await;
11511151
});
1152-
return Ok(());
1152+
return Ok(PaymentId::SelfCustodial(id.0));
11531153
},
11541154
Err(e) => {
11551155
log_debug!(self.inner.logger, "LN payment failed with {:?}", e);
@@ -1183,23 +1183,21 @@ impl Wallet {
11831183
for method in methods.clone() {
11841184
match method {
11851185
PaymentMethod::LightningBolt11(_) => {
1186-
if pay_trusted(method, || PaymentType::OutgoingLightningBolt11 {
1186+
if let Ok(id) = pay_trusted(method, || PaymentType::OutgoingLightningBolt11 {
11871187
payment_preimage: None,
11881188
})
11891189
.await
1190-
.is_ok()
11911190
{
1192-
return Ok(());
1191+
return Ok(id);
11931192
};
11941193
},
11951194
PaymentMethod::LightningBolt12(_) => {
1196-
if pay_trusted(method, || PaymentType::OutgoingLightningBolt12 {
1195+
if let Ok(id) = pay_trusted(method, || PaymentType::OutgoingLightningBolt12 {
11971196
payment_preimage: None,
11981197
})
11991198
.await
1200-
.is_ok()
12011199
{
1202-
return Ok(());
1200+
return Ok(id);
12031201
}
12041202
},
12051203
PaymentMethod::OnChain { .. } => {},
@@ -1211,23 +1209,21 @@ impl Wallet {
12111209
for method in &methods {
12121210
match method {
12131211
PaymentMethod::LightningBolt11(_) => {
1214-
if pay_lightning(method, || PaymentType::OutgoingLightningBolt11 {
1212+
if let Ok(id) = pay_lightning(method, || PaymentType::OutgoingLightningBolt11 {
12151213
payment_preimage: None,
12161214
})
12171215
.await
1218-
.is_ok()
12191216
{
1220-
return Ok(());
1217+
return Ok(id);
12211218
}
12221219
},
12231220
PaymentMethod::LightningBolt12(_) => {
1224-
if pay_lightning(method, || PaymentType::OutgoingLightningBolt12 {
1221+
if let Ok(id) = pay_lightning(method, || PaymentType::OutgoingLightningBolt12 {
12251222
payment_preimage: None,
12261223
})
12271224
.await
1228-
.is_ok()
12291225
{
1230-
return Ok(());
1226+
return Ok(id);
12311227
}
12321228
},
12331229
PaymentMethod::OnChain { .. } => {},
@@ -1239,21 +1235,20 @@ impl Wallet {
12391235
// Finally, try trusted on-chain first,
12401236
for method in methods.clone() {
12411237
if let PaymentMethod::OnChain { .. } = method {
1242-
if pay_trusted(method, || PaymentType::OutgoingOnChain { txid: None }).await.is_ok()
1238+
if let Ok(id) = pay_trusted(method, || PaymentType::OutgoingOnChain { txid: None }).await
12431239
{
1244-
return Ok(());
1240+
return Ok(id);
12451241
};
12461242
}
12471243
}
12481244

12491245
// then pay on-chain out of the lightning wallet
12501246
for method in &methods {
12511247
if let PaymentMethod::OnChain { .. } = method {
1252-
if pay_lightning(method, || PaymentType::OutgoingOnChain { txid: None })
1248+
if let Ok(id) = pay_lightning(method, || PaymentType::OutgoingOnChain { txid: None })
12531249
.await
1254-
.is_ok()
12551250
{
1256-
return Ok(());
1251+
return Ok(id);
12571252
};
12581253
}
12591254
}

scripts/uniffi_bindgen_generate_swift.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ rustup target add aarch64-apple-ios-sim --toolchain stable
1717
rustup target add aarch64-apple-darwin x86_64-apple-darwin --toolchain stable
1818

1919
# Build rust target libs
20+
export IPHONEOS_DEPLOYMENT_TARGET=15.0
2021
cargo build --profile release-smaller --features uniffi || exit 1
2122
cargo build --profile release-smaller --target x86_64-apple-darwin --features uniffi || exit 1
2223
cargo build --profile release-smaller --target aarch64-apple-darwin --features uniffi || exit 1
24+
export CFLAGS_x86_64_apple_ios=-mios-version-min=$IPHONEOS_DEPLOYMENT_TARGET
2325
cargo build --profile release-smaller --target x86_64-apple-ios --features uniffi || exit 1
26+
export CFLAGS_aarch64_apple_ios=-mios-version-min=$IPHONEOS_DEPLOYMENT_TARGET
2427
cargo build --profile release-smaller --target aarch64-apple-ios --features uniffi || exit 1
28+
export CFLAGS_aarch64_apple_ios_sim=-mios-version-min=$IPHONEOS_DEPLOYMENT_TARGET
2529
cargo +stable build --release --target aarch64-apple-ios-sim --features uniffi || exit 1
2630

2731
# Combine ios-sim and apple-darwin (macos) libs for x86_64 and aarch64 (m1)

0 commit comments

Comments
 (0)