mirror of
https://github.com/aljazceru/breez-sdk-docs.git
synced 2026-01-06 07:14:33 +01:00
Add React Native examples
This commit is contained in:
@@ -132,6 +132,68 @@ do {
|
||||
}
|
||||
```
|
||||
|
||||
</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 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);
|
||||
}
|
||||
```
|
||||
</section>
|
||||
</custom-tabs>
|
||||
|
||||
|
||||
@@ -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 sdkServices.lnurlAuth(input.data)
|
||||
if (result.status === "ok") {
|
||||
print("Successfully authenticated")
|
||||
} else {
|
||||
print("Failed to authenticate")
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</custom-tab>
|
||||
|
||||
|
||||
@@ -41,6 +41,26 @@ do {
|
||||
}
|
||||
```
|
||||
</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 sdkServices.payLnurl(input.data, amountSats, "comment")
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
```
|
||||
</section>
|
||||
</custom-tab>
|
||||
|
||||
## Supported Specs
|
||||
|
||||
@@ -40,6 +40,25 @@ do {
|
||||
print(message)
|
||||
}
|
||||
|
||||
```
|
||||
</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 sdkServices.withdrawLnurl(input.data, amountSats, "comment")
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
```
|
||||
</section>
|
||||
</custom-tab>
|
||||
|
||||
@@ -58,7 +58,38 @@ do {
|
||||
}
|
||||
```
|
||||
</section>
|
||||
<div slot="title">React Native</div>
|
||||
<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 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)
|
||||
}
|
||||
```
|
||||
</section>
|
||||
</custom-tabs>
|
||||
@@ -99,6 +99,59 @@ for rs in sdk.inProgressReverseSwaps() {
|
||||
}
|
||||
```
|
||||
</section>
|
||||
<div slot="title">React Native</div>
|
||||
<section>
|
||||
|
||||
```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 = <fee rate>
|
||||
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)
|
||||
}
|
||||
```
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user