Skip to content

Commit

Permalink
fix(didRegistry): update owned document
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Nov 9, 2021
1 parent 1536d03 commit 7f69fdb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
Expand Up @@ -171,19 +171,19 @@ ___

### updateDocument

**updateDocument**(`options`): `Promise`<`boolean`\>
**updateDocument**(`__namedParameters`): `Promise`<`boolean`\>

**`description`** updates did document based on data provided

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `options` | `Object` | Options to connect with blockchain |
| `options.data` | `IUpdateData` | New attribute value |
| `options.did?` | `string` | Asset did to be updated |
| `options.didAttribute` | `DIDAttribute` | Type of document to be updated |
| `options.validity?` | `number` | Time (s) for the attribute to expire |
| Name | Type |
| :------ | :------ |
| `__namedParameters` | `Object` |
| `__namedParameters.data` | `IUpdateData` |
| `__namedParameters.did?` | `string` |
| `__namedParameters.didAttribute` | `DIDAttribute` |
| `__namedParameters.validity?` | `number` |

#### Returns

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 26 additions & 17 deletions src/modules/didRegistry/didRegistry.service.ts
Expand Up @@ -157,32 +157,41 @@ export class DidRegistry {
* @returns true if document is updated successfuly
*
*/
async updateDocument(options: {
async updateDocument({
didAttribute,
data,
validity,
did = this._signerService.did,
}: {
didAttribute: DIDAttribute;
data: IUpdateData;
did?: string;
validity?: number;
}): Promise<boolean> {
const { didAttribute, data, validity, did } = options;

if (!did) {
if (did === this._signerService.did) {
const updated = await this._document.update(didAttribute, data, validity);
return Boolean(updated);
} else if (!(await this._assetsService.getOwnedAssets()).find((a) => a.document.id === did)) {
throw new Error(ERROR_MESSAGES.CAN_NOT_UPDATE_NOT_CONTROLLED_DOCUMENT);
}

const updateData: IUpdateData = {
algo: KeyType.Secp256k1,
encoding: Encoding.HEX,
...data,
};
} 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 updateData: IUpdateData = {
algo: KeyType.Secp256k1,
encoding: Encoding.HEX,
...data,
};

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

return Boolean(update);
return Boolean(update);
}
}

/**
Expand Down

0 comments on commit 7f69fdb

Please sign in to comment.