diff --git a/README.md b/README.md index b5d7313..560daaf 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ const { did, schema, credential } = hsSdk; schemaUrl: string; generateSchema({ name, author, description, properties }: ISchema): Promise; registerSchema(schema: ISchemaTemplate): Promise; - getSchema(schemaId: string): Promise; + getSchema(options: {schemaId?: string, author?: string}): Promise; ``` diff --git a/package.json b/package.json index e9ea77b..adb7469 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/schema/schema.ts b/src/schema/schema.ts index df5bc71..b6febdf 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -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; - 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; + 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; - registerSchema(schema: ISchemaTemplate): Promise; - getSchema(schemaId: string): Promise; +export interface IScheme { + schemaUrl: string; + generateSchema({ + name, + author, + description, + properties, + }: ISchema): Promise; + registerSchema(schema: ISchemaTemplate): Promise; + getSchema(options: {schemaId?: string, author?: string}): Promise; } -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{ - let newSchema: ISchemaTemplate = {} as ISchemaTemplate; + public async generateSchema({ + name, + author, + description, + properties, + }: ISchema): Promise { + 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{ - 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 { + 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{ - 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 { + 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; + } + } +} diff --git a/test/credential.test.js b/test/credential.test.js index d83bbcb..ae6c0be 100644 --- a/test/credential.test.js +++ b/test/credential.test.js @@ -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) diff --git a/test/did.test.js b/test/did.test.js index f5bd9f5..88bdc8a 100644 --- a/test/did.test.js +++ b/test/did.test.js @@ -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 => { diff --git a/test/schema.test.js b/test/schema.test.js index 79eb2e2..3e94b04 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -5,15 +5,31 @@ const hsSdk = new HypersignSsiSDK(options); const sdkSchema = hsSdk.schema; +// const props = { +// "name": "IDO App", +// "did": "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51", +// "owner": "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51", +// "schemaId": "sch_a18f90f4-36a9-41", +// "serviceEp": "http://192.168.43.43:4006", +// "subscriptionId": "subs_9eda0fab-82d7-4", +// "planId": "pln_1ee9aa7b-95b3-42", +// "planName": "Tier 1", +// } + +const props = { + "name": "", + "email": "" +} +const author1 = "did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51"; +const author2 = "did:hs:894865ee-4b45-40df-bda6-f8ee7750b908"; + +async function createSchema() { + sdkSchema.generateSchema({ - author: "did:v2:hs:c379647a-7a07-4a4c-8a47-1de96f843085", - name: "HS credential template", - description: "test", - properties: { - myString: "", - myNumner: 0, - myBool: false - } + author: author2, + name: "Hypersign App Credential", + description: "Credential for application to access Hypersign APIs", + properties: props }).then(schema => { console.log(JSON.stringify(schema, null, 2)); return sdkSchema.registerSchema(schema); @@ -21,4 +37,44 @@ sdkSchema.generateSchema({ console.log(resp); }) +} + +async function getSchemaTest () { + +const schemaId = 'sch_9702b363-b7bc-4efa-9800-3ab5460d62de'; +sdkSchema.getSchema({}).then(res => { // 0,0 + console.log("======= No, No ===========") + console.log(`All schema available: ${res.length}`); +}) + +sdkSchema.getSchema({ author: author1}).then(res => { //0,1 + console.log("======= No, Yes ===========") + console.log(`All schemas authored by ${author1} are ${res.length}`); +}) + +sdkSchema.getSchema({ author: author2}).then(res => { //0,1 + console.log("======= No, Yes ===========") + console.log(`All schemas authored by ${author2} are ${res.length}`); +}) + + +sdkSchema.getSchema({schemaId, author: author2}).then(res => { //1,1 + console.log("======= Yes, Yes ===========") + console.log(`Schema ${schemaId} authored by ${author2}:${JSON.stringify(res)}`); +}) + + +sdkSchema.getSchema({schemaId}).then(res => { //1,0 + console.log("======= Yes, No ===========") + console.log(`Schema ${schemaId} :${JSON.stringify(res)}`); +}) + +// In case of invalid schema +sdkSchema.getSchema({schemaId: schemaId + "----"}).then(res => { //1,0 + console.log("======= Error ===========") + console.log(`Schema ${schemaId + "----"} :${JSON.stringify(res)}`); +}) +} + +getSchemaTest();