forked from BitBoxSwiss/bitbox-api-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
96 lines (92 loc) · 2.4 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
import { useState } from 'react'
import * as bitbox from 'bitbox-api';
import './App.css'
import { Bitcoin } from './Bitcoin';
import { Cardano } from './Cardano';
import { Ethereum } from './Ethereum';
import { General } from './General';
function App() {
const [bb02, setBB02] = useState<bitbox.PairedBitBox>();
const [pairingCode, setPairingCode] = useState<string>();
const [err, setErr] = useState<bitbox.Error>();
const onClose = () => {
setBB02(undefined);
setPairingCode(undefined);
setErr(undefined);
};
const connect = async (method: 'webHID' | 'bridge' | 'auto') => {
setErr(undefined);
try {
let device: bitbox.BitBox;
switch (method) {
case 'webHID':
device = await bitbox.bitbox02ConnectWebHID(onClose);
break;
case 'bridge':
device = await bitbox.bitbox02ConnectBridge(onClose);
break;
case 'auto':
device = await bitbox.bitbox02ConnectAuto(onClose);
break;
}
const pairing = await device.unlockAndPair();
setPairingCode(pairing.getPairingCode());
setBB02(await pairing.waitConfirm());
setPairingCode(undefined);
} catch (err) {
setErr(bitbox.ensureError(err));
}
};
if (err !== undefined) {
return (
<>
<h2>Error</h2>
<pre>
{ JSON.stringify(err) }
</pre>
</>
);
}
if (pairingCode !== undefined) {
return (
<>
<h2>Pairing code</h2>
<pre>
{ pairingCode }
</pre>
</>
);
}
if (bb02 !== undefined) {
return (
<>
<h2>Connected. Product: {bb02.product()}</h2>
<h3>General</h3>
<General bb02={bb02} />
<h3>Bitcoin</h3>
<Bitcoin bb02={bb02} />
{ bb02.ethSupported() ? (
<>
<h3>Ethereum</h3>
<Ethereum bb02={bb02} />
</>
) : null }
{ bb02.cardanoSupported() ? (
<>
<h3>Cardano</h3>
<Cardano bb02={bb02} />
</>
) : null }
</>
);
}
return (
<>
<h1>BitBox sandbox</h1>
<button onClick={() => connect('webHID')}>Connect using WebHID</button><br />
<button onClick={() => connect('bridge')}>Connect using BitBoxBridge</button><br />
<button onClick={() => connect('auto')}>Choose automatically</button>
</>
);
}
export default App