Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added api call for generating access token and did #98

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions build/src/ssiApi/apiAuth/IAuth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IAuth {
generateAccessToken(): Promise<IgenerateToken>;
}
export interface IgenerateToken {
access_token: string;
expiresIn: number;
tokenType: string;
}
//# sourceMappingURL=IAuth.d.ts.map
1 change: 1 addition & 0 deletions build/src/ssiApi/apiAuth/IAuth.d.ts.map

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

2 changes: 2 additions & 0 deletions build/src/ssiApi/apiAuth/IAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
7 changes: 7 additions & 0 deletions build/src/ssiApi/apiAuth/apiAuth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IgenerateToken } from "./IAuth";
export declare class ApiAuth {
private apiKey;
constructor(apiKey: any);
generateAccessToken(): Promise<IgenerateToken>;
}
//# sourceMappingURL=apiAuth.d.ts.map
1 change: 1 addition & 0 deletions 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.

45 changes: 45 additions & 0 deletions build/src/ssiApi/apiAuth/apiAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiAuth = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
class ApiAuth {
constructor(apiKey) {
if (!apiKey || apiKey.trim() === "") {
throw new Error("HID-SSI_SDK:: Error: Please Provide apiKey");
}
this.apiKey = apiKey;
}
generateAccessToken() {
return __awaiter(this, void 0, void 0, function* () {
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 = yield (0, node_fetch_1.default)(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 = yield response.json();
return authToken;
});
}
}
exports.ApiAuth = ApiAuth;
13 changes: 13 additions & 0 deletions build/src/ssiApi/services/IDid.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IKeyType, IVerificationRelationships } from "../../did/IDID";
export interface IGenerateDid {
namespace: string;
methodSpecificId?: string;
options?: {
keyType: IKeyType;
chainId: string;
publicKey: string;
walletAddress: string;
verificationRelationships: IVerificationRelationships[];
};
}
//# sourceMappingURL=IDid.d.ts.map
1 change: 1 addition & 0 deletions 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.

2 changes: 2 additions & 0 deletions build/src/ssiApi/services/IDid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
9 changes: 9 additions & 0 deletions build/src/ssiApi/services/did/did.service.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IGenerateDid } from "../IDid";
export declare class DID {
private authService;
private accessToken;
constructor(apiKey: string);
private initAccessToken;
generateDid(params: IGenerateDid): Promise<any>;
}
//# sourceMappingURL=did.service.d.ts.map
1 change: 1 addition & 0 deletions 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.

55 changes: 55 additions & 0 deletions build/src/ssiApi/services/did/did.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DID = void 0;
const apiAuth_1 = require("../../apiAuth/apiAuth");
const node_fetch_1 = __importDefault(require("node-fetch"));
class DID {
constructor(apiKey) {
this.authService = new apiAuth_1.ApiAuth(apiKey);
this.initAccessToken();
}
initAccessToken() {
return __awaiter(this, void 0, void 0, function* () {
const accessToken = yield this.authService.generateAccessToken();
this.accessToken = accessToken.access_token;
});
}
generateDid(params) {
return __awaiter(this, void 0, void 0, function* () {
const apiUrl = "https://api.entity.hypersign.id/api/v1/did/create";
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${this.accessToken}`,
Origin: "https://entity.hypersign.id"
};
const requestOptions = {
method: "POST",
headers,
body: JSON.stringify(Object.assign({}, params))
};
const response = yield (0, node_fetch_1.default)(apiUrl, requestOptions);
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');
}
const { metaData } = yield response.json();
return metaData.didDocument;
});
}
}
exports.DID = DID;
// const test = new DID("c4dae4b264f98798920ad22456c6f.acd24a3adeaed7fddf86044925d419afc252ada299b018e961ed3ac3ae2d1aaebd577eb0550c2c011fd9f51d33097d88a")
const test = new DID("586a74602fffda655f186ca9c162f.445dbab93f8b3b24501a9bdb1254de6a1528d4db306d87e2674f2277ddcc40a5b6686c4b2fe996ae11509d72eb4bb0d38");
console.log(test, "test");
9 changes: 9 additions & 0 deletions src/ssiApi/apiAuth/IAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IAuth{
generateAccessToken():Promise<IgenerateToken>
}

export interface IgenerateToken{
access_token:string;
expiresIn:number;
tokenType:string;
}
29 changes: 29 additions & 0 deletions src/ssiApi/apiAuth/apiAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fetch from "node-fetch"
import { IgenerateToken } from "./IAuth";
export class ApiAuth {
private apiKey: string;
constructor(apiKey) {
if (!apiKey || apiKey.trim() === "") {
throw new Error("HID-SSI_SDK:: Error: Please Provide apiKey")
}
this.apiKey = apiKey
}
async generateAccessToken(): Promise<IgenerateToken> {
const studioApiUrl = "https://api.entity.hypersign.id/api/v1/app/oauth"
Copy link
Contributor

@Vishwas1 Vishwas1 Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

studioApiUrl should not be hardcoded here, put it in config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya i have added it in separate file now. Yet not commited

const headers = {
"X-Api-Secret-Key": this.apiKey,
"Origin": "https://entity.hypersign.id"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Origin should not also be hardcoded, I guess you need to take this in constructor params

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need of passing origin. Its working fine without passing origin also

}
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
}
}
13 changes: 13 additions & 0 deletions src/ssiApi/services/IDid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IKeyType, IVerificationRelationships } from "../../did/IDID";

export interface IGenerateDid{
namespace:string;
methodSpecificId?:string;
options?:{
keyType:IKeyType,
chainId:string,
publicKey:string,
walletAddress:string,
verificationRelationships:IVerificationRelationships[]
}
}
47 changes: 47 additions & 0 deletions src/ssiApi/services/did/did.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { IAuth } from "../../apiAuth/IAuth"
import { ApiAuth } from "../../apiAuth/apiAuth"
import { IGenerateDid } from "../IDid"
import fetch from "node-fetch"

export class DID {
private authService: IAuth
private accessToken
constructor(apiKey: string) {
this.authService = new ApiAuth(apiKey);
this.initAccessToken()
}

private async initAccessToken() {
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also should not be hardcoded here

const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${this.accessToken}`,
Origin: "https://entity.hypersign.id"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take this in constructor params

}
const requestOptions = {
method: "POST",
headers,
body: JSON.stringify({ ...params })
}
const response = await fetch(apiUrl, requestOptions)
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')
}
const { metaData } = await response.json()
return metaData.didDocument
}

}




// const test = new DID("c4dae4b264f98798920ad22456c6f.acd24a3adeaed7fddf86044925d419afc252ada299b018e961ed3ac3ae2d1aaebd577eb0550c2c011fd9f51d33097d88a")
const test = new DID("586a74602fffda655f186ca9c162f.445dbab93f8b3b24501a9bdb1254de6a1528d4db306d87e2674f2277ddcc40a5b6686c4b2fe996ae11509d72eb4bb0d38")

console.log(test, "test")
Loading