From 984a190da3c313789d1c3e2e2010f37cc937406d Mon Sep 17 00:00:00 2001 From: 0xPenryn Date: Thu, 7 Dec 2023 09:17:48 -0600 Subject: [PATCH] fix!: handle credential_types check when using default (#197) Co-authored-by: Paolo <149602456+pdtfh@users.noreply.github.com> --- packages/core/src/bridge.ts | 34 ++++--------------- packages/core/src/index.ts | 2 ++ packages/core/src/lib/utils.ts | 8 +++++ packages/core/src/types/bridge.ts | 6 ++++ .../IDKitWidget/States/WorldIDState.tsx | 4 +-- packages/react/src/services/wld-bridge.ts | 9 ++++- packages/react/src/store/idkit.ts | 12 +++---- 7 files changed, 38 insertions(+), 37 deletions(-) diff --git a/packages/core/src/bridge.ts b/packages/core/src/bridge.ts index 4eb5bdce..35ec2216 100644 --- a/packages/core/src/bridge.ts +++ b/packages/core/src/bridge.ts @@ -1,20 +1,14 @@ import { create } from 'zustand' -import { buffer_decode } from './lib/utils' -import { AppErrorCodes } from '@/types/bridge' +import { type IDKitConfig } from '@/types/config' import { VerificationState } from '@/types/bridge' import type { ISuccessResult } from '@/types/result' import { encodeAction, generateSignal } from '@/lib/hashing' -import { CredentialType, type IDKitConfig } from '@/types/config' +import { AppErrorCodes, ResponseStatus } from '@/types/bridge' +import { buffer_decode, credential_types_or_default } from './lib/utils' import { decryptResponse, encryptRequest, exportKey, generateKey } from '@/lib/crypto' const DEFAULT_BRIDGE_URL = 'https://bridge.worldcoin.org' -export enum ResponseStatus { - Retrieved = 'retrieved', - Completed = 'completed', - Initialized = 'initialized', -} - type BridgeResponse = | { status: ResponseStatus.Retrieved | ResponseStatus.Initialized @@ -35,17 +29,8 @@ export type WorldBridgeStore = { errorCode: AppErrorCodes | null verificationState: VerificationState - createClient: ( - app_id: IDKitConfig['app_id'], - action: IDKitConfig['action'], - signal?: IDKitConfig['signal'], - bridge_url?: IDKitConfig['bridge_url'], - credential_types?: IDKitConfig['credential_types'], - action_description?: IDKitConfig['action_description'] - ) => Promise - + createClient: (config: IDKitConfig) => Promise pollForUpdates: () => Promise - reset: () => void } @@ -59,14 +44,7 @@ export const useWorldBridgeStore = create((set, get) => ({ bridge_url: DEFAULT_BRIDGE_URL, verificationState: VerificationState.PreparingClient, - createClient: async ( - app_id: IDKitConfig['app_id'], - action: IDKitConfig['action'], - signal?: IDKitConfig['signal'], - bridge_url?: IDKitConfig['bridge_url'], - credential_types?: IDKitConfig['credential_types'], - action_description?: IDKitConfig['action_description'] - ) => { + createClient: async ({ bridge_url, app_id, credential_types, action_description, action, signal }) => { const { key, iv } = await generateKey() const res = await fetch(`${bridge_url ?? DEFAULT_BRIDGE_URL}/request`, { @@ -78,7 +56,7 @@ export const useWorldBridgeStore = create((set, get) => ({ iv, JSON.stringify({ app_id, - credential_types: credential_types ?? [CredentialType.Orb], + credential_types: credential_types_or_default(credential_types), action_description, action: encodeAction(action), signal: generateSignal(signal).digest, diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d95bc435..86961ed7 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -9,3 +9,5 @@ export { } from '@/types' export { useWorldBridgeStore, WorldBridgeStore } from '@/bridge' + +export { DEFAULT_CREDENTIAL_TYPES } from '@/lib/utils' diff --git a/packages/core/src/lib/utils.ts b/packages/core/src/lib/utils.ts index 0afe3bde..a53d26da 100644 --- a/packages/core/src/lib/utils.ts +++ b/packages/core/src/lib/utils.ts @@ -1,5 +1,9 @@ +import { CredentialType } from '..' +import type { IDKitConfig } from '..' import { Buffer } from 'buffer/index.js' +export const DEFAULT_CREDENTIAL_TYPES = [CredentialType.Orb] + export const buffer_encode = (buffer: ArrayBuffer): string => { return Buffer.from(buffer).toString('base64') } @@ -7,3 +11,7 @@ export const buffer_encode = (buffer: ArrayBuffer): string => { export const buffer_decode = (encoded: string): ArrayBuffer => { return Buffer.from(encoded, 'base64') } + +export const credential_types_or_default = (credential_types: IDKitConfig['credential_types']): CredentialType[] => { + return credential_types?.length ? credential_types : DEFAULT_CREDENTIAL_TYPES +} diff --git a/packages/core/src/types/bridge.ts b/packages/core/src/types/bridge.ts index 2fbd985a..b45b9160 100644 --- a/packages/core/src/types/bridge.ts +++ b/packages/core/src/types/bridge.ts @@ -19,3 +19,9 @@ export enum VerificationState { Confirmed = 'confirmed', Failed = 'failed', } + +export enum ResponseStatus { + Retrieved = 'retrieved', + Completed = 'completed', + Initialized = 'initialized', +} diff --git a/packages/react/src/components/IDKitWidget/States/WorldIDState.tsx b/packages/react/src/components/IDKitWidget/States/WorldIDState.tsx index ba451193..01cf15f1 100644 --- a/packages/react/src/components/IDKitWidget/States/WorldIDState.tsx +++ b/packages/react/src/components/IDKitWidget/States/WorldIDState.tsx @@ -57,9 +57,9 @@ const WorldIDState = () => { } if (result) { - if (!credential_types?.includes(result.credential_type)) { + if (!credential_types.includes(result.credential_type)) { console.error( - 'Credential type returned does not match configured credential_types. This should only happen when manually selecting disallowed credentials in the Worldcoin Simulator.' + 'Credential type received from wallet does not match configured credential_types. This should only happen when manually selecting disallowed credentials in the Worldcoin Simulator.' ) setStage(IDKITStage.ERROR) setErrorState({ code: AppErrorCodes.CredentialUnavailable }) diff --git a/packages/react/src/services/wld-bridge.ts b/packages/react/src/services/wld-bridge.ts index 25d5d0b9..1fcbd092 100644 --- a/packages/react/src/services/wld-bridge.ts +++ b/packages/react/src/services/wld-bridge.ts @@ -24,7 +24,14 @@ export const useWorldBridge = ( useEffect(() => { if (!connectorURI) { - void createClient(app_id, action, signal, bridge_url, ref_credential_types.current, action_description) + void createClient({ + app_id, + action, + signal, + bridge_url, + credential_types: ref_credential_types.current, + action_description, + }) } }, [app_id, action, signal, action_description, createClient, ref_credential_types, bridge_url, connectorURI]) diff --git a/packages/react/src/store/idkit.ts b/packages/react/src/store/idkit.ts index e36eaa9e..0c100344 100644 --- a/packages/react/src/store/idkit.ts +++ b/packages/react/src/store/idkit.ts @@ -7,6 +7,7 @@ import { createWithEqualityFn } from 'zustand/traditional' import { AppErrorCodes, CredentialType, + DEFAULT_CREDENTIAL_TYPES, type IErrorState, type IDKitConfig, type ISuccessResult, @@ -18,7 +19,7 @@ export type IDKitStore = { signal: IDKitConfig['signal'] bridge_url?: IDKitConfig['bridge_url'] action_description?: IDKitConfig['action_description'] - credential_types?: IDKitConfig['credential_types'] + credential_types: NonNullable open: boolean stage: IDKITStage @@ -54,7 +55,7 @@ const useIDKitStore = createWithEqualityFn()( action: '', action_description: '', bridge_url: '', - credential_types: [], + credential_types: DEFAULT_CREDENTIAL_TYPES, open: false, result: null, @@ -118,9 +119,8 @@ const useIDKitStore = createWithEqualityFn()( }: Config, source: ConfigSource ) => { - const sanitized_credential_types = credential_types?.filter(type => - Object.values(CredentialType).includes(type) - ) + const sanitizedCredentialTypes = + credential_types?.filter(type => Object.values(CredentialType).includes(type)) ?? [] set({ theme, @@ -129,7 +129,7 @@ const useIDKitStore = createWithEqualityFn()( app_id, autoClose, bridge_url, - credential_types: sanitized_credential_types, + credential_types: sanitizedCredentialTypes.length ? sanitizedCredentialTypes : DEFAULT_CREDENTIAL_TYPES, action_description, })