@dynamic-labs/wagmi-connector 0.13.64 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var wagmi = require('wagmi');
|
|
7
|
+
var sdkReact = require('@dynamic-labs/sdk-react');
|
|
8
|
+
var utils = require('ethers/lib/utils.js');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
13
|
+
|
|
14
|
+
/******************************************************************************
|
|
15
|
+
Copyright (c) Microsoft Corporation.
|
|
16
|
+
|
|
17
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
18
|
+
purpose with or without fee is hereby granted.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
21
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
22
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
23
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
24
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
25
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
26
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
27
|
+
***************************************************************************** */
|
|
28
|
+
|
|
29
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
30
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
31
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
32
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
33
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
34
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
35
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class Connector extends wagmi.Connector {
|
|
40
|
+
constructor({
|
|
41
|
+
handleLogOut,
|
|
42
|
+
walletConnector
|
|
43
|
+
}) {
|
|
44
|
+
super({
|
|
45
|
+
options: undefined
|
|
46
|
+
});
|
|
47
|
+
// wagmi properties
|
|
48
|
+
this.id = 'dynamic';
|
|
49
|
+
this.name = 'Dynamic';
|
|
50
|
+
// also a wagmi prop, but is only used for auto-connect
|
|
51
|
+
// which we have implemented ourselves, so this value
|
|
52
|
+
// doesn't matter
|
|
53
|
+
this.ready = false;
|
|
54
|
+
this.onAccountsChanged = accounts => __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
this.emit('change', {
|
|
56
|
+
account: utils.getAddress(accounts[0])
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
this.onChainChanged = chain => __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
this.emit('change', {
|
|
61
|
+
chain: {
|
|
62
|
+
id: Number(chain),
|
|
63
|
+
unsupported: false
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
this.onDisconnect = () => __awaiter(this, void 0, void 0, function* () {});
|
|
68
|
+
this.handleLogOut = handleLogOut;
|
|
69
|
+
this.walletConnector = walletConnector;
|
|
70
|
+
this.setupEventListeners();
|
|
71
|
+
}
|
|
72
|
+
setupEventListeners() {
|
|
73
|
+
this.walletConnector.setupEventListeners({
|
|
74
|
+
onAccountChange: this.onAccountsChanged,
|
|
75
|
+
onChainChange: this.onChainChanged,
|
|
76
|
+
onDisconnect: this.onDisconnect
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
connect(config) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
if (!this.walletConnector) {
|
|
82
|
+
throw new Error('WalletConnector is not defined');
|
|
83
|
+
}
|
|
84
|
+
const account = yield this.getAccount();
|
|
85
|
+
this.setupEventListeners();
|
|
86
|
+
this.emit('message', {
|
|
87
|
+
type: 'connecting'
|
|
88
|
+
});
|
|
89
|
+
const web3Provider = yield this.getProvider();
|
|
90
|
+
const {
|
|
91
|
+
provider
|
|
92
|
+
} = web3Provider;
|
|
93
|
+
return {
|
|
94
|
+
account,
|
|
95
|
+
chain: {
|
|
96
|
+
id: yield this.getChainId(),
|
|
97
|
+
unsupported: false
|
|
98
|
+
},
|
|
99
|
+
provider
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
disconnect() {
|
|
104
|
+
var _a, _b, _c;
|
|
105
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
const web3Provider = yield this.getProvider();
|
|
107
|
+
const {
|
|
108
|
+
provider
|
|
109
|
+
} = web3Provider;
|
|
110
|
+
provider.removeListener('accountsChanged', this.onAccountsChanged);
|
|
111
|
+
provider.removeListener('chainChanged', this.onChainChanged);
|
|
112
|
+
provider.removeListener('disconnect', this.onDisconnect);
|
|
113
|
+
yield (_b = (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.endSession) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
114
|
+
(_c = this.handleLogOut) === null || _c === void 0 ? void 0 : _c.call(this);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
getAccount() {
|
|
118
|
+
var _a;
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
const address = yield (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.fetchPublicAddress();
|
|
121
|
+
if (!address) {
|
|
122
|
+
throw new Error('Not connected');
|
|
123
|
+
}
|
|
124
|
+
return utils.getAddress(address);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
getChainId() {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
if (!this.walletConnector) {
|
|
130
|
+
throw new Error('WalletConnector is not defined');
|
|
131
|
+
}
|
|
132
|
+
const network = yield sdkReact.getNetwork(this.walletConnector);
|
|
133
|
+
if (!network) {
|
|
134
|
+
throw new Error('Network is not defined');
|
|
135
|
+
}
|
|
136
|
+
return network;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
getProvider(config) {
|
|
140
|
+
var _a;
|
|
141
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
142
|
+
return (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.getWeb3Provider();
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
getSigner(config) {
|
|
146
|
+
var _a;
|
|
147
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
return (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.getSigner();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
isAuthorized() {
|
|
152
|
+
var _a, _b;
|
|
153
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
const accounts = (_b = yield (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.getConnectedAccounts()) !== null && _b !== void 0 ? _b : [];
|
|
155
|
+
return accounts.length > 0;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
switchChain(chainId) {
|
|
159
|
+
var _a, _b;
|
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
yield (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.switchNetwork({
|
|
162
|
+
networkChainId: chainId
|
|
163
|
+
});
|
|
164
|
+
const id = utils.hexValue(chainId);
|
|
165
|
+
return (_b = this.chains.find(x => x.id === chainId)) !== null && _b !== void 0 ? _b : {
|
|
166
|
+
id: chainId,
|
|
167
|
+
name: `Chain ${id}`,
|
|
168
|
+
network: `${id}`,
|
|
169
|
+
rpcUrls: {
|
|
170
|
+
default: ''
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
watchAsset(asset) {
|
|
176
|
+
throw new Error('Method not implemented.');
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const DynamicWagmiConnector = ({
|
|
181
|
+
children
|
|
182
|
+
}) => {
|
|
183
|
+
const {
|
|
184
|
+
walletConnector,
|
|
185
|
+
handleLogOut
|
|
186
|
+
} = sdkReact.useDynamicContext();
|
|
187
|
+
const {
|
|
188
|
+
connect,
|
|
189
|
+
status
|
|
190
|
+
} = wagmi.useConnect();
|
|
191
|
+
const {
|
|
192
|
+
disconnect
|
|
193
|
+
} = wagmi.useDisconnect();
|
|
194
|
+
// storing as a singleton so we can re-setup event listeners on the same instance
|
|
195
|
+
const dynamicWagmiConnector = React.useRef();
|
|
196
|
+
if (walletConnector) {
|
|
197
|
+
// after connection is successful, re-setup event listeners after a short delay
|
|
198
|
+
// so that they are not cleared by the teardown in useWalletEventListeners
|
|
199
|
+
if (status === 'success') {
|
|
200
|
+
setTimeout(() => {
|
|
201
|
+
var _a;
|
|
202
|
+
(_a = dynamicWagmiConnector.current) === null || _a === void 0 ? void 0 : _a.setupEventListeners();
|
|
203
|
+
}, 100);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
React.useEffect(() => {
|
|
207
|
+
if (!walletConnector) {
|
|
208
|
+
dynamicWagmiConnector.current = undefined;
|
|
209
|
+
disconnect();
|
|
210
|
+
} else if (!dynamicWagmiConnector.current) {
|
|
211
|
+
dynamicWagmiConnector.current = new Connector({
|
|
212
|
+
handleLogOut,
|
|
213
|
+
walletConnector
|
|
214
|
+
});
|
|
215
|
+
connect({
|
|
216
|
+
connector: dynamicWagmiConnector.current
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}, [connect, disconnect, handleLogOut, status, walletConnector]);
|
|
220
|
+
// use React.createElement to prevent bunding react/jsx-runtime,
|
|
221
|
+
// which is not compatible when bundling apps using React 17
|
|
222
|
+
// eslint-disable-next-line react/no-children-prop
|
|
223
|
+
return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, {
|
|
224
|
+
children
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
exports.DynamicWagmiConnector = DynamicWagmiConnector;
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/wagmi-connector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/dynamic-labs/DynamicAuth.git",
|
|
8
|
+
"directory": "packages/wagmi-connector"
|
|
9
|
+
},
|
|
9
10
|
"peerDependencies": {
|
|
11
|
+
"react": "^17.0.2 || ^18.0.0",
|
|
10
12
|
"ethers": "^5.7.2",
|
|
11
13
|
"wagmi": "^0.8.10",
|
|
12
|
-
"@dynamic-labs/sdk-react": "0.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
"@dynamic-labs/sdk-react": "0.14.0"
|
|
15
|
+
},
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"module": "./index.js",
|
|
18
|
+
"main": "./index.cjs",
|
|
19
|
+
"types": "./src/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./src/index.d.ts",
|
|
23
|
+
"import": "./index.js",
|
|
24
|
+
"require": "./index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {}
|
|
28
|
+
}
|
|
File without changes
|
|
File without changes
|