Skip to content

Commit 69e0ce1

Browse files
author
Conor Okus
committed
Point to API's
1 parent 72e25c1 commit 69e0ce1

File tree

5 files changed

+172
-3
lines changed

5 files changed

+172
-3
lines changed

docs/.vuepress/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ const tutorialSidebar = [
151151
"/tutorials/building-a-node-with-ldk/sending-payments",
152152
"Sending Payments",
153153
],
154+
[
155+
"/tutorials/building-a-node-with-ldk/receiving-payments",
156+
"Receiving Payments",
157+
],
154158
[
155159
"/tutorials/building-a-node-with-ldk/closing-a-channel",
156160
"Closing a Channel",

docs/tutorials/building-a-node-with-ldk/closing-a-channel.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ channel_manager.force_close_channel(&channel_id).expect("ERROR: Failed to close
2323
<template v-slot:kotlin>
2424

2525
```kotlin
26-
val res = channelManager!!.close_channel(channelId, pubKey)
26+
val res = channelManager.close_channel(channelId, pubKey)
2727

2828
if (res is Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_Err) {
2929
// Handle error
@@ -119,7 +119,6 @@ if (event is Event.SpendableOutputs) {
119119

120120
```Swift
121121
// Example where we spend straight to our BDK based wallet
122-
123122
func handleEvent(event: Event) {
124123
if let event = event.getValueAsSpendableOutputs() {
125124
let outputs = event.getOutputs()
@@ -146,4 +145,6 @@ func handleEvent(event: Event) {
146145

147146
</template>
148147

149-
</CodeSwitcher>
148+
</CodeSwitcher>
149+
150+
**References:** [Rust `SpendableOutputs` docs](https://docs.rs/lightning/0.0.121/lightning/events/enum.Event.html#variant.SpendableOutputs), [Java/Kotlin `SpendableOutputs` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java#L802)

docs/tutorials/building-a-node-with-ldk/opening-a-channel.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ class MyBroacaster: BroadcasterInterface {
293293

294294
</CodeSwitcher>
295295

296+
**References:** [Rust `BroadcasterInterface` docs](https://docs.rs/lightning/*/lightning/chain/chaininterface/trait.BroadcasterInterface.html), [Java/Kotlin `BroadcasterInterface` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/BroadcasterInterface.java)
297+
296298
::: tip Keep LDK in sync
297299

298300
Remember if you are restarting and have open channels then you should [let LDK know about the latest channel state.](./setting-up-a-channel-manager/#sync-channelmonitors-and-channelmanager-to-chain-tip)
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Receiving Payments
2+
3+
To receive a payment, you'll need to create an invoice of your own with an
4+
amount and description. `ChannelManager` contains the remaining information
5+
needed for the invoice. Use the provided utility to generate an invoice and
6+
register a pending payment in `ChannelManager`.
7+
8+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin', swift: 'Swift'}">
9+
<template v-slot:rust>
10+
11+
```rust
12+
let invoice = match utils::create_invoice_from_channelmanager(
13+
channel_manager,
14+
keys_manager,
15+
logger,
16+
currency,
17+
Some(amt_msat),
18+
"description".to_string(),
19+
expiry_secs,
20+
None,
21+
) {
22+
Ok(inv) => {
23+
println!("SUCCESS: generated invoice: {}", inv);
24+
inv
25+
}
26+
Err(e) => {
27+
println!("ERROR: failed to create invoice: {:?}", e);
28+
return;
29+
}
30+
};
31+
32+
let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
33+
inbound_payments.payments.insert(
34+
payment_hash,
35+
PaymentInfo {
36+
preimage: None,
37+
secret: Some(invoice.payment_secret().clone()),
38+
status: HTLCStatus::Pending,
39+
amt_msat: MillisatAmount(Some(amt_msat)),
40+
},
41+
);
42+
43+
```
44+
45+
</template>
46+
<template v-slot:kotlin>
47+
48+
```kotlin
49+
val description = "description"
50+
val amtMsat: Long = 3000000
51+
val invoice = UtilMethods.create_invoice_from_channelmanager(
52+
channelManager,
53+
keysManager.inner.as_NodeSigner(),
54+
logger,
55+
Currency.LDKCurrency_Regtest,
56+
Option_u64Z.some(amtMsat),
57+
description,
58+
300,
59+
Option_u16Z.some(144)
60+
)
61+
62+
val invoiceResult = (invoice as Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK).res
63+
val encodedInvoice = invoiceResult.to_str()
64+
```
65+
66+
</template>
67+
68+
<template v-slot:swift>
69+
70+
```swift
71+
let invoice = Bindings.createInvoiceFromChannelmanager(
72+
channelmanager: self.channelManager!,
73+
nodeSigner: myKeysManager.inner.asNodeSigner(),
74+
logger: self.logger,
75+
network: currency,
76+
amtMsat: amount,
77+
description: "Test Invoice",
78+
invoiceExpiryDeltaSecs: expiry,
79+
minFinalCltvExpiryDelta: nil
80+
)
81+
82+
invoice.getValue()!.toStr()
83+
```
84+
85+
</template>
86+
</CodeSwitcher>
87+
88+
While it is possible to create an invoice without using the utility,
89+
`ChannelManager` will reject any incoming HTLCs for unregistered payments to
90+
protect your privacy. In this case, use either `create_inbound_payment` or
91+
`create_inbound_payment_for_hash` to register a payment with `ChannelManager`
92+
before creating the invoice with the returned payment hash and/or secret.
93+
You might also opt to for `inbound_payment`, useful for generating invoices for [phantom node payments](https://docs.rs/lightning/*/lightning/sign/struct.PhantomKeysManager.html) without a ChannelManager.
94+
95+
# PaymentClaimable Event Handling
96+
97+
As with sending a payment, LDK will generate an event once a payment is
98+
received. It is your responsibility to handle the `PaymentClaimable` event by
99+
using `ChannelManager` to release the preimage and claim the funds.
100+
101+
<CodeSwitcher :languages="{rust:'Rust', kotlin:'Kotlin', swift:'Swift'}">
102+
<template v-slot:rust>
103+
104+
```rust
105+
Event::PaymentClaimable {
106+
payment_hash,
107+
purpose,
108+
amount_msat,
109+
receiver_node_id: _,
110+
via_channel_id: _,
111+
via_user_channel_id: _,
112+
claim_deadline: _,
113+
onion_fields: _,
114+
counterparty_skimmed_fee_msat: _,
115+
} => {
116+
println!(
117+
"\nEVENT: received payment from payment hash {} of {} millisatoshis",
118+
payment_hash, amount_msat,
119+
);
120+
print!("> ");
121+
io::stdout().flush().unwrap();
122+
let payment_preimage = match purpose {
123+
PaymentPurpose::InvoicePayment { payment_preimage, .. } => payment_preimage,
124+
PaymentPurpose::SpontaneousPayment(preimage) => Some(preimage),
125+
};
126+
channel_manager.claim_funds(payment_preimage.unwrap());
127+
}
128+
```
129+
130+
</template>
131+
<template v-slot:kotlin>
132+
133+
```kotlin
134+
if (event is Event.PaymentClaimable) {
135+
if (event.payment_hash != null) {
136+
val purpose = event.purpose as InvoicePayment
137+
val paymentPreimage = (purpose.payment_preimage as Option_ThirtyTwoBytesZ.Some).some
138+
139+
channelManager.claim_funds(paymentPreimage)
140+
}
141+
}
142+
```
143+
144+
</template>
145+
146+
<template v-slot:swift>
147+
148+
```swift
149+
if let paymentClaimedEvent = event.getValueAsPaymentClaimable() {
150+
let paymentPreimage = paymentClaimedEvent.getPurpose().getValueAsInvoicePayment()?.getPaymentPreimage()
151+
let _ = channelManager.claimFunds(paymentPreimage: paymentPreimage!)
152+
}
153+
```
154+
155+
</template>
156+
</CodeSwitcher>
157+
158+
**References:** [Rust `PaymentClaimable` docs](https://docs.rs/lightning/*/lightning/events/enum.Event.html#variant.PaymentClaimable), [Java/Kotlin `PaymentClaimable` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java#L261)

docs/tutorials/building-a-node-with-ldk/sending-payments.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ if let invoiceVal = parsedInvoice.getValue() {
7878

7979
</CodeSwitcher>
8080

81+
# PaymentSent & PaymentFailed Event Handling
82+
8183
An event is generated once a payment has completed. Successful payments result
8284
in a `PaymentSent` event with the preimage of the payment hash. Be sure to look
8385
out for a `PaymentFailed` event, if the payment fails for some reason, and act
@@ -129,3 +131,5 @@ if let paymentSentEvent = event.getValueAsPaymentSent() {
129131
</template>
130132

131133
</CodeSwitcher>
134+
135+
**References:** [Rust `PaymentSent` docs](https://docs.rs/lightning/*/lightning/events/enum.Event.html#variant.PaymentSent),[Rust `PaymentFailed` docs](https://docs.rs/lightning/*/lightning/events/enum.Event.html#variant.PaymentFailed), [Java/Kotlin `PaymentSent` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java#L464), [Java/Kotlin `PaymentFailed` bindings](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/Event.java#L512)

0 commit comments

Comments
 (0)