Skip to content

Commit

Permalink
refactor: parse func checks encodedDetails to decode
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusGarmeister committed Aug 13, 2024
1 parent 20e69f0 commit ecc5a3a
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions packages/did/src/Did.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import type {
VerificationMethod,
} from '@kiltprotocol/types'
import { DataUtils, Multikey, SDKErrors, ss58Format } from '@kiltprotocol/utils'
import { blake2AsU8a, encodeAddress } from '@polkadot/util-crypto'
import { base58Decode, blake2AsU8a, encodeAddress } from '@polkadot/util-crypto'

import type { DidVerificationMethodType } from './DidDetails/DidDetails.js'
import { parseDocumentFromLightDid } from './DidDetails/LightDidDetails.js'

// The latest version for KILT light DIDs.
const LIGHT_DID_LATEST_VERSION = 1
Expand Down Expand Up @@ -107,6 +106,14 @@ export function parse(did: Did | DidUrl): IDidParsingResult {
? parseInt(versionString, 10)
: LIGHT_DID_LATEST_VERSION
const queryParameters = exportQueryParamsFromDidUrl(did as DidUrl)
// checking if encodedDetails can be decoded
if (encodedDetails) {
try {
base58Decode(encodedDetails, true)
} catch {
throw new SDKErrors.InvalidDidFormatError(did)
}
}
return {
did: did.replace(fragment || '', '') as Did,
version,
Expand Down Expand Up @@ -208,7 +215,7 @@ export function validateDid(
if (typeof input !== 'string') {
throw new TypeError(`DID string expected, got ${typeof input}`)
}
const { address, fragment, type } = parse(input as DidUrl)
const { address, fragment } = parse(input as DidUrl)

if (
fragment &&
Expand All @@ -228,17 +235,6 @@ export function validateDid(
}

DataUtils.verifyKiltAddress(address)

// Check if the encoded details represent something that can be decoded, or just random jargon, in which case the DID is not really a valid one.
if (type === 'light') {
try {
parseDocumentFromLightDid(input as Did, false)
} catch {
throw new SDKErrors.DidError(
`The provided light DID "${input}" contains incorrect base58-encoded details.`
)
}
}
}

/**
Expand Down

0 comments on commit ecc5a3a

Please sign in to comment.