@worldcoin/minikit-js 2.0.0-dev.0 → 2.0.0-dev.1
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/README.md +4 -1
- package/build/chunk-2UPJKPQ6.js +272 -0
- package/build/{chunk-OTAA7OOI.js → chunk-EHBM7OXH.js} +30 -269
- package/build/chunk-TGXD24YD.js +279 -0
- package/build/{chunk-DIACPBCB.js → chunk-Z2UGRZJ2.js} +241 -298
- package/build/command-exports.cjs +232 -295
- package/build/command-exports.d.cts +14 -11
- package/build/command-exports.d.ts +14 -11
- package/build/command-exports.js +9 -1
- package/build/connector/index.cjs +333 -120
- package/build/connector/index.js +8 -4
- package/build/index.cjs +244 -307
- package/build/index.d.cts +16 -7
- package/build/index.d.ts +16 -7
- package/build/index.js +5 -3
- package/build/minikit-provider.cjs +312 -123
- package/build/minikit-provider.js +9 -2040
- package/build/siwe-exports.d.cts +1 -1
- package/build/siwe-exports.d.ts +1 -1
- package/build/{types-DO2UGrgp.d.cts → types-CC2x79HX.d.ts} +125 -38
- package/build/{types-CKn5C-Ro.d.ts → types-CSyzFDPt.d.cts} +4 -1
- package/build/{types-CKn5C-Ro.d.cts → types-CSyzFDPt.d.ts} +4 -1
- package/build/{types-CGVVuGiN.d.ts → types-_jfLbcJW.d.cts} +125 -38
- package/package.json +12 -13
package/README.md
CHANGED
|
@@ -86,7 +86,10 @@ import {
|
|
|
86
86
|
SendTransactionErrorCodes,
|
|
87
87
|
} from '@worldcoin/minikit-js/commands';
|
|
88
88
|
import { getIsUserVerified } from '@worldcoin/minikit-js/address-book';
|
|
89
|
-
import {
|
|
89
|
+
import {
|
|
90
|
+
parseSiweMessage,
|
|
91
|
+
verifySiweMessage,
|
|
92
|
+
} from '@worldcoin/minikit-js/siwe';
|
|
90
93
|
```
|
|
91
94
|
|
|
92
95
|
You can still import `MiniKit` itself from the package root:
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import {
|
|
2
|
+
setFallbackAdapter
|
|
3
|
+
} from "./chunk-Z2UGRZJ2.js";
|
|
4
|
+
|
|
5
|
+
// src/commands/wagmi-fallback.ts
|
|
6
|
+
var SIWE_NONCE_REGEX = /^[a-zA-Z0-9]{8,}$/;
|
|
7
|
+
var WAGMI_KEY = "__minikit_wagmi_config__";
|
|
8
|
+
function setWagmiConfig(config) {
|
|
9
|
+
globalThis[WAGMI_KEY] = config;
|
|
10
|
+
registerWagmiFallbacks();
|
|
11
|
+
}
|
|
12
|
+
function getWagmiConfig() {
|
|
13
|
+
return globalThis[WAGMI_KEY];
|
|
14
|
+
}
|
|
15
|
+
function hasWagmiConfig() {
|
|
16
|
+
return globalThis[WAGMI_KEY] !== void 0;
|
|
17
|
+
}
|
|
18
|
+
function registerWagmiFallbacks() {
|
|
19
|
+
setFallbackAdapter({
|
|
20
|
+
walletAuth: wagmiWalletAuth,
|
|
21
|
+
signMessage: wagmiSignMessage,
|
|
22
|
+
signTypedData: wagmiSignTypedData,
|
|
23
|
+
sendTransaction: wagmiSendTransaction
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async function loadWagmiActions() {
|
|
27
|
+
console.log("[MiniKit WagmiFallback] loadWagmiActions:start", {
|
|
28
|
+
hasWindow: typeof window !== "undefined",
|
|
29
|
+
hasWagmiConfig: hasWagmiConfig()
|
|
30
|
+
});
|
|
31
|
+
try {
|
|
32
|
+
const actions = await import(
|
|
33
|
+
/* webpackIgnore: true */
|
|
34
|
+
"wagmi/actions"
|
|
35
|
+
);
|
|
36
|
+
console.log("[MiniKit WagmiFallback] loadWagmiActions:success");
|
|
37
|
+
return actions;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.log("[MiniKit WagmiFallback] loadWagmiActions:error", {
|
|
40
|
+
message: error instanceof Error ? error.message : String(error)
|
|
41
|
+
});
|
|
42
|
+
const wrappedError = new Error(
|
|
43
|
+
'Wagmi fallback requires the "wagmi" package. Install wagmi or provide a custom fallback.'
|
|
44
|
+
);
|
|
45
|
+
wrappedError.cause = error;
|
|
46
|
+
throw wrappedError;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function loadSiwe() {
|
|
50
|
+
try {
|
|
51
|
+
return await import(
|
|
52
|
+
/* webpackIgnore: true */
|
|
53
|
+
"siwe"
|
|
54
|
+
);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
const wrappedError = new Error(
|
|
57
|
+
'Wagmi walletAuth fallback requires the "siwe" package. Install siwe or provide a custom fallback.'
|
|
58
|
+
);
|
|
59
|
+
wrappedError.cause = error;
|
|
60
|
+
throw wrappedError;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function checksumAddress(addr) {
|
|
64
|
+
try {
|
|
65
|
+
const { getAddress } = await import(
|
|
66
|
+
/* webpackIgnore: true */
|
|
67
|
+
"viem"
|
|
68
|
+
);
|
|
69
|
+
return getAddress(addr);
|
|
70
|
+
} catch {
|
|
71
|
+
return addr;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function ensureConnected(config) {
|
|
75
|
+
const { connect, getConnections } = await loadWagmiActions();
|
|
76
|
+
const isWorldApp = typeof window !== "undefined" && Boolean(window.WorldApp);
|
|
77
|
+
const existingConnection = getConnections(config).find(
|
|
78
|
+
(connection) => connection.accounts && connection.accounts.length > 0 && (isWorldApp || connection.connector?.id !== "worldApp")
|
|
79
|
+
);
|
|
80
|
+
if (existingConnection && existingConnection.accounts) {
|
|
81
|
+
return checksumAddress(existingConnection.accounts[0]);
|
|
82
|
+
}
|
|
83
|
+
const connectors = config.connectors;
|
|
84
|
+
if (!connectors || connectors.length === 0) {
|
|
85
|
+
throw new Error("No Wagmi connectors configured");
|
|
86
|
+
}
|
|
87
|
+
const candidateConnectors = isWorldApp ? connectors : connectors.filter(
|
|
88
|
+
(connector) => connector.id !== "worldApp"
|
|
89
|
+
);
|
|
90
|
+
if (!isWorldApp && candidateConnectors.length === 0) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
"No web Wagmi connectors configured. Add a web connector (e.g. injected or walletConnect) after worldApp()."
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
const selectedConnector = candidateConnectors[0];
|
|
96
|
+
try {
|
|
97
|
+
const result = await connect(config, { connector: selectedConnector });
|
|
98
|
+
if (result.accounts.length > 0) {
|
|
99
|
+
const account = result.accounts[0];
|
|
100
|
+
const address = typeof account === "string" ? account : account.address;
|
|
101
|
+
if (address) {
|
|
102
|
+
return checksumAddress(address);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
} catch (error) {
|
|
106
|
+
const connectorId = selectedConnector.id ?? "unknown";
|
|
107
|
+
const wrappedError = new Error(
|
|
108
|
+
`Failed to connect with connector "${connectorId}". Reorder connectors to change the default connector.`
|
|
109
|
+
);
|
|
110
|
+
wrappedError.cause = error;
|
|
111
|
+
throw wrappedError;
|
|
112
|
+
}
|
|
113
|
+
throw new Error("Failed to connect wallet");
|
|
114
|
+
}
|
|
115
|
+
async function wagmiWalletAuth(params) {
|
|
116
|
+
console.log("[MiniKit WagmiFallback] walletAuth:start", {
|
|
117
|
+
hasWagmiConfig: hasWagmiConfig(),
|
|
118
|
+
nonceLength: params.nonce?.length ?? 0
|
|
119
|
+
});
|
|
120
|
+
const config = getWagmiConfig();
|
|
121
|
+
if (!config) {
|
|
122
|
+
console.log("[MiniKit WagmiFallback] walletAuth:error:no-config");
|
|
123
|
+
throw new Error(
|
|
124
|
+
"Wagmi config not available. Pass wagmiConfig to MiniKitProvider."
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const { signMessage } = await loadWagmiActions();
|
|
128
|
+
const { SiweMessage } = await loadSiwe();
|
|
129
|
+
const address = await ensureConnected(config);
|
|
130
|
+
if (!SIWE_NONCE_REGEX.test(params.nonce)) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
"Invalid nonce: must be alphanumeric and at least 8 characters (EIP-4361)"
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
const siweMessage = new SiweMessage({
|
|
136
|
+
domain: typeof window !== "undefined" ? window.location.host : "localhost",
|
|
137
|
+
address,
|
|
138
|
+
statement: params.statement,
|
|
139
|
+
uri: typeof window !== "undefined" ? window.location.origin : "http://localhost",
|
|
140
|
+
version: "1",
|
|
141
|
+
chainId: 480,
|
|
142
|
+
// World Chain
|
|
143
|
+
nonce: params.nonce,
|
|
144
|
+
expirationTime: params.expirationTime?.toISOString()
|
|
145
|
+
});
|
|
146
|
+
const message = siweMessage.prepareMessage();
|
|
147
|
+
const signature = await signMessage(config, { message });
|
|
148
|
+
return {
|
|
149
|
+
address,
|
|
150
|
+
message,
|
|
151
|
+
signature
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
async function wagmiSignMessage(params) {
|
|
155
|
+
console.log("[MiniKit WagmiFallback] signMessage:start", {
|
|
156
|
+
hasWagmiConfig: hasWagmiConfig()
|
|
157
|
+
});
|
|
158
|
+
const config = getWagmiConfig();
|
|
159
|
+
if (!config) {
|
|
160
|
+
console.log("[MiniKit WagmiFallback] signMessage:error:no-config");
|
|
161
|
+
throw new Error(
|
|
162
|
+
"Wagmi config not available. Pass wagmiConfig to MiniKitProvider."
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
const { signMessage } = await loadWagmiActions();
|
|
166
|
+
const address = await ensureConnected(config);
|
|
167
|
+
const signature = await signMessage(config, {
|
|
168
|
+
account: address,
|
|
169
|
+
message: params.message
|
|
170
|
+
});
|
|
171
|
+
return {
|
|
172
|
+
status: "success",
|
|
173
|
+
version: 1,
|
|
174
|
+
signature,
|
|
175
|
+
address
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
async function wagmiSignTypedData(params) {
|
|
179
|
+
console.log("[MiniKit WagmiFallback] signTypedData:start", {
|
|
180
|
+
hasWagmiConfig: hasWagmiConfig(),
|
|
181
|
+
hasChainId: params.chainId !== void 0
|
|
182
|
+
});
|
|
183
|
+
const config = getWagmiConfig();
|
|
184
|
+
if (!config) {
|
|
185
|
+
console.log("[MiniKit WagmiFallback] signTypedData:error:no-config");
|
|
186
|
+
throw new Error(
|
|
187
|
+
"Wagmi config not available. Pass wagmiConfig to MiniKitProvider."
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
const { getChainId, signTypedData, switchChain } = await loadWagmiActions();
|
|
191
|
+
const address = await ensureConnected(config);
|
|
192
|
+
if (params.chainId !== void 0) {
|
|
193
|
+
const currentChainId = await getChainId(config);
|
|
194
|
+
if (currentChainId !== params.chainId) {
|
|
195
|
+
await switchChain(config, { chainId: params.chainId });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const signature = await signTypedData(config, {
|
|
199
|
+
account: address,
|
|
200
|
+
types: params.types,
|
|
201
|
+
primaryType: params.primaryType,
|
|
202
|
+
domain: params.domain,
|
|
203
|
+
message: params.message
|
|
204
|
+
});
|
|
205
|
+
return {
|
|
206
|
+
status: "success",
|
|
207
|
+
version: 1,
|
|
208
|
+
signature,
|
|
209
|
+
address
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function isChainMismatchError(error) {
|
|
213
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
214
|
+
return message.includes("does not match the target chain");
|
|
215
|
+
}
|
|
216
|
+
async function wagmiSendTransaction(params) {
|
|
217
|
+
console.log("[MiniKit WagmiFallback] sendTransaction:start", {
|
|
218
|
+
hasWagmiConfig: hasWagmiConfig(),
|
|
219
|
+
chainId: params.chainId,
|
|
220
|
+
hasData: Boolean(params.transaction.data)
|
|
221
|
+
});
|
|
222
|
+
const config = getWagmiConfig();
|
|
223
|
+
if (!config) {
|
|
224
|
+
console.log("[MiniKit WagmiFallback] sendTransaction:error:no-config");
|
|
225
|
+
throw new Error(
|
|
226
|
+
"Wagmi config not available. Pass wagmiConfig to MiniKitProvider."
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
const { getChainId, getWalletClient, sendTransaction, switchChain } = await loadWagmiActions();
|
|
230
|
+
await ensureConnected(config);
|
|
231
|
+
const targetChainId = params.chainId ?? config.chains?.[0]?.id;
|
|
232
|
+
const ensureTargetChain = async () => {
|
|
233
|
+
if (targetChainId === void 0) return;
|
|
234
|
+
const currentChainId = await getChainId(config);
|
|
235
|
+
if (currentChainId !== targetChainId) {
|
|
236
|
+
await switchChain(config, { chainId: targetChainId });
|
|
237
|
+
}
|
|
238
|
+
const walletClient = await getWalletClient(config);
|
|
239
|
+
const providerChainId = walletClient ? await walletClient.getChainId() : await getChainId(config);
|
|
240
|
+
if (providerChainId !== targetChainId) {
|
|
241
|
+
throw new Error(
|
|
242
|
+
`Wallet network mismatch: expected chain ${targetChainId}, got ${providerChainId}. Please switch networks in your wallet and retry.`
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
await ensureTargetChain();
|
|
247
|
+
let transactionHash;
|
|
248
|
+
try {
|
|
249
|
+
transactionHash = await sendTransaction(config, {
|
|
250
|
+
chainId: targetChainId,
|
|
251
|
+
to: params.transaction.address,
|
|
252
|
+
data: params.transaction.data,
|
|
253
|
+
value: params.transaction.value ? BigInt(params.transaction.value) : void 0
|
|
254
|
+
});
|
|
255
|
+
} catch (error) {
|
|
256
|
+
if (targetChainId === void 0 || !isChainMismatchError(error)) {
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
await ensureTargetChain();
|
|
260
|
+
transactionHash = await sendTransaction(config, {
|
|
261
|
+
chainId: targetChainId,
|
|
262
|
+
to: params.transaction.address,
|
|
263
|
+
data: params.transaction.data,
|
|
264
|
+
value: params.transaction.value ? BigInt(params.transaction.value) : void 0
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
return { transactionHash };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export {
|
|
271
|
+
setWagmiConfig
|
|
272
|
+
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
EventManager,
|
|
3
3
|
MiniKitInstallErrorMessage,
|
|
4
|
+
attestation,
|
|
4
5
|
chat,
|
|
6
|
+
closeMiniApp,
|
|
5
7
|
getPermissions,
|
|
6
8
|
isInWorldApp,
|
|
7
9
|
pay,
|
|
@@ -15,7 +17,7 @@ import {
|
|
|
15
17
|
signTypedData,
|
|
16
18
|
validateCommands,
|
|
17
19
|
walletAuth
|
|
18
|
-
} from "./chunk-
|
|
20
|
+
} from "./chunk-Z2UGRZJ2.js";
|
|
19
21
|
|
|
20
22
|
// src/helpers/microphone.ts
|
|
21
23
|
var microphoneSetupDone = false;
|
|
@@ -175,11 +177,10 @@ var _MiniKit = class _MiniKit {
|
|
|
175
177
|
* ```typescript
|
|
176
178
|
* const result = await MiniKit.sendTransaction({
|
|
177
179
|
* chainId: 480,
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
* args: [],
|
|
180
|
+
* transactions: [{
|
|
181
|
+
* to: '0x...',
|
|
182
|
+
* data: '0x...',
|
|
183
|
+
* value: '0x0',
|
|
183
184
|
* }],
|
|
184
185
|
* });
|
|
185
186
|
* ```
|
|
@@ -304,6 +305,26 @@ var _MiniKit = class _MiniKit {
|
|
|
304
305
|
}
|
|
305
306
|
return sendHapticFeedback(options, this.getContext());
|
|
306
307
|
}
|
|
308
|
+
/**
|
|
309
|
+
* Request app attestation token for a request hash
|
|
310
|
+
*/
|
|
311
|
+
static attestation(options) {
|
|
312
|
+
const active = this.getActiveMiniKit();
|
|
313
|
+
if (active !== this) {
|
|
314
|
+
return active.attestation(options);
|
|
315
|
+
}
|
|
316
|
+
return attestation(options, this.getContext());
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Close the mini app
|
|
320
|
+
*/
|
|
321
|
+
static closeMiniApp(options = {}) {
|
|
322
|
+
const active = this.getActiveMiniKit();
|
|
323
|
+
if (active !== this) {
|
|
324
|
+
return active.closeMiniApp(options);
|
|
325
|
+
}
|
|
326
|
+
return closeMiniApp(options, this.getContext());
|
|
327
|
+
}
|
|
307
328
|
// ============================================================================
|
|
308
329
|
// Public State Accessors
|
|
309
330
|
// ============================================================================
|
|
@@ -482,6 +503,8 @@ var _MiniKit = class _MiniKit {
|
|
|
482
503
|
* - `MiniKit.commands.getPermissions()` → `await MiniKit.getPermissions()`
|
|
483
504
|
* - `MiniKit.commands.requestPermission(payload)` → `await MiniKit.requestPermission(input)`
|
|
484
505
|
* - `MiniKit.commands.sendHapticFeedback(payload)` → `await MiniKit.sendHapticFeedback(input)`
|
|
506
|
+
* - `MiniKit.commands.attestation(payload)` → `await MiniKit.attestation(options)`
|
|
507
|
+
* - `MiniKit.commands.closeMiniApp()` → `await MiniKit.closeMiniApp()`
|
|
485
508
|
*/
|
|
486
509
|
static get commands() {
|
|
487
510
|
throw new Error(
|
|
@@ -568,268 +591,6 @@ _MiniKit.showProfileCard = (username, walletAddress) => {
|
|
|
568
591
|
};
|
|
569
592
|
var MiniKit = _MiniKit;
|
|
570
593
|
|
|
571
|
-
// src/provider.ts
|
|
572
|
-
function _getAddress() {
|
|
573
|
-
if (typeof window === "undefined") return void 0;
|
|
574
|
-
return window.__worldapp_eip1193_address__;
|
|
575
|
-
}
|
|
576
|
-
function _setAddress(addr) {
|
|
577
|
-
if (typeof window === "undefined") return;
|
|
578
|
-
window.__worldapp_eip1193_address__ = addr;
|
|
579
|
-
}
|
|
580
|
-
function _clearAddress() {
|
|
581
|
-
if (typeof window === "undefined") return;
|
|
582
|
-
window.__worldapp_eip1193_address__ = void 0;
|
|
583
|
-
}
|
|
584
|
-
function rpcError(code, message) {
|
|
585
|
-
return Object.assign(new Error(message), { code });
|
|
586
|
-
}
|
|
587
|
-
function isHexString(value) {
|
|
588
|
-
return /^0x[0-9a-fA-F]*$/.test(value);
|
|
589
|
-
}
|
|
590
|
-
function isAddressString(value) {
|
|
591
|
-
return /^0x[0-9a-fA-F]{40}$/.test(value);
|
|
592
|
-
}
|
|
593
|
-
function decodeHexToUtf8(hex) {
|
|
594
|
-
const raw = hex.slice(2);
|
|
595
|
-
if (raw.length % 2 !== 0) {
|
|
596
|
-
throw new Error("Invalid hex string length");
|
|
597
|
-
}
|
|
598
|
-
const bytes = new Uint8Array(raw.length / 2);
|
|
599
|
-
for (let i = 0; i < raw.length; i += 2) {
|
|
600
|
-
bytes[i / 2] = parseInt(raw.slice(i, i + 2), 16);
|
|
601
|
-
}
|
|
602
|
-
return new TextDecoder().decode(bytes);
|
|
603
|
-
}
|
|
604
|
-
function asArrayParams(params) {
|
|
605
|
-
if (params === void 0) return [];
|
|
606
|
-
return Array.isArray(params) ? params : [params];
|
|
607
|
-
}
|
|
608
|
-
function decodeMaybeHexMessage(value) {
|
|
609
|
-
if (!isHexString(value)) {
|
|
610
|
-
return value;
|
|
611
|
-
}
|
|
612
|
-
try {
|
|
613
|
-
return decodeHexToUtf8(value);
|
|
614
|
-
} catch {
|
|
615
|
-
return value;
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
function extractPersonalSignMessage(params) {
|
|
619
|
-
const items = asArrayParams(params);
|
|
620
|
-
if (items.length === 0) {
|
|
621
|
-
throw new Error("Missing personal_sign params");
|
|
622
|
-
}
|
|
623
|
-
const [first, second] = items;
|
|
624
|
-
const maybeMessage = typeof first === "string" && isAddressString(first) && typeof second === "string" ? second : first;
|
|
625
|
-
if (typeof maybeMessage !== "string") {
|
|
626
|
-
throw new Error("Invalid personal_sign message payload");
|
|
627
|
-
}
|
|
628
|
-
return decodeMaybeHexMessage(maybeMessage);
|
|
629
|
-
}
|
|
630
|
-
function extractEthSignMessage(params) {
|
|
631
|
-
const items = asArrayParams(params);
|
|
632
|
-
if (items.length === 0) {
|
|
633
|
-
throw new Error("Missing eth_sign params");
|
|
634
|
-
}
|
|
635
|
-
const [first, second] = items;
|
|
636
|
-
const maybeMessage = typeof second === "string" ? second : typeof first === "string" && !isAddressString(first) ? first : void 0;
|
|
637
|
-
if (typeof maybeMessage !== "string") {
|
|
638
|
-
throw new Error("Invalid eth_sign message payload");
|
|
639
|
-
}
|
|
640
|
-
return decodeMaybeHexMessage(maybeMessage);
|
|
641
|
-
}
|
|
642
|
-
function parseTypedDataInput(params) {
|
|
643
|
-
const items = asArrayParams(params);
|
|
644
|
-
const candidate = items.length > 1 ? items[1] : items[0];
|
|
645
|
-
if (!candidate) {
|
|
646
|
-
throw new Error("Missing typed data payload");
|
|
647
|
-
}
|
|
648
|
-
const parsed = typeof candidate === "string" ? JSON.parse(candidate) : candidate;
|
|
649
|
-
if (!parsed || typeof parsed !== "object" || typeof parsed.primaryType !== "string" || typeof parsed.message !== "object" || !parsed.message || typeof parsed.types !== "object" || !parsed.types) {
|
|
650
|
-
throw new Error("Invalid typed data payload");
|
|
651
|
-
}
|
|
652
|
-
const domainValue = parsed.domain;
|
|
653
|
-
const chainIdValue = domainValue?.chainId ?? parsed.chainId;
|
|
654
|
-
const parsedChainId = typeof chainIdValue === "string" ? Number(chainIdValue) : typeof chainIdValue === "number" ? chainIdValue : void 0;
|
|
655
|
-
return {
|
|
656
|
-
types: parsed.types,
|
|
657
|
-
primaryType: parsed.primaryType,
|
|
658
|
-
domain: domainValue,
|
|
659
|
-
message: parsed.message,
|
|
660
|
-
...Number.isFinite(parsedChainId) ? { chainId: parsedChainId } : {}
|
|
661
|
-
};
|
|
662
|
-
}
|
|
663
|
-
function normalizeRpcValue(value) {
|
|
664
|
-
if (value === void 0 || value === null) return void 0;
|
|
665
|
-
if (typeof value === "string") return value;
|
|
666
|
-
if (typeof value === "bigint") return `0x${value.toString(16)}`;
|
|
667
|
-
if (typeof value === "number") return `0x${value.toString(16)}`;
|
|
668
|
-
return String(value);
|
|
669
|
-
}
|
|
670
|
-
function extractTransactionParams(params) {
|
|
671
|
-
const items = asArrayParams(params);
|
|
672
|
-
const tx = items[0] ?? {};
|
|
673
|
-
if (typeof tx.to !== "string" || !isAddressString(tx.to)) {
|
|
674
|
-
throw new Error('Invalid transaction "to" address');
|
|
675
|
-
}
|
|
676
|
-
const chainId = typeof tx.chainId === "string" ? Number(tx.chainId) : typeof tx.chainId === "number" ? tx.chainId : void 0;
|
|
677
|
-
const normalizedValue = normalizeRpcValue(tx.value);
|
|
678
|
-
return {
|
|
679
|
-
to: tx.to,
|
|
680
|
-
...typeof tx.data === "string" ? { data: tx.data } : {},
|
|
681
|
-
...normalizedValue !== void 0 ? { value: normalizedValue } : {},
|
|
682
|
-
...Number.isFinite(chainId) ? { chainId } : {}
|
|
683
|
-
};
|
|
684
|
-
}
|
|
685
|
-
function extractSwitchChainId(params) {
|
|
686
|
-
const items = asArrayParams(params);
|
|
687
|
-
const payload = items[0] ?? {};
|
|
688
|
-
const rawChainId = payload.chainId;
|
|
689
|
-
const chainId = typeof rawChainId === "string" ? Number(rawChainId) : typeof rawChainId === "number" ? rawChainId : NaN;
|
|
690
|
-
if (!Number.isFinite(chainId)) {
|
|
691
|
-
throw new Error("Invalid chainId for wallet_switchEthereumChain");
|
|
692
|
-
}
|
|
693
|
-
return chainId;
|
|
694
|
-
}
|
|
695
|
-
function createProvider() {
|
|
696
|
-
const listeners = {};
|
|
697
|
-
function emit(event, ...args) {
|
|
698
|
-
listeners[event]?.forEach((fn) => fn(...args));
|
|
699
|
-
}
|
|
700
|
-
let authInFlight;
|
|
701
|
-
async function doAuth() {
|
|
702
|
-
if (!MiniKit.isInWorldApp()) {
|
|
703
|
-
throw rpcError(4900, "World App provider only works inside World App");
|
|
704
|
-
}
|
|
705
|
-
try {
|
|
706
|
-
const result = await MiniKit.walletAuth({
|
|
707
|
-
nonce: crypto.randomUUID().replace(/-/g, ""),
|
|
708
|
-
statement: "Sign in with World App"
|
|
709
|
-
});
|
|
710
|
-
const addr = result.data.address;
|
|
711
|
-
_setAddress(addr);
|
|
712
|
-
emit("accountsChanged", [addr]);
|
|
713
|
-
return [addr];
|
|
714
|
-
} catch (e) {
|
|
715
|
-
throw rpcError(4001, `World App wallet auth failed: ${e.message}`);
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
return {
|
|
719
|
-
async request({ method, params }) {
|
|
720
|
-
switch (method) {
|
|
721
|
-
case "eth_requestAccounts": {
|
|
722
|
-
const existing = _getAddress();
|
|
723
|
-
if (existing) return [existing];
|
|
724
|
-
if (!authInFlight) {
|
|
725
|
-
authInFlight = doAuth().finally(() => {
|
|
726
|
-
authInFlight = void 0;
|
|
727
|
-
});
|
|
728
|
-
}
|
|
729
|
-
return authInFlight;
|
|
730
|
-
}
|
|
731
|
-
case "eth_accounts": {
|
|
732
|
-
const addr = _getAddress();
|
|
733
|
-
return addr ? [addr] : [];
|
|
734
|
-
}
|
|
735
|
-
case "eth_chainId":
|
|
736
|
-
return "0x1e0";
|
|
737
|
-
// 480 = World Chain
|
|
738
|
-
case "personal_sign": {
|
|
739
|
-
const message = extractPersonalSignMessage(params);
|
|
740
|
-
try {
|
|
741
|
-
const result = await MiniKit.signMessage({ message });
|
|
742
|
-
return result.data.signature;
|
|
743
|
-
} catch (e) {
|
|
744
|
-
throw rpcError(4001, `Sign message failed: ${e.message}`);
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
case "eth_sign": {
|
|
748
|
-
const message = extractEthSignMessage(params);
|
|
749
|
-
try {
|
|
750
|
-
const result = await MiniKit.signMessage({ message });
|
|
751
|
-
return result.data.signature;
|
|
752
|
-
} catch (e) {
|
|
753
|
-
throw rpcError(4001, `Sign message failed: ${e.message}`);
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
case "eth_signTypedData":
|
|
757
|
-
case "eth_signTypedData_v3":
|
|
758
|
-
case "eth_signTypedData_v4": {
|
|
759
|
-
try {
|
|
760
|
-
const typedData = parseTypedDataInput(params);
|
|
761
|
-
const result = await MiniKit.signTypedData({
|
|
762
|
-
types: typedData.types,
|
|
763
|
-
primaryType: typedData.primaryType,
|
|
764
|
-
domain: typedData.domain,
|
|
765
|
-
message: typedData.message,
|
|
766
|
-
chainId: typedData.chainId
|
|
767
|
-
});
|
|
768
|
-
if (result.data.status === "error") {
|
|
769
|
-
throw rpcError(
|
|
770
|
-
4001,
|
|
771
|
-
`Sign typed data failed: ${result.data.error_code}`
|
|
772
|
-
);
|
|
773
|
-
}
|
|
774
|
-
return result.data.signature;
|
|
775
|
-
} catch (e) {
|
|
776
|
-
throw rpcError(4001, `Sign typed data failed: ${e.message}`);
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
case "eth_sendTransaction": {
|
|
780
|
-
const tx = extractTransactionParams(params);
|
|
781
|
-
try {
|
|
782
|
-
const result = await MiniKit.sendTransaction({
|
|
783
|
-
...tx.chainId !== void 0 ? { chainId: tx.chainId } : {},
|
|
784
|
-
transaction: [
|
|
785
|
-
{
|
|
786
|
-
address: tx.to,
|
|
787
|
-
...tx.data && tx.data !== "0x" ? { data: tx.data } : {},
|
|
788
|
-
value: tx.value
|
|
789
|
-
}
|
|
790
|
-
]
|
|
791
|
-
});
|
|
792
|
-
return result.data.transactionId;
|
|
793
|
-
} catch (e) {
|
|
794
|
-
throw rpcError(4001, `Send transaction failed: ${e.message}`);
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
case "wallet_switchEthereumChain": {
|
|
798
|
-
const chainId = extractSwitchChainId(params);
|
|
799
|
-
if (chainId !== 480) {
|
|
800
|
-
throw rpcError(4902, "World App only supports World Chain (480)");
|
|
801
|
-
}
|
|
802
|
-
return null;
|
|
803
|
-
}
|
|
804
|
-
case "wallet_addEthereumChain": {
|
|
805
|
-
throw rpcError(4200, "World App only supports World Chain (480)");
|
|
806
|
-
}
|
|
807
|
-
default:
|
|
808
|
-
throw rpcError(4200, `Unsupported method: ${method}`);
|
|
809
|
-
}
|
|
810
|
-
},
|
|
811
|
-
on(event, fn) {
|
|
812
|
-
(listeners[event] ?? (listeners[event] = /* @__PURE__ */ new Set())).add(fn);
|
|
813
|
-
},
|
|
814
|
-
removeListener(event, fn) {
|
|
815
|
-
listeners[event]?.delete(fn);
|
|
816
|
-
}
|
|
817
|
-
};
|
|
818
|
-
}
|
|
819
|
-
function getWorldAppProvider() {
|
|
820
|
-
if (typeof window === "undefined") {
|
|
821
|
-
return createProvider();
|
|
822
|
-
}
|
|
823
|
-
if (!window.__worldapp_eip1193_provider__) {
|
|
824
|
-
window.__worldapp_eip1193_provider__ = createProvider();
|
|
825
|
-
}
|
|
826
|
-
return window.__worldapp_eip1193_provider__;
|
|
827
|
-
}
|
|
828
|
-
|
|
829
594
|
export {
|
|
830
|
-
MiniKit
|
|
831
|
-
_getAddress,
|
|
832
|
-
_setAddress,
|
|
833
|
-
_clearAddress,
|
|
834
|
-
getWorldAppProvider
|
|
595
|
+
MiniKit
|
|
835
596
|
};
|