diff --git a/build/src/ssiApi/api-constant.d.ts b/build/src/ssiApi/api-constant.d.ts new file mode 100644 index 0000000..0005b17 --- /dev/null +++ b/build/src/ssiApi/api-constant.d.ts @@ -0,0 +1,11 @@ +export declare const APIENDPOINT: { + STUDIO_API_BASE_URL: string; + AUTH: string; + DID: { + CREATE_DID_ENDPOINT: string; + REGISTER_DID_ENDPOINT: string; + UPDATE_DID_ENDPOINT: string; + RESOLVE_DID_ENDPOINT: string; + }; +}; +//# sourceMappingURL=api-constant.d.ts.map \ No newline at end of file diff --git a/build/src/ssiApi/api-constant.d.ts.map b/build/src/ssiApi/api-constant.d.ts.map new file mode 100644 index 0000000..18677a6 --- /dev/null +++ b/build/src/ssiApi/api-constant.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"api-constant.d.ts","sourceRoot":"","sources":["../../../src/ssiApi/api-constant.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW;;;;;;;;;CASvB,CAAC"} \ No newline at end of file diff --git a/build/src/ssiApi/api-constant.js b/build/src/ssiApi/api-constant.js new file mode 100644 index 0000000..2a7d59d --- /dev/null +++ b/build/src/ssiApi/api-constant.js @@ -0,0 +1,13 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.APIENDPOINT = void 0; +exports.APIENDPOINT = { + STUDIO_API_BASE_URL: 'https://api.entity.hypersign.id/api/v1', + AUTH: '/app/oauth', + DID: { + CREATE_DID_ENDPOINT: '/did/create', + REGISTER_DID_ENDPOINT: '/did/register', + UPDATE_DID_ENDPOINT: '/did', + RESOLVE_DID_ENDPOINT: '/did/resolve', + }, +}; diff --git a/build/src/ssiApi/apiAuth/apiAuth.d.ts.map b/build/src/ssiApi/apiAuth/apiAuth.d.ts.map index 4500010..dfd5ffe 100644 --- a/build/src/ssiApi/apiAuth/apiAuth.d.ts.map +++ b/build/src/ssiApi/apiAuth/apiAuth.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"apiAuth.d.ts","sourceRoot":"","sources":["../../../../src/ssiApi/apiAuth/apiAuth.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,qBAAa,OAAO;IAChB,OAAO,CAAC,MAAM,CAAS;gBACX,MAAM,KAAA;IAMZ,mBAAmB,IAAI,OAAO,CAAC,cAAc,CAAC;CAkBvD"} \ No newline at end of file +{"version":3,"file":"apiAuth.d.ts","sourceRoot":"","sources":["../../../../src/ssiApi/apiAuth/apiAuth.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC,qBAAa,OAAO;IAChB,OAAO,CAAC,MAAM,CAAS;gBACX,MAAM,KAAA;IAMZ,mBAAmB,IAAI,OAAO,CAAC,cAAc,CAAC;CAkBvD"} \ No newline at end of file diff --git a/build/src/ssiApi/apiAuth/apiAuth.js b/build/src/ssiApi/apiAuth/apiAuth.js index e1752c0..b8e8e06 100644 --- a/build/src/ssiApi/apiAuth/apiAuth.js +++ b/build/src/ssiApi/apiAuth/apiAuth.js @@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiAuth = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); +const api_constant_1 = require("../api-constant"); class ApiAuth { constructor(apiKey) { if (!apiKey || apiKey.trim() === "") { @@ -23,21 +24,19 @@ class ApiAuth { } generateAccessToken() { return __awaiter(this, void 0, void 0, function* () { - const studioApiUrl = "https://api.entity.hypersign.id/api/v1/app/oauth"; + const studioApiUrl = `${api_constant_1.APIENDPOINT.STUDIO_API_BASE_URL}${api_constant_1.APIENDPOINT.AUTH}`; const headers = { "X-Api-Secret-Key": this.apiKey, - "Origin": "https://entity.hypersign.id" }; const requestOptions = { method: "POST", headers, }; const response = yield (0, node_fetch_1.default)(studioApiUrl, requestOptions); + const authToken = yield response.json(); if (!response.ok) { - // what error to send not getting error message from api - throw new Error('HID-SSI_SDK:: Error: Unauthorized'); + throw new Error(`HID-SSI-SDK:: Error: ${authToken.message}`); } - const authToken = yield response.json(); return authToken; }); } diff --git a/build/src/ssiApi/services/IDid.d.ts b/build/src/ssiApi/services/IDid.d.ts index f6409c4..2317f69 100644 --- a/build/src/ssiApi/services/IDid.d.ts +++ b/build/src/ssiApi/services/IDid.d.ts @@ -1,4 +1,5 @@ -import { IKeyType, IVerificationRelationships } from "../../did/IDID"; +import { Did } from "../../../libs/generated/ssi/did"; +import { IClientSpec, IKeyType, IVerificationRelationships } from "../../did/IDID"; export interface IGenerateDid { namespace: string; methodSpecificId?: string; @@ -10,4 +11,22 @@ export interface IGenerateDid { verificationRelationships: IVerificationRelationships[]; }; } +interface ClientSpec { + type: IClientSpec; + adr036SignerAddress?: string; +} +interface SignInfo { + verification_method_id: string; + signature: string; + clientSpec: ClientSpec; +} +export interface IRegister { + didDocument: Did; + verificationMethodId?: string; + signInfos?: Array; +} +export interface IUpdate extends IRegister { + deactivate: boolean; +} +export {}; //# sourceMappingURL=IDid.d.ts.map \ No newline at end of file diff --git a/build/src/ssiApi/services/IDid.d.ts.map b/build/src/ssiApi/services/IDid.d.ts.map index b32429b..7ce23b1 100644 --- a/build/src/ssiApi/services/IDid.d.ts.map +++ b/build/src/ssiApi/services/IDid.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"IDid.d.ts","sourceRoot":"","sources":["../../../../src/ssiApi/services/IDid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAC;AAEtE,MAAM,WAAW,YAAY;IACzB,SAAS,EAAC,MAAM,CAAC;IACjB,gBAAgB,CAAC,EAAC,MAAM,CAAC;IACzB,OAAO,CAAC,EAAC;QACL,OAAO,EAAC,QAAQ,CAAC;QACjB,OAAO,EAAC,MAAM,CAAC;QACf,SAAS,EAAC,MAAM,CAAC;QACjB,aAAa,EAAC,MAAM,CAAC;QACrB,yBAAyB,EAAC,0BAA0B,EAAE,CAAA;KACzD,CAAA;CACJ"} \ No newline at end of file +{"version":3,"file":"IDid.d.ts","sourceRoot":"","sources":["../../../../src/ssiApi/services/IDid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iCAAiC,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAC;AAEnF,MAAM,WAAW,YAAY;IACzB,SAAS,EAAC,MAAM,CAAC;IACjB,gBAAgB,CAAC,EAAC,MAAM,CAAC;IACzB,OAAO,CAAC,EAAC;QACL,OAAO,EAAC,QAAQ,CAAC;QACjB,OAAO,EAAC,MAAM,CAAC;QACf,SAAS,EAAC,MAAM,CAAC;QACjB,aAAa,EAAC,MAAM,CAAC;QACrB,yBAAyB,EAAC,0BAA0B,EAAE,CAAA;KACzD,CAAA;CACJ;AAED,UAAU,UAAU;IAChB,IAAI,EAAC,WAAW,CAAC;IACjB,mBAAmB,CAAC,EAAC,MAAM,CAAC;CAC/B;AACD,UAAU,QAAQ;IACd,sBAAsB,EAAC,MAAM,CAAC;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAC,UAAU,CAAA;CACxB;AACD,MAAM,WAAW,SAAS;IACtB,WAAW,EAAC,GAAG,CAAC;IAChB,oBAAoB,CAAC,EAAC,MAAM,CAAC;IAC7B,SAAS,CAAC,EAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;CAE7B;AAGD,MAAM,WAAW,OAAQ,SAAQ,SAAS;IACtC,UAAU,EAAC,OAAO,CAAA;CACrB"} \ No newline at end of file diff --git a/build/src/ssiApi/services/did/did.service.d.ts b/build/src/ssiApi/services/did/did.service.d.ts index 8932939..2f436ec 100644 --- a/build/src/ssiApi/services/did/did.service.d.ts +++ b/build/src/ssiApi/services/did/did.service.d.ts @@ -1,9 +1,18 @@ -import { IGenerateDid } from "../IDid"; +import { Did } from '../../../../libs/generated/ssi/did'; +import { IDIDResolve } from '../../../did/IDID'; +import { IGenerateDid, IRegister } from '../IDid'; export declare class DID { private authService; private accessToken; constructor(apiKey: string); private initAccessToken; generateDid(params: IGenerateDid): Promise; + registerDid(params: IRegister): Promise<{ + didDocument: Did; + transactionHash: string; + }>; + resolveDid(params: { + did: string; + }): Promise; } //# sourceMappingURL=did.service.d.ts.map \ No newline at end of file diff --git a/build/src/ssiApi/services/did/did.service.d.ts.map b/build/src/ssiApi/services/did/did.service.d.ts.map index 54207fb..95e077d 100644 --- a/build/src/ssiApi/services/did/did.service.d.ts.map +++ b/build/src/ssiApi/services/did/did.service.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"did.service.d.ts","sourceRoot":"","sources":["../../../../../src/ssiApi/services/did/did.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAGtC,qBAAa,GAAG;IACd,OAAO,CAAC,WAAW,CAAO;IAC1B,OAAO,CAAC,WAAW,CAAA;gBACP,MAAM,EAAE,MAAM;YAKZ,eAAe;IAIhB,WAAW,CAAC,MAAM,EAAE,YAAY;CAqB9C"} \ No newline at end of file +{"version":3,"file":"did.service.d.ts","sourceRoot":"","sources":["../../../../../src/ssiApi/services/did/did.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,oCAAoC,CAAC;AACzD,OAAO,EAAe,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAI7D,OAAO,EAAE,YAAY,EAAE,SAAS,EAAW,MAAM,SAAS,CAAC;AAG3D,qBAAa,GAAG;IACd,OAAO,CAAC,WAAW,CAAQ;IAC3B,OAAO,CAAC,WAAW,CAAC;gBACR,MAAM,EAAE,MAAM;YAKZ,eAAe;IAIhB,WAAW,CAAC,MAAM,EAAE,YAAY;IA2BhC,WAAW,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC;QAAE,WAAW,EAAE,GAAG,CAAC;QAAC,eAAe,EAAE,MAAM,CAAA;KAAE,CAAC;IA4DtF,UAAU,CAAC,MAAM,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,WAAW,CAAC;CAuBvE"} \ No newline at end of file diff --git a/build/src/ssiApi/services/did/did.service.js b/build/src/ssiApi/services/did/did.service.js index c659476..7279858 100644 --- a/build/src/ssiApi/services/did/did.service.js +++ b/build/src/ssiApi/services/did/did.service.js @@ -13,6 +13,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DID = void 0; +const IDID_1 = require("../../../did/IDID"); +const api_constant_1 = require("../../api-constant"); const apiAuth_1 = require("../../apiAuth/apiAuth"); const node_fetch_1 = __importDefault(require("node-fetch")); class DID { @@ -28,28 +30,107 @@ class DID { } generateDid(params) { return __awaiter(this, void 0, void 0, function* () { - const apiUrl = "https://api.entity.hypersign.id/api/v1/did/create"; + if (!params.namespace) { + throw new Error('HID-SSI-SDK:: Error: params.namespace is required to generate new did doc '); + } + const apiUrl = `${api_constant_1.APIENDPOINT.STUDIO_API_BASE_URL}${api_constant_1.APIENDPOINT.DID.CREATE_DID_ENDPOINT}`; const headers = { - "Content-Type": "application/json", + 'Content-Type': 'application/json', Authorization: `Bearer ${this.accessToken}`, - Origin: "https://entity.hypersign.id" }; const requestOptions = { - method: "POST", + method: 'POST', headers, - body: JSON.stringify(Object.assign({}, params)) + body: JSON.stringify(Object.assign({}, params)), }; const response = yield (0, node_fetch_1.default)(apiUrl, requestOptions); + const result = yield response.json(); + console.log(result); + // need to add scenario of checking if token is expired or not + // can be done either by directly call in studio-api and then based on error regenerate it + // or add jwt package to validate its expiry if (!response.ok) { - // need to figure out how to send correct error message as not getting exact message from studio - throw new Error('HID-SSI_SDK:: Error: Issue in generating did'); + throw new Error(`HID-SSI-SDK:: Error: ${result.message}`); } - const { metaData } = yield response.json(); + const { metaData } = result; return metaData.didDocument; }); } + registerDid(params) { + var _a, _b, _c, _d; + return __awaiter(this, void 0, void 0, function* () { + if (!params.didDocument || Object.keys(params.didDocument).length === 0) { + throw new Error('HID-SSI-SDK:: Error: params.didDocument is required to register a did'); + } + if (!params.signInfos) { + if (!params.verificationMethodId) { + throw new Error('HID-SSI-SDK:: Error: either params.verificationMethodId or params.signInfos is required to register a did'); + } + } + if (params.signInfos && params.signInfos.length === 0) { + throw new Error('HID-SSI-SDK:: Error: params.signInfos is required to register a did'); + } + if (params.signInfos && params.signInfos.length > 0) { + for (const i in params.signInfos) { + if (!params.signInfos[i].verification_method_id) { + throw new Error(`HID-SSI-SDK:: Error: params.signInfos[${i}].verification_method_id is required to register a did`); + } + if (!params.signInfos[i].clientSpec) { + throw new Error(`HID-SSI-SDK:: Error: params.signInfos[${i}].clientSpec is required to register a did`); + } + if (!(params.signInfos[i].clientSpec.type in IDID_1.IClientSpec)) { + throw new Error('HID-SSI-SDK:: Error: params.clientSpec is invalid'); + } + if (((_a = params.signInfos[i].clientSpec) === null || _a === void 0 ? void 0 : _a.type) === IDID_1.IClientSpec['cosmos-ADR036']) { + if (((_b = params.signInfos[i].clientSpec) === null || _b === void 0 ? void 0 : _b.adr036SignerAddress) === '' || + ((_c = params.signInfos[i].clientSpec) === null || _c === void 0 ? void 0 : _c.adr036SignerAddress) === undefined) { + throw new Error(`HID-SSI-SDK:: Error: params.signInfos[${i}].adr036SignerAddress is required to register a did, when clientSpec type is${(_d = params.signInfos[i].clientSpec) === null || _d === void 0 ? void 0 : _d.type} `); + } + } + if (!params.signInfos[i].signature) { + throw new Error(`HID-SSI-SDK:: Error: params.signInfos[${i}].signature is required to register a did`); + } + } + } + const apiUrl = `${api_constant_1.APIENDPOINT.STUDIO_API_BASE_URL}${api_constant_1.APIENDPOINT.DID.REGISTER_DID_ENDPOINT}`; + const headers = { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.accessToken}`, + }; + const requestOptions = { + method: 'POST', + headers, + body: JSON.stringify(Object.assign({}, params)), + }; + const response = yield (0, node_fetch_1.default)(apiUrl, requestOptions); + const result = yield response.json(); + if (!response.ok) { + throw new Error(`HID-SSI-SDK:: Error: ${result.message}`); + } + const { metaData } = result; + return { didDocument: metaData.didDocument, transactionHash: result.transactionHash }; + }); + } + resolveDid(params) { + return __awaiter(this, void 0, void 0, function* () { + if (!params.did) { + throw new Error('HID-SSI-SDK:: Error: params.did is required to resolve a did'); + } + const apiUrl = `${api_constant_1.APIENDPOINT.STUDIO_API_BASE_URL}${api_constant_1.APIENDPOINT.DID.RESOLVE_DID_ENDPOINT}/${params.did}`; + const headers = { + Authorization: `Bearer ${this.accessToken}`, + }; + const requestOptions = { + method: 'GET', + headers, + }; + const response = yield (0, node_fetch_1.default)(apiUrl, requestOptions); + const result = yield response.json(); + if (!response.ok) { + throw new Error(`HID-SSI-SDK:: Error: ${result.message}`); + } + return result; + }); + } } exports.DID = DID; -// const test = new DID("c4dae4b264f98798920ad22456c6f.acd24a3adeaed7fddf86044925d419afc252ada299b018e961ed3ac3ae2d1aaebd577eb0550c2c011fd9f51d33097d88a") -const test = new DID("586a74602fffda655f186ca9c162f.445dbab93f8b3b24501a9bdb1254de6a1528d4db306d87e2674f2277ddcc40a5b6686c4b2fe996ae11509d72eb4bb0d38"); -console.log(test, "test"); diff --git a/src/ssiApi/api-constant.ts b/src/ssiApi/api-constant.ts new file mode 100644 index 0000000..4d87aeb --- /dev/null +++ b/src/ssiApi/api-constant.ts @@ -0,0 +1,10 @@ +export const APIENDPOINT = { + STUDIO_API_BASE_URL: 'https://api.entity.hypersign.id/api/v1', + AUTH: '/app/oauth', + DID: { + CREATE_DID_ENDPOINT: '/did/create', + REGISTER_DID_ENDPOINT: '/did/register', + UPDATE_DID_ENDPOINT: '/did', + RESOLVE_DID_ENDPOINT: '/did/resolve', + }, +}; diff --git a/src/ssiApi/apiAuth/apiAuth.ts b/src/ssiApi/apiAuth/apiAuth.ts index 38befa9..546507d 100644 --- a/src/ssiApi/apiAuth/apiAuth.ts +++ b/src/ssiApi/apiAuth/apiAuth.ts @@ -1,5 +1,6 @@ import fetch from "node-fetch" import { IgenerateToken } from "./IAuth"; +import {APIENDPOINT} from "../api-constant" export class ApiAuth { private apiKey: string; constructor(apiKey) { @@ -9,21 +10,21 @@ export class ApiAuth { this.apiKey = apiKey } async generateAccessToken(): Promise { - const studioApiUrl = "https://api.entity.hypersign.id/api/v1/app/oauth" - const headers = { - "X-Api-Secret-Key": this.apiKey, - "Origin": "https://entity.hypersign.id" - } - const requestOptions = { - method: "POST", - headers, - } - const response = await fetch(studioApiUrl, requestOptions) - if (!response.ok) { - // what error to send not getting error message from api - throw new Error('HID-SSI_SDK:: Error: Unauthorized') - } - const authToken = await response.json() - return authToken + const studioApiUrl = `${APIENDPOINT.STUDIO_API_BASE_URL}${APIENDPOINT.AUTH}` + const headers = { + "X-Api-Secret-Key": this.apiKey, + } + const requestOptions = { + method: "POST", + headers, + } + const response = await fetch(studioApiUrl, requestOptions) + const authToken = await response.json() + if (!response.ok) { + throw new Error(`HID-SSI-SDK:: Error: ${authToken.message}`) + } + return authToken + + } } diff --git a/src/ssiApi/services/IDid.ts b/src/ssiApi/services/IDid.ts index ff55748..fe62877 100644 --- a/src/ssiApi/services/IDid.ts +++ b/src/ssiApi/services/IDid.ts @@ -1,4 +1,5 @@ -import { IKeyType, IVerificationRelationships } from "../../did/IDID"; +import { Did } from "../../../libs/generated/ssi/did"; +import { IClientSpec, IKeyType, IVerificationRelationships } from "../../did/IDID"; export interface IGenerateDid{ namespace:string; @@ -10,4 +11,25 @@ export interface IGenerateDid{ walletAddress:string, verificationRelationships:IVerificationRelationships[] } +} + +interface ClientSpec{ + type:IClientSpec; + adr036SignerAddress?:string; +} +interface SignInfo{ + verification_method_id:string; + signature: string; + clientSpec:ClientSpec +} +export interface IRegister{ + didDocument:Did; + verificationMethodId?:string; + signInfos?:Array + +} + + +export interface IUpdate extends IRegister{ + deactivate:boolean } \ No newline at end of file diff --git a/src/ssiApi/services/did/did.service.ts b/src/ssiApi/services/did/did.service.ts index f679428..f3b59f2 100644 --- a/src/ssiApi/services/did/did.service.ts +++ b/src/ssiApi/services/did/did.service.ts @@ -1,47 +1,131 @@ -import { IAuth } from "../../apiAuth/IAuth" -import { ApiAuth } from "../../apiAuth/apiAuth" -import { IGenerateDid } from "../IDid" -import fetch from "node-fetch" +import { Did } from '../../../../libs/generated/ssi/did'; +import { IClientSpec, IDIDResolve } from '../../../did/IDID'; +import { APIENDPOINT } from '../../api-constant'; +import { IAuth } from '../../apiAuth/IAuth'; +import { ApiAuth } from '../../apiAuth/apiAuth'; +import { IGenerateDid, IRegister, IUpdate } from '../IDid'; +import fetch from 'node-fetch'; export class DID { - private authService: IAuth - private accessToken + private authService: IAuth; + private accessToken; constructor(apiKey: string) { this.authService = new ApiAuth(apiKey); - this.initAccessToken() + this.initAccessToken(); } private async initAccessToken() { - const accessToken = await this.authService.generateAccessToken() - this.accessToken = accessToken.access_token + const accessToken = await this.authService.generateAccessToken(); + this.accessToken = accessToken.access_token; } public async generateDid(params: IGenerateDid) { - const apiUrl = "https://api.entity.hypersign.id/api/v1/did/create" + if (!params.namespace) { + throw new Error('HID-SSI-SDK:: Error: params.namespace is required to generate new did doc '); + } + const apiUrl = `${APIENDPOINT.STUDIO_API_BASE_URL}${APIENDPOINT.DID.CREATE_DID_ENDPOINT}`; const headers = { - "Content-Type": "application/json", + 'Content-Type': 'application/json', Authorization: `Bearer ${this.accessToken}`, - Origin: "https://entity.hypersign.id" - } + }; const requestOptions = { - method: "POST", + method: 'POST', headers, - body: JSON.stringify({ ...params }) - } - const response = await fetch(apiUrl, requestOptions) + body: JSON.stringify({ ...params }), + }; + const response = await fetch(apiUrl, requestOptions); + const result = await response.json(); + console.log(result); + // need to add scenario of checking if token is expired or not + // can be done either by directly call in studio-api and then based on error regenerate it + // or add jwt package to validate its expiry if (!response.ok) { - // need to figure out how to send correct error message as not getting exact message from studio - throw new Error('HID-SSI_SDK:: Error: Issue in generating did') + throw new Error(`HID-SSI-SDK:: Error: ${result.message}`); } - const { metaData } = await response.json() - return metaData.didDocument + const { metaData } = result; + return metaData.didDocument; } -} - + public async registerDid(params: IRegister): Promise<{ didDocument: Did; transactionHash: string }> { + if (!params.didDocument || Object.keys(params.didDocument).length === 0) { + throw new Error('HID-SSI-SDK:: Error: params.didDocument is required to register a did'); + } + if(!params.signInfos){ + if(!params.verificationMethodId){ + throw new Error('HID-SSI-SDK:: Error: either params.verificationMethodId or params.signInfos is required to register a did') + } + } + if (params.signInfos && params.signInfos.length === 0) { + throw new Error('HID-SSI-SDK:: Error: params.signInfos is required to register a did'); + } + if (params.signInfos && params.signInfos.length > 0) { + for (const i in params.signInfos) { + if (!params.signInfos[i].verification_method_id) { + throw new Error( + `HID-SSI-SDK:: Error: params.signInfos[${i}].verification_method_id is required to register a did` + ); + } + if (!params.signInfos[i].clientSpec) { + throw new Error(`HID-SSI-SDK:: Error: params.signInfos[${i}].clientSpec is required to register a did`); + } + if (!(params.signInfos[i].clientSpec.type in IClientSpec)) { + throw new Error('HID-SSI-SDK:: Error: params.clientSpec is invalid'); + } + if (params.signInfos[i].clientSpec?.type === IClientSpec['cosmos-ADR036']) { + if ( + params.signInfos[i].clientSpec?.adr036SignerAddress === '' || + params.signInfos[i].clientSpec?.adr036SignerAddress === undefined + ) { + throw new Error( + `HID-SSI-SDK:: Error: params.signInfos[${i}].adr036SignerAddress is required to register a did, when clientSpec type is${params.signInfos[i].clientSpec?.type} ` + ); + } + } + if (!params.signInfos[i].signature) { + throw new Error(`HID-SSI-SDK:: Error: params.signInfos[${i}].signature is required to register a did`); + } + } + } + const apiUrl = `${APIENDPOINT.STUDIO_API_BASE_URL}${APIENDPOINT.DID.REGISTER_DID_ENDPOINT}`; + const headers = { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.accessToken}`, + }; + const requestOptions = { + method: 'POST', + headers, + body: JSON.stringify({ ...params }), + }; + const response = await fetch(apiUrl, requestOptions); + const result = await response.json(); + if (!response.ok) { + throw new Error(`HID-SSI-SDK:: Error: ${result.message}`); + } + const { metaData } = result; + return { didDocument: metaData.didDocument, transactionHash: result.transactionHash }; + } + public async resolveDid(params: { did: string }): Promise { + if (!params.did) { + throw new Error('HID-SSI-SDK:: Error: params.did is required to resolve a did'); + } + const apiUrl = `${APIENDPOINT.STUDIO_API_BASE_URL}${APIENDPOINT.DID.RESOLVE_DID_ENDPOINT}/${params.did}`; + const headers = { + Authorization: `Bearer ${this.accessToken}`, + }; + const requestOptions = { + method: 'GET', + headers, + }; + const response = await fetch(apiUrl, requestOptions); + const result = await response.json(); + if (!response.ok) { + throw new Error(`HID-SSI-SDK:: Error: ${result.message}`); + } + return result; + } -// const test = new DID("c4dae4b264f98798920ad22456c6f.acd24a3adeaed7fddf86044925d419afc252ada299b018e961ed3ac3ae2d1aaebd577eb0550c2c011fd9f51d33097d88a") -const test = new DID("586a74602fffda655f186ca9c162f.445dbab93f8b3b24501a9bdb1254de6a1528d4db306d87e2674f2277ddcc40a5b6686c4b2fe996ae11509d72eb4bb0d38") + // public async updateDid(params:IUpdate){ -console.log(test, "test") \ No newline at end of file + // } +}