-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
159 lines (146 loc) · 4.15 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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 (
<SafeAreaView style={styles.safeArea}>
<PolyfillCrypto />
<NWCPlayground />
</SafeAreaView>
);
}
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<number | undefined>();
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 (
<WebView
source={{ uri: nwcAuthUrl }}
injectedJavaScriptBeforeContentLoaded={`
// TODO: remove once NWC also posts messages to the window
window.opener = window;
// Listen for window messages
window.addEventListener("message", (event) => {
window.ReactNativeWebView.postMessage(event.data?.type);
});
`}
onMessage={(event) => {
if (event.nativeEvent.data === "nwc:success") {
setNwcAuthUrl("");
setNwcUrl(pendingNwcUrl);
}
}}
/>
);
}
return (
<>
{!nwcUrl && (
<>
<Text>Paste NWC URL</Text>
<TextInput
onChangeText={(text) => setNwcUrl(text)}
style={{
borderColor: "black",
borderWidth: 1,
padding: 10,
margin: 10,
}}
/>
<Text>or</Text>
<Button title="Connect with Alby NWC" onPress={connectWithAlby} />
</>
)}
{nwcUrl && (
<>
<Text>Balance</Text>
<Text>{balance ?? "Loading..."}</Text>
<Text>Pay an invoice</Text>
<Text>{paymentRequest ?? "Loading..."}</Text>
{paymentRequest && (
<Button title="Pay invoice (1 sat)" onPress={payInvoice} />
)}
<Text>{preimage ? `PAID: ${preimage}` : "Not paid yet"}</Text>
</>
)}
</>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: "white",
paddingTop: Platform.OS === "android" ? StatusBar.currentHeight : 0,
},
});