Skip to content

Commit

Permalink
feat(did): update did document params validation
Browse files Browse the repository at this point in the history
checking params on updateSignedDidPublicKey, updateSignedDidDelegate functions
checking params on updateDocument conditionally depending on didAttribute value
in case of validation issues - throws Error with the list of invalid or missing properties
  • Loading branch information
Passerino committed Mar 2, 2022
1 parent 6f2a1b4 commit 16494a9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 25 deletions.
7 changes: 7 additions & 0 deletions docs/api/enums/ERROR_MESSAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enumeration members

- [APP\_WITH\_ROLES](ERROR_MESSAGES.md#app_with_roles)
- [CAN\_NOT\_UPDATE\_DOCUMENT\_PROPERTIES\_INVALID\_OR\_MISSING](ERROR_MESSAGES.md#can_not_update_document_properties_invalid_or_missing)
- [CAN\_NOT\_UPDATE\_NOT\_CONTROLLED\_DOCUMENT](ERROR_MESSAGES.md#can_not_update_not_controlled_document)
- [CLAIM\_TYPE\_REQUIRED\_FOR\_ON\_CHAIN\_REGISTRATION](ERROR_MESSAGES.md#claim_type_required_for_on_chain_registration)
- [CLAIM\_WAS\_NOT\_ISSUED](ERROR_MESSAGES.md#claim_was_not_issued)
Expand Down Expand Up @@ -38,6 +39,12 @@

___

### CAN\_NOT\_UPDATE\_DOCUMENT\_PROPERTIES\_INVALID\_OR\_MISSING

**CAN\_NOT\_UPDATE\_DOCUMENT\_PROPERTIES\_INVALID\_OR\_MISSING** = `"Cannot update document. Properties invalid or missing: "`

___

### CAN\_NOT\_UPDATE\_NOT\_CONTROLLED\_DOCUMENT

**CAN\_NOT\_UPDATE\_NOT\_CONTROLLED\_DOCUMENT** = `"Can not update not controlled document"`
Expand Down
1 change: 1 addition & 0 deletions src/errors/ErrorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum ERROR_MESSAGES {
ROLE_PREREQUISITES_NOT_MET = "Enrolment subject doesn't have required roles",
ROLE_NOT_EXISTS = 'Role you want to enroll to does not exists',
CAN_NOT_UPDATE_NOT_CONTROLLED_DOCUMENT = 'Can not update not controlled document',
CAN_NOT_UPDATE_DOCUMENT_PROPERTIES_INVALID_OR_MISSING = 'Cannot update document. Properties invalid or missing: ',
ONCHAIN_ROLE_VERSION_NOT_SPECIFIED = 'On-chain role version not specified',
WITHDRAWAL_WAS_NOT_REQUESTED = 'Stake withdrawal was not requested',
STAKE_WAS_NOT_PUT = 'Stake was not put',
Expand Down
116 changes: 91 additions & 25 deletions src/modules/didRegistry/didRegistry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,6 @@ export class DidRegistry {
this._setClaims();
}

private async getDIDDocFull(did) {
if (did === this._signerService.did) {
return this._document;
} else {
const assetDID = (await this._assetsService.getOwnedAssets()).find(
(a) => a.document.id === did
)?.id;
if (!assetDID) {
throw new Error(
ERROR_MESSAGES.CAN_NOT_UPDATE_NOT_CONTROLLED_DOCUMENT
);
}

const { didRegistryAddress: didContractAddress } =
chainConfigs()[this._signerService.chainId];
const operator = new ProxyOperator(
this._identityOwner,
{ address: didContractAddress },
addressOf(assetDID)
);
return new DIDDocumentFull(did, operator);
}
}

async getDidDocument({
did = this._did,
includeClaims = true,
Expand Down Expand Up @@ -169,7 +145,7 @@ export class DidRegistry {
async getDidPublicKeys({
did = this._signerService.did,
}): Promise<IPublicKey[]> {
return ( await this.getDidDocument({ did: did, includeClaims: false }))
return (await this.getDidDocument({ did: did, includeClaims: false }))
.publicKey;
}

Expand Down Expand Up @@ -256,6 +232,12 @@ export class DidRegistry {
did?: string;
validity?: number;
}): Promise<boolean> {
this.validDateUpdateDocumentRequest({
didAttribute,
data,
did,
});

const didDocument = await this.getDIDDocFull(did);
const updateData: IUpdateData = {
algo: KeyType.Secp256k1,
Expand Down Expand Up @@ -287,6 +269,11 @@ export class DidRegistry {
tag: string;
validity?: number;
}): Promise<boolean> {
if (!publicKey)
throw new Error(
ERROR_MESSAGES.CAN_NOT_UPDATE_DOCUMENT_PROPERTIES_INVALID_OR_MISSING +
'publicKey'
);
const didDocument = await this.getDIDDocFull(did);
const isDIdDocUpdated = await didDocument.updatePublicKey({
publicKey,
Expand Down Expand Up @@ -316,6 +303,11 @@ export class DidRegistry {
type: PubKeyType;
validity?: number;
}): Promise<boolean> {
if (!delegatePublicKey)
throw new Error(
ERROR_MESSAGES.CAN_NOT_UPDATE_DOCUMENT_PROPERTIES_INVALID_OR_MISSING +
'delegatePublicKey'
);
const didDocument = await this.getDIDDocFull(did);
const isDIdDocUpdated = await didDocument.updateDelegate({
delegatePublicKey,
Expand Down Expand Up @@ -357,6 +349,28 @@ export class DidRegistry {
return this._jwt.decode(token);
}

private async getDIDDocFull(did) {
if (did === this._signerService.did) {
return this._document;
} else {
const assetDID = (await this._assetsService.getOwnedAssets()).find(
(a) => a.document.id === did
)?.id;
if (!assetDID) {
throw new Error(ERROR_MESSAGES.CAN_NOT_UPDATE_NOT_CONTROLLED_DOCUMENT);
}

const { didRegistryAddress: didContractAddress } =
chainConfigs()[this._signerService.chainId];
const operator = new ProxyOperator(
this._identityOwner,
{ address: didContractAddress },
addressOf(assetDID)
);
return new DIDDocumentFull(did, operator);
}
}

private async _setOperator() {
const signer = this._signerService.signer;
const provider = signer.provider;
Expand Down Expand Up @@ -418,4 +432,56 @@ export class DidRegistry {
})
);
}

/**
* validates update document request
*/
private validDateUpdateDocumentRequest({
didAttribute,
data,
did,
}: {
didAttribute: DIDAttribute;
data: IUpdateData;
did: string;
}) {
const invalidProps: Array<string> = [];
if (!did) invalidProps.push('did');
if (!data) invalidProps.push('data');
if (!didAttribute) invalidProps.push('didAttribute');
if (!data?.value) invalidProps.push('data.value');
if (!data?.type) invalidProps.push('data.type');

if (invalidProps.length === 0) {
if (didAttribute === DIDAttribute.ServicePoint) {
if (data.type !== DIDAttribute.ServicePoint)
invalidProps.push('data.type');
if (!data.value?.id) invalidProps.push('data.value.id');
if (!data.value?.hash) invalidProps.push('data.value.hash');
if (!data.value?.hashAlg) invalidProps.push('data.value.hashAlg');
}
if (didAttribute === DIDAttribute.Authenticate) {
if (!data?.algo) invalidProps.push('data.algo');
if (!data?.encoding) invalidProps.push('data.encoding');
if (!data.delegate) invalidProps.push('data.delegate');
if (!Object.values(PubKeyType).includes(data.type as PubKeyType))
invalidProps.push('data.type');
}

if (didAttribute === DIDAttribute.PublicKey) {
if (!data?.algo) invalidProps.push('data.algo');
if (!data?.encoding) invalidProps.push('data.encoding');
if (!data.value?.publicKey) invalidProps.push('data.value.publicKey');
if (!data.value?.tag && data.value?.tag !== '') invalidProps.push('data.value.tag');
if (!Object.values(PubKeyType).includes(data.type as PubKeyType))
invalidProps.push('data.type');
}
}
if (invalidProps.length > 0)
throw new Error(
`${
ERROR_MESSAGES.CAN_NOT_UPDATE_DOCUMENT_PROPERTIES_INVALID_OR_MISSING
} ${invalidProps.join(', ')}`
);
}
}

0 comments on commit 16494a9

Please sign in to comment.