Skip to content

Commit

Permalink
Merge pull request #178 from identity-com/feature/IDCOM-1383__correct…
Browse files Browse the repository at this point in the history
…_uca_schema_loading

Feature/idcom 1383  correct uca schema loading
  • Loading branch information
explicit-projects authored Nov 22, 2021
2 parents 04d4c7e + df76235 commit ca56739
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 232 deletions.
80 changes: 0 additions & 80 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,6 @@ workflows:
build-and-test:
jobs:
- build
- publish-schemas:
context: Development
requires:
- build
filters:
branches:
only:
- master
- check-schemas:
context: Development
requires:
- publish-schemas
filters:
branches:
only:
- master

build-tag:
jobs:
Expand Down Expand Up @@ -63,24 +47,6 @@ workflows:
only: /^v\d+\.\d+\.\d+.*/
branches:
ignore: /.*/
- publish-schemas:
context: Production
requires:
- deploy
filters:
tags:
only: /^v\d+\.\d+\.\d+.*/
branches:
ignore: /.*/
- check-schemas:
context: Production
requires:
- publish-schemas
filters:
tags:
only: /^v\d+\.\d+\.\d+.*/
branches:
ignore: /.*/

jobs:
build:
Expand Down Expand Up @@ -156,49 +122,3 @@ jobs:
- run:
name: Publish package
command: npm publish --access=public

publish-schemas:
docker:
# image extended from circleci/node and includes AWS CLI
- image: civicteam/circle-aws-node:12.13
# environment:
#S3_BUCKET_SCHEMA_URL: put your bucket URL here
#S3_PUBLIC_SCHEMA_URL: put your public schema URL here
#AWS_ACCESS_KEY_ID: put your token here
#AWS_SECRET_ACCESS_KEY: put your token here
working_directory: ~/repo
steps:
- checkout:
path: ~/repo
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}-{{checksum "package-lock.json"}}
- run: sudo npm install -g npm
- run: npm install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}-{{checksum "package-lock.json"}}
- run: npm run generate-schemas
- run: npm run publish-schemas

