Skip to content

Commit

Permalink
Add examples for adding extra TLVs to a spontaneous payment
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Jan 29, 2024
1 parent 60552e8 commit 8367307
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
name: setup
runs-on: ubuntu-latest
outputs:
sdk-ref: ${{ inputs.sdk-ref || '0.2.10' }}
package-version: '0.2.10'
sdk-ref: ${{ inputs.sdk-ref || '0.2.11' }}
package-version: '0.2.11'
steps:
- run: echo "set pre-setup output variables"

Expand Down
23 changes: 23 additions & 0 deletions snippets/csharp/SendSpontaneousPayment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Breez.Sdk;
using System;
using System.Text;

public class SendSpontaneousPaymentSnippets
{
Expand All @@ -17,5 +19,26 @@ public void SendSpontaneousPayment(BlockingBreezServices sdk)
// Handle error
}
// ANCHOR_END: send-spontaneous-payment
}

public void SendSpontaneousPaymentWithTlvs(BlockingBreezServices sdk)
{
// ANCHOR: send-spontaneous-payment-with-tlvs
var nodeId = "...";
ulong amountMsat = 3_000_000;
var extraTlvs = new List<TlvEntry>{
new TlvEntry(34349334, Encoding.ASCII.GetBytes("Hello world!"))
};

try
{
var response = sdk.SendSpontaneousPayment(
new SendSpontaneousPaymentRequest(nodeId, amountMsat, extraTlvs));
}
catch (Exception)
{
// Handle error
}
// ANCHOR_END: send-spontaneous-payment-with-tlvs
}
}
16 changes: 16 additions & 0 deletions snippets/dart_snippets/lib/send_spontaneous_payment.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:breez_sdk/breez_sdk.dart';
import 'package:breez_sdk/bridge_generated.dart';
import 'dart:convert';

