Skip to content

Commit

Permalink
feat: open-attestation v3
Browse files Browse the repository at this point in the history
BREAKING CHANGE: New schema to be w3c compliant

Changes are:
- issuers is not an array anymore. Consequence, proof.value is not an array anymore
- issuer.id is a new mandatory field which must be an URI
- issuer.identityProof.subject has been remove because it's not needed anymore
- @context has been added for w3c compliance to help consumer provide terminology. It's an optional field
- id has been updated to follow w3c compliance. It's an optional uri about the subject of the VC
- reference has been added and is equal to the previous id
- type has been added as the type of the VC (w3c compliance)
- validFrom has been added (w3c compliance)
- validUntil has been added (w3c compliance). It's an optional field
- attachments.type has been updated to follow w3c compliance. It refers to a valid evidence type.
- attachments.mimeType has been added and is equal to the previous attachments.type
- rename $template to template

Adding v2 schema which is a simple version of schema used in tradetrust
and opencerts

Generate types from schema. Types are exported

Update wording:
- sign => wrap
- SignedDocument => WrappedDocument
  • Loading branch information
Nebulis committed Dec 19, 2019
1 parent e6b4d8a commit f553343
Show file tree
Hide file tree
Showing 36 changed files with 5,564 additions and 2,578 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
command: npm run lint
- run:
name: test
command: npm run build
command: npm run test
- run:
name: build
command: npm run test
command: npm run build
- run:
name: release
command: npx --no-install semantic-release
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
dist
dist
src/__generated__
7 changes: 4 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"extends": [
"airbnb-base",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint",
Expand All @@ -22,7 +21,9 @@
"prettier/prettier": "error",
"import/no-unresolved": "off",
"import/prefer-default-export": "off",
"@typescript-eslint/explicit-function-return-type":"off",
"@typescript-eslint/no-explicit-any":"off"
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-unused-expressions": "off",
"@typescript-eslint/no-unused-expressions": ["error"]
}
}
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
node_modules
coverage
/node_modules
/coverage
.nyc_output
yarn-error.log
yarn.lock
dist/
.idea
/dist
/.idea
*.iml
122 changes: 55 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# Open Attestation

Attestation and notary framework for any document types on the blockchain.
Attestation and notary framework for any document types on the blockchain.

OpenAttestation allows any entity to proof the existence of a document or a batch of documents. It makes use of smart contracts on the Ethereum blockchain to store cryptographic proofs of individual documents.
OpenAttestation allows any entity to proof the existence of a document or a batch of documents. It makes use of smart contracts on the Ethereum blockchain to store cryptographic proofs of individual documents.

This repository allows you to batch the documents to obtain the merkle root of the batch to be committed to the blockchain. It also allows you to verify the signature and schema of the document issued using the OpenAttestation framework.
This repository allows you to batch the documents to obtain the merkle root of the batch to be committed to the blockchain. It also allows you to verify the signature and schema of the document issued using the OpenAttestation framework.

Simply define your document schema to start using OpenAttestation!

## JSON Schema

[JSON Schema](http://json-schema.org/) is used to define the shape of the document. OpenAttestation supports schema versioning, simply add all versions of the schema using the `addSchema` method and define the default (or latest) schema for newly batched documents.
[JSON Schema](http://json-schema.org/) is used to define the shape of the document. OpenAttestation provides its own schema and makes sure every document is valid against it.

## Reference Implementation

Expand All @@ -29,95 +29,84 @@ const {
getData,
issueDocument,
issueDocuments,
addSchema,
verifySignature,
validateSchema,
obfuscateDocument
obfuscateDocument,
validateSchema
} = require("@govtechsg/open-attestation");

const schema = {
"$id": "http://example.com/schema-v1.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"key1": {
"type": "string",
},
"key2": {
"type": "string",
$id: "http://example.com/schema.json",
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
key1: {
type: "string"
},
key2: {
type: "string"
}
},
"required": [ "key1" ],
"additionalProperties": false,
}

const defaultSchema = schema;

// Add all versions of the document schema below for backward compatibility
addSchema(schema);
required: ["key1"],
additionalProperties: false
};

const issueDocument = data => issueDocument(data, defaultSchema);
const issuedDocument = data => issueDocument(data, schema);
const isValid = validateSchema(issuedDocument);

const issueDocuments = dataArray => issueDocuments(dataArray, defaultSchema);
const issueDocuments = dataArray => issueDocuments(dataArray, schema);

const obfuscateFields = (document, fields) =>
obfuscateDocument(document, fields);
const obfuscateFields = (document, fields) => obfuscateDocument(document, fields);

const documentData = document => getData(document);

module.exports = {
issueDocument,
issueDocuments,
verifySignature,
validateSchema,
obfuscateFields,
documentData
};

```

## API References

### Signing documents

`issueDocuments` takes in an array of document as well as a schema and returns the batched documents. It computes the merkle root of the batch and appends the signature to each document. This merkle root can be published on the blockchain and queried against to prove the provenance of the document issued this way.
`issueDocuments` takes in an array of document and returns the batched documents. It computes the merkle root of the batch and appends the signature to each document. This merkle root can be published on the blockchain and queried against to prove the provenance of the document issued this way.

```js
const schema = {
"$id": "http://example.com/schema-v1.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"key1": {
"type": "string",
},
"key2": {
"type": "string",
$id: "http://example.com/schema.json",
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
key1: {
type: "string"
},
key2: {
type: "string"
}
},
required: ["key1"],
additionalProperties: false
};

const datum = [
{
key1: "test"
},
{
key1: "hello",
key2: "item2"
},
"required": [ "key1" ],
"additionalProperties": false,
}

const datum = [{
key1: 'test',
},{
key1: 'hello',
key2: 'item2',
},{
key1: 'item1',
key2: 'item4',
},{
key1: 'item2',
}];
{
key1: "item1",
key2: "item4"
},
{
key1: "item2"
}
];

signedDocuments = issueDocuments(datum, schema);
console.log(signedDocuments);
```

### Validate schema of document

`validateSchema` checks that the document conforms to the data structure as specified by the schema.
`validateSchema` checks that the document conforms to open attestation data structure.

```js
const validatedSchema = validateSchema(signedDocument);
Expand Down Expand Up @@ -146,14 +135,13 @@ console.log(data);

### Obfuscating data

`obfuscateFields` removes a key-value pair from the document's data section, without causing the file hash to change. This can be used to generate a new document containing a subset of the original data, yet allow the recipient to proof the provenance of the document.
`obfuscateFields` removes a key-value pair from the document's data section, without causing the file hash to change. This can be used to generate a new document containing a subset of the original data, yet allow the recipient to proof the provenance of the document.

```js
const newData = obfuscateFields(signedDocument, 'key1');
const newData = obfuscateFields(signedDocument, "key1");
console.log(newData);
```


## Test

```
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
preset: "ts-jest",
setupFiles: ["core-js"],
testEnvironment: "node",
testPathIgnorePatterns: ["node_modules", "dist"]
};
Loading

0 comments on commit f553343

Please sign in to comment.