@worldcoin/minikit-js 2.0.0-dev.0 → 2.0.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.
@@ -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-DIACPBCB.js";
20
+ } from "./chunk-XHYUUG6Y.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
- * transaction: [{
179
- * address: '0x...',
180
- * abi: ContractABI,
181
- * functionName: 'mint',
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
  };