Skip to content

DidHelper Remove Verification Method #876

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions packages/sdk-js/src/DidHelpers/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand All @@ -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 & {
Expand Down
84 changes: 81 additions & 3 deletions packages/sdk-js/src/DidHelpers/setVerificationMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 & {
Expand All @@ -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,
Expand Down Expand Up @@ -82,3 +84,79 @@ export function setVerificationMethod(
expectedEvents: [{ section: 'did', method: 'DidUpdated' }],
})
}

/**
* Removes the verification method for the selected `verificationMethodId` and `relationship`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Removes the verification method for the selected `verificationMethodId` and `relationship`.
* 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<VerificationRelationship, 'authentication'>
}
): 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' }],
})
}
Loading