@getpara/wagmi-v2-integration 1.4.4-dev.1 → 1.4.4-dev.3
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/dist/index.js +347 -2
- package/dist/index.js.br +0 -0
- package/dist/index.js.gz +0 -0
- package/dist/paraConnector.d.ts +5 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,348 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
// src/paraConnector.ts
|
|
4
|
+
import { injected } from "wagmi/connectors";
|
|
5
|
+
|
|
6
|
+
// src/ParaEIP1193Provider.ts
|
|
7
|
+
import {
|
|
8
|
+
ProviderRpcError,
|
|
9
|
+
webSocket,
|
|
10
|
+
publicActions,
|
|
11
|
+
http,
|
|
12
|
+
formatTransaction
|
|
13
|
+
} from "viem";
|
|
14
|
+
import { EventEmitter } from "eventemitter3";
|
|
15
|
+
import { extractRpcUrls } from "@wagmi/core";
|
|
16
|
+
import { getViemChain, createParaViemClient, createParaAccount } from "@getpara/viem-v2-integration";
|
|
17
|
+
import { decimalToHex, hexToDecimal } from "@getpara/react-sdk";
|
|
18
|
+
|
|
19
|
+
// src/connectorModal.tsx
|
|
20
|
+
import { ParaModal } from "@getpara/react-sdk";
|
|
21
|
+
import { jsx } from "react/jsx-runtime";
|
|
22
|
+
function renderModal(para, modalProps, onCloseArg) {
|
|
23
|
+
const existingContainer = document.getElementById("para-modal");
|
|
24
|
+
const container = existingContainer ?? document.createElement("div");
|
|
25
|
+
container.id = "para-modal";
|
|
26
|
+
if (!existingContainer) {
|
|
27
|
+
document.body.appendChild(container);
|
|
28
|
+
}
|
|
29
|
+
const onClose = () => {
|
|
30
|
+
onCloseArg();
|
|
31
|
+
modalProps.onClose && modalProps.onClose();
|
|
32
|
+
render(false);
|
|
33
|
+
};
|
|
34
|
+
const render = async (isOpen) => {
|
|
35
|
+
const Modal = /* @__PURE__ */ jsx(ParaModal, { ...modalProps, onClose, para, isOpen });
|
|
36
|
+
try {
|
|
37
|
+
const client = await import("react-dom/client");
|
|
38
|
+
const root = client.createRoot(container);
|
|
39
|
+
root.render(Modal);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
const ReactDOM = await import("react-dom");
|
|
42
|
+
ReactDOM.render(Modal, container);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
render(true);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/ParaEIP1193Provider.ts
|
|
49
|
+
var STORAGE_CHAIN_ID_KEY = "@CAPSULE/chainId";
|
|
50
|
+
var TEN_MINUTES_MS = 6e5;
|
|
51
|
+
var serverSessionStorageStub = {
|
|
52
|
+
setItem: () => {
|
|
53
|
+
},
|
|
54
|
+
getItem: () => null
|
|
55
|
+
};
|
|
56
|
+
var ParaEIP1193Provider = class extends EventEmitter {
|
|
57
|
+
constructor(opts) {
|
|
58
|
+
super();
|
|
59
|
+
this.getRpcUrlsFromViemChain = (chain) => {
|
|
60
|
+
return extractRpcUrls({ chain, transports: this.transports });
|
|
61
|
+
};
|
|
62
|
+
this.wagmiChainToAddEthereumChainParameters = (chain) => {
|
|
63
|
+
const hexChainId = decimalToHex(`${chain.id}`);
|
|
64
|
+
return [
|
|
65
|
+
hexChainId,
|
|
66
|
+
{
|
|
67
|
+
chainId: hexChainId,
|
|
68
|
+
chainName: chain.name,
|
|
69
|
+
nativeCurrency: chain.nativeCurrency,
|
|
70
|
+
rpcUrls: this.getRpcUrlsFromViemChain(chain)
|
|
71
|
+
}
|
|
72
|
+
];
|
|
73
|
+
};
|
|
74
|
+
this.wagmiChainsToAddEthereumChainParameters = (chains) => {
|
|
75
|
+
return Object.fromEntries(chains.map(this.wagmiChainToAddEthereumChainParameters));
|
|
76
|
+
};
|
|
77
|
+
this.accountFromAddress = (address) => {
|
|
78
|
+
return createParaAccount(this.para, address);
|
|
79
|
+
};
|
|
80
|
+
this.setCurrentChain = (chainId) => {
|
|
81
|
+
const chain = this.chains[chainId];
|
|
82
|
+
this.setChainId(chainId);
|
|
83
|
+
const viemChain = this.viemChains[chainId] || getViemChain(hexToDecimal(chainId));
|
|
84
|
+
const rpcUrls = this.getRpcUrlsFromViemChain(viemChain);
|
|
85
|
+
let transport;
|
|
86
|
+
if (this.transports[viemChain.id]) {
|
|
87
|
+
transport = this.transports[viemChain.id];
|
|
88
|
+
} else if (rpcUrls[0].startsWith("ws")) {
|
|
89
|
+
transport = webSocket(chain.rpcUrls[0]);
|
|
90
|
+
} else {
|
|
91
|
+
transport = http(rpcUrls[0]);
|
|
92
|
+
this.chainTransportSubscribe = void 0;
|
|
93
|
+
}
|
|
94
|
+
const chainTransport = transport({
|
|
95
|
+
chain: viemChain
|
|
96
|
+
});
|
|
97
|
+
if (chainTransport.config.type === "ws") {
|
|
98
|
+
this.chainTransportSubscribe = chainTransport.value.subscribe;
|
|
99
|
+
}
|
|
100
|
+
this.walletClient = createParaViemClient(
|
|
101
|
+
this.para,
|
|
102
|
+
{
|
|
103
|
+
chain: viemChain,
|
|
104
|
+
transport
|
|
105
|
+
// @ts-ignore
|
|
106
|
+
},
|
|
107
|
+
{ noAccount: true }
|
|
108
|
+
).extend(publicActions);
|
|
109
|
+
this.emit("chainChanged", this.currentHexChainId);
|
|
110
|
+
};
|
|
111
|
+
this.closeModal = () => {
|
|
112
|
+
this.isModalClosed = true;
|
|
113
|
+
};
|
|
114
|
+
this.request = async (args) => {
|
|
115
|
+
const { method, params } = args;
|
|
116
|
+
switch (method) {
|
|
117
|
+
case "eth_accounts": {
|
|
118
|
+
const accounts = this.accounts;
|
|
119
|
+
return accounts || [];
|
|
120
|
+
}
|
|
121
|
+
case "eth_chainId": {
|
|
122
|
+
return this.currentHexChainId;
|
|
123
|
+
}
|
|
124
|
+
case "eth_requestAccounts": {
|
|
125
|
+
if (await this.para.isFullyLoggedIn()) {
|
|
126
|
+
const accounts = this.accounts;
|
|
127
|
+
if (accounts && accounts.length > 0) {
|
|
128
|
+
return accounts;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (!this.disableModal) {
|
|
132
|
+
this.isModalClosed = false;
|
|
133
|
+
renderModal(this.para, this.modalProps, () => {
|
|
134
|
+
this.isModalClosed = true;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
await this.waitForLogin();
|
|
138
|
+
try {
|
|
139
|
+
const accounts = await this.waitForAccounts();
|
|
140
|
+
this.emit("accountsChanged", accounts);
|
|
141
|
+
return accounts;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
throw new ProviderRpcError(new Error("accounts not available after login"), {
|
|
144
|
+
code: 4001,
|
|
145
|
+
shortMessage: "accounts not available after login"
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
case "eth_sendTransaction": {
|
|
150
|
+
const fromAddress = params[0].from;
|
|
151
|
+
return this.walletClient.sendTransaction({
|
|
152
|
+
...formatTransaction(params[0]),
|
|
153
|
+
chain: void 0,
|
|
154
|
+
// uses the chain from the wallet client
|
|
155
|
+
account: this.accountFromAddress(fromAddress)
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
case "eth_sign":
|
|
159
|
+
case "personal_sign": {
|
|
160
|
+
return this.walletClient.signMessage({
|
|
161
|
+
message: { raw: params[0] },
|
|
162
|
+
account: this.accountFromAddress(params[1])
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
case "eth_signTransaction": {
|
|
166
|
+
const fromAddress = params[0].from;
|
|
167
|
+
return this.accountFromAddress(fromAddress).signTransaction(formatTransaction(params[0]));
|
|
168
|
+
}
|
|
169
|
+
case "eth_signTypedData_v4": {
|
|
170
|
+
const fromAddress = params[0];
|
|
171
|
+
let typedMessage = params[1];
|
|
172
|
+
if (typeof typedMessage === "string") {
|
|
173
|
+
typedMessage = JSON.parse(typedMessage);
|
|
174
|
+
}
|
|
175
|
+
return this.walletClient.signTypedData({
|
|
176
|
+
...typedMessage,
|
|
177
|
+
account: this.accountFromAddress(fromAddress)
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
case "eth_subscribe": {
|
|
181
|
+
if (!this.chainTransportSubscribe) {
|
|
182
|
+
throw new ProviderRpcError(new Error("chain does not support subscriptions"), {
|
|
183
|
+
code: 4200,
|
|
184
|
+
shortMessage: "chain does not support subscriptions"
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
const res = await this.chainTransportSubscribe({
|
|
188
|
+
params,
|
|
189
|
+
onData: (data) => {
|
|
190
|
+
this.emit("message", {
|
|
191
|
+
type: "eth_subscription",
|
|
192
|
+
data
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
return res.subscriptionId;
|
|
197
|
+
}
|
|
198
|
+
case "wallet_addEthereumChain": {
|
|
199
|
+
if (!this.chains[params[0].chainId]) {
|
|
200
|
+
this.chains[params[0].chainId] = params[0];
|
|
201
|
+
}
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
case "wallet_getPermissions": {
|
|
205
|
+
return [];
|
|
206
|
+
}
|
|
207
|
+
case "wallet_requestPermissions": {
|
|
208
|
+
return [];
|
|
209
|
+
}
|
|
210
|
+
case "wallet_switchEthereumChain": {
|
|
211
|
+
if (!this.chains[params[0].chainId]) {
|
|
212
|
+
const chain = getViemChain(hexToDecimal(params[0].chainId));
|
|
213
|
+
const [hexChainId, addEthereumChainParameter] = this.wagmiChainToAddEthereumChainParameters(chain);
|
|
214
|
+
this.chains[hexChainId] = addEthereumChainParameter;
|
|
215
|
+
this.setCurrentChain(params[0].chainId);
|
|
216
|
+
}
|
|
217
|
+
if (this.currentHexChainId !== params[0].chainId) {
|
|
218
|
+
this.setCurrentChain(params[0].chainId);
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
case "wallet_watchAsset": {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
default: {
|
|
226
|
+
return this.walletClient.request({
|
|
227
|
+
method,
|
|
228
|
+
params
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
this.storage = opts.storageOverride || typeof window === "undefined" ? serverSessionStorageStub : sessionStorage;
|
|
234
|
+
this.isModalClosed = true;
|
|
235
|
+
this.para = opts.para;
|
|
236
|
+
this.modalProps = { ...opts };
|
|
237
|
+
this.disableModal = !!opts.disableModal;
|
|
238
|
+
this.viemChains = opts.chains.reduce((acc, curChain) => {
|
|
239
|
+
acc[decimalToHex(`${curChain.id}`)] = curChain;
|
|
240
|
+
return acc;
|
|
241
|
+
}, {});
|
|
242
|
+
this.chains = this.wagmiChainsToAddEthereumChainParameters(opts.chains);
|
|
243
|
+
this.transports = opts.transports;
|
|
244
|
+
const defaultChainId = this.getStorageChainId() || opts.chainId;
|
|
245
|
+
const currentChainId = this.chains[decimalToHex(defaultChainId)] ? defaultChainId : `${opts.chains[0].id}`;
|
|
246
|
+
this.setCurrentChain(decimalToHex(currentChainId));
|
|
247
|
+
this.emit("connect", { chainId: this.currentHexChainId });
|
|
248
|
+
}
|
|
249
|
+
get accounts() {
|
|
250
|
+
return this.para.getWalletsByType("EVM").map((w) => w.address);
|
|
251
|
+
}
|
|
252
|
+
getStorageChainId() {
|
|
253
|
+
return this.storage.getItem(STORAGE_CHAIN_ID_KEY);
|
|
254
|
+
}
|
|
255
|
+
setChainId(hexChainId) {
|
|
256
|
+
this.currentHexChainId = hexChainId;
|
|
257
|
+
this.storage.setItem(STORAGE_CHAIN_ID_KEY, hexToDecimal(hexChainId));
|
|
258
|
+
}
|
|
259
|
+
async waitForLogin(timeoutMs = TEN_MINUTES_MS) {
|
|
260
|
+
const startTime = Date.now();
|
|
261
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
262
|
+
if (await this.para.isFullyLoggedIn()) {
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
if (this.isModalClosed) {
|
|
266
|
+
throw new ProviderRpcError(new Error("user closed modal"), {
|
|
267
|
+
code: 4001,
|
|
268
|
+
shortMessage: "user closed modal"
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
await new Promise((resolve) => setTimeout(resolve, 2e3));
|
|
272
|
+
}
|
|
273
|
+
throw new ProviderRpcError(new Error("timed out waiting for user to log in"), {
|
|
274
|
+
code: 4900,
|
|
275
|
+
//provider is disconnected code
|
|
276
|
+
shortMessage: "timed out waiting for user to log in"
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
async waitForAccounts(timeoutMs = 5e3) {
|
|
280
|
+
const startTime = Date.now();
|
|
281
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
282
|
+
const accounts = this.accounts;
|
|
283
|
+
if (accounts && accounts.length > 0) {
|
|
284
|
+
return accounts;
|
|
285
|
+
}
|
|
286
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
287
|
+
}
|
|
288
|
+
throw new ProviderRpcError(new Error("timed out waiting for accounts to load"), {
|
|
289
|
+
code: 4900,
|
|
290
|
+
//provider is disconnected code
|
|
291
|
+
shortMessage: "timed out waiting for accounts to load"
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// src/paraConnector.ts
|
|
297
|
+
import { createConnector } from "wagmi";
|
|
298
|
+
var PARA_ID = "para";
|
|
299
|
+
var PARA_NAME = "Para";
|
|
300
|
+
var PARA_ICON = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjE2IiBoZWlnaHQ9IjIwNCIgdmlld0JveD0iMCAwIDIxNiAyMDQiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGQ9Ik02MCAwSDE0NEMxODMuNzY0IDAgMjE2IDMyLjIzNTUgMjE2IDcyQzIxNiAxMTEuNzY1IDE4My43NjQgMTQ0IDE0NCAxNDRIOTZDODIuNzQ1MiAxNDQgNzIgMTU0Ljc0NSA3MiAxNjhWMjA0SDBWMTMySDM2QzQ5LjI1NDggMTMyIDYwIDEyMS4yNTUgNjAgMTA4TDYwIDBaIiBmaWxsPSJibGFjayIvPgo8L3N2Zz4K";
|
|
301
|
+
var paraConnector = ({
|
|
302
|
+
para,
|
|
303
|
+
chains: _chains,
|
|
304
|
+
disableModal,
|
|
305
|
+
storageOverride,
|
|
306
|
+
options,
|
|
307
|
+
iconOverride,
|
|
308
|
+
nameOverride,
|
|
309
|
+
idOverride,
|
|
310
|
+
transports,
|
|
311
|
+
...modalProps
|
|
312
|
+
}) => {
|
|
313
|
+
return createConnector((config) => {
|
|
314
|
+
const chains = [...config.chains];
|
|
315
|
+
const eip1193Provider = new ParaEIP1193Provider({
|
|
316
|
+
para,
|
|
317
|
+
chainId: `${chains[0].id}`,
|
|
318
|
+
chains,
|
|
319
|
+
disableModal,
|
|
320
|
+
storageOverride,
|
|
321
|
+
transports: transports || config.transports,
|
|
322
|
+
...modalProps
|
|
323
|
+
});
|
|
324
|
+
const injectedObj = injected({
|
|
325
|
+
target: {
|
|
326
|
+
name: PARA_NAME,
|
|
327
|
+
id: idOverride ?? PARA_ID,
|
|
328
|
+
provider: eip1193Provider
|
|
329
|
+
},
|
|
330
|
+
...options
|
|
331
|
+
})(config);
|
|
332
|
+
return {
|
|
333
|
+
...injectedObj,
|
|
334
|
+
type: idOverride ?? PARA_ID,
|
|
335
|
+
name: nameOverride ?? PARA_NAME,
|
|
336
|
+
icon: iconOverride ?? PARA_ICON,
|
|
337
|
+
disconnect: async () => {
|
|
338
|
+
eip1193Provider.closeModal();
|
|
339
|
+
await injectedObj.disconnect();
|
|
340
|
+
para.logout();
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
});
|
|
344
|
+
};
|
|
345
|
+
export {
|
|
346
|
+
ParaEIP1193Provider,
|
|
347
|
+
paraConnector
|
|
348
|
+
};
|
package/dist/index.js.br
CHANGED
|
Binary file
|
package/dist/index.js.gz
CHANGED
|
Binary file
|
package/dist/paraConnector.d.ts
CHANGED
|
@@ -21,17 +21,17 @@ export declare const paraConnector: ({ para, chains: _chains, disableModal, stor
|
|
|
21
21
|
type: string;
|
|
22
22
|
name: string;
|
|
23
23
|
icon: string;
|
|
24
|
-
|
|
24
|
+
disconnect: () => Promise<void>;
|
|
25
|
+
id: string;
|
|
26
|
+
supportsSimulation?: boolean;
|
|
27
|
+
setup?: () => Promise<void>;
|
|
28
|
+
connect: (parameters?: {
|
|
25
29
|
chainId?: number;
|
|
26
30
|
isReconnecting?: boolean;
|
|
27
31
|
}) => Promise<{
|
|
28
32
|
accounts: readonly `0x${string}`[];
|
|
29
33
|
chainId: number;
|
|
30
34
|
}>;
|
|
31
|
-
disconnect: () => Promise<void>;
|
|
32
|
-
id: string;
|
|
33
|
-
supportsSimulation?: boolean;
|
|
34
|
-
setup?: () => Promise<void>;
|
|
35
35
|
getAccounts: () => Promise<readonly `0x${string}`[]>;
|
|
36
36
|
getChainId: () => Promise<number>;
|
|
37
37
|
getProvider: (parameters?: {
|