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