Skip to content

Commit

Permalink
feat: add open-attestation schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebulis committed Nov 28, 2019
1 parent e6b4d8a commit ce8f9fa
Show file tree
Hide file tree
Showing 22 changed files with 3,174 additions and 2,423 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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"]
}
}
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-v1.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-v1.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
Loading

0 comments on commit ce8f9fa

Please sign in to comment.