Skip to content

Commit fa6818a

Browse files
committed
updated the schema search functionality
1 parent f895dd5 commit fa6818a

File tree

6 files changed

+209
-109
lines changed

6 files changed

+209
-109
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const { did, schema, credential } = hsSdk;
5858
schemaUrl: string;
5959
generateSchema({ name, author, description, properties }: ISchema): Promise<ISchemaTemplate>;
6060
registerSchema(schema: ISchemaTemplate): Promise<any>;
61-
getSchema(schemaId: string): Promise<any>;
61+
getSchema(options: {schemaId?: string, author?: string}): Promise<any>;
6262

6363
```
6464

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hs-ssi-sdk",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"description": "sdk is an implementation of proposed DID by W3C",
55
"main": "dist/index.js",
66
"scripts": {

src/schema/schema.ts

Lines changed: 106 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,126 @@
1-
import { ISchema, ISchemaTemplateSchema, ISchemaTemplate} from './ISchema';
2-
import { v4 as uuidv4 } from 'uuid';
3-
import * as constant from '../constants'
4-
import Utils from '../utils';
1+
import { ISchema, ISchemaTemplateSchema, ISchemaTemplate } from "./ISchema";
2+
import { v4 as uuidv4 } from "uuid";
3+
import * as constant from "../constants";
4+
import Utils from "../utils";
55
import axios from "axios";
6-
import IOptions from '../IOptions';
6+
import IOptions from "../IOptions";
77

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

12-
class SchemaTemplateSchema implements ISchemaTemplateSchema{
13-
$schema: string;
14-
description: string;
15-
type: string;
16-
properties: any;
17-
required: Array<string>;
18-
additionalProperties: boolean
19-
constructor({properties, additionalProperties, description}: ISchemaTemplateSchema){
20-
this.$schema = SCHEMA_URL;
21-
this.description = description;
22-
this.type = "object";
23-
this.properties = {};
24-
this.required = Object.keys(properties); // TODO: right now all requried... later we can change this.
25-
this.additionalProperties = additionalProperties as boolean;
26-
this.required.forEach(key => {
27-
this.properties[`${key}`] = {
28-
type: typeof properties[key] ? typeof properties[key] : "string"
29-
}
30-
});
31-
}
13+
class SchemaTemplateSchema implements ISchemaTemplateSchema {
14+
$schema: string;
15+
description: string;
16+
type: string;
17+
properties: any;
18+
required: Array<string>;
19+
additionalProperties: boolean;
20+
constructor({
21+
properties,
22+
additionalProperties,
23+
description,
24+
}: ISchemaTemplateSchema) {
25+
this.$schema = SCHEMA_URL;
26+
this.description = description;
27+
this.type = "object";
28+
this.properties = {};
29+
this.required = Object.keys(properties); // TODO: right now all requried... later we can change this.
30+
this.additionalProperties = additionalProperties as boolean;
31+
this.required.forEach((key) => {
32+
this.properties[`${key}`] = {
33+
type: typeof properties[key] ? typeof properties[key] : "string",
34+
};
35+
});
36+
}
3237

33-
get(): ISchemaTemplateSchema{
34-
return this;
35-
}
38+
get(): ISchemaTemplateSchema {
39+
return this;
40+
}
3641
}
3742

38-
export interface IScheme{
39-
schemaUrl: string;
40-
generateSchema({ name, author, description, properties }: ISchema): Promise<ISchemaTemplate>;
41-
registerSchema(schema: ISchemaTemplate): Promise<any>;
42-
getSchema(schemaId: string): Promise<any>;
43+
export interface IScheme {
44+
schemaUrl: string;
45+
generateSchema({
46+
name,
47+
author,
48+
description,
49+
properties,
50+
}: ISchema): Promise<ISchemaTemplate>;
51+
registerSchema(schema: ISchemaTemplate): Promise<any>;
52+
getSchema(options: {schemaId?: string, author?: string}): Promise<any>;
4353
}
4454

45-
export default class Schema implements IScheme{
46-
private utils: Utils;
47-
schemaUrl: string;
48-
constructor(options: IOptions) {
49-
this.utils = new Utils({ nodeUrl: options.nodeUrl });
50-
this.schemaUrl = this.utils.nodeurl + constant.HYPERSIGN_NETWORK_SCHEMA_EP;
51-
}
55+
export default class Schema implements IScheme {
56+
private utils: Utils;
57+
schemaUrl: string;
58+
constructor(options: IOptions) {
59+
this.utils = new Utils({ nodeUrl: options.nodeUrl });
60+
this.schemaUrl = this.utils.nodeurl + constant.HYPERSIGN_NETWORK_SCHEMA_EP;
61+
}
5262

53-
public async generateSchema({ name, author, description, properties }: ISchema): Promise<ISchemaTemplate>{
54-
let newSchema: ISchemaTemplate = {} as ISchemaTemplate;
63+
public async generateSchema({
64+
name,
65+
author,
66+
description,
67+
properties,
68+
}: ISchema): Promise<ISchemaTemplate> {
69+
let newSchema: ISchemaTemplate = {} as ISchemaTemplate;
5570

56-
const didDoc = await this.utils.resolve(author);
57-
if(!didDoc) throw new Error("Could not resolve author did");
71+
const didDoc = await this.utils.resolve(author);
72+
if (!didDoc) throw new Error("Could not resolve author did");
5873

59-
newSchema.type = W3_SCHEMA_JSON_URL;
60-
newSchema.modelVersion = "v1.0";
61-
newSchema.id = SC_PREFIX + uuidv4();;
62-
newSchema.name = name;
63-
newSchema.author = author;
64-
newSchema.authored = new Date().toString();
65-
newSchema.schema = (new SchemaTemplateSchema({properties, additionalProperties: false , description})).get();
66-
return newSchema;
67-
}
74+
newSchema.type = W3_SCHEMA_JSON_URL;
75+
newSchema.modelVersion = "v1.0";
76+
newSchema.id = SC_PREFIX + uuidv4();
77+
newSchema.name = name;
78+
newSchema.author = author;
79+
newSchema.authored = new Date().toString();
80+
newSchema.schema = new SchemaTemplateSchema({
81+
properties,
82+
additionalProperties: false,
83+
description,
84+
}).get();
85+
return newSchema;
86+
}
6887

69-
public async registerSchema(schema: ISchemaTemplate): Promise<any>{
70-
try{
71-
const response = await axios.post(this.schemaUrl, schema);
72-
return response.data;
73-
}catch(e){
74-
const { response } = e;
75-
return response.data;
76-
}
77-
88+
public async registerSchema(schema: ISchemaTemplate): Promise<any> {
89+
try {
90+
const response = await axios.post(this.schemaUrl, schema);
91+
return response.data;
92+
} catch (e) {
93+
const { response } = e;
94+
return response.data;
7895
}
96+
}
7997

80-
public async getSchema(schemaId: string): Promise<any>{
81-
try{
82-
const get_didUrl = this.schemaUrl + schemaId;
83-
const response = await axios.get(get_didUrl);
84-
return response.data;
85-
}catch(e){
86-
const { response } = e;
87-
return response.data;
88-
}
89-
90-
}
91-
92-
}
98+
public async getSchema(options: {schemaId?: string, author?: string}): Promise<any> {
99+
try {
100+
let get_didUrl = "";
101+
const { author, schemaId } = options;
93102

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

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

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

115+
else if(author == undefined && schemaId == undefined){ // 0,0
116+
get_didUrl = this.schemaUrl;
117+
}
118+
119+
const response = await axios.get(get_didUrl);
120+
return response.data;
121+
} catch (e) {
122+
const { response } = e;
123+
return response.data;
124+
}
125+
}
126+
}

test/credential.test.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,66 @@ const sdkVc = hsSdk.credential
88

99
const challenge = "ch_adbshd131323"
1010
let sCredential = {}
11-
const schemaUrl = "http://localhost:5000/api/v1/schema/sch_f3ab4b78-48fa-4a4d-9fe4-8f06dc501a6b"
11+
const schemaUrl = "http://localhost:5000/api/v1/schema/sch_4bcf878a-4751-4401-af19-d5f620d47960"
12+
13+
// let attributesMap = {
14+
// name: "Vishwas Anand",
15+
// email: "[email protected]",
16+
// phoneNumber: "+91-123123123213"
17+
// }
18+
1219

13-
let attributesMap = {}
14-
15-
attributesMap['myString'] = "Vishwas Anand";
16-
attributesMap['myNumner'] = 12;
17-
attributesMap['myBool'] = false;
1820

21+
let attributesMap = {
22+
"name": "IDO App",
23+
"did": "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51",
24+
"owner": "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51",
25+
"schemaId": "sch_d0d8488d-5bad-42fd-8ac3-7066b473bbf5",
26+
"serviceEp": "http://192.168.43.43:4006",
27+
"subscriptionId": "subs_9eda0fab-82d7-4",
28+
"planId": "pln_1ee9aa7b-95b3-42",
29+
"planName": "Tier 1",
30+
}
1931
const issuerKeys = {
2032
"publicKey": {
2133
"@context": "https://w3id.org/security/v2",
22-
"id": "did:v2:hs:3b29a5f3-8cb2-4603-8fef-b40eccbb798b#z6Mkh3BfBASqPDLrRRqSMEsDQsMantmPFq98ti7uyPz2ryTA",
34+
"id": "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51#z6MkjAwRoZNV1oifLPb2GzLatZ7sVxY5jZ16xYTTAgSgCqQQ",
2335
"type": "Ed25519VerificationKey2018",
24-
"publicKeyBase58": "3avcavCQ3frPJvzjffuNZmoayKVXqwtnChCz9821wkfn"
36+
"publicKeyBase58": "5igPDK83gGECDtkKbRNk3TZsgPGEKfkkGXYXLQUfHcd2"
2537
},
26-
"privateKeyBase58": "5YUTrfquiGoMPaZnT1jeRyMQgsBy43dkwdXzywTWkhmhnyg7BW8r5f9wU71jKsuh8i49iFvBxae75DjJEkqJhQuJ"
27-
}
28-
const holderKeys = {
29-
"publicKey": {
30-
"@context": "https://w3id.org/security/v2",
31-
"id": "did:v2:hs:3ea9975f-4726-479c-b69e-8f5c2e8cbc23#z6Mkn79DTEh6Fo73A2LTEYumAjGkHzqVLupMxyPGtv7tmxZs",
32-
"type": "Ed25519VerificationKey2018",
33-
"publicKeyBase58": "8etArzSevFca3XVkYywvKdikURZdw2a1GxUM4e9srjnV"
34-
},
35-
"privateKeyBase58": "3ApK2iC4sJoEQK6KrNh4g2ZzjtU7inoUYHdB1QFEezNm6n73Tw5YKx5UjYjs44yeASviyDtQaXnVnH8U43zM9ee9"
38+
"privateKeyBase58": "34uPqEWKuz4MxaPFZtRR4zCFZKrKsn3gEQgHop4crSArPZ3LHQ2HJq8jh39d6Aa7Jnqftnv6BxqCtxyq4izU2TPz"
3639
}
40+
// const holderKeys = {
41+
// "publicKey": {
42+
// "@context": "https://w3id.org/security/v2",
43+
// "id": "did:hs:894865ee-4b45-40df-bda6-f8ee7750b908#z6Mkkhgzxs3zMjsPK8cBvPnvnzbzXFHMLx7TeXc3xZsfzTqS",
44+
// "type": "Ed25519VerificationKey2018",
45+
// "publicKeyBase58": "7FRxNcoZ2CNvCdmVEpq5wu3zhg1Vw4s6xWh88Huf5F44"
46+
// },
47+
// "privateKeyBase58": "4sN9b9rDWqXjVSiEcJoHdc2RyhxbtWVwowiKBDrmjisyUrJzaqhkL32DJv2Lez9mszK6KeSsTbuHdmhsDkpoVjLL"
48+
// }
3749

50+
const holderKeys = issuerKeys;
51+
3852
sdkVc.generateCredential(schemaUrl, {
3953
subjectDid: holderKeys.publicKey.id,
4054
issuerDid: issuerKeys.publicKey.id,
4155
expirationDate: new Date().toISOString(),
4256
attributesMap
4357
}).then(credential => {
44-
// console.log(credential)
58+
console.log(credential)
4559
console.log("Credentials end=======================================")
4660
console.log("SignedCredential start=======================================")
4761
return sdkVc.signCredential(credential, issuerKeys.publicKey.id, issuerKeys.privateKeyBase58)
4862
}).then(signedCredential => {
49-
// console.log(signedCredential)
63+
// console.log(JSON.stringify(signedCredential))
5064
sCredential = signedCredential;
5165
console.log("SignedCredential end=======================================")
5266
console.log("VerifyCredential start=======================================")
67+
console.log(sCredential)
5368
return sdkVc.verifyCredential(signedCredential, issuerKeys.publicKey.id)
5469
}).then(result => {
55-
// console.log(result)
70+
console.log(result)
5671
console.log("VerifyCredential end=======================================")
5772
console.log("Presentation start=======================================")
5873
return sdkVc.generatePresentation(sCredential, holderKeys.publicKey.id)

test/did.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ did.getDid({
1414
}).then(res => {
1515
console.log(JSON.stringify(res, null, 2))
1616
const { didDoc } = res;
17-
// delete didDoc["id"];
1817
return did.register(didDoc);
1918
}).then(res => {
20-
console.log(JSON.stringify(res, null, 2))
19+
// console.log(JSON.stringify(res, null, 2))
2120
const {did: dcentId} = res;
2221
return did.resolve(dcentId);
2322
}).then(res => {

0 commit comments

Comments
 (0)