@wagmi/core 0.0.0-20220909032847

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.
Files changed (35) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +30 -0
  3. package/chains/dist/wagmi-core-chains.esm.d.ts +11 -0
  4. package/chains/dist/wagmi-core-chains.esm.js +2 -0
  5. package/chains/package.json +3 -0
  6. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.esm.d.ts +11 -0
  7. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.esm.js +246 -0
  8. package/connectors/coinbaseWallet/package.json +3 -0
  9. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.esm.d.ts +11 -0
  10. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.esm.js +146 -0
  11. package/connectors/metaMask/package.json +3 -0
  12. package/connectors/mock/dist/wagmi-core-connectors-mock.esm.d.ts +11 -0
  13. package/connectors/mock/dist/wagmi-core-connectors-mock.esm.js +257 -0
  14. package/connectors/mock/package.json +3 -0
  15. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.esm.d.ts +11 -0
  16. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.esm.js +207 -0
  17. package/connectors/walletConnect/package.json +3 -0
  18. package/dist/chains-de91362e.esm.js +493 -0
  19. package/dist/getProvider-34b63ce9.esm.js +1055 -0
  20. package/dist/rpcs-f39d022b.esm.js +50 -0
  21. package/dist/wagmi-core.esm.d.ts +11 -0
  22. package/dist/wagmi-core.esm.js +1515 -0
  23. package/package.json +76 -0
  24. package/providers/alchemy/dist/wagmi-core-providers-alchemy.esm.d.ts +11 -0
  25. package/providers/alchemy/dist/wagmi-core-providers-alchemy.esm.js +32 -0
  26. package/providers/alchemy/package.json +3 -0
  27. package/providers/infura/dist/wagmi-core-providers-infura.esm.d.ts +11 -0
  28. package/providers/infura/dist/wagmi-core-providers-infura.esm.js +32 -0
  29. package/providers/infura/package.json +3 -0
  30. package/providers/jsonRpc/dist/wagmi-core-providers-jsonRpc.esm.d.ts +11 -0
  31. package/providers/jsonRpc/dist/wagmi-core-providers-jsonRpc.esm.js +42 -0
  32. package/providers/jsonRpc/package.json +3 -0
  33. package/providers/public/dist/wagmi-core-providers-public.esm.d.ts +11 -0
  34. package/providers/public/dist/wagmi-core-providers-public.esm.js +28 -0
  35. package/providers/public/package.json +3 -0
