Merge branch 'multi-lang-tabs' of github.com:breez/breez-sdk-docs into multi-lang-tabs

* 'multi-lang-tabs' of github.com:breez/breez-sdk-docs:
  Remove sdkServices as its managed by the native module
  Add React Native examples
  Indentation fixes
This commit is contained in:
Roei Erez
2023-07-03 19:38:37 +03:00
7 changed files with 312 additions and 49 deletions

View File

@@ -127,7 +127,7 @@ do {
At any point we can fetch our balance from the Greenlight node: At any point we can fetch our balance from the Greenlight node:
```swift ```swift
do { do {
let nodeInfo = try sdk.nodeInfo(); let nodeInfo = try sdk.nodeInfo();
let lnBalance = nodeInfo?.channelsBalanceMsat; let lnBalance = nodeInfo?.channelsBalanceMsat;
let onchainBalance = nodeInfo?.onchainBalanceMsat; let onchainBalance = nodeInfo?.onchainBalanceMsat;
@@ -136,6 +136,68 @@ At any point we can fetch our balance from the Greenlight node:
} }
``` ```
</section>
<div slot="title">React Native</div>
<section>
The first step is to register a new node
## Registering a new node
```typescript
try {
const seed = await mnemonicToSeed("<mnemonics words>");
const invite_code = "<your greenlight 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("<mnemonics words>");
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 start();
} catch (error) {
console.log(error);
}
```
At any point we can fetch our balance from the Greenlight node:
```typescript
try {
const nodeInfo = await nodeInfo();
const lnBalance = nodeInfo.channelsBalanceMsat;
const onchainBalance = nodeInfo.onchainBalanceMsat;
} catch (error) {
console.log(error);
}
```
</section> </section>
</custom-tabs> </custom-tabs>

View File

@@ -49,6 +49,30 @@ do {
} }
``` ```
</section>
<div slot="title">React Native</div>
<section>
```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 lnurlAuth(input.data)
if (result.status === "ok") {
print("Successfully authenticated")
} else {
print("Failed to authenticate")
}
}
} catch (error) {
console.log(error)
}
```
</section> </section>
</custom-tab> </custom-tab>

View File

@@ -41,6 +41,26 @@ do {
} }
``` ```
</section> </section>
<div slot="title">React Native</div>
<section>
```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 payLnurl(input.data, amountSats, "comment")
}
} catch (error) {
console.log(error)
}
```
</section>
</custom-tab> </custom-tab>
## Supported Specs ## Supported Specs

View File

@@ -40,6 +40,25 @@ do {
// handle error // handle error
} }
```
</section>
<div slot="title">React Native</div>
<section>
```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 withdrawLnurl(input.data, amountSats, "comment")
}
} catch (error) {
console.log(error)
}
``` ```
</section> </section>
</custom-tab> </custom-tab>

View File