Future<SendPaymentResponse> sendSpontaneousPayment({
required String nodeId,
Expand All @@ -13,3 +14,18 @@ Future<SendPaymentResponse> sendSpontaneousPayment({
// ANCHOR_END: send-spontaneous-payment
return resp;
}

Future<SendPaymentResponse> sendSpontaneousPaymentWithTlvs({
required String nodeId,
}) async {
// ANCHOR: send-spontaneous-payment-with-tlvs
List<TlvEntry> extraTlvs = [TlvEntry(34349334, utf8.encode("Hello world!"))];
SendSpontaneousPaymentRequest req = SendSpontaneousPaymentRequest(
amountMsat: 3000000,
nodeId: nodeId,
extraTlvs: extraTlvs,
);
SendPaymentResponse resp = await BreezSDK().sendSpontaneousPayment(req: req);
// ANCHOR_END: send-spontaneous-payment-with-tlvs
return resp;
}
2 changes: 1 addition & 1 deletion snippets/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module main

go 1.19

require github.com/breez/breez-sdk-go v0.2.10
require github.com/breez/breez-sdk-go v0.2.11

replace github.com/breez/breez-sdk-go => ./packages/breez-sdk-go
20 changes: 20 additions & 0 deletions snippets/go/send_spontaneous_payment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package example

import (
"encoding/hex"
"log"

"github.com/breez/breez-sdk-go/breez_sdk"
Expand All @@ -17,3 +18,22 @@ func SendSpontaneousPayment() {
}
// ANCHOR_END: send-spontaneous-payment
}
func SendSpontaneousPaymentWithTlvs() {
// ANCHOR: send-spontaneous-payment-with-tlvs
value, _ := hex.DecodeString("Hello world!")
extraTlvs := []breez_sdk.TlvEntry{
breez_sdk.TlvEntry{
FieldNumber: uint64(34349334),
Value: value,
},
}
sendSpontaneousPaymentRequest := breez_sdk.SendSpontaneousPaymentRequest{
NodeId: "...",
AmountMsat: uint64(3_000_000),
ExtraTlvs: &extraTlvs,
}
if response, err := sdk.SendSpontaneousPayment(sendSpontaneousPaymentRequest); err == nil {
log.Printf("%#v", response)
}
// ANCHOR_END: send-spontaneous-payment-with-tlvs
}
2 changes: 1 addition & 1 deletion snippets/kotlin_mpp_lib/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ kotlin {
}
val commonMain by getting {
dependencies {
implementation("technology.breez:breez-sdk-kmp:0.2.10")
implementation("technology.breez:breez-sdk-kmp:0.2.11")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,20 @@ class SendSpontaneousPayment {
}
// ANCHOR_END: send-spontaneous-payment
}

fun send_spontaneous_payment_with_tlvs(sdk: BlockingBreezServices) {
// ANCHOR: send-spontaneous-payment-with-tlvs
val nodeId = "..."
val amountMsat = 3_000_000.toULong()
val extraTlvs = listOf<TlvEntry>(
TlvEntry(34_349_334.toULong(), "Hello world!".toByteArray())
)
try {
val response = sdk.sendSpontaneousPayment(
SendSpontaneousPaymentRequest(nodeId, amountMsat, extraTlvs))
} catch (e: Exception) {
// handle error
}
// ANCHOR_END: send-spontaneous-payment-with-tlvs
}
}
3 changes: 2 additions & 1 deletion snippets/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from src.buy_btc import buy
from src.send_onchain import get_current_fees, list_current_fees, check_reverse_swap_status, start_reverse_swap
from src.static_channel_backup import retrieve_backup_files
from src.send_spontaneous_payment import send_spontaneous_payment
from src.send_spontaneous_payment import send_spontaneous_payment, send_spontaneous_payment_with_tlvs
from src.receive_payment import receive_payment
from src.receive_onchain import generate_receive_onchain_address, get_in_progress_swap, list_refundables, execute_refund, get_channel_opening_fees
from src.fiat_currencies import list_supported_fiat_currencies, get_current_rates
Expand Down Expand Up @@ -73,6 +73,7 @@ def main():

#send spontaneous payment
send_spontaneous_payment(sdk_services)
send_spontaneous_payment_with_tlvs(sdk_services)

# fiat currencies
list_supported_fiat_currencies(sdk_services)
Expand Down
17 changes: 16 additions & 1 deletion snippets/python/src/send_spontaneous_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@ def send_spontaneous_payment(sdk_services):
node_id = "..."
amount_msat = 300000
try:
req = breez_sdk.SendSpontaneousPaymentRequest(node_id,amount_msat)
req = breez_sdk.SendSpontaneousPaymentRequest(node_id, amount_msat)
result = sdk_services.send_spontaneous_payment(req)
# ANCHOR: send-spontaneous-payment
return result
except Exception as error:
print(error)
raise


def send_spontaneous_payment_with_tlvs(sdk_services):
# ANCHOR: send-spontaneous-payment-with-tlvs
node_id = "..."
amount_msat = 300000
extra_tlvs = [breez_sdk.TlvEntry(34349334, str.encode("Hello world!"))]
try:
req = breez_sdk.SendSpontaneousPaymentRequest(node_id, amount_msat, extra_tlvs)
result = sdk_services.send_spontaneous_payment(req)
# ANCHOR: send-spontaneous-payment-with-tlvs
return result
except Exception as error:
print(error)
raise
28 changes: 27 additions & 1 deletion snippets/react-native/send_spontaneous_payment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { sendSpontaneousPayment } from '@breeztech/react-native-breez-sdk'
import {
sendSpontaneousPayment,
type TlvEntry
} from '@breeztech/react-native-breez-sdk'

const exampleSendSpontaneousPayment = async () => {
// ANCHOR: send-spontaneous-payment
Expand All @@ -10,3 +13,26 @@ const exampleSendSpontaneousPayment = async () => {
})
// ANCHOR_END: send-spontaneous-payment
}

const stringToBytes = (str: string): number[] => {
const bytes: number[] = []
for (let i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i))
}
return bytes
}

const exampleSendSpontaneousPaymentWithTlvs = async () => {
// ANCHOR: send-spontaneous-payment-with-tlvs
const nodeId = '...'
const extraTlvs: TlvEntry[] = [
{ fieldNumber: 34349334, value: stringToBytes('Hello world!') }
]

const sendPaymentResponse = await sendSpontaneousPayment({
nodeId,
amountMsat: 3000000,
extraTlvs
})
// ANCHOR_END: send-spontaneous-payment-with-tlvs
}
2 changes: 1 addition & 1 deletion snippets/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
[dependencies]
anyhow = "1"
bip39 = { version = "2", features = ["rand"] }
breez-sdk-core = { git = "https://github.com/breez/breez-sdk", tag = "0.2.10" }
breez-sdk-core = { git = "https://github.com/breez/breez-sdk", tag = "0.2.11" }
log = "0.4"
tokio = "1.29"
20 changes: 20 additions & 0 deletions snippets/rust/src/send_spontaneous_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ async fn send_spontaneous_payment(sdk: Arc<BreezServices>) -> Result<()> {
sdk.send_spontaneous_payment(SendSpontaneousPaymentRequest {
amount_msat: 3_000_000,
node_id,
extra_tlvs: None,
})
.await?;
// ANCHOR_END: send-spontaneous-payment

Ok(())
}

