import { LightningAddress } from "@getalby/lightning-tools";
import "./applyGlobalPolyfills";
import { webln } from "@getalby/sdk";
import React from "react";
import {
Button,
Platform,
SafeAreaView,
StatusBar,
StyleSheet,
Text,
TextInput,
} from "react-native";
import PolyfillCrypto from "react-native-webview-crypto";
import WebView from "react-native-webview";
export default function App() {
return (
);
}
function NWCPlayground() {
const [nwcUrl, setNwcUrl] = React.useState("");
const [pendingNwcUrl, setPendingNwcUrl] = React.useState("");
const [nwcAuthUrl, setNwcAuthUrl] = React.useState("");
const [paymentRequest, setPaymentRequest] = React.useState("");
const [preimage, setPreimage] = React.useState("");
const [nostrWebLN, setNostrWebLN] = React.useState<
webln.NostrWebLNProvider | undefined
>(undefined);
const [balance, setBalance] = React.useState();
React.useEffect(() => {
if (!nwcUrl) {
return;
}
(async () => {
try {
const _nostrWebLN = new webln.NostrWebLNProvider({
nostrWalletConnectUrl: nwcUrl,
});
setNostrWebLN(_nostrWebLN);
await _nostrWebLN.enable();
const response = await _nostrWebLN.getBalance();
console.log("Balance response", response);
setBalance(response.balance);
} catch (error) {
console.error(error);
}
})();
(async () => {
try {
const lightningAddress = new LightningAddress("hello@getalby.com");
await lightningAddress.fetch();
const invoice = await lightningAddress.requestInvoice({
satoshi: 1,
});
setPaymentRequest(invoice.paymentRequest);
} catch (error) {
console.error(error);
}
})();
}, [nwcUrl]);
async function payInvoice() {
try {
if (!nostrWebLN) {
throw new Error("No WebLN provider");
}
const result = await nostrWebLN.sendPayment(paymentRequest);
setPreimage(result.preimage);
} catch (error) {
console.error(error);
}
}
async function connectWithAlby() {
const nwc = webln.NostrWebLNProvider.withNewSecret({
//authorizationUrl: "http://192.168.1.102:8080",
});
const authUrl = nwc.getAuthorizationUrl({
name: "React Native NWC demo",
});
setPendingNwcUrl(nwc.getNostrWalletConnectUrl(true));
setNwcAuthUrl(authUrl.toString());
}
if (nwcAuthUrl) {
return (
{
window.ReactNativeWebView.postMessage(event.data?.type);
});
`}
onMessage={(event) => {
if (event.nativeEvent.data === "nwc:success") {
setNwcAuthUrl("");
setNwcUrl(pendingNwcUrl);
}
}}
/>
);
}
return (
<>
{!nwcUrl && (
<>
Paste NWC URL
setNwcUrl(text)}
style={{
borderColor: "black",
borderWidth: 1,
padding: 10,
margin: 10,
}}
/>
or
>
)}
{nwcUrl && (
<>
Balance
{balance ?? "Loading..."}
Pay an invoice
{paymentRequest ?? "Loading..."}
{paymentRequest && (
)}
{preimage ? `PAID: ${preimage}` : "Not paid yet"}
>
)}
>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: "white",
paddingTop: Platform.OS === "android" ? StatusBar.currentHeight : 0,
},
});