From b6a72d980cb7a06335207694e173e8ba2a8c947f Mon Sep 17 00:00:00 2001 From: ok300 <106775972+ok300@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:56:56 +0200 Subject: [PATCH] Add dart snippets --- .github/workflows/main.yml | 2 +- snippets/dart_snippets/.gitignore | 7 + snippets/dart_snippets/README.md | 9 + snippets/dart_snippets/analysis_options.yaml | 30 ++ snippets/dart_snippets/bin/dart_snippets.dart | 6 + snippets/dart_snippets/flake.lock | 61 +++ snippets/dart_snippets/flake.nix | 26 + .../dart_snippets/lib/fiat_currencies.dart | 17 + .../dart_snippets/lib/getting_started.dart | 74 +++ snippets/dart_snippets/lib/list_payments.dart | 36 ++ snippets/dart_snippets/lib/lnurl_auth.dart | 21 + snippets/dart_snippets/lib/lnurl_pay.dart | 26 + .../dart_snippets/lib/lnurl_withdraw.dart | 23 + snippets/dart_snippets/lib/pay_onchain.dart | 53 ++ .../dart_snippets/lib/receive_onchain.dart | 69 +++ .../dart_snippets/lib/receive_payment.dart | 16 + snippets/dart_snippets/lib/sdk_instance.dart | 3 + snippets/dart_snippets/lib/send_payment.dart | 14 + snippets/dart_snippets/pubspec.lock | 504 ++++++++++++++++++ snippets/dart_snippets/pubspec.yaml | 15 + 20 files changed, 1011 insertions(+), 1 deletion(-) create mode 100644 snippets/dart_snippets/.gitignore create mode 100644 snippets/dart_snippets/README.md create mode 100644 snippets/dart_snippets/analysis_options.yaml create mode 100644 snippets/dart_snippets/bin/dart_snippets.dart create mode 100644 snippets/dart_snippets/flake.lock create mode 100644 snippets/dart_snippets/flake.nix create mode 100644 snippets/dart_snippets/lib/fiat_currencies.dart create mode 100644 snippets/dart_snippets/lib/getting_started.dart create mode 100644 snippets/dart_snippets/lib/list_payments.dart create mode 100644 snippets/dart_snippets/lib/lnurl_auth.dart create mode 100644 snippets/dart_snippets/lib/lnurl_pay.dart create mode 100644 snippets/dart_snippets/lib/lnurl_withdraw.dart create mode 100644 snippets/dart_snippets/lib/pay_onchain.dart create mode 100644 snippets/dart_snippets/lib/receive_onchain.dart create mode 100644 snippets/dart_snippets/lib/receive_payment.dart create mode 100644 snippets/dart_snippets/lib/sdk_instance.dart create mode 100644 snippets/dart_snippets/lib/send_payment.dart create mode 100644 snippets/dart_snippets/pubspec.lock create mode 100644 snippets/dart_snippets/pubspec.yaml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d27f4005..2cfa1ff4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -111,7 +111,7 @@ jobs: - uses: actions/download-artifact@v3 with: name: breez-sdk-flutter-${{ needs.setup.outputs.package-version }} - path: snippets/dart_snippets/packages/breez-sdk-flutter + path: snippets/dart_snippets/packages/breez-liquid-sdk-flutter - name: pub-get working-directory: snippets/dart_snippets diff --git a/snippets/dart_snippets/.gitignore b/snippets/dart_snippets/.gitignore new file mode 100644 index 00000000..bf02cc3b --- /dev/null +++ b/snippets/dart_snippets/.gitignore @@ -0,0 +1,7 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ +packages/ +.flutter-version +.flutter-plugins +.flutter-plugins-dependencies \ No newline at end of file diff --git a/snippets/dart_snippets/README.md b/snippets/dart_snippets/README.md new file mode 100644 index 00000000..26e925ad --- /dev/null +++ b/snippets/dart_snippets/README.md @@ -0,0 +1,9 @@ +## Steps to compile the snippets locally +1. Build a flutter package + - By running the publish-all-platforms CI in the breez-liquid-sdk repository (use dummy binaries) + - or by cloning https://github.com/breez/breez-liquid-sdk-flutter +2. Place the files in the folder `snippets/dart-snippets/packages/breez-liquid-sdk-flutter` +3. Happy coding + +## Nix +Use the command `nix develop` \ No newline at end of file diff --git a/snippets/dart_snippets/analysis_options.yaml b/snippets/dart_snippets/analysis_options.yaml new file mode 100644 index 00000000..6dd972f3 --- /dev/null +++ b/snippets/dart_snippets/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +analyzer: + exclude: + - packages/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/snippets/dart_snippets/bin/dart_snippets.dart b/snippets/dart_snippets/bin/dart_snippets.dart new file mode 100644 index 00000000..1aaf2ace --- /dev/null +++ b/snippets/dart_snippets/bin/dart_snippets.dart @@ -0,0 +1,6 @@ +import 'package:flutter_breez_liquid/breez_liquid.dart' as liquid_sdk; + +void main() async { + // Initialize library + await liquid_sdk.initialize(); +} \ No newline at end of file diff --git a/snippets/dart_snippets/flake.lock b/snippets/dart_snippets/flake.lock new file mode 100644 index 00000000..a068d8e3 --- /dev/null +++ b/snippets/dart_snippets/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1719838683, + "narHash": "sha256-Zw9rQjHz1ilNIimEXFeVa1ERNRBF8DoXDhLAZq5B4pE=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d032c1a6dfad4eedec7e35e91986becc699d7d69", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/snippets/dart_snippets/flake.nix b/snippets/dart_snippets/flake.nix new file mode 100644 index 00000000..3a989cd2 --- /dev/null +++ b/snippets/dart_snippets/flake.nix @@ -0,0 +1,26 @@ +{ + description = "Flutter flake"; + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05"; + flake-utils.url = "github:numtide/flake-utils"; + }; + outputs = + { self + , nixpkgs + , flake-utils + , + }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { + buildInputs = with pkgs; [ + flutter + ]; + }; + + formatter = pkgs.nixpkgs-fmt; + }); +} diff --git a/snippets/dart_snippets/lib/fiat_currencies.dart b/snippets/dart_snippets/lib/fiat_currencies.dart new file mode 100644 index 00000000..445dc87f --- /dev/null +++ b/snippets/dart_snippets/lib/fiat_currencies.dart @@ -0,0 +1,17 @@ +import 'package:flutter_breez_liquid/bridge_generated.dart'; + +Future> listFiatCurrencies() async { + // ANCHOR: list-fiat-currencies + List fiatCurrencyList = await breezSDK.listFiatCurrencies(); + // ANCHOR_END: list-fiat-currencies + return fiatCurrencyList; +} + +Future> fetchFiatRates(String lspId) async { + // ANCHOR: fetch-fiat-rates + Map fiatRatesMap = await breezSDK.fetchFiatRates(); + // print your desired rate + print(fiatRatesMap["USD"]?.value); + // ANCHOR_END: fetch-fiat-rates + return fiatRatesMap; +} diff --git a/snippets/dart_snippets/lib/getting_started.dart b/snippets/dart_snippets/lib/getting_started.dart new file mode 100644 index 00000000..1b5eb755 --- /dev/null +++ b/snippets/dart_snippets/lib/getting_started.dart @@ -0,0 +1,74 @@ +import 'dart:typed_data'; + +import 'package:flutter_breez_liquid/breez_liquid.dart'; +import 'package:flutter_breez_liquid/bridge_generated.dart'; + +Future initializeSDK() async { + // ANCHOR: init-sdk + + // It is recommended to use a single instance of BreezSDK across your Dart/Flutter app. + // + // All of the snippets assume a BreezSDK object is created on entrypoint of the app as such: + // + // BreezSDK breezSDK = BreezSDK(); + // + // and is accessible throughout the app. There are various approaches on how to achieve this; creating a Singleton class using factory constructor, using state management libraries such as 'provider', 'GetX', 'Riverpod' and 'Redux' to name a few. + + // Initializes SDK events & log streams. + // + // Call once on your Dart entrypoint file, e.g.; `lib/main.dart`. + breezSDK.initialize(); + + // Create the default config + Uint8List seed = await breezSDK.mnemonicToSeed(""); + String inviteCode = ""; + String apiKey = ""; + NodeConfig nodeConfig = NodeConfig.greenlight( + config: GreenlightNodeConfig( + partnerCredentials: null, + inviteCode: inviteCode, + ), + ); + Config config = await breezSDK.defaultConfig( + envType: EnvironmentType.Production, + apiKey: apiKey, + nodeConfig: nodeConfig, + ); + + // Customize the config object according to your needs + config = config.copyWith(workingDir: "path to an existing directory"); + + // Connect to the Breez SDK make it ready for use + ConnectRequest connectRequest = ConnectRequest(config: config, seed: seed); + return await breezSDK.connect(req: connectRequest); + // ANCHOR_END: init-sdk +} + +Future connectRestoreOnly(Config config, Uint8List seed) async { + // ANCHOR: init-sdk-restore-only + ConnectRequest connectRequest = ConnectRequest(config: config, seed: seed, restoreOnly: true); + return await breezSDK.connect(req: connectRequest); + // ANCHOR_END: init-sdk-restore-only +} + +Future fetchBalance(String lspId) async { + // ANCHOR: fetch-balance + NodeState? nodeInfo = await breezSDK.nodeInfo(); + if (nodeInfo != null) { + int lnBalance = nodeInfo.channelsBalanceMsat; + int onchainBalance = nodeInfo.onchainBalanceMsat; + print(lnBalance); + print(onchainBalance); + } + // ANCHOR_END: fetch-balance +} + +// ANCHOR: logging +void onLogEntry(log) { + print("Received log ${log.level}]: ${log.line}"); +} + +void logging(BreezSDK breezSDK) { + breezSDK.logStream.listen(onLogEntry); +} +// ANCHOR_END: logging diff --git a/snippets/dart_snippets/lib/list_payments.dart b/snippets/dart_snippets/lib/list_payments.dart new file mode 100644 index 00000000..847f3dce --- /dev/null +++ b/snippets/dart_snippets/lib/list_payments.dart @@ -0,0 +1,36 @@ +import 'package:flutter_breez_liquid/breez_liquid.dart'; +import 'package:flutter_breez_liquid/bridge_generated.dart'; + +Future> listPayments() async { + // ANCHOR: list-payments + ListPaymentsRequest req = ListPaymentsRequest(); + List paymentsList = await breezSDK.listPayments(req: req); + print(paymentsList); + // ANCHOR_END: list-payments + return paymentsList; +} + +Future> listPaymentsFiltered({ + int? fromTimestamp, + int? toTimestamp, + bool? includeFailures, + int? offset, + int? limit, +}) async { + // ANCHOR: list-payments-filtered + /// Get the desired epoch timestamp in seconds + int fromTimestamp = DateTime.now().subtract(const Duration(minutes: 30)).millisecondsSinceEpoch ~/ 1000; + + ListPaymentsRequest req = ListPaymentsRequest( + filters: [PaymentTypeFilter.Sent], + fromTimestamp: fromTimestamp, + toTimestamp: toTimestamp, + includeFailures: includeFailures, + offset: offset, + limit: limit, + ); + List paymentsList = await breezSDK.listPayments(req: req); + print(paymentsList); + // ANCHOR_END: list-payments-filtered + return paymentsList; +} diff --git a/snippets/dart_snippets/lib/lnurl_auth.dart b/snippets/dart_snippets/lib/lnurl_auth.dart new file mode 100644 index 00000000..1372a2ae --- /dev/null +++ b/snippets/dart_snippets/lib/lnurl_auth.dart @@ -0,0 +1,21 @@ +import 'package:flutter_breez_liquid/breez_liquid.dart'; +import 'package:flutter_breez_liquid/bridge_generated.dart'; + +Future lnurlAuth() async { + // ANCHOR: lnurl-auth + /// Endpoint can also be of the form: + /// keyauth://domain.com/auth?key=val + String lnurlAuthUrl = + "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu"; + + InputType inputType = await breezSDK.parseInput(input: lnurlAuthUrl); + if (inputType is InputType_LnUrlAuth) { + LnUrlCallbackStatus result = await breezSDK.lnurlAuth(reqData: inputType.data); + if (result is LnUrlCallbackStatus_Ok) { + print("Successfully authenticated"); + } else { + print("Failed to authenticate"); + } + } + // ANCHOR_END: lnurl-auth +} diff --git a/snippets/dart_snippets/lib/lnurl_pay.dart b/snippets/dart_snippets/lib/lnurl_pay.dart new file mode 100644 index 00000000..8d4811c5 --- /dev/null +++ b/snippets/dart_snippets/lib/lnurl_pay.dart @@ -0,0 +1,26 @@ +import 'package:flutter_breez_liquid/breez_liquid.dart'; +import 'package:flutter_breez_liquid/bridge_generated.dart'; + +Future lnurlPay() async { + // ANCHOR: lnurl-pay + /// Endpoint can also be of the form: + /// lnurlp://domain.com/lnurl-pay?key=val + /// lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf + String lnurlPayUrl = "lightning@address.com"; + + InputType inputType = await breezSDK.parseInput(input: lnurlPayUrl); + if (inputType is InputType_LnUrlPay) { + int amountMsat = inputType.data.minSendable; + String optionalComment = ""; + String optionalPaymentLabel = "