@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
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MiniKit
|
|
3
|
+
} from "./chunk-EHBM7OXH.js";
|
|
4
|
+
|
|
5
|
+
// src/provider.ts
|
|
6
|
+
import { getAddress } from "viem";
|
|
7
|
+
function _getAddress() {
|
|
8
|
+
if (typeof window === "undefined") return void 0;
|
|
9
|
+
return window.__worldapp_eip1193_address__;
|
|
10
|
+
}
|
|
11
|
+
function _setAddress(addr) {
|
|
12
|
+
if (typeof window === "undefined") return;
|
|
13
|
+
try {
|
|
14
|
+
window.__worldapp_eip1193_address__ = getAddress(addr);
|
|
15
|
+
} catch {
|
|
16
|
+
window.__worldapp_eip1193_address__ = addr;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function _clearAddress() {
|
|
20
|
+
if (typeof window === "undefined") return;
|
|
21
|
+
window.__worldapp_eip1193_address__ = void 0;
|
|
22
|
+
}
|
|
23
|
+
function rpcError(code, message) {
|
|
24
|
+
return Object.assign(new Error(message), { code });
|
|
25
|
+
}
|
|
26
|
+
function isHexString(value) {
|
|
27
|
+
return /^0x[0-9a-fA-F]*$/.test(value);
|
|
28
|
+
}
|
|
29
|
+
function isAddressString(value) {
|
|
30
|
+
return /^0x[0-9a-fA-F]{40}$/.test(value);
|
|
31
|
+
}
|
|
32
|
+
function decodeHexToUtf8(hex) {
|
|
33
|
+
const raw = hex.slice(2);
|
|
34
|
+
if (raw.length % 2 !== 0) {
|
|
35
|
+
throw new Error("Invalid hex string length");
|
|
36
|
+
}
|
|
37
|
+
const bytes = new Uint8Array(raw.length / 2);
|
|
38
|
+
for (let i = 0; i < raw.length; i += 2) {
|
|
39
|
+
bytes[i / 2] = parseInt(raw.slice(i, i + 2), 16);
|
|
40
|
+
}
|
|
41
|
+
return new TextDecoder().decode(bytes);
|
|
42
|
+
}
|
|
43
|
+
function asArrayParams(params) {
|
|
44
|
+
if (params === void 0) return [];
|
|
45
|
+
return Array.isArray(params) ? params : [params];
|
|
46
|
+
}
|
|
47
|
+
function decodeMaybeHexMessage(value) {
|
|
48
|
+
if (!isHexString(value)) {
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return decodeHexToUtf8(value);
|
|
53
|
+
} catch {
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function extractPersonalSignMessage(params) {
|
|
58
|
+
const items = asArrayParams(params);
|
|
59
|
+
if (items.length === 0) {
|
|
60
|
+
throw new Error("Missing personal_sign params");
|
|
61
|
+
}
|
|
62
|
+
const [first, second] = items;
|
|
63
|
+
const maybeMessage = typeof first === "string" && isAddressString(first) && typeof second === "string" ? second : first;
|
|
64
|
+
if (typeof maybeMessage !== "string") {
|
|
65
|
+
throw new Error("Invalid personal_sign message payload");
|
|
66
|
+
}
|
|
67
|
+
return decodeMaybeHexMessage(maybeMessage);
|
|
68
|
+
}
|
|
69
|
+
function extractEthSignMessage(params) {
|
|
70
|
+
const items = asArrayParams(params);
|
|
71
|
+
if (items.length === 0) {
|
|
72
|
+
throw new Error("Missing eth_sign params");
|
|
73
|
+
}
|
|
74
|
+
const [first, second] = items;
|
|
75
|
+
const maybeMessage = typeof second === "string" ? second : typeof first === "string" && !isAddressString(first) ? first : void 0;
|
|
76
|
+
if (typeof maybeMessage !== "string") {
|
|
77
|
+
throw new Error("Invalid eth_sign message payload");
|
|
78
|
+
}
|
|
79
|
+
return decodeMaybeHexMessage(maybeMessage);
|
|
80
|
+
}
|
|
81
|
+
function parseTypedDataInput(params) {
|
|
82
|
+
const items = asArrayParams(params);
|
|
83
|
+
const candidate = items.length > 1 ? items[1] : items[0];
|
|
84
|
+
if (!candidate) {
|
|
85
|
+
throw new Error("Missing typed data payload");
|
|
86
|
+
}
|
|
87
|
+
const parsed = typeof candidate === "string" ? JSON.parse(candidate) : candidate;
|
|
88
|
+
if (!parsed || typeof parsed !== "object" || typeof parsed.primaryType !== "string" || typeof parsed.message !== "object" || !parsed.message || typeof parsed.types !== "object" || !parsed.types) {
|
|
89
|
+
throw new Error("Invalid typed data payload");
|
|
90
|
+
}
|
|
91
|
+
const domainValue = parsed.domain;
|
|
92
|
+
const chainIdValue = domainValue?.chainId ?? parsed.chainId;
|
|
93
|
+
const parsedChainId = typeof chainIdValue === "string" ? Number(chainIdValue) : typeof chainIdValue === "number" ? chainIdValue : void 0;
|
|
94
|
+
return {
|
|
95
|
+
types: parsed.types,
|
|
96
|
+
primaryType: parsed.primaryType,
|
|
97
|
+
domain: domainValue,
|
|
98
|
+
message: parsed.message,
|
|
99
|
+
...Number.isFinite(parsedChainId) ? { chainId: parsedChainId } : {}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function normalizeRpcValue(value) {
|
|
103
|
+
if (value === void 0 || value === null) return void 0;
|
|
104
|
+
if (typeof value === "string") return value;
|
|
105
|
+
if (typeof value === "bigint") return `0x${value.toString(16)}`;
|
|
106
|
+
if (typeof value === "number") return `0x${value.toString(16)}`;
|
|
107
|
+
return String(value);
|
|
108
|
+
}
|
|
109
|
+
function extractTransactionParams(params) {
|
|
110
|
+
const items = asArrayParams(params);
|
|
111
|
+
const tx = items[0] ?? {};
|
|
112
|
+
if (typeof tx.to !== "string" || !isAddressString(tx.to)) {
|
|
113
|
+
throw new Error('Invalid transaction "to" address');
|
|
114
|
+
}
|
|
115
|
+
const chainId = typeof tx.chainId === "string" ? Number(tx.chainId) : typeof tx.chainId === "number" ? tx.chainId : void 0;
|
|
116
|
+
const normalizedValue = normalizeRpcValue(tx.value);
|
|
117
|
+
return {
|
|
118
|
+
to: tx.to,
|
|
119
|
+
...typeof tx.data === "string" ? { data: tx.data } : {},
|
|
120
|
+
...normalizedValue !== void 0 ? { value: normalizedValue } : {},
|
|
121
|
+
...Number.isFinite(chainId) ? { chainId } : {}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function extractSwitchChainId(params) {
|
|
125
|
+
const items = asArrayParams(params);
|
|
126
|
+
const payload = items[0] ?? {};
|
|
127
|
+
const rawChainId = payload.chainId;
|
|
128
|
+
const chainId = typeof rawChainId === "string" ? Number(rawChainId) : typeof rawChainId === "number" ? rawChainId : NaN;
|
|
129
|
+
if (!Number.isFinite(chainId)) {
|
|
130
|
+
throw new Error("Invalid chainId for wallet_switchEthereumChain");
|
|
131
|
+
}
|
|
132
|
+
return chainId;
|
|
133
|
+
}
|
|
134
|
+
function createProvider() {
|
|
135
|
+
const listeners = {};
|
|
136
|
+
function emit(event, ...args) {
|
|
137
|
+
listeners[event]?.forEach((fn) => fn(...args));
|
|
138
|
+
}
|
|
139
|
+
let authInFlight;
|
|
140
|
+
async function doAuth() {
|
|
141
|
+
if (!MiniKit.isInWorldApp() || !MiniKit.isInstalled()) {
|
|
142
|
+
throw rpcError(
|
|
143
|
+
4900,
|
|
144
|
+
"World App provider only works inside World App and must be installed"
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const result = await MiniKit.walletAuth({
|
|
149
|
+
nonce: crypto.randomUUID().replace(/-/g, ""),
|
|
150
|
+
statement: "Sign in with World App"
|
|
151
|
+
});
|
|
152
|
+
_setAddress(result.data.address);
|
|
153
|
+
const addr = _getAddress();
|
|
154
|
+
emit("accountsChanged", [addr]);
|
|
155
|
+
return [addr];
|
|
156
|
+
} catch (e) {
|
|
157
|
+
throw rpcError(4001, `World App wallet auth failed: ${e.message}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
async request({ method, params }) {
|
|
162
|
+
switch (method) {
|
|
163
|
+
case "eth_requestAccounts": {
|
|
164
|
+
const existing = _getAddress();
|
|
165
|
+
if (existing) return [existing];
|
|
166
|
+
if (!authInFlight) {
|
|
167
|
+
authInFlight = doAuth().finally(() => {
|
|
168
|
+
authInFlight = void 0;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return authInFlight;
|
|
172
|
+
}
|
|
173
|
+
case "eth_accounts": {
|
|
174
|
+
const addr = _getAddress();
|
|
175
|
+
return addr ? [addr] : [];
|
|
176
|
+
}
|
|
177
|
+
case "eth_chainId":
|
|
178
|
+
return "0x1e0";
|
|
179
|
+
// 480 = World Chain
|
|
180
|
+
case "personal_sign": {
|
|
181
|
+
const message = extractPersonalSignMessage(params);
|
|
182
|
+
try {
|
|
183
|
+
const result = await MiniKit.signMessage({ message });
|
|
184
|
+
return result.data.signature;
|
|
185
|
+
} catch (e) {
|
|
186
|
+
throw rpcError(4001, `Sign message failed: ${e.message}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
case "eth_sign": {
|
|
190
|
+
const message = extractEthSignMessage(params);
|
|
191
|
+
try {
|
|
192
|
+
const result = await MiniKit.signMessage({ message });
|
|
193
|
+
return result.data.signature;
|
|
194
|
+
} catch (e) {
|
|
195
|
+
throw rpcError(4001, `Sign message failed: ${e.message}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
case "eth_signTypedData":
|
|
199
|
+
case "eth_signTypedData_v3":
|
|
200
|
+
case "eth_signTypedData_v4": {
|
|
201
|
+
try {
|
|
202
|
+
const typedData = parseTypedDataInput(params);
|
|
203
|
+
const result = await MiniKit.signTypedData({
|
|
204
|
+
types: typedData.types,
|
|
205
|
+
primaryType: typedData.primaryType,
|
|
206
|
+
domain: typedData.domain,
|
|
207
|
+
message: typedData.message,
|
|
208
|
+
chainId: typedData.chainId
|
|
209
|
+
});
|
|
210
|
+
if (result.data.status === "error") {
|
|
211
|
+
throw rpcError(
|
|
212
|
+
4001,
|
|
213
|
+
`Sign typed data failed: ${result.data.error_code}`
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
return result.data.signature;
|
|
217
|
+
} catch (e) {
|
|
218
|
+
throw rpcError(4001, `Sign typed data failed: ${e.message}`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
case "eth_sendTransaction": {
|
|
222
|
+
const tx = extractTransactionParams(params);
|
|
223
|
+
if (tx.chainId !== void 0 && tx.chainId !== 480) {
|
|
224
|
+
throw rpcError(4902, "World App only supports World Chain (480)");
|
|
225
|
+
}
|
|
226
|
+
try {
|
|
227
|
+
const result = await MiniKit.sendTransaction({
|
|
228
|
+
chainId: tx.chainId ?? 480,
|
|
229
|
+
transactions: [
|
|
230
|
+
{
|
|
231
|
+
to: tx.to,
|
|
232
|
+
...tx.data && tx.data !== "0x" ? { data: tx.data } : {},
|
|
233
|
+
value: tx.value
|
|
234
|
+
}
|
|
235
|
+
]
|
|
236
|
+
});
|
|
237
|
+
return result.data.userOpHash;
|
|
238
|
+
} catch (e) {
|
|
239
|
+
throw rpcError(4001, `Send transaction failed: ${e.message}`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
case "wallet_switchEthereumChain": {
|
|
243
|
+
const chainId = extractSwitchChainId(params);
|
|
244
|
+
if (chainId !== 480) {
|
|
245
|
+
throw rpcError(4902, "World App only supports World Chain (480)");
|
|
246
|
+
}
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
case "wallet_addEthereumChain": {
|
|
250
|
+
throw rpcError(4200, "World App only supports World Chain (480)");
|
|
251
|
+
}
|
|
252
|
+
default:
|
|
253
|
+
throw rpcError(4200, `Unsupported method: ${method}`);
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
on(event, fn) {
|
|
257
|
+
(listeners[event] ?? (listeners[event] = /* @__PURE__ */ new Set())).add(fn);
|
|
258
|
+
},
|
|
259
|
+
removeListener(event, fn) {
|
|
260
|
+
listeners[event]?.delete(fn);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function getWorldAppProvider() {
|
|
265
|
+
if (typeof window === "undefined") {
|
|
266
|
+
return createProvider();
|
|
267
|
+
}
|
|
268
|
+
if (!window.__worldapp_eip1193_provider__) {
|
|
269
|
+
window.__worldapp_eip1193_provider__ = createProvider();
|
|
270
|
+
}
|
|
271
|
+
return window.__worldapp_eip1193_provider__;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export {
|
|
275
|
+
_getAddress,
|
|
276
|
+
_setAddress,
|
|
277
|
+
_clearAddress,
|
|
278
|
+
getWorldAppProvider
|
|
279
|
+
};
|