Skip to content

Commit

Permalink
Feature/ver 72 (#27)
Browse files Browse the repository at this point in the history
- added schema 
- added tests for schema and credential test creation and fetching
  • Loading branch information
Ellenn-A authored Aug 30, 2023
1 parent ffbb2bb commit 26a42e9
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 4 deletions.
33 changes: 33 additions & 0 deletions README_VERITABLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,36 @@ So in this case when a connection event is triggered, it will be sent to: http:/
The payload of the webhook contains the serialized record related to the topic of the event. For the `connections` topic this will be a `ConnectionRecord`, for the `credentials` topic it will be a `CredentialRecord`, and so on.

For the WebSocket clients, the events are sent as JSON stringified objects

### Schema Definition

The repo contains 'schema' folder with a schema body json which can be imported into ts files like so:

```
import _schema from './schema/schemaAttributes.json'
const schema = _schema as AnonCredsSchema
```

The Json file contains attributes required for a schema to be registered.
The attributes are:

- checkName => what kind of check this is (e.g. NASDAQ, Rev >500, Production Capacity etc)
- companyName => name of the company that is being checked (max 200 characters)
- companiesHouseNumber => unique identifier for companies provided by Companiess House (max 8 characters - alphanumeric)
- issueDate => date when this check was issued (dateInt e.g. 20230101 for 1st Jan 2023)
- expiryDate => date when this check expires (dateInt e.g. 20230101 for 1st Jan 2023)

The schema definition can be posted to `/schemas` to register it. Upon a successful call a response is returned with an 'id' property which refers to this schema and can be used to refer to the schema when creating a new credential definition like so:

```
{
"tag": "myTestDefinition",
"schemaId": "ipfs://example",
"issuerId": "did:key:exampleDidKey"
}
```

(Note: Each credential definition is unique, because a different set of cryptographic materials is created each time.)

A credential definition can then be used to issue a credential which contains both information about an issuer of a credential and the check itself.
(Note: Because the schema and definition is saved on ipfs. One must have an instance of ipfs running or be connected to global ipfs when registering a schema and definition.)
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/veritable-cloudagent",
"version": "0.3.1",
"version": "0.4.0",
"main": "build/index",
"types": "build/index",
"files": [
Expand Down
13 changes: 13 additions & 0 deletions schema/schemaAttributes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"issuerId": "did:key:z6MkrDn3MqmedCnj4UPBwZ7nLTBmK9T9BwB3njFmQRUqoFn1",
"version": "1.0",
"name":"string",
"attrNames": [
"checkName",
"companyName" ,
"companiesHouseNumber",
"issueDate",
"expiryDate"]

}

2 changes: 1 addition & 1 deletion src/routes/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@
},
"info": {
"title": "@digicatapult/veritable-cloudagent",
"version": "0.3.1",
"version": "0.4.0",
"description": "Rest endpoint wrapper for using your agent over HTTP",
"license": {
"name": "Apache-2.0"
Expand Down
78 changes: 78 additions & 0 deletions tests/credentialDefinition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { setupServer } from '../src/server'

import { getTestAgent, getTestCredDef, getTestSchema } from './utils/helpers'

import _schema from '../schema/schemaAttributes.json'
const schema = _schema as AnonCredsSchema

describe('CredentialDefinitionController', () => {
let app: Express
let agent: RestAgent
Expand Down Expand Up @@ -82,12 +85,47 @@ describe('CredentialDefinitionController', () => {
})

test('should return 404 NotFound when credential definition not found', async () => {
const spy = stub(agent.modules.anoncreds, 'getCredentialDefinition')
spy.resolves({
credentialDefinitionId: 'x',
credentialDefinitionMetadata: {},
resolutionMetadata: {
error: 'notFound',
},
})

const id = encodeURIComponent('ipfs://QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR')
const response = await request(app).get(`/credential-definitions/${id}`)
expect(response.statusCode).to.be.equal(404)
})
})

describe('get credential definition by id using schema definition json', () => {
test('should return credential definition ', async () => {
testCredDef.schemaId = 'ipfs://bafkreihiz6z2hcostlo73fycbflmhsayoyuqkicwinpqc7knxwmhhwahqq'
const spy = stub(agent.modules.anoncreds, 'getCredentialDefinition')
spy.resolves({
credentialDefinitionId: 'WgWxqztrNooG92RXvxSTWv:3:CL:20:tag',
credentialDefinitionMetadata: {},
resolutionMetadata: {},
credentialDefinition: testCredDef,
})
const getResult = () => ({
id: 'WgWxqztrNooG92RXvxSTWv:3:CL:20:tag',
...testCredDef,
})

const response = await request(app).get(`/credential-definitions/WgWxqztrNooG92RXvxSTWv:3:CL:20:tag`)
const result = await getResult()

expect(response.statusCode).to.be.equal(200)
expect(response.body.id).to.deep.equal(result.id)
expect(response.body.schemaId).to.deep.equal(result.schemaId)
expect(response.body.tag).to.deep.equal(result.tag)
expect(response.body.type).to.deep.equal(result.type)
})
})

describe('create credential definition', () => {
test('should return created credential definition ', async () => {
const registerCredentialDefinitionSpy = stub(agent.modules.anoncreds, 'registerCredentialDefinition')
Expand Down Expand Up @@ -140,6 +178,46 @@ describe('CredentialDefinitionController', () => {
})
})

describe('create credential definition using new json schema ', () => {
test('should return created credential definitionusing new json ', async () => {
const registerSchemaStub = stub(agent.modules.anoncreds, 'registerSchema')
const registerCredentialDefinitionStub = stub(agent.modules.anoncreds, 'registerCredentialDefinition')
// mock out getSchema from CredentialDefinitionController
const getSchemaStub = stub(agent.modules.anoncreds, 'getSchema').resolves({
resolutionMetadata: {},
schemaMetadata: {},
schemaId: 'ipfs://bafkreihiz6z2hcostlo73fycbflmhsayoyuqkicwinpqc7knxwmhhwahqq',
schema: schema,
})

testCredDef.schemaId = 'ipfs://bafkreihiz6z2hcostlo73fycbflmhsayoyuqkicwinpqc7knxwmhhwahqq'

registerCredentialDefinitionStub.resolves({
credentialDefinitionState: {
state: 'finished',
credentialDefinition: testCredDef,
credentialDefinitionId: `ipfs://bafkreiafhhldn47xkq4r5frf4lgaqqq35h66xikhttp5s7g5peotmi2szu`,
},
registrationMetadata: {},
credentialDefinitionMetadata: {},
})

const getCredentialDefResult = () => ({
id: 'ipfs://bafkreiafhhldn47xkq4r5frf4lgaqqq35h66xikhttp5s7g5peotmi2szu',
...testCredDef,
})

const responseCredentialDeff = await request(app).post(`/credential-definitions/`).send({
issuerId: testCredDef.issuerId,
schemaId: testCredDef.schemaId,
tag: testCredDef.tag,
})

expect(responseCredentialDeff.statusCode).to.be.equal(200)
expect(responseCredentialDeff.body).to.deep.equal(getCredentialDefResult())
})
})

after(async () => {
await agent.shutdown()
await agent.wallet.delete()
Expand Down
50 changes: 50 additions & 0 deletions tests/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import request from 'supertest'
import { setupServer } from '../src/server'

import { getTestAgent, getTestSchema } from './utils/helpers'
import _schema from '../schema/schemaAttributes.json'
const schema = _schema as AnonCredsSchema

describe('SchemaController', () => {
let app: Express
Expand Down Expand Up @@ -95,6 +97,29 @@ describe('SchemaController', () => {
})
})

describe('get schema by id using schema definition', () => {
test('should return schema', async () => {
const getSchemaStub = stub(agent.modules.anoncreds, 'getSchema')
getSchemaStub.resolves({
schemaId: 'WgWxqztrNooG92RXvxSTWv:2:test:1.0',
schema: schema,
schemaMetadata: {},
resolutionMetadata: {},
})

const getResult = () => ({
id: 'WgWxqztrNooG92RXvxSTWv:2:test:1.0',
...schema,
})

const response = await request(app).get(`/schemas/WgWxqztrNooG92RXvxSTWv:2:test:1.0`)
const result = await getResult()

expect(response.statusCode).to.be.equal(200)
expect(response.body).to.deep.equal(result)
})
})

describe('create schema', () => {
test('should return created schema ', async () => {
const registerSchemaStub = stub(agent.modules.anoncreds, 'registerSchema')
Expand Down Expand Up @@ -129,6 +154,31 @@ describe('SchemaController', () => {
expect(response.statusCode).to.be.equal(422)
})
})
describe('create schema using new json schema ', () => {
test('should return created schema using new json ', async () => {
const registerSchemaStub = stub(agent.modules.anoncreds, 'registerSchema')
//register schema
registerSchemaStub.resolves({
schemaState: {
state: 'finished',
schema: schema,
schemaId: 'WgWxqztrNooG92RXvxSTWv:2:test:1.0',
},
registrationMetadata: {},
schemaMetadata: {},
})

const getResult = () => ({
id: 'WgWxqztrNooG92RXvxSTWv:2:test:1.0',
...schema,
})

const response = await request(app).post(`/schemas/`).send(schema)

expect(response.statusCode).to.be.equal(200)
expect(response.body).to.deep.equal(getResult())
})
})

after(async () => {
await agent.shutdown()
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"paths": {
"@aries-framework/rest": ["./src"]
},
"esModuleInterop": true,
"resolveJsonModule": true,
"types": ["mocha", "node"]
},
"exclude": ["node_modules", "build"]
Expand Down

0 comments on commit 26a42e9

Please sign in to comment.