Skip to content

Commit

Permalink
fix!: handle credential_types check when using default (#197)
Browse files Browse the repository at this point in the history
Co-authored-by: Paolo <[email protected]>
  • Loading branch information
0xPenryn and pdtfh authored Dec 7, 2023
1 parent c81bbd2 commit 984a190
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 37 deletions.
34 changes: 6 additions & 28 deletions packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<void>

createClient: (config: IDKitConfig) => Promise<void>
pollForUpdates: () => Promise<void>

reset: () => void
}

Expand All @@ -59,14 +44,7 @@ export const useWorldBridgeStore = create<WorldBridgeStore>((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`, {
Expand All @@ -78,7 +56,7 @@ export const useWorldBridgeStore = create<WorldBridgeStore>((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,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export {
} from '@/types'

export { useWorldBridgeStore, WorldBridgeStore } from '@/bridge'

export { DEFAULT_CREDENTIAL_TYPES } from '@/lib/utils'
8 changes: 8 additions & 0 deletions packages/core/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
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')
}

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
}
6 changes: 6 additions & 0 deletions packages/core/src/types/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ export enum VerificationState {
Confirmed = 'confirmed',
Failed = 'failed',
}

export enum ResponseStatus {
Retrieved = 'retrieved',
Completed = 'completed',
Initialized = 'initialized',
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/services/wld-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
12 changes: 6 additions & 6 deletions packages/react/src/store/idkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createWithEqualityFn } from 'zustand/traditional'
import {
AppErrorCodes,
CredentialType,
DEFAULT_CREDENTIAL_TYPES,
type IErrorState,
type IDKitConfig,
type ISuccessResult,
Expand All @@ -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<IDKitConfig['credential_types']>

open: boolean
stage: IDKITStage
Expand Down Expand Up @@ -54,7 +55,7 @@ const useIDKitStore = createWithEqualityFn<IDKitStore>()(
action: '',
action_description: '',
bridge_url: '',
credential_types: [],
credential_types: DEFAULT_CREDENTIAL_TYPES,

open: false,
result: null,
Expand Down Expand Up @@ -118,9 +119,8 @@ const useIDKitStore = createWithEqualityFn<IDKitStore>()(
}: 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,
Expand All @@ -129,7 +129,7 @@ const useIDKitStore = createWithEqualityFn<IDKitStore>()(
app_id,
autoClose,
bridge_url,
credential_types: sanitized_credential_types,
credential_types: sanitizedCredentialTypes.length ? sanitizedCredentialTypes : DEFAULT_CREDENTIAL_TYPES,
action_description,
})

Expand Down

0 comments on commit 984a190

Please sign in to comment.