Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add open-attestation schema #57

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__
yehjxraymond marked this conversation as resolved.
Show resolved Hide resolved
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",
yehjxraymond marked this conversation as resolved.
Show resolved Hide resolved
"@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