From a4a480bcef287748517a6821c642392f8130cc09 Mon Sep 17 00:00:00 2001 From: Igor Osipov Date: Wed, 29 May 2024 20:56:46 +0200 Subject: [PATCH] implement worldAppInit method --- src/minikit.ts | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/minikit.ts b/src/minikit.ts index 6f3f050..8eb73b4 100644 --- a/src/minikit.ts +++ b/src/minikit.ts @@ -19,6 +19,15 @@ import { VerificationLevel } from "@worldcoin/idkit-core"; import { validateWalletAuthCommandInput } from "helpers/siwe/validate-wallet-auth-command-input"; import { generateSiweMessage } from "helpers/siwe/siwe"; +type WorldAppInitInput = { + world_app_version: number; + device_os: "ios|android"; + supported_commands: Array<{ + name: Command; + supported_versions: Array; + }>; +}; + export const sendMiniKitEvent = < T extends WebViewBasePayload = WebViewBasePayload, >( @@ -28,6 +37,14 @@ export const sendMiniKitEvent = < }; export class MiniKit { + private static readonly MINIKIT_VERSION = 1; + + private static readonly commandVersion = { + [Command.Verify]: 1, + [Command.Pay]: 1, + [Command.WalletAuth]: 1, + }; + private static listeners: Record = { [ResponseEvent.MiniAppVerifyAction]: () => {}, [ResponseEvent.MiniAppPayment]: () => {}, @@ -37,10 +54,16 @@ export class MiniKit { private static sendInit() { sendWebviewEvent({ command: "init", - payload: { "minikit-version": 1 }, + payload: { "minikit-version": this.MINIKIT_VERSION }, }); } + private static commandsValid(input: WorldAppInitInput["supported_commands"]) { + return input.every((command) => + command.supported_versions.includes(this.commandVersion[command.name]) + ); + } + public static subscribe( event: E, handler: EventHandler @@ -63,6 +86,7 @@ export class MiniKit { public static install() { if (typeof window !== "undefined" && !Boolean(window.MiniKit)) { + console.log("userAgent: ", window.navigator.userAgent); try { window.MiniKit = MiniKit; this.sendInit(); @@ -86,6 +110,20 @@ export class MiniKit { return true; } + public static worldAppInit(input: WorldAppInitInput) { + if (typeof window === "undefined") { + console.error("worldAppInit can only be called in the browser"); + } + + window.WorldApp = true; + + if (!this.commandsValid(input.supported_commands)) { + throw new Error("Unsupported app version, please, update your app"); + } + + this.sendInit(); + } + public static commands = { verify: (payload: VerifyCommandInput): VerifyCommandPayload => { const timestamp = new Date().toISOString(); @@ -97,7 +135,7 @@ export class MiniKit { }; sendMiniKitEvent({ command: Command.Verify, - version: 1, + version: this.commandVersion[Command.Verify], payload: eventPayload, }); @@ -127,7 +165,7 @@ export class MiniKit { sendMiniKitEvent({ command: Command.Pay, - version: 1, + version: this.commandVersion[Command.Pay], payload: eventPayload, }); @@ -182,7 +220,7 @@ export class MiniKit { sendMiniKitEvent({ command: Command.WalletAuth, - version: 1, + version: this.commandVersion[Command.WalletAuth], payload: walletAuthPayload, });