@@ -0,0 +1,257 @@
1
+ import { t as _defineProperty, v as _classPrivateFieldInitSpec, w as _classPrivateFieldSet, x as _classPrivateFieldGet, U as UserRejectedRequestError, l as Connector, _ as _classPrivateMethodInitSpec, n as normalizeChainId, u as _classPrivateMethodGet } from '../../../dist/getProvider-34b63ce9.esm.js';
2
+ import { getAddress } from 'ethers/lib/utils';
3
+ import { providers } from 'ethers';
4
+ import EventEmitter from 'eventemitter3';
5
+ import 'zustand/middleware';
6
+ import 'zustand/vanilla';
7
+ import '../../../dist/chains-de91362e.esm.js';
8
+ import '../../../dist/rpcs-f39d022b.esm.js';
9
+
10
+ var _options = /*#__PURE__*/new WeakMap();
11
+
12
+ var _signer = /*#__PURE__*/new WeakMap();
13
+
14
+ class MockProvider extends providers.BaseProvider {
15
+ constructor(options) {
16
+ var _options$chainId;
17
+
18
+ super({
19
+ name: 'Network',
20
+ chainId: (_options$chainId = options.chainId) !== null && _options$chainId !== void 0 ? _options$chainId : 1
21
+ });
22
+
23
+ _defineProperty(this, "events", new EventEmitter());
24
+
25
+ _classPrivateFieldInitSpec(this, _options, {
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+
30
+ _classPrivateFieldInitSpec(this, _signer, {
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+
35
+ _classPrivateFieldSet(this, _options, options);
36
+ }
37
+
38
+ async enable() {
39
+ var _classPrivateFieldGet2;
40
+
41
+ if ((_classPrivateFieldGet2 = _classPrivateFieldGet(this, _options).flags) !== null && _classPrivateFieldGet2 !== void 0 && _classPrivateFieldGet2.failConnect) throw new UserRejectedRequestError(new Error('Failed to connect'));
42
+ if (!_classPrivateFieldGet(this, _signer)) _classPrivateFieldSet(this, _signer, _classPrivateFieldGet(this, _options).signer);
43
+ const address = await _classPrivateFieldGet(this, _signer).getAddress();
44
+ this.events.emit('accountsChanged', [address]);
45
+ return [address];
46
+ }
47
+
48
+ async disconnect() {
49
+ this.events.emit('disconnect');
50
+
51
+ _classPrivateFieldSet(this, _signer, undefined);
52
+ }
53
+
54
+ async getAccounts() {
55
+ var _classPrivateFieldGet3;
56
+
57
+ const address = await ((_classPrivateFieldGet3 = _classPrivateFieldGet(this, _signer)) === null || _classPrivateFieldGet3 === void 0 ? void 0 : _classPrivateFieldGet3.getAddress());
58
+ if (!address) return [];
59
+ return [getAddress(address)];
60
+ }
61
+
62
+ getSigner() {
63
+ const signer = _classPrivateFieldGet(this, _signer);
64
+
65
+ if (!signer) throw new Error('Signer not found');
66
+ return signer;
67
+ }
68
+
69
+ async switchChain(chainId) {
70
+ var _classPrivateFieldGet4;
71
+
72
+ if ((_classPrivateFieldGet4 = _classPrivateFieldGet(this, _options).flags) !== null && _classPrivateFieldGet4 !== void 0 && _classPrivateFieldGet4.failSwitchChain) throw new UserRejectedRequestError(new Error('Failed to switch chain'));
73
+ _classPrivateFieldGet(this, _options).chainId = chainId;
74
+ this.network.chainId = chainId;
75
+ this.events.emit('chainChanged', chainId);
76
+ }
77
+
78
+ async watchAsset(_asset) {
79
+ return true;
80
+ }
81
+
82
+ on(event, listener) {
83
+ this.events.on(event, listener);
84
+ return this;
85
+ }
86
+
87
+ once(event, listener) {
88
+ this.events.once(event, listener);
89
+ return this;
90
+ }
91
+
92
+ removeListener(event, listener) {
93
+ this.events.removeListener(event, listener);
94
+ return this;
95
+ }
96
+
97
+ off(event, listener) {
98
+ this.events.off(event, listener);
99
+ return this;
100
+ }
101
+
102
+ toJSON() {
103
+ return '<MockProvider>';
104
+ }
105
+
106
+ }
107
+
108
+ var _provider = /*#__PURE__*/new WeakMap();
109
+
110
+ var _switchChain = /*#__PURE__*/new WeakSet();
111
+
112
+ class MockConnector extends Connector {
113
+ constructor(config) {
114
+ super(config);
115
+
116
+ _classPrivateMethodInitSpec(this, _switchChain);
117
+
118
+ _defineProperty(this, "id", 'mock');
119
+
120
+ _defineProperty(this, "name", 'Mock');
121
+
122
+ _defineProperty(this, "ready", true);
123
+
124
+ _classPrivateFieldInitSpec(this, _provider, {
125
+ writable: true,
126
+ value: void 0
127
+ });
128
+
129
+ _defineProperty(this, "onAccountsChanged", accounts => {
130
+ if (accounts.length === 0) this.emit('disconnect');else this.emit('change', {
131
+ account: getAddress(accounts[0])
132
+ });
133
+ });
134
+
135
+ _defineProperty(this, "onChainChanged", chainId => {
136
+ const id = normalizeChainId(chainId);
137
+ const unsupported = this.isChainUnsupported(id);
138
+ this.emit('change', {
139
+ chain: {
140
+ id,
141
+ unsupported
142
+ }
143
+ });
144
+ });
145
+
146
+ _defineProperty(this, "onDisconnect", () => {
147
+ this.emit('disconnect');
148
+ });
149
+ }
150
+
151
+ async connect() {
152
+ var _this$options$flags;
153
+
154
+ let {
155
+ chainId
156
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
157
+ const provider = await this.getProvider({
158
+ chainId
159
+ });
160
+ provider.on('accountsChanged', this.onAccountsChanged);
161
+ provider.on('chainChanged', this.onChainChanged);
162
+ provider.on('disconnect', this.onDisconnect);
163
+ this.emit('message', {
164
+ type: 'connecting'
165
+ });
166
+ const accounts = await provider.enable();
167
+ const account = getAddress(accounts[0]);
168
+ const id = normalizeChainId(provider._network.chainId);
169
+ const unsupported = this.isChainUnsupported(id);
170
+ const data = {
171
+ account,
172
+ chain: {
173
+ id,
174
+ unsupported
175
+ },
176
+ provider
177
+ };
178
+ if (!((_this$options$flags = this.options.flags) !== null && _this$options$flags !== void 0 && _this$options$flags.noSwitchChain)) this.switchChain = _classPrivateMethodGet(this, _switchChain, _switchChain2);
179
+ return new Promise(res => setTimeout(() => res(data), 100));
180
+ }
181
+
182
+ async disconnect() {
183
+ const provider = await this.getProvider();
184
+ await provider.disconnect();
185
+ provider.removeListener('accountsChanged', this.onAccountsChanged);
186
+ provider.removeListener('chainChanged', this.onChainChanged);
187
+ provider.removeListener('disconnect', this.onDisconnect);
188
+ }
189
+
190
+ async getAccount() {
191
+ const provider = await this.getProvider();
192
+ const accounts = await provider.getAccounts();
193
+ const account = accounts[0];
194
+ if (!account) throw new Error('Failed to get account'); // return checksum address
195
+
196
+ return getAddress(account);
197
+ }
198
+
199
+ async getChainId() {
200
+ const provider = await this.getProvider();
201
+ return normalizeChainId(provider.network.chainId);
202
+ }
203
+
204
+ async getProvider() {
205
+ let {
206
+ chainId
207
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
208
+ if (!_classPrivateFieldGet(this, _provider) || chainId) _classPrivateFieldSet(this, _provider, new MockProvider({ ...this.options,
209
+ chainId
210
+ }));
211
+ return _classPrivateFieldGet(this, _provider);
212
+ }
213
+
214
+ async getSigner() {
215
+ const provider = await this.getProvider();
216
+ return provider.getSigner();
217
+ }
218
+
219
+ async isAuthorized() {
220
+ try {
221
+ var _this$options$flags$i, _this$options$flags2;
222
+
223
+ const provider = await this.getProvider();
224
+ const account = await provider.getAccounts();
225
+ return (_this$options$flags$i = (_this$options$flags2 = this.options.flags) === null || _this$options$flags2 === void 0 ? void 0 : _this$options$flags2.isAuthorized) !== null && _this$options$flags$i !== void 0 ? _this$options$flags$i : !!account;
226
+ } catch {
227
+ return false;
228
+ }
229
+ }
230
+
231
+ async watchAsset(asset) {
232
+ const provider = await this.getProvider();
233
+ return await provider.watchAsset(asset);
234
+ }
235
+
236
+ toJSON() {
237
+ return '<MockConnector>';
238
+ }
239
+
240
+ }
241
+
242
+ async function _switchChain2(chainId) {
243
+ var _this$chains$find;
244
+
245
+ const provider = await this.getProvider();
246
+ await provider.switchChain(chainId);
247
+ return (_this$chains$find = this.chains.find(x => x.id === chainId)) !== null && _this$chains$find !== void 0 ? _this$chains$find : {
248
+ id: chainId,
249
+ name: "Chain ".concat(chainId),
250
+ network: "".concat(chainId),
251
+ rpcUrls: {
252
+ default: ''
253
+ }
254
+ };
255
+ }
256
+
257
+ export { MockConnector, MockProvider };
@@ -0,0 +1,3 @@
1
+ {
2
+ "main": "dist/wagmi-core-connectors-mock.esm.js"
3
+ }
@@ -0,0 +1,11 @@
1
+ // are you seeing an error that a default export doesn't exist but your source file has a default export?
2
+ // you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
3
+
4
+ // curious why you need to?
5
+ // this file exists so that you can import from the entrypoint normally
6
+ // except that it points to your source file and you don't need to run build constantly
7
+ // which means we need to re-export all of the modules from your source file
8
+ // and since export * doesn't include default exports, we need to read your source file
9
+ // to check for a default export and re-export it if it exists
10
+ // it's not ideal, but it works pretty well ¯\_(ツ)_/¯
11
+ export * from "../../../src/connectors/walletConnect";
@@ -0,0 +1,207 @@
1
+ import { l as Connector, _ as _classPrivateMethodInitSpec, t as _defineProperty, v as _classPrivateFieldInitSpec, n as normalizeChainId, g as getClient, u as _classPrivateMethodGet, U as UserRejectedRequestError, x as _classPrivateFieldGet, w as _classPrivateFieldSet, q as SwitchChainError } from '../../../dist/getProvider-34b63ce9.esm.js';
2
+ import { providers } from 'ethers';
3
+ import { getAddress, hexValue } from 'ethers/lib/utils';
4
+ import 'zustand/middleware';
5
+ import 'zustand/vanilla';
6
+ import 'eventemitter3';
7
+ import '../../../dist/chains-de91362e.esm.js';
8
+ import '../../../dist/rpcs-f39d022b.esm.js';
9
+
10
+ /**
11
+ * Wallets that support chain switching through WalletConnect
12
+ * - imToken (token.im)
13
+ * - MetaMask (metamask.io)
14
+ * - Rainbow (rainbow.me)
15
+ */
16
+
17
+ const switchChainAllowedRegex = /(imtoken|metamask|rainbow)/i;
18
+
19
+ var _provider = /*#__PURE__*/new WeakMap();
20
+
21
+ var _switchChain = /*#__PURE__*/new WeakSet();
22
+
23
+ class WalletConnectConnector extends Connector {
24
+ constructor(config) {
25
+ super(config);
26
+
27
+ _classPrivateMethodInitSpec(this, _switchChain);
28
+
29
+ _defineProperty(this, "id", 'walletConnect');
30
+
31
+ _defineProperty(this, "name", 'WalletConnect');
32
+
33
+ _defineProperty(this, "ready", true);
34
+
35
+ _classPrivateFieldInitSpec(this, _provider, {
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+
40
+ _defineProperty(this, "onAccountsChanged", accounts => {
41
+ if (accounts.length === 0) this.emit('disconnect');else this.emit('change', {
42
+ account: getAddress(accounts[0])
43
+ });
44
+ });
45
+
46
+ _defineProperty(this, "onChainChanged", chainId => {
47
+ const id = normalizeChainId(chainId);
48
+ const unsupported = this.isChainUnsupported(id);
49
+ this.emit('change', {
50
+ chain: {
51
+ id,
52
+ unsupported
53
+ }
54
+ });
55
+ });
56
+
57
+ _defineProperty(this, "onDisconnect", () => {
58
+ this.emit('disconnect');
59
+ });
60
+ }
61
+
62
+ async connect() {
63
+ let {
64
+ chainId
65
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
66
+
67
+ try {
68
+ var _provider$connector$p, _provider$connector, _provider$connector$p2;
69
+
70
+ let targetChainId = chainId;
71
+
72
+ if (!targetChainId) {
73
+ const lastUsedChainId = getClient().lastUsedChainId;
74
+ if (lastUsedChainId && !this.isChainUnsupported(lastUsedChainId)) targetChainId = lastUsedChainId;
75
+ }
76
+
77
+ const provider = await this.getProvider({
78
+ chainId: targetChainId,
79
+ create: true
80
+ });
81
+ provider.on('accountsChanged', this.onAccountsChanged);
82
+ provider.on('chainChanged', this.onChainChanged);
83
+ provider.on('disconnect', this.onDisconnect); // Defer message to the next tick to ensure wallet connect data (provided by `.enable()`) is available
84
+
85
+ setTimeout(() => this.emit('message', {
86
+ type: 'connecting'
87
+ }), 0);
88
+ const accounts = await provider.enable();
89
+ const account = getAddress(accounts[0]);
90
+ const id = await this.getChainId();
91
+ const unsupported = this.isChainUnsupported(id); // Not all WalletConnect options support programmatic chain switching
92
+ // Only enable for wallet options that do
93
+
94
+ const walletName = (_provider$connector$p = (_provider$connector = provider.connector) === null || _provider$connector === void 0 ? void 0 : (_provider$connector$p2 = _provider$connector.peerMeta) === null || _provider$connector$p2 === void 0 ? void 0 : _provider$connector$p2.name) !== null && _provider$connector$p !== void 0 ? _provider$connector$p : '';
95
+ if (switchChainAllowedRegex.test(walletName)) this.switchChain = _classPrivateMethodGet(this, _switchChain, _switchChain2);
96
+ return {
97
+ account,
98
+ chain: {
99
+ id,
100
+ unsupported
101
+ },
102
+ provider: new providers.Web3Provider(provider)
103
+ };
104
+ } catch (error) {
105
+ if (/user closed modal/i.test(error.message)) throw new UserRejectedRequestError(error);
106
+ throw error;
107
+ }
108
+ }
109
+
110
+ async disconnect() {
111
+ const provider = await this.getProvider();
112
+ await provider.disconnect();
113
+ provider.removeListener('accountsChanged', this.onAccountsChanged);
114
+ provider.removeListener('chainChanged', this.onChainChanged);
115
+ provider.removeListener('disconnect', this.onDisconnect);
116
+ typeof localStorage !== 'undefined' && localStorage.removeItem('walletconnect');
117
+ }
118
+
119
+ async getAccount() {
120
+ const provider = await this.getProvider();
121
+ const accounts = provider.accounts; // return checksum address
122
+
123
+ return getAddress(accounts[0]);
124
+ }
125
+
126
+ async getChainId() {
127
+ const provider = await this.getProvider();
128
+ const chainId = normalizeChainId(provider.chainId);
129
+ return chainId;
130
+ }
131
+
132
+ async getProvider() {
133
+ let {
134
+ chainId,
135
+ create
136
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
137
+
138
+ // Force create new provider
139
+ if (!_classPrivateFieldGet(this, _provider) || chainId || create) {
140
+ var _this$options, _this$options2;
141
+
142
+ const rpc = !((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.infuraId) ? this.chains.reduce((rpc, chain) => ({ ...rpc,
143
+ [chain.id]: chain.rpcUrls.default
144
+ }), {}) : {};
145
+ const WalletConnectProvider = (await import('@walletconnect/ethereum-provider')).default;
146
+
147
+ _classPrivateFieldSet(this, _provider, new WalletConnectProvider({ ...this.options,
148
+ chainId,
149
+ rpc: { ...rpc,
150
+ ...((_this$options2 = this.options) === null || _this$options2 === void 0 ? void 0 : _this$options2.rpc)
151
+ }
152
+ }));
153
+ }
154
+
155
+ return _classPrivateFieldGet(this, _provider);
156
+ }
157
+
158
+ async getSigner() {
159
+ let {
160
+ chainId
161
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
162
+ const [provider, account] = await Promise.all([this.getProvider({
163
+ chainId
164
+ }), this.getAccount()]);
165
+ return new providers.Web3Provider(provider).getSigner(account);
166
+ }
167
+
168
+ async isAuthorized() {
169
+ try {
170
+ const account = await this.getAccount();
171
+ return !!account;
172
+ } catch {
173
+ return false;
174
+ }
175
+ }
176
+
177
+ }
178
+
179
+ async function _switchChain2(chainId) {
180
+ const provider = await this.getProvider();
181
+ const id = hexValue(chainId);
182
+
183
+ try {
184
+ var _this$chains$find;
185
+
186
+ await provider.request({
187
+ method: 'wallet_switchEthereumChain',
188
+ params: [{
189
+ chainId: id
190
+ }]
191
+ });
192
+ return (_this$chains$find = this.chains.find(x => x.id === chainId)) !== null && _this$chains$find !== void 0 ? _this$chains$find : {
193
+ id: chainId,
194
+ name: "Chain ".concat(id),
195
+ network: "".concat(id),
196
+ rpcUrls: {
197
+ default: ''
198
+ }
199
+ };
200
+ } catch (error) {
201
+ const message = typeof error === 'string' ? error : error === null || error === void 0 ? void 0 : error.message;
202
+ if (/user rejected request/i.test(message)) throw new UserRejectedRequestError(error);
203
+ throw new SwitchChainError(error);
204
+ }
205
+ }
206
+
207
+ export { WalletConnectConnector };
@@ -0,0 +1,3 @@
1
+ {
2
+ "main": "dist/wagmi-core-connectors-walletConnect.esm.js"
3
+ }