Skip to content

Commit

Permalink
implement worldAppInit method
Browse files Browse the repository at this point in the history
  • Loading branch information
igorosip0v committed May 29, 2024
1 parent e791e47 commit a4a480b
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions src/minikit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>;
}>;
};

export const sendMiniKitEvent = <
T extends WebViewBasePayload = WebViewBasePayload,
>(
Expand All @@ -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, EventHandler> = {
[ResponseEvent.MiniAppVerifyAction]: () => {},
[ResponseEvent.MiniAppPayment]: () => {},
Expand All @@ -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<E extends ResponseEvent>(
event: E,
handler: EventHandler<E>
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -97,7 +135,7 @@ export class MiniKit {
};
sendMiniKitEvent({
command: Command.Verify,
version: 1,
version: this.commandVersion[Command.Verify],
payload: eventPayload,
});

Expand Down Expand Up @@ -127,7 +165,7 @@ export class MiniKit {

sendMiniKitEvent<WebViewBasePayload>({
command: Command.Pay,
version: 1,
version: this.commandVersion[Command.Pay],
payload: eventPayload,
});

Expand Down Expand Up @@ -182,7 +220,7 @@ export class MiniKit {

sendMiniKitEvent<WebViewBasePayload>({
command: Command.WalletAuth,
version: 1,
version: this.commandVersion[Command.WalletAuth],
payload: walletAuthPayload,
});

Expand Down

0 comments on commit a4a480b

Please sign in to comment.