From 83673070bdd01eed259e2b75d9ecdf8d268438e6 Mon Sep 17 00:00:00 2001 From: Ross Savage Date: Tue, 12 Dec 2023 20:38:34 +0100 Subject: [PATCH] Add examples for adding extra TLVs to a spontaneous payment --- .github/workflows/main.yml | 4 +- snippets/csharp/SendSpontaneousPayment.cs | 23 ++++++ .../lib/send_spontaneous_payment.dart | 16 +++++ snippets/go/go.mod | 2 +- snippets/go/send_spontaneous_payment.go | 20 ++++++ .../kotlin_mpp_lib/shared/build.gradle.kts | 2 +- .../kotlinmpplib/SendSpontaneousPayment.kt | 16 +++++ snippets/python/main.py | 3 +- .../python/src/send_spontaneous_payment.py | 17 ++++- .../react-native/send_spontaneous_payment.ts | 28 +++++++- snippets/rust/Cargo.toml | 2 +- snippets/rust/src/send_spontaneous_payment.rs | 20 ++++++ snippets/swift/BreezSDKExamples/Package.swift | 2 +- .../Sources/SendSpontaneous.swift | 13 +++- src/guide/install.md | 2 +- src/guide/send_spontaneous_payment.md | 70 +++++++++++++++++++ 16 files changed, 229 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3a54d0b4..dea458b4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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" diff --git a/snippets/csharp/SendSpontaneousPayment.cs b/snippets/csharp/SendSpontaneousPayment.cs index 6bc32b20..946b6500 100644 --- a/snippets/csharp/SendSpontaneousPayment.cs +++ b/snippets/csharp/SendSpontaneousPayment.cs @@ -1,4 +1,6 @@ using Breez.Sdk; +using System; +using System.Text; public class SendSpontaneousPaymentSnippets { @@ -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{ + 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 } } diff --git a/snippets/dart_snippets/lib/send_spontaneous_payment.dart b/snippets/dart_snippets/lib/send_spontaneous_payment.dart index 8cdbb7be..e2dd86ef 100644 --- a/snippets/dart_snippets/lib/send_spontaneous_payment.dart +++ b/snippets/dart_snippets/lib/send_spontaneous_payment.dart @@ -1,5 +1,6 @@ import 'package:breez_sdk/breez_sdk.dart'; import 'package:breez_sdk/bridge_generated.dart'; +import 'dart:convert'; Future sendSpontaneousPayment({ required String nodeId, @@ -13,3 +14,18 @@ Future sendSpontaneousPayment({ // ANCHOR_END: send-spontaneous-payment return resp; } + +Future sendSpontaneousPaymentWithTlvs({ + required String nodeId, +}) async { + // ANCHOR: send-spontaneous-payment-with-tlvs + List 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; +} diff --git a/snippets/go/go.mod b/snippets/go/go.mod index dfe8a584..0dcec055 100644 --- a/snippets/go/go.mod +++ b/snippets/go/go.mod @@ -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 diff --git a/snippets/go/send_spontaneous_payment.go b/snippets/go/send_spontaneous_payment.go index a4930b57..a2d1773a 100644 --- a/snippets/go/send_spontaneous_payment.go +++ b/snippets/go/send_spontaneous_payment.go @@ -1,6 +1,7 @@ package example import ( + "encoding/hex" "log" "github.com/breez/breez-sdk-go/breez_sdk" @@ -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 +} diff --git a/snippets/kotlin_mpp_lib/shared/build.gradle.kts b/snippets/kotlin_mpp_lib/shared/build.gradle.kts index a5a045af..2a3a7965 100644 --- a/snippets/kotlin_mpp_lib/shared/build.gradle.kts +++ b/snippets/kotlin_mpp_lib/shared/build.gradle.kts @@ -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") } } } diff --git a/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendSpontaneousPayment.kt b/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendSpontaneousPayment.kt index 1d0948f8..52171175 100644 --- a/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendSpontaneousPayment.kt +++ b/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendSpontaneousPayment.kt @@ -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(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 + } } \ No newline at end of file diff --git a/snippets/python/main.py b/snippets/python/main.py index c0343bd8..50036a7a 100755 --- a/snippets/python/main.py +++ b/snippets/python/main.py @@ -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 @@ -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) diff --git a/snippets/python/src/send_spontaneous_payment.py b/snippets/python/src/send_spontaneous_payment.py index 5ffefe75..4e5d68bd 100644 --- a/snippets/python/src/send_spontaneous_payment.py +++ b/snippets/python/src/send_spontaneous_payment.py @@ -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 \ No newline at end of file diff --git a/snippets/react-native/send_spontaneous_payment.ts b/snippets/react-native/send_spontaneous_payment.ts index bdfcbc24..2a784a3e 100644 --- a/snippets/react-native/send_spontaneous_payment.ts +++ b/snippets/react-native/send_spontaneous_payment.ts @@ -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 @@ -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 +} diff --git a/snippets/rust/Cargo.toml b/snippets/rust/Cargo.toml index ff6a3f1e..a0c51f9a 100644 --- a/snippets/rust/Cargo.toml +++ b/snippets/rust/Cargo.toml @@ -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" \ No newline at end of file diff --git a/snippets/rust/src/send_spontaneous_payment.rs b/snippets/rust/src/send_spontaneous_payment.rs index 001bd449..89af0f11 100644 --- a/snippets/rust/src/send_spontaneous_payment.rs +++ b/snippets/rust/src/send_spontaneous_payment.rs @@ -10,9 +10,29 @@ async fn send_spontaneous_payment(sdk: Arc) -> 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) -> 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(()) +} diff --git a/snippets/swift/BreezSDKExamples/Package.swift b/snippets/swift/BreezSDKExamples/Package.swift index 86f6df79..acd9968b 100644 --- a/snippets/swift/BreezSDKExamples/Package.swift +++ b/snippets/swift/BreezSDKExamples/Package.swift @@ -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. diff --git a/snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift b/snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift index a5302eb3..256a3b1b 100644 --- a/snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift +++ b/snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift @@ -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 - } diff --git a/src/guide/install.md b/src/guide/install.md index a491490b..d70b0f01 100644 --- a/src/guide/install.md +++ b/src/guide/install.md @@ -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 diff --git a/src/guide/send_spontaneous_payment.md b/src/guide/send_spontaneous_payment.md index 778f0ba0..345c7b3c 100644 --- a/src/guide/send_spontaneous_payment.md +++ b/src/guide/send_spontaneous_payment.md @@ -67,3 +67,73 @@ They can even be spontaneous payments to a node without a bolt11 invoice. ``` + +## Adding Extra TLVs to a Spontaneous Payment + +A list of extra TLV data can also be sent with the sponaneous payment. + + +
Rust
+
+ +```rust,ignore +{{#include ../../snippets/rust/src/send_spontaneous_payment.rs:send-spontaneous-payment-with-tlvs}} +``` +
+ +
Swift
+
+ +```swift,ignore +{{#include ../../snippets/swift/BreezSDKExamples/Sources/SendSpontaneous.swift:send-spontaneous-payment-with-tlvs}} +``` +
+ +
Kotlin
+
+ +```kotlin,ignore +{{#include ../../snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/SendSpontaneousPayment.kt:send-spontaneous-payment-with-tlvs}} +``` +
+ +
React Native
+
+ +```typescript +{{#include ../../snippets/react-native/send_spontaneous_payment.ts:send-spontaneous-payment-with-tlvs}} +``` +
+ +
Dart
+
+ +```dart,ignore +{{#include ../../snippets/dart_snippets/lib/send_spontaneous_payment.dart:send-spontaneous-payment}} +``` +
+ +
Python
+
+ +```python,ignore +{{#include ../../snippets/python/src/send_spontaneous_payment.py:send-spontaneous-payment-with-tlvs}} +``` +
+ +
Go
+
+ +```go,ignore +{{#include ../../snippets/go/send_spontaneous_payment.go:send-spontaneous-payment-with-tlvs}} +``` +
+ +
C#
+
+ +```cs,ignore +{{#include ../../snippets/csharp/SendSpontaneousPayment.cs:send-spontaneous-payment-with-tlvs}} +``` +
+