-
Notifications
You must be signed in to change notification settings - Fork 2
/
Home.tsx
236 lines (210 loc) · 6.37 KB
/
Home.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
import {
IonButton,
IonContent,
IonHeader,
IonInput,
IonItem,
IonPage,
IonTitle,
IonToolbar,
} from '@ionic/react';
import './Home.css';
import { DemoWalletConnect } from '../DemoWalletConnect';
import React, { useEffect, useRef, useState } from 'react';
import QrScanner from 'qr-scanner';
import { icon } from '../common';
import { IConnectMessage } from '@fabianbormann/cardano-peer-connect/dist/src/types';
const Home = () => {
const [meerkatAddress, setMeerkatAddress] = useState('');
let walletInfo = {
address: 'http://localhost:3002/home',
name: 'demo_wallet',
icon: icon,
version: '0.0.1',
requestAutoconnect: true,
};
let announce = [
'wss://tracker.openwebtorrent.com',
'wss://dev.tracker.cf-identity-wallet.metadata.dev.cf-deployments.org',
'wss://tracker.files.fm:7073/announce',
'ws://tracker.files.fm:7072/announce',
'wss://tracker.openwebtorrent.com:443/announce',
];
const [boostPeerConnect, setBoostPeerConnect] = React.useState(
() =>
new DemoWalletConnect(
walletInfo,
localStorage.getItem('meerkat-boostwallet-seed'),
announce
)
);
window.addEventListener('beforeunload', (event: any) => {
if (boostPeerConnect) {
boostPeerConnect?.disconnect(dAppIdentifier);
}
});
boostPeerConnect.setOnConnect((connectMessage: IConnectMessage) => {
console.log('connect', connectMessage);
identicon.current = boostPeerConnect.getIdenticon();
setConnected(
'Connected to ' +
connectMessage.dApp.name +
' (' +
connectMessage.dApp.address +
' at: ' +
connectMessage.dApp.url +
')'
);
});
boostPeerConnect.setOnDisconnect((connectMessage: IConnectMessage) => {
console.log('disconnect', connectMessage);
identicon.current = null;
setConnected('Disconnected');
});
boostPeerConnect.setOnServerShutdown((connectMessage: IConnectMessage) => {
console.log('server shutdown', connectMessage);
identicon.current = null;
setConnected('Disconnected');
});
boostPeerConnect.setOnApiInject((connectMessage: IConnectMessage) => {
console.log('on api inject message', connectMessage);
});
const [dAppIdentifier, setDAppIdentifier] = useState('');
const videoElement = useRef<HTMLVideoElement | null>(null);
const qrScanner = useRef<QrScanner | undefined>();
const [qrOverlayVisible, setQrOverlayVisible] = useState(false);
const identicon = useRef<string | null>(null);
const [connected, setConnected] = useState('Disconnected');
useEffect(() => {
if (videoElement.current) {
qrScanner.current = new QrScanner(
videoElement.current,
(result) => {
if (qrScanner.current) {
setDAppIdentifier(result.data.split(':')[0]);
qrScanner.current.stop();
setQrOverlayVisible(false);
}
},
{
returnDetailedScanResult: true,
}
);
}
}, []);
const connectWithDApp = () => {
console.log('connect with dapp');
if (boostPeerConnect) {
const seed = boostPeerConnect.connect(dAppIdentifier);
localStorage.setItem('meerkat-boostwallet-seed', seed);
}
// setMeerkatAddress(boostPeerConnect.current.getAddress());
};
const disconnectDApp = () => {
if (boostPeerConnect) {
boostPeerConnect.disconnect(dAppIdentifier);
}
};
const startQrScanner = () => {
if (qrScanner.current) {
qrScanner.current.start();
setQrOverlayVisible(true);
}
};
const stopQrScanner = () => {
if (qrScanner.current) {
qrScanner.current.stop();
setQrOverlayVisible(false);
}
};
return (
<IonPage>
<IonHeader>
<IonToolbar color="primary">
<IonTitle>Example Wallet</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent fullscreen>
<IonHeader collapse="condense">
<IonToolbar color="primary">
<IonTitle size="large">Example Wallet</IonTitle>
</IonToolbar>
</IonHeader>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<div style={{ maxWidth: 600, width: '80%' }}>
<p>
Paste the dapp identifier into this input field and click the
connect button, or{' '}
<span
style={{
cursor: 'pointer',
color: 'var(--ion-color-primary, #3880ff)',
}}
onClick={startQrScanner}
>
scan the QR code.
</span>
</p>
<video
style={{
width: '100%',
height: qrOverlayVisible ? 'unset' : '0px',
}}
ref={videoElement}
></video>
{!qrOverlayVisible && (
<IonItem className="address-container">
<IonInput
value={dAppIdentifier}
onIonChange={(event) =>
setDAppIdentifier(`${event.target.value}`)
}
placeholder="dApp identifier"
></IonInput>
</IonItem>
)}
<div
style={{
display: 'flex',
justifyContent: 'flex-end',
marginRight: 16,
}}
>
{qrOverlayVisible ? (
<IonButton onClick={stopQrScanner} fill="solid">
Stop Scanner
</IonButton>
) : connected === 'Disconnected' ? (
<IonButton onClick={connectWithDApp} fill="solid">
Connect
</IonButton>
) : (
<IonButton onClick={disconnectDApp} fill="solid">
Disconnect
</IonButton>
)}
</div>
<div>
<span style={{ paddingLeft: '4px', textDecoration: 'underline' }}>
meerkatAddress: {meerkatAddress}
</span>
</div>
<div>{connected}</div>
{identicon.current && (
<div>
<img src={identicon.current} alt={'identicon'} />
</div>
)}
</div>
</div>
</IonContent>
</IonPage>
);
};
export default Home;