@@ -25,7 +25,7 @@ sdk.send_payment(node_id.into(), Some(3000)).await?;
</section> </section>
<div slot="title">Swift</div> <div slot="title">Swift</div>
<section>
## Receiving Lightning Payments ## Receiving Lightning Payments
Breez SDK doesn't require you to open a channel and set up your inbound liquidity. 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: Breez SDK automatically connects your node to the LSP peer and you can now receive payments:
@@ -57,8 +57,39 @@ do {
// handle error // handle error
} }
``` ```
</section>
<div slot="title">React Native</div>
<section> <section>
## 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 receivePayment(3000, "Invoice for 3000 sats")
} catch (error) {
console.log(error)
}
```
## Sending Lightning Payments
```typescript
const bolt11 = "...";
try {
const payment = await sendPayment(bolt11, 3000)
} catch (error) {
console.log(error)
}
```
## Sending Spontaneous Lightning Payments
```typescript
const nodeId = "...";
try {
const payment = await sendSpontaneousPayment(nodeId, 3000)
} catch (error) {
console.log(error)
}
```
</section> </section>
</custom-tabs> </custom-tabs>

View File

@@ -8,7 +8,7 @@ There are cases when you have funds in some bitcoin address and you would like t
```rust,no_run ```rust,no_run
let swap_info = sdk.receive_onchain().await?; let swap_info = sdk.receive_onchain().await?;
// Send your funds to the bellow bitcoin address // Send your funds to the below bitcoin address
let address = swap_info.bitcoin_address; let address = swap_info.bitcoin_address;
``` ```
@@ -92,4 +92,55 @@ do {
} }
``` ```
</section> </section>
<div slot="title">React Native</div>
<section>
```typescript
try {
const swapInfo = await receiveOnchain();
// Send your funds to the below bitcoin address
const address = swapInfo.bitcoinAddress;
} catch (error) {
console.log(error)
}
```
Once you've sent the funds to the above address, the SDK will monitor this address for unspent confirmed outputs and use a trustless submarine swap to receive these into your Lightning node. You can always monitor the status of the current in-progress swap using the following code:
```typescript
try {
const swapInfo = await inProgressSwap()
} catch (error) {
console.log(error)
}
```
The process of receiving funds via an on-chain address is trustless and uses a submarine swap. This means there are two ways to spend the sent funds:
1. Either by a preimage that is exposed when the Lightning payment is completed - this is the positive case where the swap was successful.
2. Or by your node when the swap didn't complete within a certain timeout - this is the negative case where your node will execute a refund.
In order to execute a refund, you need to supply an on-chain address to where the refunded amount will be sent. The following code will retrieve the refundable swaps:
```typescript
try {
const refundables = await listRefundables()
} catch (error) {
console.log(error)
}
```
Once you have a refundable swap in hand, use the follwing code to execute a refund:
```typescript
const destinationAddress = "..."
const satPerVbyte = <refund tx fee rate>
try {
const result = await refund(refundable.bitcoinAddress, destinationAddress, satPerVbyte)
} catch (error) {
console.log(error)
}
```
</section>
</custom-tabs> </custom-tabs>

View File

@@ -55,11 +55,11 @@ for rs in sdk.in_progress_reverse_swaps().await? {
try { try {
let currentFees = try sdk.fetchReverseSwapFees() let currentFees = try sdk.fetchReverseSwapFees()
println("Percentage fee for the reverse swap service: \(currentFees.feesPercentage))"); println("Percentage fee for the reverse swap service: \(currentFees.feesPercentage)");
println("Estimated miner fees in sats for locking up funds: \(currentFees.feesLockup)"); println("Estimated miner fees in sats for locking up funds: \(currentFees.feesLockup)");
println("Estimated miner fees in sats for claiming funds: \(currentFees.feesClaim)"); println("Estimated miner fees in sats for claiming funds: \(currentFees.feesClaim)");
} catch { } catch SdkError.Error(let message) {
print(error) print(message)
} }
``` ```
@@ -82,8 +82,8 @@ let amountSat = currentFees.min;
let satPerVbyte = <fee rate> let satPerVbyte = <fee rate>
try { try {
try sdk.sendOnchain(amountSat: amountSat, onchainRecipientAddress: destinationAddress, pairHash: currentFees.feesHash, satPerVbyte: satPerVbyte) try sdk.sendOnchain(amountSat: amountSat, onchainRecipientAddress: destinationAddress, pairHash: currentFees.feesHash, satPerVbyte: satPerVbyte)
} catch { } catch SdkError.Error(let message) {
print(error) print(message)
} }
``` ```
@@ -99,6 +99,62 @@ for rs in sdk.inProgressReverseSwaps() {
} }
``` ```
</section> </section>
<div slot="title">React Native</div>
<section>
```typescript
try {
const currentFees = await 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 = <fee rate>
try {
const reverseSwapInfo = 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 inProgressReverseSwaps()
for (const swap in swaps) {
println(`Reverse swap ${swap.id} in progress, status is ${swap.breezStatus}`);
}
} catch (error) {
console.log(error)
}
```
</section>
</custom-tabs> </custom-tabs>
If the reverse swap is successful, you'll get the on-chain payment on your destination address and the HODL invoice will If the reverse swap is successful, you'll get the on-chain payment on your destination address and the HODL invoice will
change from pending to settled. change from pending to settled.