From 3a7d2b6cbf1f28964f1c00a8121a739222948816 Mon Sep 17 00:00:00 2001 From: Ross Savage Date: Sun, 2 Jul 2023 22:05:33 +0200 Subject: [PATCH] Add React Native examples --- src/guide/getting_started.md | 62 ++++++++++++++++++++++++++++++++++++ src/guide/lnurl_auth.md | 24 ++++++++++++++ src/guide/lnurl_pay.md | 20 ++++++++++++ src/guide/lnurl_withdraw.md | 19 +++++++++++ src/guide/payments.md | 31 ++++++++++++++++++ src/guide/send_onchain.md | 53 ++++++++++++++++++++++++++++++ 6 files changed, 209 insertions(+) diff --git a/src/guide/getting_started.md b/src/guide/getting_started.md index c052dcc..44d86ff 100644 --- a/src/guide/getting_started.md +++ b/src/guide/getting_started.md @@ -132,6 +132,68 @@ do { } ``` + +
React Native
+
+ +The first step is to register a new node +## Registering a new node +```typescript +try { + const seed = await mnemonicToSeed(""); + const invite_code = ""; + + // register_node takes either greenlight credentials (certifate & key) or invite code. + // At this example we are using the invite code option. + const credentials = await registerNode(Network.BITCOIN, seed, inviteCode); +} catch (error) { + console.log(error) +} +``` + +## Recovering an existing node +```typescript + const seed = await mnemonicToSeed(""); + const credentials = await recoverNode(Network.BITCOIN, seed); +``` + +Once the credentials are retrieved they should be saved in a secured storage. +The next step is to initialize the SDK and start the node: + +## Initializing the SDK +```typescript + +// SDK events listener +addEventListener((type, data) => { + console.log(`received event ${type}`); +}) + +// Create the default config +let config = defaultConfig(EnvironmentType.PRODUCTION) + +// Customize the config object according to your needs +config.apiKey = "your API key"; +config.workingDir = "path to an existing directory"; + +try { + const sdkServices = await initServices(config, credentials.deviceKey, credentials.deviceCert, seed); + await sdkServices.start(); +} catch (error) { + console.log(error); +} +``` + +At any point we can fetch our balance from the Greenlight node: + +```typescript +try { + const nodeInfo = await sdkServices.nodeInfo(); + const lnBalance = nodeInfo.channelsBalanceMsat; + const onchainBalance = nodeInfo.onchainBalanceMsat; +} catch (error) { + console.log(error); +} +```
diff --git a/src/guide/lnurl_auth.md b/src/guide/lnurl_auth.md index a3f2b2e..af4bca2 100644 --- a/src/guide/lnurl_auth.md +++ b/src/guide/lnurl_auth.md @@ -49,6 +49,30 @@ do { } ``` + +
React Native
+
+ +```typescript +// Endpoint can also be of the form: +// keyauth://domain.com/auth?key=val +let lnurlAuthUrl = "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu"; + +try { + const input = await parseInput(lnurlAuthUrl) + if (input.type === InputType.LNURL_AUTH) { + const result = await sdkServices.lnurlAuth(input.data) + if (result.status === "ok") { + print("Successfully authenticated") + } else { + print("Failed to authenticate") + } + } +} catch (error) { + console.log(error) +} +``` +
diff --git a/src/guide/lnurl_pay.md b/src/guide/lnurl_pay.md index baa6546..3fef874 100644 --- a/src/guide/lnurl_pay.md +++ b/src/guide/lnurl_pay.md @@ -41,6 +41,26 @@ do { } ``` +
React Native
+
+ +```typescript +// Endpoint can also be of the form: +// lnurlp://domain.com/lnurl-pay?key=val +// lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf +let lnurlPayUrl = "lightning@address.com"; + +try { + const input = await parseInput(lnurlAuthUrl) + if (input.type === InputType.LNURL_PAY) { + const amountSats = input.minSendable; + const result = await sdkServices.payLnurl(input.data, amountSats, "comment") + } +} catch (error) { + console.log(error) +} +``` +
## Supported Specs diff --git a/src/guide/lnurl_withdraw.md b/src/guide/lnurl_withdraw.md index 3a4855f..5c8addb 100644 --- a/src/guide/lnurl_withdraw.md +++ b/src/guide/lnurl_withdraw.md @@ -40,6 +40,25 @@ do { print(message) } +``` + +
React Native
+
+ +```typescript +// Endpoint can also be of the form: +// lnurlw://domain.com/lnurl-withdraw?key=val +let lnurlWithdrawUrl = "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk"; + +try { + const input = await parseInput(lnurlAuthUrl) + if (input.type === InputType.LNURL_WITHDRAW) { + const amountSats = input.minWithdrawable; + const result = await sdkServices.withdrawLnurl(input.data, amountSats, "comment") + } +} catch (error) { + console.log(error) +} ```
diff --git a/src/guide/payments.md b/src/guide/payments.md index a1350a0..6b40d9a 100644 --- a/src/guide/payments.md +++ b/src/guide/payments.md @@ -58,7 +58,38 @@ do { } ``` +
React Native
+## Receiving Lightning Payments +Breez SDK doesn't require you to open a channel and set up your inbound liquidity. +Breez SDK automatically connects your node to the LSP peer and you can now receive payments: +```typescript +try { + const invoice = await sdkServices.receivePayment(3000, "Invoice for 3000 sats") +} catch (error) { + console.log(error) +} +``` + +## Sending Lightning Payments +```typescript +const bolt11 = "..."; +try { + const payment = await sdkServices.sendPayment(bolt11, 3000) +} catch (error) { + console.log(error) +} +``` + +## Sending Spontaneous Lightning Payments +```typescript +const nodeId = "..."; +try { + const payment = await sdkServices.sendSpontaneousPayment(nodeId, 3000) +} catch (error) { + console.log(error) +} +```
\ No newline at end of file diff --git a/src/guide/send_onchain.md b/src/guide/send_onchain.md index 1d2423f..9dd8341 100644 --- a/src/guide/send_onchain.md +++ b/src/guide/send_onchain.md @@ -99,6 +99,59 @@ for rs in sdk.inProgressReverseSwaps() { } ``` +
React Native
+
+ +```typescript +try { + const currentFees = await sdkServices.fetchReverseSwapFees() + + console.log(`Percentage fee for the reverse swap service: ${currentFees.feesPercentage}`); + console.log(`Estimated miner fees in sats for locking up funds: ${currentFees.feesLockup}`); + console.log(`Estimated miner fees in sats for claiming funds: ${currentFees.feesClaim}`); +} catch (error) { + console.log(error) +} +``` + +The reverse swap will involve two on-chain transactions, for which the mining fees can only be estimated. They will happen +automatically once the process is started, but the last two values above are these estimates to help you get a picture +of the total costs. + +Fetching the fees also tells you what is the range of amounts you can send: + +```typescript +console.log(`Minimum amount, in sats: ${current_fees.min}`); +console.log(`Maximum amount, in sats: ${current_fees.max}`); +``` + +Once you checked the fees are acceptable, you can start the reverse swap: + +```typescript +const destinationAddress = "bc1.."; +const amountSat = currentFees.min; +const satPerVbyte = +try { + const reverseSwapInfo = sdkServices.sendOnchain(amountSat, destinationAddress, currentFees.feesHash, satPerVbyte) +} catch (error) { + console.log(error) +} +``` + +Starting the reverse swap will trigger a HODL invoice payment, which will only be settled if the entire swap completes. +This means you will see an outgoing pending payment in your list of payments, which locks those funds until the invoice +is either settled or cancelled. This will happen automatically at the end of the reverse swap. + +You can check its status with: + +```typescript +try { + const swaps = await sdk.inProgressReverseSwaps() + for (const swap in swaps) { + println(`Reverse swap ${swap.id} in progress, status is ${swap.breezStatus}`); + } +} catch (error) { + console.log(error) } ```