Skip to content

Commit

Permalink
updated the schema search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwas1 committed Apr 10, 2021
1 parent f895dd5 commit fa6818a
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 109 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const { did, schema, credential } = hsSdk;
schemaUrl: string;
generateSchema({ name, author, description, properties }: ISchema): Promise<ISchemaTemplate>;
registerSchema(schema: ISchemaTemplate): Promise<any>;
getSchema(schemaId: string): Promise<any>;
getSchema(options: {schemaId?: string, author?: string}): Promise<any>;

```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hs-ssi-sdk",
"version": "5.0.0",
"version": "5.0.1",
"description": "sdk is an implementation of proposed DID by W3C",
"main": "dist/index.js",
"scripts": {
Expand Down
182 changes: 106 additions & 76 deletions src/schema/schema.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,126 @@
import { ISchema, ISchemaTemplateSchema, ISchemaTemplate} from './ISchema';
import { v4 as uuidv4 } from 'uuid';
import * as constant from '../constants'
import Utils from '../utils';
import { ISchema, ISchemaTemplateSchema, ISchemaTemplate } from "./ISchema";
import { v4 as uuidv4 } from "uuid";
import * as constant from "../constants";
import Utils from "../utils";
import axios from "axios";
import IOptions from '../IOptions';
import IOptions from "../IOptions";

const SC_PREFIX = "sch_";
const SCHEMA_URL = "https://json-schema.org/draft-07/schema#";
const W3_SCHEMA_JSON_URL = "https://w3c-ccg.github.io/vc-json-schemas/schema/1.0/schema.json";
const W3_SCHEMA_JSON_URL =
"https://w3c-ccg.github.io/vc-json-schemas/schema/1.0/schema.json";

class SchemaTemplateSchema implements ISchemaTemplateSchema{
$schema: string;
description: string;
type: string;
properties: any;
required: Array<string>;
additionalProperties: boolean
constructor({properties, additionalProperties, description}: ISchemaTemplateSchema){
this.$schema = SCHEMA_URL;
this.description = description;
this.type = "object";
this.properties = {};
this.required = Object.keys(properties); // TODO: right now all requried... later we can change this.
this.additionalProperties = additionalProperties as boolean;
this.required.forEach(key => {
this.properties[`${key}`] = {
type: typeof properties[key] ? typeof properties[key] : "string"
}
});
}
class SchemaTemplateSchema implements ISchemaTemplateSchema {
$schema: string;
description: string;
type: string;
properties: any;
required: Array<string>;
additionalProperties: boolean;
constructor({
properties,
additionalProperties,
description,
}: ISchemaTemplateSchema) {
this.$schema = SCHEMA_URL;
this.description = description;
this.type = "object";
this.properties = {};
this.required = Object.keys(properties); // TODO: right now all requried... later we can change this.
this.additionalProperties = additionalProperties as boolean;
this.required.forEach((key) => {
this.properties[`${key}`] = {
type: typeof properties[key] ? typeof properties[key] : "string",
};
});
}

get(): ISchemaTemplateSchema{
return this;
}
get(): ISchemaTemplateSchema {
return this;
}
}

export interface IScheme{
schemaUrl: string;
generateSchema({ name, author, description, properties }: ISchema): Promise<ISchemaTemplate>;
registerSchema(schema: ISchemaTemplate): Promise<any>;
getSchema(schemaId: string): Promise<any>;
export interface IScheme {
schemaUrl: string;
generateSchema({
name,
author,
description,
properties,
}: ISchema): Promise<ISchemaTemplate>;
registerSchema(schema: ISchemaTemplate): Promise<any>;
getSchema(options: {schemaId?: string, author?: string}): Promise<any>;
}

export default class Schema implements IScheme{
private utils: Utils;
schemaUrl: string;
constructor(options: IOptions) {
this.utils = new Utils({ nodeUrl: options.nodeUrl });
this.schemaUrl = this.utils.nodeurl + constant.HYPERSIGN_NETWORK_SCHEMA_EP;
}
export default class Schema implements IScheme {
private utils: Utils;
schemaUrl: string;
constructor(options: IOptions) {
this.utils = new Utils({ nodeUrl: options.nodeUrl });
this.schemaUrl = this.utils.nodeurl + constant.HYPERSIGN_NETWORK_SCHEMA_EP;
}

public async generateSchema({ name, author, description, properties }: ISchema): Promise<ISchemaTemplate>{
let newSchema: ISchemaTemplate = {} as ISchemaTemplate;
public async generateSchema({
name,
author,
description,
properties,
}: ISchema): Promise<ISchemaTemplate> {
let newSchema: ISchemaTemplate = {} as ISchemaTemplate;

const didDoc = await this.utils.resolve(author);
if(!didDoc) throw new Error("Could not resolve author did");
const didDoc = await this.utils.resolve(author);
if (!didDoc) throw new Error("Could not resolve author did");

newSchema.type = W3_SCHEMA_JSON_URL;
newSchema.modelVersion = "v1.0";
newSchema.id = SC_PREFIX + uuidv4();;
newSchema.name = name;
newSchema.author = author;
newSchema.authored = new Date().toString();
newSchema.schema = (new SchemaTemplateSchema({properties, additionalProperties: false , description})).get();
return newSchema;
}
newSchema.type = W3_SCHEMA_JSON_URL;
newSchema.modelVersion = "v1.0";
newSchema.id = SC_PREFIX + uuidv4();
newSchema.name = name;
newSchema.author = author;
newSchema.authored = new Date().toString();
newSchema.schema = new SchemaTemplateSchema({
properties,
additionalProperties: false,
description,
}).get();
return newSchema;
}

public async registerSchema(schema: ISchemaTemplate): Promise<any>{
try{
const response = await axios.post(this.schemaUrl, schema);
return response.data;
}catch(e){
const { response } = e;
return response.data;
}

public async registerSchema(schema: ISchemaTemplate): Promise<any> {
try {
const response = await axios.post(this.schemaUrl, schema);
return response.data;
} catch (e) {
const { response } = e;
return response.data;
}
}

public async getSchema(schemaId: string): Promise<any>{
try{
const get_didUrl = this.schemaUrl + schemaId;
const response = await axios.get(get_didUrl);
return response.data;
}catch(e){
const { response } = e;
return response.data;
}

}

}
public async getSchema(options: {schemaId?: string, author?: string}): Promise<any> {
try {
let get_didUrl = "";
const { author, schemaId } = options;

if(author != undefined && schemaId != undefined){ // 1,1
get_didUrl = this.schemaUrl + schemaId + "?author=" + author;
}

else if(author == undefined && schemaId != undefined){ // 1,0
get_didUrl = this.schemaUrl + schemaId;
}

else if(author != undefined && schemaId == undefined){ // 0,1
get_didUrl = this.schemaUrl + "?author=" + author;
}

else if(author == undefined && schemaId == undefined){ // 0,0
get_didUrl = this.schemaUrl;
}

const response = await axios.get(get_didUrl);
return response.data;
} catch (e) {
const { response } = e;
return response.data;
}
}
}
57 changes: 36 additions & 21 deletions test/credential.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,66 @@ const sdkVc = hsSdk.credential

const challenge = "ch_adbshd131323"
let sCredential = {}
const schemaUrl = "http://localhost:5000/api/v1/schema/sch_f3ab4b78-48fa-4a4d-9fe4-8f06dc501a6b"
const schemaUrl = "http://localhost:5000/api/v1/schema/sch_4bcf878a-4751-4401-af19-d5f620d47960"

// let attributesMap = {
// name: "Vishwas Anand",
// email: "vishu.anand1@gmail.com",
// phoneNumber: "+91-123123123213"
// }


let attributesMap = {}

attributesMap['myString'] = "Vishwas Anand";
attributesMap['myNumner'] = 12;
attributesMap['myBool'] = false;

let attributesMap = {
"name": "IDO App",
"did": "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51",
"owner": "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51",
"schemaId": "sch_d0d8488d-5bad-42fd-8ac3-7066b473bbf5",
"serviceEp": "http://192.168.43.43:4006",
"subscriptionId": "subs_9eda0fab-82d7-4",
"planId": "pln_1ee9aa7b-95b3-42",
"planName": "Tier 1",
}
const issuerKeys = {
"publicKey": {
"@context": "https://w3id.org/security/v2",
"id": "did:v2:hs:3b29a5f3-8cb2-4603-8fef-b40eccbb798b#z6Mkh3BfBASqPDLrRRqSMEsDQsMantmPFq98ti7uyPz2ryTA",
"id": "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51#z6MkjAwRoZNV1oifLPb2GzLatZ7sVxY5jZ16xYTTAgSgCqQQ",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "3avcavCQ3frPJvzjffuNZmoayKVXqwtnChCz9821wkfn"
"publicKeyBase58": "5igPDK83gGECDtkKbRNk3TZsgPGEKfkkGXYXLQUfHcd2"
},
"privateKeyBase58": "5YUTrfquiGoMPaZnT1jeRyMQgsBy43dkwdXzywTWkhmhnyg7BW8r5f9wU71jKsuh8i49iFvBxae75DjJEkqJhQuJ"
}
const holderKeys = {
"publicKey": {
"@context": "https://w3id.org/security/v2",
"id": "did:v2:hs:3ea9975f-4726-479c-b69e-8f5c2e8cbc23#z6Mkn79DTEh6Fo73A2LTEYumAjGkHzqVLupMxyPGtv7tmxZs",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "8etArzSevFca3XVkYywvKdikURZdw2a1GxUM4e9srjnV"
},
"privateKeyBase58": "3ApK2iC4sJoEQK6KrNh4g2ZzjtU7inoUYHdB1QFEezNm6n73Tw5YKx5UjYjs44yeASviyDtQaXnVnH8U43zM9ee9"
"privateKeyBase58": "34uPqEWKuz4MxaPFZtRR4zCFZKrKsn3gEQgHop4crSArPZ3LHQ2HJq8jh39d6Aa7Jnqftnv6BxqCtxyq4izU2TPz"
}
// const holderKeys = {
// "publicKey": {
// "@context": "https://w3id.org/security/v2",
// "id": "did:hs:894865ee-4b45-40df-bda6-f8ee7750b908#z6Mkkhgzxs3zMjsPK8cBvPnvnzbzXFHMLx7TeXc3xZsfzTqS",
// "type": "Ed25519VerificationKey2018",
// "publicKeyBase58": "7FRxNcoZ2CNvCdmVEpq5wu3zhg1Vw4s6xWh88Huf5F44"
// },
// "privateKeyBase58": "4sN9b9rDWqXjVSiEcJoHdc2RyhxbtWVwowiKBDrmjisyUrJzaqhkL32DJv2Lez9mszK6KeSsTbuHdmhsDkpoVjLL"
// }

const holderKeys = issuerKeys;

sdkVc.generateCredential(schemaUrl, {
subjectDid: holderKeys.publicKey.id,
issuerDid: issuerKeys.publicKey.id,
expirationDate: new Date().toISOString(),
attributesMap
}).then(credential => {
// console.log(credential)
console.log(credential)
console.log("Credentials end=======================================")
console.log("SignedCredential start=======================================")
return sdkVc.signCredential(credential, issuerKeys.publicKey.id, issuerKeys.privateKeyBase58)
}).then(signedCredential => {
// console.log(signedCredential)
// console.log(JSON.stringify(signedCredential))
sCredential = signedCredential;
console.log("SignedCredential end=======================================")
console.log("VerifyCredential start=======================================")
console.log(sCredential)
return sdkVc.verifyCredential(signedCredential, issuerKeys.publicKey.id)
}).then(result => {
// console.log(result)
console.log(result)
console.log("VerifyCredential end=======================================")
console.log("Presentation start=======================================")
return sdkVc.generatePresentation(sCredential, holderKeys.publicKey.id)
Expand Down
3 changes: 1 addition & 2 deletions test/did.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ did.getDid({
}).then(res => {
console.log(JSON.stringify(res, null, 2))
const { didDoc } = res;
// delete didDoc["id"];
return did.register(didDoc);
}).then(res => {
console.log(JSON.stringify(res, null, 2))
// console.log(JSON.stringify(res, null, 2))
const {did: dcentId} = res;
return did.resolve(dcentId);
}).then(res => {
Expand Down
Loading

0 comments on commit fa6818a

Please sign in to comment.