From d64c692731451295f102eebbfd9304bb66b604f1 Mon Sep 17 00:00:00 2001 From: Michael Pretorius Date: Thu, 6 Apr 2023 17:49:05 +0200 Subject: [PATCH] fix: replace id templates and add w3 did context --- .env.example | 2 +- package.json | 2 +- src/helpers.ts | 22 ++++++++++++++++++++++ src/resolver.ts | 12 ++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/helpers.ts diff --git a/.env.example b/.env.example index 1c120a1..a827904 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,2 @@ -RPC_ENDPOINT=https://devnet.ixo.earth/rpc/ +RPC_ENDPOINT=https://devnet.ixo.earth/rpc/ #https://rpc-ixo-ia.cosmosia.notional.ventures/ PORT=8080 diff --git a/package.json b/package.json index c173b35..446746d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ixo-did-resolver", - "version": "0.0.1", + "version": "0.0.2", "description": "DID resolver for x/ixo DID methods", "author": "Ixo ", "license": "Apache 2.0", diff --git a/src/helpers.ts b/src/helpers.ts new file mode 100644 index 0000000..d1b627a --- /dev/null +++ b/src/helpers.ts @@ -0,0 +1,22 @@ +/** + * Recursively updates string values in a JavaScript object, replacing only part of the string + * if it contains the `oldValue`. + * @param obj The object to update. + * @param oldValue The string value to search for and replace. + * @param newValue The new string value to use in the update. + */ +export const updateObjectStrings = ( + obj: any, + oldValue: string, + newValue: string, +) => { + for (let key in obj) { + if (typeof obj[key] === 'object') { + // if the value is an object or array, recursively call the function + updateObjectStrings(obj[key], oldValue, newValue); + } else if (typeof obj[key] === 'string' && obj[key].includes(oldValue)) { + // if the value is a string and contains the old value, update it + obj[key] = obj[key].replace(oldValue, newValue); + } + } +}; diff --git a/src/resolver.ts b/src/resolver.ts index fb02d3b..7807828 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1,6 +1,7 @@ import { createQueryClient, utils } from '@ixo/impactxclient-sdk'; import { QueryIidDocumentResponse } from '@ixo/impactxclient-sdk/types/codegen/ixo/iid/v1beta1/query'; import { DidResolution, QueryClientType } from './types'; +import { updateObjectStrings } from './helpers'; require('dotenv').config(); @@ -57,6 +58,17 @@ export class IxoResolver { return didResolution; } + // update did doc to replace all string tempaltes {id} with the id of the did doc + updateObjectStrings(didDoc.iidDocument, '{id}', parsed.did); + + // replace context key with @context + didDoc.iidDocument['@context'] = [ + // temporarily adding below context for other services till most services support object contexts + 'https://www.w3.org/ns/did/v1', + ...didDoc.iidDocument.context, + ]; + delete didDoc.iidDocument.context; + // convert Timestamp to js dates didDoc.iidDocument.metadata.created = utils.proto.fromTimestamp( didDoc.iidDocument.metadata.created,