-
Notifications
You must be signed in to change notification settings - Fork 89
/
HDWalletProvider.tsx
118 lines (99 loc) · 3.16 KB
/
HDWalletProvider.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
import React from "react";
import { Keyring, HDWallet, Events } from "@shapeshiftoss/hdwallet-core";
import { WebUSBKeepKeyAdapter } from "@shapeshiftoss/hdwallet-keepkey-webusb";
import { TrezorAdapter } from "@shapeshiftoss/hdwallet-trezor-connect";
import { getHDWalletContext } from "./HDWalletContext";
const HDWalletContext = getHDWalletContext();
type Adapter = any;
interface HDWalletProviderProps {
adapters: { name: string; adapter: Adapter; config?: any }[];
}
/*
Seperation of concerns:
* Pin Required
* Passphrase Required
* Which wallet(s) are connected
*/
export class HDWalletProvider extends React.Component<HDWalletProviderProps> {
keyring: Keyring = new Keyring();
adapters: {
name: string;
adapter: Adapter;
config: {};
}[] = [];
state: {
pairedDevices: { [index: string]: HDWallet };
} = {
pairedDevices: {}
};
static defaultProps = {
adapters: []
};
async componentDidMount() {
// This might break SSR
// Attach each of the adapters to the Keyring
const adapters = this.props.adapters.map(({ name, adapter, config }) => {
// TODO - Ask Jon why this is breaking 🤷♂️
return {
name,
adapter: (adapter as any).useKeyring(this.keyring, config),
config
};
});
// Initialize each of the adapters
await Promise.all(
adapters.map(async ({ name, adapter, config }) => {
console.debug(`[HDWallet] - ✅ Initializing ${name} adapter`, config);
try {
return await adapter.initialize(/* undefined, false, false*/);
} catch (err) {
console.error(err.message);
}
})
);
this.keyring.on(["*", "*", Events.CONNECT], this.handleDeviceConnect);
this.keyring.on(["*", "*", Events.DISCONNECT], this.handleDeviceDisconnect);
// This will fail out of sync if device is connected/disconnected
const pairedDevices = this.keyring.wallets;
this.adapters = adapters;
this.setState({
pairedDevices
});
}
componentWillUnmount() {
this.keyring.off(`*.*.${Events.CONNECT}`, this.handleDeviceConnect);
this.keyring.off(`*.*.${Events.DISCONNECT}`, this.handleDeviceDisconnect);
}
handleDeviceConnect = (deviceID: string) => {
console.debug(`[HDWallet] - ✅🔌 Paired Device Connected: ${deviceID}`);
this.setState({ pairedDevices: this.keyring.wallets });
};
handleDeviceDisconnect = (deviceID: string) => {
console.debug(`[HDWallet] - ❌🔌 Paired Device Disconnected: ${deviceID}`);
this.setState({ pairedDevices: this.keyring.wallets });
};
getAdapter = (
adapterName: string
): WebUSBKeepKeyAdapter | TrezorAdapter | any => {
const adapter = this.adapters.find(({ name }) => name === adapterName);
if (!adapter) {
console.log("Adapter not found");
return;
}
return adapter.adapter;
};
render() {
return (
<HDWalletContext.Provider
value={{
keyring: this.keyring,
pairedDevices: this.state.pairedDevices,
getAdapter: this.getAdapter
}}
>
{this.props.children}
</HDWalletContext.Provider>
);
}
}
export default HDWalletContext;