Skip to content

Commit

Permalink
chore: improve error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
rflechtner committed Oct 23, 2023
1 parent 15d7d72 commit ec56b7a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
10 changes: 7 additions & 3 deletions packages/core/src/delegation/DelegationNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ export class DelegationNode implements IDelegationNode {
})
)
if (!signer) {
throw new Error(
`Unable to sign: No signer given for on-chain verifiable signatures by an authentication key related to ${delegateDid.id}`
)
throw new SDKErrors.NoSuitableSignerError(undefined, {
signerRequirements: {
did: delegateDid.id,
verificationRelationship: 'authentication',
algorithm: Signers.DID_PALLET_SUPPORTED_ALGORITHMS,
},
})
}
const signature = await signer.sign({
data: this.generateHash(),
Expand Down
22 changes: 17 additions & 5 deletions packages/did/src/DidDetails/FullDidDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,21 @@ export async function authorizeTx(
didDocument = (await resolve(didUri)).didDocument as DidDocument
}
if (!didDocument?.id) {
throw new Error('failed to resolve signer DID')
throw new SDKErrors.DidNotFoundError('failed to resolve signer DID')
}
const signer = await Signers.selectSigner(
signers,
verifiableOnChain(),
byDid(didDocument, { verificationRelationship })
)
if (typeof signer === 'undefined') {
throw new Error('incompatible signers') // TODO: improve error message
throw new SDKErrors.NoSuitableSignerError(undefined, {
signerRequirements: {
did: didDocument.id,
verificationRelationship,
algorithm: Signers.DID_PALLET_SUPPORTED_ALGORITHMS,
},
})
}

return generateDidAuthenticatedTx({
Expand Down Expand Up @@ -286,7 +292,7 @@ export async function authorizeBatch({
didDocument = (await resolve(didUri)).didDocument
}
if (typeof didDocument?.id !== 'string') {
throw new Error('failed to resolve signer DID')
throw new SDKErrors.DidNotFoundError('failed to resolve signer DID')
}

const promises = groups.map(async (group, batchIndex) => {
Expand All @@ -299,10 +305,16 @@ export async function authorizeBatch({
const signer = await Signers.selectSigner(
signers,
verifiableOnChain(),
byDid(didDocument!, { verificationRelationship })
byDid(didDocument as DidDocument, { verificationRelationship })
)
if (typeof signer === 'undefined') {
throw new Error('incompatible signers') // TODO: improve error message
throw new SDKErrors.NoSuitableSignerError(undefined, {
signerRequirements: {
did: (didDocument as DidDocument).id,
verificationRelationship,
algorithm: Signers.DID_PALLET_SUPPORTED_ALGORITHMS,
},
})
}

return generateDidAuthenticatedTx({
Expand Down
10 changes: 7 additions & 3 deletions packages/legacy-credentials/src/Credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,13 @@ export async function createPresentation({
byDid(didDocument, { verificationRelationship: 'authentication' })
)
if (!signer) {
throw new Error(
`Unable to sign: No signer given for on-chain verifiable signatures by an authentication key related to ${didDocument.id}`
)
throw new SDKErrors.NoSuitableSignerError(undefined, {
signerRequirements: {
did: didDocument.id,
algorithm: Signers.DID_PALLET_SUPPORTED_ALGORITHMS,
verificationRelationship: 'authentication',
},
})
}

const signature = await signer?.sign({
Expand Down
32 changes: 30 additions & 2 deletions packages/utils/src/SDKErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/* eslint-disable max-classes-per-file */

import type { SignerInterface } from '@kiltprotocol/types'

export class SDKError extends Error {
constructor(message?: string, options?: ErrorOptions) {
super(message, options)
Expand Down Expand Up @@ -45,8 +47,6 @@ export class EncryptionError extends SDKError {}

export class DidError extends SDKError {}

export class DidExporterError extends SDKError {}

export class DidBatchError extends SDKError {}

export class DidNotFoundError extends SDKError {}
Expand Down Expand Up @@ -113,6 +113,34 @@ export class ClaimNonceMapMalformedError extends SDKError {

export class SignatureMalformedError extends SDKError {}

export class NoSuitableSignerError extends SDKError {
constructor(
message?: string,
options?: ErrorOptions & {
signerRequirements?: Record<string, unknown>
availableSigners?: SignerInterface[]
}
) {
const { signerRequirements, availableSigners } = options ?? {}
const msgs = [message ?? 'No suitable signers provided to this function.']
if (signerRequirements) {
msgs.push(
`Expected signer matching conditions ${JSON.stringify(
signerRequirements,
null,
2
)}.`
)
}
if (availableSigners) {
msgs.push(
`Signers available: ${JSON.stringify(availableSigners, null, 2)}.`
)
}
super(msgs.join('\n'), options)
}
}

export class DidSubjectMismatchError extends SDKError {
constructor(actual: string, expected: string) {
super(
Expand Down
8 changes: 5 additions & 3 deletions packages/utils/src/Signers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import type {
UriFragment,
} from '@kiltprotocol/types'

import { DidError } from './SDKErrors.js'

export const ALGORITHMS = Object.freeze({
ECRECOVER_SECP256K1_BLAKE2B: 'Ecrecover-Secp256k1-Blake2b', // could also be called ES256K-R-Blake2b
ECRECOVER_SECP256K1_KECCAK: 'Ecrecover-Secp256k1-Keccak', // could also be called ES256K-R-Keccak
Expand Down Expand Up @@ -330,7 +332,7 @@ function byDid(
let eligibleVMs = didDocument.verificationMethod
// TODO: not super happy about this throwing; can I attach a diagnostics property to the returned function instead that will inform why this will never select a signer?
if (!Array.isArray(eligibleVMs) || eligibleVMs.length === 0) {
throw new Error(
throw new DidError(
`DID ${didDocument.id} not fit for signing: No verification methods are associated with the signer DID document. It may be that this DID has been deactivated.`
)
}
Expand All @@ -355,7 +357,7 @@ function byDid(
!Array.isArray(didDocument[verificationRelationship]) ||
didDocument[verificationRelationship].length === 0
) {
throw new Error(
throw new DidError(
`DID ${didDocument.id} not fit for signing: No verification methods available for the requested verification relationship ("${verificationRelationship}").`
)
}
Expand All @@ -366,7 +368,7 @@ function byDid(
)
}
if (eligibleIds.length === 0) {
throw new Error(
throw new DidError(
`DID ${
didDocument.id
} not fit for signing: The verification methods associated with this DID's document do not match the requested controller and/or verification relationship: ${JSON.stringify(
Expand Down

0 comments on commit ec56b7a

Please sign in to comment.