Skip to content

Commit

Permalink
fix: replace id templates and add w3 did context
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelPretorius committed Apr 6, 2023
1 parent 341ba91 commit d64c692
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <ixo>",
"license": "Apache 2.0",
Expand Down
22 changes: 22 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
12 changes: 12 additions & 0 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d64c692

Please sign in to comment.