Skip to content

Commit

Permalink
added api for register and resolve did as well fixed the issue of not…
Browse files Browse the repository at this point in the history
… getting correct error response
  • Loading branch information
varsha766 committed Jul 24, 2023
1 parent 23be773 commit 29a9b4d
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 64 deletions.
11 changes: 11 additions & 0 deletions build/src/ssiApi/api-constant.d.ts
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions build/src/ssiApi/api-constant.d.ts.map

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

13 changes: 13 additions & 0 deletions build/src/ssiApi/api-constant.js
Original file line number Diff line number Diff line change
@@ -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',
},
};
2 changes: 1 addition & 1 deletion build/src/ssiApi/apiAuth/apiAuth.d.ts.map

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

9 changes: 4 additions & 5 deletions build/src/ssiApi/apiAuth/apiAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() === "") {
Expand All @@ -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;
});
}
Expand Down
21 changes: 20 additions & 1 deletion build/src/ssiApi/services/IDid.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SignInfo>;
}
export interface IUpdate extends IRegister {
deactivate: boolean;
}
export {};
//# sourceMappingURL=IDid.d.ts.map
2 changes: 1 addition & 1 deletion build/src/ssiApi/services/IDid.d.ts.map

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

11 changes: 10 additions & 1 deletion build/src/ssiApi/services/did/did.service.d.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
registerDid(params: IRegister): Promise<{
didDocument: Did;
transactionHash: string;
}>;
resolveDid(params: {
did: string;
}): Promise<IDIDResolve>;
}
//# sourceMappingURL=did.service.d.ts.map
2 changes: 1 addition & 1 deletion build/src/ssiApi/services/did/did.service.d.ts.map

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

103 changes: 92 additions & 11 deletions build/src/ssiApi/services/did/did.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
10 changes: 10 additions & 0 deletions src/ssiApi/api-constant.ts
Original file line number Diff line number Diff line change
@@ -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',
},
};
33 changes: 17 additions & 16 deletions src/ssiApi/apiAuth/apiAuth.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -9,21 +10,21 @@ export class ApiAuth {
this.apiKey = apiKey
}
async generateAccessToken(): Promise<IgenerateToken> {
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


}
}
24 changes: 23 additions & 1 deletion src/ssiApi/services/IDid.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SignInfo>

}


export interface IUpdate extends IRegister{
deactivate:boolean
}
Loading

0 comments on commit 29a9b4d

Please sign in to comment.