Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 15 additions & 238 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import React, {useEffect, useState} from 'react';
import {
Alert,
Button,
SafeAreaView,
ScrollView,
Expand All @@ -22,9 +21,12 @@ import Clipboard from '@react-native-clipboard/clipboard';
import lnd, {
ENetworks,
LndConf,
lnrpc,
TCurrentLndState,
} from 'react-native-lightning';
} from '@synonymdev/react-native-lightning';

import {emoji} from './src/helpers';
import PSBT from './src/PSBT';
import Options from './src/Options';

declare const global: {HermesInternal: null | {}};

Expand All @@ -33,6 +35,8 @@ const testNodePubkey =
'034ecfd567a64f06742ac300a2985676abc0b1dc6345904a08bb52d5418e685f79';
const testNodeAddress = '35.240.72.95:9735';

const network = ENetworks.testnet;

const App = () => {
const [message, setMessage] = useState('');
const [lndState, setLndState] = useState<TCurrentLndState>({
Expand All @@ -44,11 +48,9 @@ const App = () => {
const [walletExists, setWalletExists] = useState(false);
const [seed, setSeed] = useState<string[]>([]);

const emoji = (s: boolean) => (s ? '✅' : '❌');

useEffect(() => {
(async () => {
const walletExistsRes = await lnd.walletExists(ENetworks.testnet);
const walletExistsRes = await lnd.walletExists(network);
if (walletExistsRes.isOk()) {
setWalletExists(walletExistsRes.value);
}
Expand Down Expand Up @@ -80,8 +82,8 @@ const App = () => {
return console.error(res.error);
}

const backupBytes = res.value.multiChanBackup.multiChanBackup;
console.log(`Backup required (${backupBytes.length} bytes)`);
const backupBytes = res.value.multiChanBackup?.multiChanBackup;
console.log(`Backup required (${backupBytes?.length} bytes)`);
},
() => {},
);
Expand Down Expand Up @@ -133,7 +135,7 @@ const App = () => {
setMessage('Starting LND...');

const customFields = {};
const lndConf = new LndConf(ENetworks.testnet, customFields);
const lndConf = new LndConf(network, customFields);
const res = await lnd.start(lndConf);

if (res.isErr()) {
Expand Down Expand Up @@ -201,236 +203,11 @@ const App = () => {

{lndState.grpcReady ? (
<>
<Button
title={'Get info'}
onPress={async () => {
const res = await lnd.getInfo();

if (res.isErr()) {
console.error(res.error);
return;
}

const {
identityPubkey,
syncedToChain,
blockHeight,
numPeers,
numActiveChannels,
version,
alias,
} = res.value;
let info = `Pubkey: ${identityPubkey}\n\n`;
info += `Alias: ${alias}\n\n`;
info += `Synced: ${emoji(syncedToChain)}\n\n`;
info += `BlockHeight: ${blockHeight}\n\n`;
info += `NumPeers: ${numPeers}\n\n`;
info += `NumActiveChannels: ${numActiveChannels}\n\n`;
info += `Version: ${version}`;

setMessage(info);
}}
/>
<Button
title={'Get on chain balance'}
onPress={async () => {
const res = await lnd.getWalletBalance();

if (res.isErr()) {
console.error(res.error);
return;
}

setMessage(
`Total balance: ${res.value.totalBalance}\nConfirmed balance: ${res.value.confirmedBalance}\nUnconfirmed balance: ${res.value.unconfirmedBalance}\n`,
);
}}
/>
<Button
title={'Get channel balance'}
onPress={async () => {
const res = await lnd.getChannelBalance();

if (res.isErr()) {
console.error(res.error);
return;
}

setMessage(
`Pending open balance: ${res.value.pendingOpenBalance}\nLocal balance: ${res.value.localBalance?.sat}\nRemote balance: ${res.value?.remoteBalance.sat}\n`,
);
}}
/>
<Button
title={'Copy address to clipboard'}
onPress={async () => {
const res = await lnd.getAddress();

if (res.isErr()) {
console.error(res.error);
return;
}

Clipboard.setString(res.value.address);
setMessage(`Copied: ${res.value.address}`);
}}
/>
<Button
title={'Open channel'}
onPress={async () => {
setMessage('Connecting to peer...');

const connectRes = await lnd.connectPeer(
testNodePubkey,
testNodeAddress,
);

if (
connectRes.isErr() &&
connectRes.error.message.indexOf(
'already connected to peer',
) < 0
) {
console.error(connectRes.error);
return;
}

setMessage('Opening channel...');

const balanceRes = await lnd.getWalletBalance();
if (balanceRes.isErr()) {
setMessage(balanceRes.error.message);
return;
}

let value = Number(balanceRes.value.confirmedBalance);
const maxChannel = 0.16 * 100000000;
if (value > maxChannel) {
value = maxChannel;
}

value -= 50000;

setMessage('Opening channel...');

const openRes = await lnd.openChannel(value, testNodePubkey);
if (openRes.isErr()) {
setMessage(openRes.error.message);
return;
}
}}
/>

<Button
title={'List channels'}
onPress={async () => {
const res = await lnd.listChannels();

if (res.isErr()) {
console.error(res.error);
return;
}

let list = '';
res.value.channels.forEach((channel: lnrpc.IChannel) => {
list += `Remote pubkey: ${channel.remotePubkey}\n`;
list += `Capacity: ${channel.capacity}\n`;
list += `Local balance: ${channel.localBalance}\n`;
list += `Remote balance: ${channel.remoteBalance}\n`;
list += `Active: ${emoji(channel.active || false)}\n`;

list += '\n\n';
});

setMessage(list || 'No channels');
}}
/>

<Button
title={'Paste invoice from clipboard'}
onPress={async () => {
const invoice = await Clipboard.getString();
if (!invoice) {
setMessage('Clipboard empty');
}

const decodeRes = await lnd.decodeInvoice(invoice);

if (decodeRes.isErr()) {
setMessage(decodeRes.error.message);
return;
}

const {numSatoshis, description} = decodeRes.value;

Alert.alert(
`Pay ${numSatoshis} sats?`,
description,
[
{
text: 'Cancel',
onPress: (): void => {},
style: 'cancel',
},
{
text: 'Pay',
onPress: async (): Promise<void> => {
setMessage('Paying...');
const payRes = await lnd.payInvoice(invoice);
if (payRes.isErr()) {
setMessage(payRes.error.message);
return;
}

setMessage(payRes.value.paymentError || 'Paid');
},
},
],
{cancelable: true},
);
}}
/>

<Button
title={'Create invoice and copy to clipboard'}
onPress={async () => {
const res = await lnd.createInvoice(
5000,
`Test invoice ${new Date().getTime()}`,
);

if (res.isErr()) {
console.error(res.error);
return;
}

Clipboard.setString(res.value.paymentRequest);
setMessage(res.value.paymentRequest);
}}
/>

<Button
title={'Export backup and verify it'}
onPress={async () => {
const res = await lnd.exportAllChannelBackups();

if (res.isErr()) {
console.error(res.error);
return;
}

const verifyRes = await lnd.verifyMultiChannelBackup(
res.value.multiChanBackup.multiChanBackup,
);

if (verifyRes.isErr()) {
setMessage(verifyRes.error.message);
return;
}

setMessage(`Backup verification: ${emoji(verifyRes.value)}`);
}}
<Options
nodePubKey={testNodePubkey}
nodeAddress={testNodeAddress}
/>
<PSBT nodePubKey={testNodePubkey} />
</>
) : null}
</ScrollView>
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ dependencies {
implementation jscFlavor
}

implementation files("../../node_modules/react-native-lightning/android/libs/Lndmobile.aar")
implementation files("../../node_modules/@synonymdev/react-native-lightning/android/libs/Lndmobile.aar")

}

Expand Down
8 changes: 4 additions & 4 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ target 'example' do
#
# Note that if you have use_frameworks! enabled, Flipper will not work and
# you should disable these next few lines.
use_flipper!
post_install do |installer|
flipper_post_install(installer)
end
# use_flipper!
# post_install do |installer|
# flipper_post_install(installer)
# end
end

target 'example-tvOS' do
Expand Down
Loading