-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.tsx
275 lines (255 loc) · 8.42 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import "react-native-get-random-values";
import "react-native-url-polyfill/auto";
import { Buffer } from "buffer";
global.Buffer = global.Buffer || Buffer;
import { StatusBar } from "expo-status-bar";
import React, { useEffect, useRef, useState } from "react";
import { ActivityIndicator, StyleSheet, Text, View } from "react-native";
import * as Linking from "expo-linking";
import nacl from "tweetnacl";
import bs58 from "bs58";
import {
clusterApiUrl,
Connection,
PublicKey,
Transaction,
} from "@solana/web3.js";
import { decryptPayload } from "./utils/decryptPayload";
import { encryptPayload } from "./utils/encryptPayload";
import { buildUrl } from "./utils/buildUrl";
import MovieList from "./components/MovieList";
import Button from "./components/Button";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
import ActionSheet from "react-native-actions-sheet";
import AddReviewSheet from "./components/AddReviewSheet";
import Toast from "react-native-toast-message";
import { toastConfig } from "./components/ToastConfig";
import { COLORS } from "./constants";
const onConnectRedirectLink = Linking.createURL("onConnect");
const onDisconnectRedirectLink = Linking.createURL("onDisconnect");
const onSignAndSendTransactionRedirectLink = Linking.createURL(
"onSignAndSendTransaction"
);
const connection = new Connection(clusterApiUrl("devnet"));
export default function App() {
const [deeplink, setDeepLink] = useState<string>("");
const [dappKeyPair] = useState(nacl.box.keyPair());
const [sharedSecret, setSharedSecret] = useState<Uint8Array>();
const [session, setSession] = useState<string>();
const [phantomWalletPublicKey, setPhantomWalletPublicKey] =
useState<PublicKey | null>(null);
const [submitting, setSubmitting] = useState(false);
const actionSheetRef = useRef<ActionSheet>(null);
// Initialize our app's deeplinking protocol on app start-up
useEffect(() => {
const initializeDeeplinks = async () => {
const initialUrl = await Linking.getInitialURL();
if (initialUrl) {
setDeepLink(initialUrl);
}
};
initializeDeeplinks();
const listener = Linking.addEventListener("url", handleDeepLink);
return () => {
listener.remove();
};
}, []);
const handleDeepLink = ({ url }: Linking.EventType) => setDeepLink(url);
// Handle in-bound links
useEffect(() => {
setSubmitting(false);
if (!deeplink) return;
const url = new URL(deeplink);
const params = url.searchParams;
// Handle an error response from Phantom
if (params.get("errorCode")) {
const error = Object.fromEntries([...params]);
const message =
error?.errorMessage ??
JSON.stringify(Object.fromEntries([...params]), null, 2);
Toast.show({
type: "error",
text1: "Error",
text2: message,
});
console.log("error: ", message);
return;
}
// Handle a `connect` response from Phantom
if (/onConnect/.test(url.pathname)) {
const sharedSecretDapp = nacl.box.before(
bs58.decode(params.get("phantom_encryption_public_key")!),
dappKeyPair.secretKey
);
const connectData = decryptPayload(
params.get("data")!,
params.get("nonce")!,
sharedSecretDapp
);
setSharedSecret(sharedSecretDapp);
setSession(connectData.session);
setPhantomWalletPublicKey(new PublicKey(connectData.public_key));
console.log(`connected to ${connectData.public_key.toString()}`);
}
// Handle a `disconnect` response from Phantom
if (/onDisconnect/.test(url.pathname)) {
setPhantomWalletPublicKey(null);
console.log("disconnected");
}
// Handle a `signAndSendTransaction` response from Phantom
if (/onSignAndSendTransaction/.test(url.pathname)) {
actionSheetRef.current?.hide();
const signAndSendTransactionData = decryptPayload(
params.get("data")!,
params.get("nonce")!,
sharedSecret
);
console.log("signAndSendTrasaction: ", signAndSendTransactionData);
Toast.show({
type: "success",
text1: "Review submitted 🎥",
text2: signAndSendTransactionData.signature,
});
}
}, [deeplink]);
// Initiate a new connection to Phantom
const connect = async () => {
const params = new URLSearchParams({
dapp_encryption_public_key: bs58.encode(dappKeyPair.publicKey),
cluster: "devnet",
app_url: "https://deeplink-movie-tutorial-dummy-site.vercel.app/",
redirect_link: onConnectRedirectLink,
});
const url = buildUrl("connect", params);
Linking.openURL(url);
};
// Initiate a disconnect from Phantom
const disconnect = async () => {
const payload = {
session,
};
const [nonce, encryptedPayload] = encryptPayload(payload, sharedSecret);
const params = new URLSearchParams({
dapp_encryption_public_key: bs58.encode(dappKeyPair.publicKey),
nonce: bs58.encode(nonce),
redirect_link: onDisconnectRedirectLink,
payload: bs58.encode(encryptedPayload),
});
const url = buildUrl("disconnect", params);
Linking.openURL(url);
};
// Initiate a new transaction via Phantom. We call this in `components/AddReviewSheet.tsx` to send our movie review to the Solana network
const signAndSendTransaction = async (transaction: Transaction) => {
if (!phantomWalletPublicKey) return;
setSubmitting(true);
transaction.feePayer = phantomWalletPublicKey;
transaction.recentBlockhash = (
await connection.getLatestBlockhash()
).blockhash;
const serializedTransaction = transaction.serialize({
requireAllSignatures: false,
});
const payload = {
session,
transaction: bs58.encode(serializedTransaction),
};
const [nonce, encryptedPayload] = encryptPayload(payload, sharedSecret);
const params = new URLSearchParams({
dapp_encryption_public_key: bs58.encode(dappKeyPair.publicKey),
nonce: bs58.encode(nonce),
redirect_link: onSignAndSendTransactionRedirectLink,
payload: bs58.encode(encryptedPayload),
});
const url = buildUrl("signAndSendTransaction", params);
Linking.openURL(url);
};
// Open the 'Add a Review' sheet defined in `components/AddReviewSheet.tsx`
const openAddReviewSheet = () => {
actionSheetRef.current?.show();
};
return (
<SafeAreaProvider>
<SafeAreaView style={styles.container}>
<View style={styles.header}>
{phantomWalletPublicKey ? (
<>
<View style={[styles.row, styles.wallet]}>
<View style={styles.greenDot} />
<Text
style={styles.text}
numberOfLines={1}
ellipsizeMode="middle"
>
{`Connected to: ${phantomWalletPublicKey.toString()}`}
</Text>
</View>
<View style={styles.row}>
<Button title="Add Review" onPress={openAddReviewSheet} />
<Button title="Disconnect" onPress={disconnect} />
</View>
</>
) : (
<View style={{ marginTop: 15 }}>
<Button title="Connect Phantom" onPress={connect} />
</View>
)}
</View>
{submitting && (
<ActivityIndicator
color={COLORS.WHITE}
size="large"
style={styles.spinner}
/>
)}
<MovieList connection={connection} />
<AddReviewSheet
actionSheetRef={actionSheetRef}
phantomWalletPublicKey={phantomWalletPublicKey}
signAndSendTransaction={signAndSendTransaction}
/>
<Toast config={toastConfig} />
<StatusBar style="auto" />
</SafeAreaView>
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: COLORS.DARK_GREY,
flexGrow: 1,
position: "relative",
},
greenDot: {
height: 8,
width: 8,
borderRadius: 10,
marginRight: 5,
backgroundColor: COLORS.GREEN,
},
header: {
width: "95%",
marginLeft: "auto",
marginRight: "auto",
},
row: {
flexDirection: "row",
justifyContent: "space-between",
marginBottom: 5,
},
spinner: {
position: "absolute",
alignSelf: "center",
top: "50%",
zIndex: 1000,
},
text: {
color: COLORS.LIGHT_GREY,
width: "100%",
},
wallet: {
alignItems: "center",
margin: 10,
marginBottom: 15,
},
});