diff --git a/packages/sdk-js/src/DidHelpers/service.ts b/packages/sdk-js/src/DidHelpers/service.ts index 50beabab1..7a47018ba 100644 --- a/packages/sdk-js/src/DidHelpers/service.ts +++ b/packages/sdk-js/src/DidHelpers/service.ts @@ -6,16 +6,17 @@ */ import { serviceToChain, urlFragmentToChain } from '@kiltprotocol/did' -import { DidUrl, Service, UriFragment } from '@kiltprotocol/types' +import type { DidUrl, Service, UriFragment } from '@kiltprotocol/types' import { SharedArguments, TransactionHandlers } from './interfaces.js' import { transact } from './transact.js' /** * Adds a service to the DID Document. * + * @param options Any {@link SharedArguments} and additional parameters. * @param options.service The service entry to add to the document. * If the service id is relative (begins with #) it is automatically expanded with the DID taken from didDocument.id. - * @param options + * @returns A set of {@link TransactionHandlers}. */ export function addService( options: SharedArguments & { @@ -35,9 +36,10 @@ export function addService( /** * Removes a service from the DID Document. * + * @param options Any {@link SharedArguments} and additional parameters. * @param options.id The id of the service to remove from the document. * If the service id is relative (begins with #) it is automatically expanded with the DID taken from didDocument.id. - * @param options + * @returns A set of {@link TransactionHandlers}. */ export function removeService( options: SharedArguments & { diff --git a/packages/sdk-js/src/DidHelpers/setVerificationMethod.ts b/packages/sdk-js/src/DidHelpers/setVerificationMethod.ts index 2b657e3cf..451ea8f02 100644 --- a/packages/sdk-js/src/DidHelpers/setVerificationMethod.ts +++ b/packages/sdk-js/src/DidHelpers/setVerificationMethod.ts @@ -9,8 +9,9 @@ import { type NewDidEncryptionKey, type NewDidVerificationKey, publicKeyToChain, + urlFragmentToChain, } from '@kiltprotocol/did' -import type { VerificationRelationship } from '@kiltprotocol/types' +import type { VerificationRelationship, DidUrl } from '@kiltprotocol/types' import { convertPublicKey } from './common.js' import type { @@ -23,9 +24,11 @@ import { transact } from './transact.js' /** * Replaces all existing verification methods for the selected `relationship` with `publicKey`. * + * @param options Any {@link SharedArguments} and additional parameters. * @param options.publicKey The public key to be used for this verification method. * @param options.relationship The relationship for which this verification method shall be useable. - * @param options + * + * @returns A set of {@link TransactionHandlers}. */ export function setVerificationMethod( options: SharedArguments & { @@ -37,7 +40,6 @@ export function setVerificationMethod( let didKeyUpdateTx if (options.relationship === 'keyAgreement') { - // TODO: check if types of keys are valid? const didEncryptionKey: NewDidEncryptionKey = { publicKey: pk.publicKey, type: pk.keyType as any, @@ -82,3 +84,79 @@ export function setVerificationMethod( expectedEvents: [{ section: 'did', method: 'DidUpdated' }], }) } + +/** + * Removes the verification method for the selected `verificationMethodId` and `relationship`. + * + * Note: authentication verification method can not be removed. + * + * @param options Any {@link SharedArguments} and additional parameters. + * @param options.relationship The relationship for which this verification method shall be removed. + * @param options.verificationMethodId The id of the verification method that should be removed. + * + * @returns A set of {@link TransactionHandlers}. + */ +export function removeVerificationMethod( + options: SharedArguments & { + verificationMethodId: DidUrl + relationship: Omit + } +): TransactionHandlers { + let didKeyUpdateTx + switch (options.relationship) { + case 'authentication': { + throw new Error('authentication verification methods can not be removed') + } + case 'capabilityDelegation': { + if ( + options.didDocument.capabilityDelegation?.includes( + options.verificationMethodId + ) + ) { + didKeyUpdateTx = options.api.tx.did.removeDelegationKey() + } else { + throw new Error( + 'the specified capabilityDelegation method does not exist in the DID Document' + ) + } + break + } + case 'keyAgreement': { + if ( + options.didDocument.keyAgreement?.includes(options.verificationMethodId) + ) { + didKeyUpdateTx = options.api.tx.did.removeKeyAgreementKey( + urlFragmentToChain(options.verificationMethodId) + ) + } else { + throw new Error( + 'the specified keyAgreement key does not exist in the DID Document' + ) + } + break + } + case 'assertionMethod': { + if ( + options.didDocument.assertionMethod?.includes( + options.verificationMethodId + ) + ) { + didKeyUpdateTx = options.api.tx.did.removeAttestationKey() + } else { + throw new Error( + 'the specified assertionMethod does not exist in the DID Document' + ) + } + break + } + default: { + throw new Error('the specified method relationship is not supported') + } + } + + return transact({ + ...options, + call: didKeyUpdateTx, + expectedEvents: [{ section: 'did', method: 'DidUpdated' }], + }) +}