Add examples for adding extra TLVs to a spontaneous payment

This commit is contained in:
Ross Savage
2024-01-29 17:11:25 +01:00
parent 60552e8b35
commit 37a46fa4d6
23 changed files with 290 additions and 55 deletions

View File

@@ -1,4 +1,6 @@
using Breez.Sdk;
using System;
using System.Text;
public class SendSpontaneousPaymentSnippets
{
@@ -17,5 +19,26 @@ public class SendSpontaneousPaymentSnippets
// 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
}
}

View File

@@ -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,
@@ -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;
}

View File

@@ -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

View File

@@ -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
}

View File

@@ -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")
}
}
}

View File

@@ -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
}
}

View File

@@ -7,7 +7,7 @@ from src.connecting_lsp import get_lsp_info, connect_lsp
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)

View File

@@ -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

View File

@@ -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
}

View File

@@ -714,10 +714,10 @@
"@babel/helper-validator-identifier" "^7.22.20"
to-fast-properties "^2.0.0"
"@breeztech/react-native-breez-sdk@0.2.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/react-native-breez-sdk@0.2.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/android-arm64@0.18.20":
version "0.18.20"

View File

@@ -86,6 +86,7 @@ checksum = "c258c1a017ecaccfb34c8fa46ecbb2f5402584e31082c12b5caf0be82ac5ac44"
dependencies = [
"anyhow",
"atomic",
"backtrace",
"chrono",
]
@@ -449,7 +450,8 @@ dependencies = [
[[package]]
name = "bolt-derive"
version = "0.2.0"
source = "git+https://gitlab.com/cdecker/vls?tag=snapshot-20230920#b8d42d68bb3a525a8b7340b132220a0de922d62f"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4a20c5d2dd939142aa8edcfcaa5126bf0e4483d8edce6502037c2b0adbe069"
dependencies = [
"proc-macro2",
"quote",
@@ -458,8 +460,8 @@ dependencies = [
[[package]]
name = "breez-sdk-core"
version = "0.2.10"
source = "git+https://github.com/breez/breez-sdk?tag=0.2.10#d18529988eb7b1038473161e44c88225c7a0236d"
version = "0.2.15"
source = "git+https://github.com/breez/breez-sdk?tag=0.2.15#5486f167e046d28b0a3f9885b4bf9c44cd3e1ba8"
dependencies = [
"aes",
"anyhow",
@@ -476,8 +478,8 @@ dependencies = [
"gl-client",
"hex",
"lazy_static",
"lightning 0.0.115",
"lightning-invoice 0.23.0",
"lightning 0.0.118",
"lightning-invoice 0.26.0",
"log",
"miniz_oxide",
"once_cell",
@@ -606,7 +608,8 @@ dependencies = [
[[package]]
name = "cln-grpc"
version = "0.1.7"
source = "git+https://github.com/cdecker/lightning.git?tag=gl-20231113#075927b2c5f38fdbde4c0764135be58340b0ce08"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e745ebc04340d421c7bc8305942a60d3dd8531d94df24f1b8c3906942735f0d"
dependencies = [
"anyhow",
"bitcoin 0.30.1",
@@ -935,12 +938,13 @@ dependencies = [
[[package]]
name = "flutter_rust_bridge"
version = "1.80.1"
version = "1.82.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd0305ebc9f097d9826530a55fc2acd63222e912c663f7adce3ab641ecc0f346"
checksum = "23a5a7a1cd030cd63f53eafe2496eb4e40f520800e6e9b78cae6f3f3ba7f3c2b"
dependencies = [
"allo-isolate",
"anyhow",
"backtrace",
"build-target",
"bytemuck",
"cc",
@@ -1131,7 +1135,7 @@ checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0"
[[package]]
name = "gl-client"
version = "0.1.9"
source = "git+https://github.com/Blockstream/greenlight.git?rev=84a66ac3c467569365285814ef5a691b5b97f60d#84a66ac3c467569365285814ef5a691b5b97f60d"
source = "git+https://github.com/Blockstream/greenlight.git?rev=556eedf47a837b71c4277ba6ee84322f5cbd80de#556eedf47a837b71c4277ba6ee84322f5cbd80de"
dependencies = [
"anyhow",
"async-trait",
@@ -1145,7 +1149,7 @@ dependencies = [
"hex",
"http",
"http-body",
"lightning-invoice 0.24.0",
"lightning-invoice 0.26.0",
"log",
"mockall",
"pin-project",
@@ -1585,9 +1589,9 @@ dependencies = [
[[package]]
name = "lightning"
version = "0.0.116"
version = "0.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90a0f2155316f1570446a0447c993480673f840748c8ed25bbc59dfc442ac770"
checksum = "d52cec5fa9382154fe9671e8df93095b800c7d77abc66e2a5ef839d672521c5e"
dependencies = [
"bitcoin 0.29.2",
]
@@ -1608,14 +1612,14 @@ dependencies = [
[[package]]
name = "lightning-invoice"
version = "0.24.0"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1788c0158526ec27a502043c2911ea6ea58fdc656bdf8749484942c07b790d23"
checksum = "3eb24878b0f4ef75f020976c886d9ad1503867802329cc963e0ab4623ea3b25c"
dependencies = [
"bech32",
"bitcoin 0.29.2",
"bitcoin_hashes 0.11.0",
"lightning 0.0.116",
"lightning 0.0.118",
"num-traits",
"secp256k1 0.24.3",
]
@@ -3212,8 +3216,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "vls-core"
version = "0.10.0"
source = "git+https://gitlab.com/cdecker/vls?tag=snapshot-20230920#b8d42d68bb3a525a8b7340b132220a0de922d62f"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7465dd7f8cc004b67c52c6610ebf157695a93daf33f6a69c1b06b0f4e1fecbc0"
dependencies = [
"anyhow",
"backtrace",
@@ -3239,7 +3244,8 @@ dependencies = [
[[package]]
name = "vls-persist"
version = "0.10.0"
source = "git+https://gitlab.com/cdecker/vls?tag=snapshot-20230920#b8d42d68bb3a525a8b7340b132220a0de922d62f"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86279e60e9dcdd3bcc135b7979655f1c80c6b99c38fee25d390725bfc5a24994"
dependencies = [
"hex",
"log",
@@ -3252,7 +3258,8 @@ dependencies = [
[[package]]
name = "vls-protocol"
version = "0.10.0"
source = "git+https://gitlab.com/cdecker/vls?tag=snapshot-20230920#b8d42d68bb3a525a8b7340b132220a0de922d62f"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55e525ef3cb95a27b240662fb518ea283d25b1f51afe899f0f253747acc284f4"
dependencies = [
"as-any",
"bitcoin-consensus-derive",
@@ -3265,7 +3272,8 @@ dependencies = [
[[package]]
name = "vls-protocol-signer"
version = "0.10.0"
source = "git+https://gitlab.com/cdecker/vls?tag=snapshot-20230920#b8d42d68bb3a525a8b7340b132220a0de922d62f"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a766ac452520406eed1e7d8ad1c744156fc3ceaf6bdcefe3f3a44bd5f5ea0b29"
dependencies = [
"bit-vec",
"log",

View File

@@ -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.15" }
log = "0.4"
tokio = "1.29"

View File

@@ -1,14 +1,13 @@
use anyhow::Result;
use breez_sdk_core::*;
fn production_node_config() -> Result<NodeConfig> {
// ANCHOR: moving-to-production
// Read your Greenlight credentials from secure storage
let device_key: Vec<u8> = vec![];
let device_cert: Vec<u8> = vec![];
let greenlight_credentials = GreenlightCredentials {
device_key,
device_key,
device_cert,
};

View File

@@ -12,8 +12,14 @@ async fn generate_receive_onchain_address(sdk: Arc<BreezServices>) -> Result<()>
// Send your funds to the below bitcoin address
let address = swap_info.bitcoin_address;
info!("Minimum amount allowed to deposit in sats: {}", swap_info.min_allowed_deposit);
info!("Maximum amount allowed to deposit in sats: {}", swap_info.max_allowed_deposit);
info!(
"Minimum amount allowed to deposit in sats: {}",
swap_info.min_allowed_deposit
);
info!(
"Maximum amount allowed to deposit in sats: {}",
swap_info.max_allowed_deposit
);
// ANCHOR_END: generate-receive-onchain-address
Ok(())

View File

@@ -32,14 +32,9 @@ async fn list_current_fees(current_fees: ReverseSwapPairInfo) -> Result<()> {
async fn max_reverse_swap_amount(sdk: Arc<BreezServices>) -> Result<()> {
// ANCHOR: max-reverse-swap-amount
let max_amount = sdk
.max_reverse_swap_amount()
.await?;
let max_amount = sdk.max_reverse_swap_amount().await?;
info!(
"Max reverse swap amount: {:?}",
max_amount.total_sat
);
info!("Max reverse swap amount: {:?}", max_amount.total_sat);
// ANCHOR_END: max-reverse-swap-amount
Ok(())

View File

@@ -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(())
}

View File

@@ -1,19 +1,23 @@
use std::sync::Arc;
use anyhow::Result;
use breez_sdk_core::*;
use std::sync::Arc;
async fn webhook(sdk: Arc<BreezServices>) -> Result<()> {
// ANCHOR: register-webook
sdk.register_webhook("https://yourapplication.com".to_string()).await?;
sdk.register_webhook("https://yourapplication.com".to_string())
.await?;
// ANCHOR_END: register-webook
Ok(())
}
async fn payment_webhook(sdk: Arc<BreezServices>) -> Result<()> {
// ANCHOR: register-payment-webook
sdk.register_webhook("https://your-nds-service.com/notify?platform=ios&token=<PUSH_TOKEN>".to_string()).await?;
// ANCHOR_END: register-payment-webook
// ANCHOR: register-payment-webook
sdk.register_webhook(
"https://your-nds-service.com/notify?platform=ios&token=<PUSH_TOKEN>".to_string(),
)
.await?;
// ANCHOR_END: register-payment-webook
Ok(())
Ok(())
}

View File

@@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/breez/breez-sdk-swift",
"state" : {
"revision" : "74402afba8218ff84a0f32d1bc0d4ddc7698f576",
"version" : "0.2.10"
"revision" : "73c7bf88ce51471f4a6313aa583fa1a5394442f7",
"version" : "0.2.15"
}
},
{

View File

@@ -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.15")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.

View File

@@ -16,6 +16,17 @@ func sendSpontaneousPayment(sdk: BlockingBreezServices) -> SendPaymentResponse?
nodeId: "...",
amountMsat: 3_000_000))
// ANCHOR_END: send-spontaneous-payment
return response
return response
}
func sendSpontaneousPaymentWithTlvs(sdk: BlockingBreezServices) -> SendPaymentResponse? {
// ANCHOR: send-spontaneous-payment-with-tlvs
let extraTlvs = [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
}