Skip to content

Commit

Permalink
Merge pull request #112 from breez/savage-keysend-extratlvs
Browse files Browse the repository at this point in the history
Spontaneous payment TLVs examples
  • Loading branch information
dangeross authored Jan 30, 2024
2 parents 60552e8 + 37a46fa commit 3a1053a
Show file tree
Hide file tree
Showing 23 changed files with 289 additions and 54 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.15' }}
package-version: '0.2.15'
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!").ToList())
};

try
{
var response = sdk.SendSpontaneousPayment(
new SendSpontaneousPaymentRequest(nodeId, amountMsat, extraTlvs));
}
catch (Exception)
{
// Handle error
}
// ANCHOR_END: send-spontaneous-payment-with-tlvs
}
}
21 changes: 21 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,23 @@ 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(
fieldNumber: 34349334,
value: 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.15

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.15")
}
}
}
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!".encodeToByteArray().asUByteArray().toList())
)
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
}
8 changes: 4 additions & 4 deletions snippets/react-native/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,10 @@
"@babel/helper-validator-identifier" "^7.22.20"
to-fast-properties "^2.0.0"

"@breeztech/[email protected].10":
version "0.2.10"
resolved "https://registry.yarnpkg.com/@breeztech/react-native-breez-sdk/-/react-native-breez-sdk-0.2.10.tgz#072b00f57028b5276c600a462cbcaac93d3dee22"
integrity sha512-81wtbkKGxhDaIweIQkD+LLDUL7WTtbI3qEb9vx4Itiu1hGzM8Q2RlYNF5R1jh2m4wdYKUKMWSBd3O4VRnywf1Q==
"@breeztech/[email protected].15":
version "0.2.15"
resolved "https://registry.yarnpkg.com/@breeztech/react-native-breez-sdk/-/react-native-breez-sdk-0.2.15.tgz#0a415747e94b08f0dbbca8aae23f2d4231881db3"
integrity sha512-VxT4wdZCyDrhZBe6heHJKUJc1nbVJ4Y0qbOm1B+LJa6JOq0YDooUFnbtHMq1PHQ3xviwuM1pCzAco0orWqnvDA==

"@esbuild/[email protected]":
version "0.18.20"
Expand Down
48 changes: 28 additions & 20 deletions snippets/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3a1053a

Please sign in to comment.