Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking if encodedDetails can be decoded in the parse function #899

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading