-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
99 lines (93 loc) · 2.47 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
import 'react-native-url-polyfill/auto'
import React from 'react'
import { StatusBar } from 'expo-status-bar'
import { StyleSheet, View, Button, TextInput } from 'react-native'
import * as Linking from 'expo-linking'
import {
isRedirectFromJoyID,
getRedirectResult,
initConfig,
generateAuthURL,
generateSignMessageURL,
// import this if you want to sign EVM transaction
// generateSignEvmTransactionURL,
DappRequestType,
type AuthResponseData
} from '@joyid/react-native'
import { useEffect, useState } from 'react'
initConfig({
name: 'React Native Example',
logo: 'https://fav.farm/%F0%9F%86%94'
})
// this is the URL that JoyID will redirect to,
// in real world, you should define deep link URL for your app
// ref: https://reactnative.dev/docs/linking
const redirectURL = Linking.createURL('/')
const Auth = () => {
return (
<Button
title='Auth'
onPress={() => {
const joyidUrl = generateAuthURL(redirectURL)
Linking.openURL(joyidUrl)
}}
/>
)
}
const Sign: React.FC<{ address: string }> = ({ address }) => {
const [text, onChangeText] = React.useState('Hello World!');
return (
<>
<TextInput
value={text}
onChangeText={onChangeText}
style={{
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
}}
/>
<Button
title='Sign'
onPress={() => {
const joyidUrl = generateSignMessageURL(redirectURL, text, address)
Linking.openURL(joyidUrl)
alert('See console for result')
}}
/>
</>
)
}
export default function App() {
const redirectFrom = Linking.useURL()
const [authData, setAuthData] = useState < AuthResponseData | null>(null)
useEffect(() => {
if (redirectFrom && isRedirectFromJoyID(redirectFrom)) {
const res = getRedirectResult(redirectFrom, DappRequestType.Auth)
if (res && res.error == null) {
if (res.type === DappRequestType.Auth) {
setAuthData(res.data)
} else {
console.log('Sign result:', res.data)
}
} else {
console.log('Error', res?.error)
}
}
}, [redirectFrom])
return (
<View style={styles.container}>
{authData ? <Sign address={authData.address} /> : <Auth />}
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});