async fn send_spontaneous_payment_with_tlvs(sdk: Arc<BreezServices>) -> Result<()> {
// ANCHOR: send-spontaneous-payment-with-tlvs
let node_id = "...".into();
let extra_tlvs = vec![TlvEntry {
field_number: 34349334,
value: "Hello world!".as_bytes().to_vec(),
}];

sdk.send_spontaneous_payment(SendSpontaneousPaymentRequest {
amount_msat: 3_000_000,
node_id,
extra_tlvs: Some(extra_tlvs),
})
.await?;
// ANCHOR_END: send-spontaneous-payment-with-tlvs

Ok(())
}
2 changes: 1 addition & 1 deletion snippets/swift/BreezSDKExamples/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
platforms: [.macOS(.v12)],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.3"),
.package(url: "https://github.com/breez/breez-sdk-swift", from:"0.2.10")
.package(url: "https://github.com/breez/breez-sdk-swift", from:"0.2.11")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
13 changes: 12 additions & 1 deletion snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ func sendSpontaneousPayment(sdk: BlockingBreezServices) -> SendPaymentResponse?
nodeId: "...",
amountMsat: 3_000_000))
// ANCHOR_END: send-spontaneous-payment
return response
}

func sendSpontaneousPaymentWithTlvs(sdk: BlockingBreezServices) -> SendPaymentResponse? {
// ANCHOR: send-spontaneous-payment-with-tlvs
let extraTlvs = [TlvEntry] = [TlvEntry(fieldNumber: 34_349_334, value: Array("Hello world!".utf8))]
let response = try? sdk.sendSpontaneousPayment(
req: SendSpontaneousPaymentRequest(
nodeId: "...",
amountMsat: 3_000_000,
extraTlvs: extraTlvs))
// ANCHOR_END: send-spontaneous-payment-with-tlvs
return response

}
2 changes: 1 addition & 1 deletion src/guide/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Check https://github.com/breez/breez-sdk/releases for the latest version.

```toml
[dependencies]
breez-sdk-core = { git = "https://github.com/breez/breez-sdk", tag = "0.2.10" }
breez-sdk-core = { git = "https://github.com/breez/breez-sdk", tag = "0.2.11" }
```

## Flutter/Dart
Expand Down
70 changes: 70 additions & 0 deletions src/guide/send_spontaneous_payment.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,73 @@ They can even be spontaneous payments to a node without a bolt11 invoice.
```
</section>
</custom-tabs>

## Adding Extra TLVs to a Spontaneous Payment

A list of extra TLV data can also be sent with the sponaneous payment.

<custom-tabs category="lang">
<div slot="title">Rust</div>
<section>

```rust,ignore
{{#include ../../snippets/rust/src/send_spontaneous_payment.rs:send-spontaneous-payment-with-tlvs}}
```
</section>

<div slot="title">Swift</div>
<section>

```swift,ignore
{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift:send-spontaneous-payment-with-tlvs}}
```
</section>

<div slot="title">Kotlin</div>
<section>

```kotlin,ignore
{{#include ../../snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendSpontaneousPayment.kt:send-spontaneous-payment-with-tlvs}}
```
</section>

<div slot="title">React Native</div>
<section>

```typescript
{{#include ../../snippets/react-native/send_spontaneous_payment.ts:send-spontaneous-payment-with-tlvs}}
```
</section>

<div slot="title">Dart</div>
<section>

```dart,ignore
{{#include ../../snippets/dart_snippets/lib/send_spontaneous_payment.dart:send-spontaneous-payment}}
```
</section>

<div slot="title">Python</div>
<section>

```python,ignore
{{#include ../../snippets/python/src/send_spontaneous_payment.py:send-spontaneous-payment-with-tlvs}}
```
</section>

<div slot="title">Go</div>
<section>

```go,ignore
{{#include ../../snippets/go/send_spontaneous_payment.go:send-spontaneous-payment-with-tlvs}}
```
</section>

<div slot="title">C#</div>
<section>

```cs,ignore
{{#include ../../snippets/csharp/SendSpontaneousPayment.cs:send-spontaneous-payment-with-tlvs}}
```
</section>
</custom-tabs>

0 comments on commit 8367307

Please sign in to comment.