Skip to content

Commit

Permalink
feat: return verification_level instead of credential_type (#203)
Browse files Browse the repository at this point in the history
* feat: return verification_level instead of credential_type

* nit

* Update packages/core/src/bridge.ts

Co-authored-by: Miguel Piedrafita <github@miguel.build>

* chore: release 1.0.0-alpha.6

Release-As: 1.0.0-alpha.6

* fix: satisfaction

* nit: default verification level

---------

Co-authored-by: Miguel Piedrafita <github@miguel.build>
  • Loading branch information
0xPenryn and m1guelpf committed Dec 10, 2023
1 parent eea51da commit a6d52f0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
41 changes: 34 additions & 7 deletions packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { create } from 'zustand'
import { DEFAULT_VERIFICATION_LEVEL, buffer_decode, verification_level_to_credential_types } from './lib/utils'
import { type IDKitConfig } from '@/types/config'
import { VerificationState } from '@/types/bridge'
import type { ISuccessResult } from '@/types/result'
import type { CredentialType } from '@/types/config'
import { encodeAction, generateSignal } from '@/lib/hashing'
import { AppErrorCodes, ResponseStatus } from '@/types/bridge'
import { VerificationLevel, type IDKitConfig } from '@/types/config'

Check warning on line 7 in packages/core/src/bridge.ts

View workflow job for this annotation

GitHub Actions / Lint, Typecheck & Test Build

'VerificationLevel' is defined but never used
import { decryptResponse, encryptRequest, exportKey, generateKey } from '@/lib/crypto'
import {
DEFAULT_VERIFICATION_LEVEL,
buffer_decode,
credential_type_to_verification_level,
verification_level_to_credential_types,
} from './lib/utils'

const DEFAULT_BRIDGE_URL = 'https://bridge.worldcoin.org'

Expand All @@ -19,6 +25,11 @@ type BridgeResponse =
response: { iv: string; payload: string }
}

type BridgeResult =
| ISuccessResult
| (Omit<ISuccessResult, 'verification_level'> & { credential_type: CredentialType })
| { error_code: AppErrorCodes }

export type WorldBridgeStore = {
bridge_url: string
iv: Uint8Array | null
Expand Down Expand Up @@ -59,7 +70,10 @@ export const useWorldBridgeStore = create<WorldBridgeStore>((set, get) => ({
action_description,
action: encodeAction(action),
signal: generateSignal(signal).digest,
credential_types: verification_level_to_credential_types(verification_level ?? DEFAULT_VERIFICATION_LEVEL),
credential_types: verification_level_to_credential_types(
verification_level ?? DEFAULT_VERIFICATION_LEVEL
),
verification_level: verification_level ?? DEFAULT_VERIFICATION_LEVEL,
})
)
),
Expand Down Expand Up @@ -108,9 +122,9 @@ export const useWorldBridgeStore = create<WorldBridgeStore>((set, get) => ({
})
}

const result = JSON.parse(await decryptResponse(key, buffer_decode(response.iv), response.payload)) as
| ISuccessResult
| { error_code: AppErrorCodes }
let result = JSON.parse(
await decryptResponse(key, buffer_decode(response.iv), response.payload)
) as BridgeResult

if ('error_code' in result) {
return set({
Expand All @@ -119,7 +133,20 @@ export const useWorldBridgeStore = create<WorldBridgeStore>((set, get) => ({
})
}

set({ result, verificationState: VerificationState.Confirmed, key: null, connectorURI: null, requestId: null })
if ('credential_type' in result) {
result = {
verification_level: credential_type_to_verification_level(result.credential_type),
...result,
} satisfies ISuccessResult
}

set({
result,
key: null,
requestId: null,
connectorURI: null,
verificationState: VerificationState.Confirmed,
})
},

reset: () => {
Expand Down
18 changes: 17 additions & 1 deletion packages/core/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const buffer_decode = (encoded: string): ArrayBuffer => {
}

/**
* @deprecated use to transition to verification levels from credential types
* @dev use to convert verification level to accepted credential types for proof request
* @param verification_level
* @returns
*/
Expand All @@ -26,3 +26,19 @@ export const verification_level_to_credential_types = (verification_level: Verif
throw new Error(`Unknown verification level: ${verification_level}`)
}
}

/**
* @dev use to convert credential type to verification level upon proof response
* @param verification_level
* @returns
*/
export const credential_type_to_verification_level = (credential_type: CredentialType): VerificationLevel => {
switch (credential_type) {
case CredentialType.Orb:
return VerificationLevel.Orb
case CredentialType.Device:
return VerificationLevel.Lite
default:
throw new Error(`Unknown credential_type: ${credential_type}`)
}
}
4 changes: 2 additions & 2 deletions packages/core/src/types/result.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { AppErrorCodes } from './bridge'
import type { CredentialType } from './config'
import type { VerificationLevel } from './config'

export interface ISuccessResult {
proof: string
merkle_root: string
nullifier_hash: string
credential_type: CredentialType
verification_level: VerificationLevel
}

export interface IErrorState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import AboutWorldID from '@/components/AboutWorldID'
import { useWorldBridge } from '@/services/wld-bridge'
import LoadingIcon from '@/components/Icons/LoadingIcon'
import WorldcoinIcon from '@/components/Icons/WorldcoinIcon'
import { AppErrorCodes, VerificationState, verification_level_to_credential_types } from '@worldcoin/idkit-core'
import { AppErrorCodes, VerificationState, VerificationLevel } from '@worldcoin/idkit-core'

const getOptions = (store: IDKitStore) => ({
signal: store.signal,
Expand Down Expand Up @@ -57,8 +57,7 @@ const WorldIDState = () => {
}

if (result) {
const credential_types = verification_level_to_credential_types(verification_level)
if (!credential_types.includes(result.credential_type)) {
if (verification_level == VerificationLevel.Orb && result.verification_level == VerificationLevel.Lite) {
console.error(
'Credential type received from wallet does not match configured credential_types. This should only happen when manually selecting disallowed credentials in the Worldcoin Simulator.'
)
Expand Down

0 comments on commit a6d52f0

Please sign in to comment.