Skip to content

Commit

Permalink
chore: rename matchverifiable to isIdMatchVerifiable, update Badge an…
Browse files Browse the repository at this point in the history
…d verifyRawServiceCredential function
  • Loading branch information
KevinRohlf committed Nov 27, 2024
1 parent ec4d463 commit 15a1e5b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
14 changes: 8 additions & 6 deletions src/@context/Asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface AssetProviderValue {
serviceCredentialIdMatch?: boolean
serviceCredentialVersion: string
verifiedServiceProviderName: string
matchVerifiable?: boolean
isIdMatchVerifiable?: boolean | string
fetchAsset: (token?: CancelToken) => Promise<void>
}

Expand Down Expand Up @@ -83,7 +83,9 @@ function AssetProvider({
useState<string>()
const [verifiedServiceProviderName, setVerifiedServiceProviderName] =
useState<string>()
const [matchVerifiable, setMatchVerifiable] = useState<boolean>()
const [isIdMatchVerifiable, setisIdMatchVerifiable] = useState<
boolean | string
>()

const newCancelToken = useCancelToken()
const isMounted = useIsMounted()
Expand Down Expand Up @@ -204,14 +206,14 @@ function AssetProvider({
? await getServiceCredential(serviceCredential?.url)
: serviceCredential?.raw

const { verified, complianceApiVersion, idMatch, matchVerifiable } =
const { verified, complianceApiVersion, idMatch, isIdMatchVerifiable } =
await verifyRawServiceCredential(serviceCredentialContent, asset.id)

setIsServiceCredentialVerified(verified && !!serviceCredentialContent)
setServiceCredentialIdMatch(
verified && !!serviceCredentialContent && idMatch && matchVerifiable
verified && !!serviceCredentialContent && idMatch
)
setMatchVerifiable(matchVerifiable)
setisIdMatchVerifiable(isIdMatchVerifiable)
setServiceCredentialVersion(complianceApiVersion)
const serviceProviderName = getPublisherFromServiceCredential(
serviceCredentialContent
Expand Down Expand Up @@ -325,7 +327,7 @@ function AssetProvider({
serviceCredentialIdMatch,
serviceCredentialVersion,
verifiedServiceProviderName,
matchVerifiable
isIdMatchVerifiable
} as AssetProviderValue
}
>
Expand Down
36 changes: 17 additions & 19 deletions src/components/@shared/VerifiedBadge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ const cx = classNames.bind(styles)

export function Badge({
isValid,
matchVerifiable,
isIdMatchVerifiable,
verifiedService,
className
}: {
isValid: boolean
matchVerifiable?: boolean
isIdMatchVerifiable?: boolean | string
verifiedService: string
className?: string
}): ReactElement {
Expand All @@ -25,24 +25,22 @@ export function Badge({
className={cx({
mainLabel: true,
isValid,
isWarning: matchVerifiable === false,
isWarning: isIdMatchVerifiable === false,
[className]: className
})}
>
{matchVerifiable === false && (
<Tooltip content="gx:dependsOn could not be found" />
)}
<span>{verifiedService}</span>
{typeof matchVerifiable === 'undefined' ? (
isValid ? (
<VerifiedPatch />
) : (
<Cross />
)
) : isValid && matchVerifiable ? (
<VerifiedPatch />
{typeof isIdMatchVerifiable === 'string' ? (
<Tooltip content={isIdMatchVerifiable}>
<div className={`${styles.mainLabel} ${styles.isWarning}`}>
<span>{verifiedService}</span>
<Cross />
</div>
</Tooltip>
) : (
<Cross />
<>
<span>{verifiedService}</span>
{isValid ? <VerifiedPatch /> : <Cross />}
</>
)}
</div>
)
Expand All @@ -52,15 +50,15 @@ export default function VerifiedBadge({
className,
isValid,
idMatch,
matchVerifiable,
isIdMatchVerifiable,
isLoading,
apiVersion,
timestamp
}: {
className?: string
isValid?: boolean
idMatch?: boolean
matchVerifiable?: boolean
isIdMatchVerifiable?: boolean | string
isLoading?: boolean
apiVersion?: string
timestamp?: boolean
Expand All @@ -82,7 +80,7 @@ export default function VerifiedBadge({
<Badge isValid={isValid} verifiedService="Service Credential" />
<Badge
isValid={idMatch}
matchVerifiable={matchVerifiable}
isIdMatchVerifiable={isIdMatchVerifiable}
verifiedService="Credential ID match"
/>
<div className={styles.details}>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Asset/AssetContent/MetaMain/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function MetaMain({
const {
isServiceCredentialVerified,
serviceCredentialIdMatch,
matchVerifiable,
isIdMatchVerifiable,
serviceCredentialVersion,
isVerifyingServiceCredential,
verifiedServiceProviderName
Expand All @@ -45,7 +45,7 @@ export default function MetaMain({
isLoading={isVerifyingServiceCredential}
isValid={isServiceCredentialVerified}
idMatch={serviceCredentialIdMatch}
matchVerifiable={matchVerifiable}
isIdMatchVerifiable={isIdMatchVerifiable}
apiVersion={serviceCredentialVersion}
timestamp={isServiceCredentialVerified}
/>
Expand Down
59 changes: 40 additions & 19 deletions src/components/Publish/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export async function verifyRawServiceCredential(
verified: boolean
complianceApiVersion?: string
idMatch?: boolean
matchVerifiable?: boolean
isIdMatchVerifiable?: boolean | string
responseBody?: any
}> {
if (!rawServiceCredential) return { verified: false }
Expand All @@ -507,27 +507,48 @@ export async function verifyRawServiceCredential(
}
}
if (response?.status === 201) {
const dependsOn = parsedServiceCredential.verifiableCredential
.filter(
(credential) =>
credential?.credentialSubject['gx:dependsOn'] !== undefined
)
.map(
(credential) => credential?.credentialSubject['gx:dependsOn'][0].id
)
const serviceOffering =
const serviceOfferings =
parsedServiceCredential.verifiableCredential.filter(
(credential) =>
credential?.credentialSubject?.type === 'gx:ServiceOffering' &&
credential?.credentialSubject?.id !== dependsOn?.[0]
credential?.credentialSubject?.type === 'gx:ServiceOffering'
)
const credentialId = serviceOffering[0]?.credentialSubject?.id

return {
verified: true,
complianceApiVersion,
idMatch: did && did?.toLowerCase() === credentialId?.toLowerCase(),
matchVerifiable: dependsOn.length > 0
if (serviceOfferings.length === 1) {
return {
verified: true,
complianceApiVersion,
idMatch:
did &&
did?.toLowerCase() ===
serviceOfferings?.credentialSubject?.id.toLowerCase(),
isIdMatchVerifiable: true
}
} else {
const dependsOnIds = serviceOfferings
.filter((service) => service?.credentialSubject?.['gx:dependsOn'])
.flatMap(
(service) => service?.credentialSubject?.['gx:dependsOn']?.[0]?.id
)
const rootService = serviceOfferings
.filter(
(service) => !dependsOnIds.includes(service?.credentialSubject?.id)
)
.flatMap((service) => service?.credentialSubject?.id)

if (rootService.length !== 1) {
return {
verified: true,
complianceApiVersion,
idMatch: rootService?.includes(did || did.toLowerCase()),
isIdMatchVerifiable: 'Too many root services'
}
} else {
return {
verified: true,
complianceApiVersion,
idMatch: rootService?.includes(did || did.toLowerCase()),
isIdMatchVerifiable: true
}
}
}
}

Expand Down

0 comments on commit 15a1e5b

Please sign in to comment.