check-schemas:
docker:
# image extended from circleci/node and includes AWS CLI
- image: civicteam/circle-aws-node:12.13
# environment:
#S3_PUBLIC_SCHEMA_URL: put your public schema URL here
working_directory: ~/repo
steps:
- checkout:
path: ~/repo
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}-{{checksum "package-lock.json"}}
- run: sudo npm install -g npm
- run: npm install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}-{{checksum "package-lock.json"}}
- run: npm run check-schemas
138 changes: 34 additions & 104 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ All versions follow SemVer (https://semver.org/)
- `npm run coverage` - run code coverage and generate report in the `coverage` folder
- `npm test` - run all tests
- `npm run test:watch` - run all tests in watch mode
- `npm run generate-schema` - run the CLI command and generate all schemas


## Configuration

Expand Down Expand Up @@ -121,39 +119,15 @@ If you are not sure how to get those informations, see the tutorial down below.

A "User Collectable Attribute" is **a unit of user-related data** (attribute or knowledge) with a specific identifier that can be captured from the user normally during mobile app. A Claim once verified can be part of a Credential as Claim with the same identifier.

#### Defining new Claim

Just add a new definition entry with the [format](http://) on the definitions [file](http://)

#### Exporting Claim to the Claim Registry Services

Claim definitions are packed inside this library but also are available for public consumption at the [Claim Registry](http://) to export new defined Claims just run:

```
npm run export-definitions
```

#### Using a Claim in javascript (with this library)

##### creating Claim instances with the constructor
##### creating Claim instances with the async `create` function

`Claim(identifier, value, version)`
`Claim.create(identifier, value)`

Example
```
const name = new Claim('claim-cvc:Identity.address-v1', {
street: 'Alameda dos Anjos',
unit: '102',
city: 'Belo Horizonte',
zipCode: '94103345',
state: 'Minas Gerais',
county: 'Sao Bento',
country: 'Brazil',
}, '1');
```
Or use the shorthand
```
const name = new Claim.IdentityAddress({
const name = await Claim.create('claim-cvc:Identity.address-v1', {
street: 'Alameda dos Anjos',
unit: '102',
city: 'Belo Horizonte',
Expand Down Expand Up @@ -337,23 +311,15 @@ A Credential with an associated Proof. Every consumer of a verifiable Credential

Just add a new definition entry with the [format](http://) on the definitions [file](http://)

#### Exporting Claim to the Claim Registry Services

Credentials definitions are packed inside this library but also are available for public consumption at the [Credential Registry](http://) to export new defined Credentials

```
npm run export-definitions
```

#### Using a VerifiableCredential in javascript (with this library)

##### creating VerifiableCredential instances with the constructor

To construct a new VC you need first to get instances of all Claim dependencies
```
const name = new Claim.IdentityName({ first: 'Joao', middle: 'Barbosa', last: 'Santos' });
const dob = new Claim.IdentityDateOfBirth({ day: 20, month: 3, year: 1978 });
const cred = new VC('cvc:cred:Test', 'jest:test', null, [name, dob]);
const name = await Claim.create('claim-cvc:Identity.name-v1', { first: 'Joao', middle: 'Barbosa', last: 'Santos' });
const dob = await Claim.create('claim-cvc:Identity.dateOfBirth-v1', { day: 20, month: 3, year: 1978 });
const cred = await VC.create('cvc:cred:Test', 'jest:test', null, [name, dob]);
```

##### creating VerifiableCredential with evidence
Expand All @@ -362,8 +328,8 @@ Evidence can be included in a verifiable credential to provide the verifier with

The evidence is an optional parameter on VC construction:
```
const name = new Claim.IdentityName({ first: 'Joao', middle: 'Barbosa', last: 'Santos' });
const dob = new Claim.IdentityDateOfBirth({ day: 20, month: 3, year: 1978 });
const name = await Claim.create('claim-cvc:Identity.name-v1', { first: 'Joao', middle: 'Barbosa', last: 'Santos' });
const dob = await Claim.create('claim-cvc:Identity.dateOfBirth-v1', { day: 20, month: 3, year: 1978 });
const evidence = {
id: 'https://idv.civic.com/evidence/f2aeec97-fc0d-42bf-8ca7-0548192dxyzab',
type: ['DocumentVerification'],
Expand All @@ -373,15 +339,15 @@ const evidence = {
documentPresence: 'Digital',
};
const cred = new VC('cvc:cred:Test', 'jest:test', null, [name, dob], '1', evidence);
const cred = await VC.create('cvc:cred:Test', 'jest:test', null, [name, dob], '1', evidence);
```

##### anchoring VerifiableCredential instances with the constructor
To construct a new VC you need first to get instances of all Claim dependencies
```
const name = new Claim.IdentityName({ first: 'Joao', middle: 'Barbosa', last: 'Santos' });
const dob = new Claim.IdentityDateOfBirth({ day: 20, month: 3, year: 1978 });
const cred = new VC('cvc:cred:Test', 'jest:test', [name, dob]);
const name = await Claim.create('claim-cvc:Identity.name-v1', { first: 'Joao', middle: 'Barbosa', last: 'Santos' });
const dob = await Claim.create('claim-cvc:Identity.dateOfBirth-v1', { day: 20, month: 3, year: 1978 });
const cred = await VC.create('cvc:cred:Test', 'jest:test', [name, dob]);
cred.requestAnchor().then(() => {
//The original instance is updated
})
Expand All @@ -390,9 +356,9 @@ cred.requestAnchor().then(() => {
##### refreshing an anchor (temp => permanent) VerifiableCredential instances with the constructor
To construct a new VC you need first to get instances of all Claim dependencies
```
const name = new Claim.IdentityName({ first: 'Joao', middle: 'Barbosa', last: 'Santos' });
const dob = new Claim.IdentityDateOfBirth({ day: 20, month: 3, year: 1978 });
const cred = new VC('cvc:cred:Test', 'jest:test', [name, dob]);
const name = await Claim.create('claim-cvc:Identity.name-v1', { first: 'Joao', middle: 'Barbosa', last: 'Santos' });
const dob = await Claim.create('claim-cvc:Identity.dateOfBirth-v1', { day: 20, month: 3, year: 1978 });
const cred = await VC.create('cvc:cred:Test', 'jest:test', [name, dob]);
cred.updateAnchor().then(() => {
//The original instance is updated
})
Expand All @@ -406,7 +372,7 @@ To prevent that to happen is important that the owner always grant the usage of

The library provide ways to do both.

##### Grating
##### Granting
```js
cred.grantUsageFor(requestorId, requestId)
```
Expand Down Expand Up @@ -575,7 +541,7 @@ Where `options` may contatins:
To construct a new VC given a JSON, just use the `.fromJSON` method:
```
const credJSon = require('./ACred.json');
const cred = VC.fromJSON(credJSon);
const cred = await VC.fromJSON(credJSon);
```
Now you can access any method of a `cred` instance, like `.updateAnchor()` or `.verify()`
Expand All @@ -586,7 +552,7 @@ Remember to check the section about configuration or else this part will fail.
To verify a credential JSON, you can construct a VC using `.fromJSON` and call `.verify()` method:
```
const credJSon = require('./ACred.json');
const cred = VC.fromJSON(credJSon);
const cred = await VC.fromJSON(credJSon);
const verifiedLevel = cred.verify();
```
Expand All @@ -602,66 +568,30 @@ VERIFY_LEVELS = {
```
## Schema Generator
The json schema generator will get a previous definition and build a sample JSON (with random values).
## Loading schemas
On top of the sample data and combining the identifier properties it will infer an JSON Schema for validating the data.
Schema's can be loaded by providing a schema loader as follows:
A identifier like this:
Example
```javascript
const name = new Claim('claim-cvc:Identity.name-v1', {
first: 'Joao',
middle: 'Barbosa',
last: 'Santos'
})
```

Will generate a JSON like this:


```
{
first: 'Joao',
middle: 'Barbosa',
last: 'Santos'
}
const { schemaLoader } = require('@identity/credential-commons');
schemaLoader.addLoader(new SchemaLoader())
```

The schema generator will generate an json schema like this:

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "claim-cvc:Identity.name-v1.first",
"type": "object",
"properties": {
"first": {
"type": "string"
}
},
"required": [
"first"
],
"additionalProperties": false
The provided SchemaLoader needs to confirm to the following interface:
```javascript
interface SchemaLoader {
// Returns a boolean if the schema loader can load the supplied identitifier (in the case of multiple loaders)
valid(identifier);

// Returns the schema id for the provided identifier (e.g. http://identity.com/schemas/claim-cvc:Identity.address-v1)
schemaId(identifier);

// Load the schema based on the provided identifier and returns the schema as an object
async loadSchema(identifier)
}
```

## Publishing schemas

The Verifiable Credential Library has a script, avaiable in the *package.json*, to publish the generate schemas to a bucket in AWS. The following command will publish the schemas:
```bash
S3_BUCKET_SCHEMA_URL=<s3://your-bucket-url> npm run publish-schemas
```

There is also a script to check the published schemas:
```bash
S3_PUBLIC_SCHEMA_URL=<http://your-schem-url> npm run check-schemas
```

To publish and check the schemas it is required to have the environment variables for AWS credentials defined (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).
A default schema loaded is provided [here](/src/schemas/jsonSchema/loaders/cvc.js)


## Conventions:
Expand Down
9 changes: 4 additions & 5 deletions __test__/claim/Claim.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { UserCollectableAttribute } = require('@identity.com/uca');
const { Claim } = require('../../src/claim/Claim');
const { schemaLoader, CVCSchemaLoader } = require('../../src');
const { schemaLoader, CVCSchemaLoader, UserCollectableAttribute } = require('../../src');

jest.setTimeout(30000);

Expand Down Expand Up @@ -285,7 +284,7 @@ describe('Claim Constructions tests', () => {
year: 1978,
};

const dateOfBirthUCA = new UserCollectableAttribute(identifier, value);
const dateOfBirthUCA = await UserCollectableAttribute.create(identifier, value);
expect(dateOfBirthUCA).toBeDefined();
expect(dateOfBirthUCA.value).toBeDefined();
expect(dateOfBirthUCA.value.day).toBeDefined();
Expand All @@ -310,8 +309,8 @@ describe('Claim Constructions tests', () => {
selfie: { algorithm: 'sha256', data: 'sha256(selfie)' },
};

const evidencesUCA = new UserCollectableAttribute(identifier, value);
const evidencesAliasUCA = new UserCollectableAttribute(aliasIdentifier, value);
const evidencesUCA = await UserCollectableAttribute.create(identifier, value);
const evidencesAliasUCA = await UserCollectableAttribute.create(aliasIdentifier, value);

// converting UCAs to Claims
const evidencesClaim = await Claim.create(identifier, evidencesUCA.getPlainValue());
Expand Down
10 changes: 5 additions & 5 deletions __test__/creds/VerifiableCredential.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ describe('Unit tests for Verifiable Credentials', () => {
expect(credential).toBeDefined();
});

// TODO: Fix schema's for this test to pass
it.skip('Should create and verify a credential with an array of clains ', async () => {
const covidDetails = {
patient: {
Expand Down Expand Up @@ -700,7 +699,9 @@ describe('Unit tests for Verifiable Credentials', () => {
expect(await cred.verifyProofs()).toBeTruthy();
});

test('cred.verify(): with a valid cred without expirationDate, should return at least'

// This breaks VC.verify - verify has been deprecated
test.skip('cred.verify(): with a valid cred without expirationDate, should return at least'
+ ' VERIFY_LEVELS.PROOFS level', async () => {
const credJSon = require('./fixtures/Cred1.json'); // eslint-disable-line
const cred = await VC.fromJSON(credJSon);
Expand Down Expand Up @@ -1794,8 +1795,7 @@ describe('Transient Credential Tests', () => {
schemaLoader.reset();
});

// TODO: Double check this
it.skip('Should create an US Address Transient Credential', async () => {
it('Should create an US Address Transient Credential', async () => {
const value = {
country: 'US',
county: 'Melo Park',
Expand Down Expand Up @@ -1824,7 +1824,7 @@ describe('Transient Credential Tests', () => {
expect(proved).toBeTruthy();
});

it.skip('Should create an US SSN Transient Credential', async () => {
it('Should create an US SSN Transient Credential', async () => {
const value = {
areaNumber: '111',
groupNumber: '11',
Expand Down
Loading

0 comments on commit ca56739

Please sign in to comment.