-
Notifications
You must be signed in to change notification settings - Fork 16
/
WithWalletConnector.ts
281 lines (257 loc) · 12.1 KB
/
WithWalletConnector.ts
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
276
277
278
279
280
281
import { Network, WalletConnection, WalletConnectionDelegate, WalletConnector } from '@concordium/wallet-connectors';
import { Component } from 'react';
import { errorString } from './error';
/**
* Activation/deactivation controller of a given connector type.
*/
export interface ConnectorType {
/**
* Called when the connection type is being activated.
* The connector instance returned by this method becomes the new {@link State.activeConnector activeConnector}.
* @param component The component in which the instance is being activated.
* This object doubles as the delegate to pass to new connector instances.
* @param network The network to pass to new connector instances.
*/
activate(component: WithWalletConnector, network: Network): Promise<WalletConnector>;
/**
* Called from {@link WithWalletConnector} when the connection type is being deactivated,
* i.e. right after {@link State.activeConnector activeConnector} has been unset from this value.
* @param component The component in which the instance is being deactivated.
* @param connector The connector to deactivate.
*/
deactivate(component: WithWalletConnector, connector: WalletConnector): Promise<void>;
}
/**
* Produce a {@link ConnectorType} that creates a new connector instance on activation
* and disconnects the existing one on deactivation.
* This is the simplest connection type and should be used unless there's a reason not to.
* @param create Factory function for creating new connector instances.
*/
export function ephemeralConnectorType(create: (c: WithWalletConnector, n: Network) => Promise<WalletConnector>) {
return {
activate: create,
deactivate: (w: WithWalletConnector, c: WalletConnector) => c.disconnect(),
};
}
/**
* Produce a {@link ConnectorType} that reuse connectors between activation cycles.
* That is, once a connector is created, it's never automatically disconnected.
* Note that only the connector is permanent. Individual connections may still be disconnected by the application.
* @param create Factory function for creating new connector instances.
*/
export function persistentConnectorType(create: (c: WithWalletConnector, n: Network) => Promise<WalletConnector>) {
const connectorPromises = new Map<WithWalletConnector, Map<Network, Promise<WalletConnector>>>();
return {
activate: (component: WithWalletConnector, network: Network) => {
const delegateConnectorPromises =
connectorPromises.get(component) || new Map<Network, Promise<WalletConnector>>();
connectorPromises.set(component, delegateConnectorPromises);
const connectorPromise = delegateConnectorPromises.get(network) || create(component, network);
delegateConnectorPromises.set(network, connectorPromise);
return connectorPromise;
},
deactivate: async () => undefined,
};
}
/**
* The internal state of the component.
*/
interface State {
/**
* The active connector type. This value is updated using {@link WalletConnectionProps.setActiveConnectorType}.
* Changes to this value trigger activation of a connector managed by the connector type.
* This will cause {@link activeConnector} or {@link activeConnectorError} to change depending on the outcome.
*/
activeConnectorType: ConnectorType | undefined;
/**
* The active connector. Connector instances get (de)activated appropriately when {@link activeConnectorType} changes.
*
* It's up to the {@link ConnectorType} in {@link activeConnectorType} to implement any synchronization between
* the active connector and {@link activeConnector}:
* In general, it is perfectly possible for the active connection to not originate from the active connector.
*
* If the application disconnects the active connector manually, they must also call
* {@link WalletConnectionProps.setActiveConnectorType} to
*/
activeConnector: WalletConnector | undefined;
/**
* Any of the following kinds of errors:
* - Error activating a connector with {@link activeConnectorType}.
* In this case {@link activeConnector} is undefined.
* - Error deactivating the previous connector.
* In this case {@link activeConnectorType} and {@link activeConnector} are undefined.
*/
activeConnectorError: string;
/**
* A map from open connections to their selected accounts or the empty string
* if the connection doesn't have an associated account.
*/
connectedAccounts: Map<WalletConnection, string>;
/**
* A map from open connections to the hash of the genesis block for the chain that the selected accounts
* of the connections live on.
* Connections without a selected account (or the account's chain is unknown) will not have an entry in this map.
*
* TODO The reported hash values are not too reliable as they're updated only when the `onChainChanged` event fires.
* And this doesn't happen when the connection is initiated.
* For WalletConnect we could do that manually as we control what chain we connect to.
* For BrowserWallet we don't have that option (see also https://concordium.atlassian.net/browse/CBW-633).
*/
genesisHashes: Map<WalletConnection, string>;
}
function updateMapEntry<K, V>(map: Map<K, V>, key: K | undefined, value: V | undefined) {
const res = new Map(map);
if (key !== undefined) {
if (value !== undefined) {
res.set(key, value);
} else {
res.delete(key);
}
}
return res;
}
interface Props {
/**
* The network on which the connected accounts are expected to live on.
*
* Changes to this value will cause all connections managed by {@link State.activeConnector} to get disconnected.
*/
network: Network; // reacting to change in 'componentDidUpdate'
/**
* Function for generating the child component based on the props derived from the state of this component.
*
* JSX automatically supplies the nested expression as this prop field, so callers usually don't set it explicitly.
*
* @param props Connection state and management functions.
* @return Child component.
*/
children: (props: WalletConnectionProps) => JSX.Element;
}
/**
* The props to be passed to the child component.
*/
export interface WalletConnectionProps extends State {
/**
* The network provided to {@link WithWalletConnector} via its props.
*
* This is only passed for convenience as the value is always available to the child component anyway.
*/
network: Network;
/**
* Function for setting or resetting {@link State.activeConnectorType activeConnectorType}.
*
* Any existing connector type value is deactivated and any new one is activated.
*
* @param type The new connector type or undefined to reset the value.
*/
setActiveConnectorType: (type: ConnectorType | undefined) => void;
}
/**
* React component that helps managing wallet connections
* by introducing a notion of "active" {@link WalletConnector} and {@link WalletConnection},
* and maintaining their states as part of its own component state.
* This allows child components to access all relevant information in a reactive manner
* and provides methods for managing the active connection.
*
* The component implements {@link WalletConnectionDelegate} and passes itself to all {@link WalletConnector}s
* that it initializes.
* This allows it to receive events from the underlying clients.
* Once it receives an event for the active {@link WalletConnection},
* it performs the relevant updates to its component state which then bubble down to child components.
*
* This component significantly reduces the complexity of integrating with wallets,
* even if one only needs to support a single protocol and network.
*/
// TODO Rename to WalletConnectionManager?
export class WithWalletConnector extends Component<Props, State> implements WalletConnectionDelegate {
constructor(props: Props) {
super(props);
this.state = {
activeConnectorType: undefined,
activeConnector: undefined,
activeConnectorError: '',
genesisHashes: new Map(),
connectedAccounts: new Map(),
};
}
/**
* @see WalletConnectionProps.setActiveConnectorType
*/
setActiveConnectorType = (type: ConnectorType | undefined) => {
console.debug("WithWalletConnector: calling 'setActiveConnectorType'", { type, state: this.state });
const { network } = this.props;
const { activeConnectorType, activeConnector } = this.state;
this.setState({
activeConnectorType: type,
activeConnector: undefined,
activeConnectorError: '',
});
if (activeConnectorType && activeConnector) {
activeConnectorType.deactivate(this, activeConnector).catch((err) =>
this.setState((state) => {
// Don't set error if user switched connector type since initializing this connector.
// It's OK to show it if the user switched away and back...
if (state.activeConnectorType !== type) {
return state;
}
return { ...state, activeConnectorError: errorString(err) };
})
);
}
if (type) {
type.activate(this, network)
.then((connector: WalletConnector) => {
console.log('WithWalletConnector: setting active connector', { connector });
// Switch the connector (type) back in case the user changed it since initiating the connection.
this.setState({ activeConnectorType: type, activeConnector: connector, activeConnectorError: '' });
})
.catch((err) =>
this.setState((state) => {
if (state.activeConnectorType !== type) {
return state;
}
return { ...state, activeConnectorError: errorString(err) };
})
);
}
};
onAccountChanged = (connection: WalletConnection, address: string | undefined) => {
console.debug("WithWalletConnector: calling 'onAccountChanged'", { connection, address, state: this.state });
this.setState((state) => ({
...state,
connectedAccounts: updateMapEntry(state.connectedAccounts, connection, address || ''),
}));
};
onChainChanged = (connection: WalletConnection, genesisHash: string) => {
console.debug("WithWalletConnector: calling 'onChainChanged'", { connection, genesisHash, state: this.state });
this.setState((state) => ({
...state,
genesisHashes: updateMapEntry(state.genesisHashes, connection, genesisHash),
}));
};
onConnected = (connection: WalletConnection, address: string | undefined) => {
console.debug("WithWalletConnector: calling 'onConnected'", { connection, state: this.state });
this.onAccountChanged(connection, address);
};
onDisconnected = (connection: WalletConnection) => {
console.debug("WithWalletConnector: calling 'onDisconnected'", { connection, state: this.state });
this.setState((state) => ({
...state,
connectedAccounts: updateMapEntry(state.connectedAccounts, connection, undefined),
}));
};
render() {
const { children, network } = this.props;
return children({ ...this.state, network, setActiveConnectorType: this.setActiveConnectorType });
}
componentDidUpdate(prevProps: Props) {
if (prevProps.network !== this.props.network) {
// Reset active connector and connection when user changes network.
// In the future there may be a mechanism for negotiating with the wallet.
this.setActiveConnectorType(undefined);
}
}
componentWillUnmount() {
// TODO Disconnect everything?
}
}