Skip to content

Commit

Permalink
feat: isUserVerified helper & react bindings (#118)
Browse files Browse the repository at this point in the history
feature implementation, import cleanup
  • Loading branch information
michalstruck authored Dec 3, 2024
1 parent 41faf32 commit ab8ba8d
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 3 deletions.
60 changes: 60 additions & 0 deletions packages/core/helpers/address-book/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createPublicClient, http } from "viem";
import { worldchain } from "viem/chains";

const worldIdAddressBookContractAddress =
"0x57b930D551e677CC36e2fA036Ae2fe8FdaE0330D";
const addressVerifiedUntilAbi = [
{
inputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
name: "addressVerifiedUntil",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
];

export const getIsUserVerified = async (
walletAddress: string,
rpcUrl?: string
): Promise<boolean> => {
const publicClient = createPublicClient({
chain: worldchain,
transport: http(
rpcUrl || "https://worldchain-mainnet.g.alchemy.com/public"
),
});

try {
const verifiedUntilResponse = (await publicClient.readContract({
address: worldIdAddressBookContractAddress,
abi: addressVerifiedUntilAbi,
functionName: "addressVerifiedUntil",
args: [walletAddress],
})) as BigInt;

const verifiedUntil = Number(verifiedUntilResponse.toString());

if (!Number.isFinite(verifiedUntil)) {
console.warn("Invalid verifiedUntil value:", verifiedUntil);
return false;
}

const currentTime = Math.floor(Date.now() / 1000);
return verifiedUntil > currentTime;
} catch (error) {
console.error("Error verifying user:", error);
return false;
}
};
2 changes: 0 additions & 2 deletions packages/core/helpers/payment/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { PayCommandInput, Tokens } from "types";
import { PaymentErrorMessage } from "types/errors";
import { TokenDecimals } from "types/payment";
import { MiniAppPaymentErrorPayload } from "types/responses";

// This is a helper function to convert token amount to decimals for payment
// Amount should be in expected amount ie $25.12 should be 25.12
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { WalletAuthInput } from "types/commands";
import {} from "types/responses";

type ValidationResult =
| {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ export {
SAFE_CONTRACT_ABI,
verifySiweMessage,
} from "helpers/siwe/siwe";

export { getIsUserVerified } from "helpers/address-book";
3 changes: 3 additions & 0 deletions packages/core/minikit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export class MiniKit {
};

public static appId: string | null = null;
/**
* @deprecated you should use MiniKit.user.walletAddress instead
*/
public static walletAddress: string | null = null;
public static user: {
walletAddress: string | null;
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/address-book/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useIsUserVerified } from "./is-verified";
31 changes: 31 additions & 0 deletions packages/react/src/address-book/is-verified.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useEffect, useState } from "react";
import { getIsUserVerified } from "@worldcoin/minikit-js";

/**
* Checks if a user is Orb verified
*
* @param walletAddress - The wallet address of the user
* @param rpcUrl - Your preferred RPC node URL, https://worldchain-mainnet.g.alchemy.com/public by default
*/
export const useIsUserVerified = (walletAddress: string, rpcUrl?: string) => {
const [isUserVerified, setIsUserVerified] = useState<boolean | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [isError, setIsError] = useState<any>(null);

useEffect(() => {
const fetchIsUserVerified = async () => {
try {
const data = await getIsUserVerified(walletAddress);
setIsUserVerified(data);
} catch (err) {
setIsError(err);
} finally {
setIsLoading(false);
}
};

fetchIsUserVerified();
}, [walletAddress]);

return { isUserVerified, isLoading, isError };
};
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useWaitForTransactionReceipt } from "./transaction/hooks";
export { useIsUserVerified } from "./address-book/is-verified";
export * from "./types/client";
export * from "./components";

0 comments on commit ab8ba8d

Please sign in to comment.