Skip to content

Commit

Permalink
0.31 Minikit: Improve init logic (#70)
Browse files Browse the repository at this point in the history
* update links

* double sub domain

* re organise install
  • Loading branch information
andy-t-wang authored Sep 25, 2024
1 parent 4a12247 commit 8a9e982
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 22 deletions.
24 changes: 20 additions & 4 deletions demo/with-next/components/ClientContent/ExternalLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,45 @@ export const ExternalLinks = () => {
<p className="text-xl font-bold">Test External Links</p>
<div className="grid md:grid-cols-2 gap-4">
<Link
href="https://worldcoin.org/apps"
href="https://worldcoin.org/world-chain"
className="bg-green-500 text-white text-center rounded-lg p-3"
>
Valid Associated Domain (Link)
</Link>
<button
onClick={() => window.open("https://worldcoin.org/apps")}
onClick={() => window.open("https://worldcoin.org/world-chain")}
className="text-white bg-green-500 hover:bg-blue-300 transition p-4 leading-[1] rounded-lg"
>
Valid Associated Domain (Button)
</button>
<Link
href="https://whitepaper.worldcoin.org"
href="https://docs.worldcoin.org"
className="bg-green-500 text-white text-center rounded-lg p-3"
>
Valid Subdomain (Link)
</Link>
<button
onClick={() => window.open("https://whitepaper.worldcoin.org")}
onClick={() => window.open("https://docs.worldcoin.org")}
className="text-white bg-green-500 transition p-4 leading-[1] rounded-lg"
>
Valid Subdomain (Button)
</button>
<Link
href="https://worldcoin-developer-portal.eu.auth0.com/u/login/identifier?state=hKFo2SBHakd3VGFGMllRb0E4QVJMY3pDeS04VG5lWGNwTFVkdKFur3VuaXZlcnNhbC1sb2dpbqN0aWTZIEpFNTd0NjdIOTZLVzdVMG5FVXdrQ0dHUV92dHVwaE1ho2NpZNkgbU1vVGJqbFI4RmZYVEJrU05nVzk3d1BxR2tmUmNWUk8"
className="bg-green-500 text-white text-center rounded-lg p-3"
>
Valid double Subdomain (Link)
</Link>
<button
onClick={() =>
window.open(
"https://worldcoin-developer-portal.eu.auth0.com/u/login/identifier?state=hKFo2SBHakd3VGFGMllRb0E4QVJMY3pDeS04VG5lWGNwTFVkdKFur3VuaXZlcnNhbC1sb2dpbqN0aWTZIEpFNTd0NjdIOTZLVzdVMG5FVXdrQ0dHUV92dHVwaE1ho2NpZNkgbU1vVGJqbFI4RmZYVEJrU05nVzk3d1BxR2tmUmNWUk8"
)
}
className="text-white bg-green-500 transition p-4 leading-[1] rounded-lg"
>
Valid double subdomain (Button)
</button>

<button
onClick={() => window.open("https://google.com", "_blank")}
Expand Down
105 changes: 88 additions & 17 deletions src/minikit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ export class MiniKit {
[Command.SignTypedData]: 1,
};

private static isCommandAvailable = {
[Command.Verify]: false,
[Command.Pay]: false,
[Command.WalletAuth]: false,
[Command.SendTransaction]: false,
[Command.SignMessage]: false,
[Command.SignTypedData]: false,
};

private static listeners: Record<ResponseEvent, EventHandler> = {
[ResponseEvent.MiniAppVerifyAction]: () => {},
[ResponseEvent.MiniAppPayment]: () => {},
Expand Down Expand Up @@ -113,6 +122,15 @@ export class MiniKit {
const commandInput = input.find(
(command) => command.name === commandName
);

if (!commandInput) {
console.error(
`Command ${commandName} is not supported by the app. Try updating the app version`
);
} else {
MiniKit.isCommandAvailable[commandName] = true;
}

return commandInput
? commandInput.supported_versions.includes(version)
: false;
Expand Down Expand Up @@ -141,15 +159,6 @@ export class MiniKit {
};
}

if (!this.commandsValid(window.WorldApp.supported_commands)) {
return {
success: false,
errorCode: MiniKitInstallErrorCodes.AppOutOfDate,
errorMessage:
MiniKitInstallErrorMessage[MiniKitInstallErrorCodes.AppOutOfDate],
};
}

try {
window.MiniKit = MiniKit;
this.sendInit();
Expand All @@ -167,6 +176,16 @@ export class MiniKit {
};
}

// If commands are missing we will install minikit regardless
if (!this.commandsValid(window.WorldApp.supported_commands)) {
return {
success: false,
errorCode: MiniKitInstallErrorCodes.AppOutOfDate,
errorMessage:
MiniKitInstallErrorMessage[MiniKitInstallErrorCodes.AppOutOfDate],
};
}

return { success: true };
}

Expand All @@ -176,7 +195,18 @@ export class MiniKit {
}

public static commands = {
verify: (payload: VerifyCommandInput): VerifyCommandPayload => {
verify: (payload: VerifyCommandInput): VerifyCommandPayload | null => {
if (
typeof window === "undefined" ||
!this.isCommandAvailable[Command.Verify]
) {
console.error(
"'verify' command is unavailable. Check MiniKit.install() or update the app version"
);

return null;
}

const timestamp = new Date().toISOString();
const eventPayload: VerifyCommandPayload = {
...payload,
Expand All @@ -194,9 +224,12 @@ export class MiniKit {
},

pay: (payload: PayCommandInput): PayCommandPayload | null => {
if (typeof window === "undefined") {
if (
typeof window === "undefined" ||
!this.isCommandAvailable[Command.Pay]
) {
console.error(
"'pay' method is only available in a browser environment."
"'pay' command is unavailable. Check MiniKit.install() or update the app version"
);
return null;
}
Expand All @@ -223,9 +256,12 @@ export class MiniKit {
},

walletAuth: (payload: WalletAuthInput): WalletAuthPayload | null => {
if (typeof window === "undefined") {
if (
typeof window === "undefined" ||
!this.isCommandAvailable[Command.WalletAuth]
) {
console.error(
"'walletAuth' method is only available in a browser environment."
"'walletAuth' command is unavailable. Check MiniKit.install() or update the app version"
);

return null;
Expand Down Expand Up @@ -279,7 +315,18 @@ export class MiniKit {

sendTransaction: (
payload: SendTransactionInput
): SendTransactionPayload => {
): SendTransactionPayload | null => {
if (
typeof window === "undefined" ||
!this.isCommandAvailable[Command.SendTransaction]
) {
console.error(
"'sendTransaction' command is unavailable. Check MiniKit.install() or update the app version"
);

return null;
}

sendMiniKitEvent<WebViewBasePayload>({
command: Command.SendTransaction,
version: 1,
Expand All @@ -289,7 +336,18 @@ export class MiniKit {
return payload;
},

signMessage: (payload: SignMessageInput): SignMessagePayload => {
signMessage: (payload: SignMessageInput): SignMessagePayload | null => {
if (
typeof window === "undefined" ||
!this.isCommandAvailable[Command.SignMessage]
) {
console.error(
"'signMessage' command is unavailable. Check MiniKit.install() or update the app version"
);

return null;
}

sendMiniKitEvent<WebViewBasePayload>({
command: Command.SignMessage,
version: 1,
Expand All @@ -299,7 +357,20 @@ export class MiniKit {
return payload;
},

signTypedData: (payload: SignTypedDataInput): SignTypedDataPayload => {
signTypedData: (
payload: SignTypedDataInput
): SignTypedDataPayload | null => {
if (
typeof window === "undefined" ||
!this.isCommandAvailable[Command.SignTypedData]
) {
console.error(
"'signTypedData' command is unavailable. Check MiniKit.install() or update the app version"
);

return null;
}

sendMiniKitEvent<WebViewBasePayload>({
command: Command.SignTypedData,
version: 1,
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@worldcoin/minikit-js",
"version": "0.0.30-internal-alpha",
"version": "0.0.31-internal-alpha",
"homepage": "https://docs.worldcoin.org/id/minikit",
"description": "Internal Alpha: Mini-kit JS is a lightweight sdk for building mini-apps compatible with World App",
"license": "MIT",
Expand Down

0 comments on commit 8a9e982

Please sign in to comment.