Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

feat: add metadata to document, data contract and identity #318

Merged
merged 4 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 29 additions & 0 deletions lib/Metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Metadata {
/**
* @param {Object} metadataObject
* @param {number} metadataObject.blockHeight
* @param {number} metadataObject.coreChainLockedHeight
*/
constructor(metadataObject) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We call it rawSomething usually in DPP, let's call it rawMetadata?

this.blockHeight = metadataObject.blockHeight;
this.coreChainLockedHeight = metadataObject.coreChainLockedHeight;
}

/**
* Get block height
* @returns {number}
*/
getBlockHeight() {
return this.blockHeight;
}

/**
* Get core chain-locked height
* @returns {number}
*/
getCoreChainLockedHeight() {
return this.coreChainLockedHeight;
}
}

module.exports = Metadata;
16 changes: 16 additions & 0 deletions lib/dataContract/DataContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,22 @@ class DataContract {
return this.binaryProperties[type];
}

/**
* Set metadata
* @param {Metadata} metadata
*/
setMetadata(metadata) {
this.metadata = metadata;
}

/**
* Get metadata
* @returns {Metadata|null}
*/
getMetadata() {
return this.metadata;
}

/**
* Return Data Contract as plain object
*
Expand Down
16 changes: 16 additions & 0 deletions lib/document/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ class Document {
return this.updatedAt;
}

/**
* Set metadata
* @param {Metadata} metadata
*/
setMetadata(metadata) {
this.metadata = metadata;
}

/**
* Get metadata
* @returns {Metadata|null}
*/
getMetadata() {
return this.metadata;
}

/**
* Return Document as plain object
*
Expand Down
16 changes: 16 additions & 0 deletions lib/identity/Identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ class Identity {

return this;
}

/**
* Set metadata
* @param {Metadata} metadata
*/
setMetadata(metadata) {
this.metadata = metadata;
}

/**
* Get metadata
* @returns {Metadata|null}
*/
getMetadata() {
return this.metadata;
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/integration/document/Document.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const Identifier = require('../../../lib/identifier/Identifier');
const Metadata = require('../../../lib/Metadata');

const getDataContractFixture = require('../../../lib/test/fixtures/getDataContractFixture');
const getDocumentsFixture = require('../../../lib/test/fixtures/getDocumentsFixture');

describe('Document', () => {
let document;
let dataContract;
let metadataFixture;

beforeEach(() => {
dataContract = getDataContractFixture();
[document] = getDocumentsFixture(dataContract).slice(8);

metadataFixture = new Metadata(42, 0);

document.setMetadata(metadataFixture);
});

describe('#toJSON', () => {
Expand Down Expand Up @@ -65,4 +71,20 @@ describe('Document', () => {
expect(result.identifierField).to.be.an.instanceOf(Identifier);
});
});

describe('#setMetadata', () => {
it('should set metadata', () => {
const otherMetadata = new Metadata(43, 1);

document.setMetadata(otherMetadata);

expect(document.metadata).to.deep.equal(otherMetadata);
});
});

describe('#getMetadata', () => {
it('should get metadata', () => {
expect(document.getMetadata()).to.deep.equal(metadataFixture);
});
});
});
37 changes: 37 additions & 0 deletions test/unit/Metadata.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const Metadata = require('../../lib/Metadata');

describe('Metadata', () => {
describe('#constructor', () => {
it('should set height and core chain-locked height', () => {
const result = new Metadata({
blockHeight: 42,
coreChainLockedHeight: 1,
});

expect(result.blockHeight).to.equal(42);
expect(result.coreChainLockedHeight).to.equal(1);
});
});

describe('#getBlockHeight', () => {
it('should get block height', () => {
const result = new Metadata({
blockHeight: 42,
coreChainLockedHeight: 1,
});

expect(result.getBlockHeight()).to.equal(42);
});
});

describe('#getCoreChainLockedHeight', () => {
it('should get core chain-locked height', () => {
const result = new Metadata({
blockHeight: 1,
coreChainLockedHeight: 42,
});

expect(result.getCoreChainLockedHeight()).to.equal(42);
});
});
});
22 changes: 22 additions & 0 deletions test/unit/dataContract/DataContract.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Identifier = require('../../../lib/identifier/Identifier');
const InvalidDocumentTypeError = require('../../../lib/errors/InvalidDocumentTypeError');

const generateRandomIdentifier = require('../../../lib/test/utils/generateRandomIdentifier');
const Metadata = require('../../../lib/Metadata');

describe('DataContract', () => {
let hashMock;
Expand All @@ -19,6 +20,7 @@ describe('DataContract', () => {
let entropy;
let contractId;
let getBinaryPropertiesFromSchemaMock;
let metadataFixture;

beforeEach(function beforeEach() {
hashMock = this.sinonSandbox.stub();
Expand Down Expand Up @@ -66,6 +68,10 @@ describe('DataContract', () => {
documents,
$defs: {},
});

metadataFixture = new Metadata(42, 0);

dataContract.setMetadata(metadataFixture);
});

describe('constructor', () => {
Expand Down Expand Up @@ -369,4 +375,20 @@ describe('DataContract', () => {
}
});
});

describe('#setMetadata', () => {
it('should set metadata', () => {
const otherMetadata = new Metadata(43, 1);

dataContract.setMetadata(otherMetadata);

expect(dataContract.metadata).to.deep.equal(otherMetadata);
});
});

describe('#getMetadata', () => {
it('should get metadata', () => {
expect(dataContract.getMetadata()).to.deep.equal(metadataFixture);
});
});
});
22 changes: 22 additions & 0 deletions test/unit/identity/Identity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const rewiremock = require('rewiremock/node');
const generateRandomIdentifier = require('../../../lib/test/utils/generateRandomIdentifier');

const IdentityPublicKey = require('../../../lib/identity/IdentityPublicKey');
const Metadata = require('../../../lib/Metadata');

describe('Identity', () => {
let rawIdentity;
let identity;
let Identity;
let hashMock;
let encodeMock;
let metadataFixture;

beforeEach(function beforeEach() {
hashMock = this.sinonSandbox.stub();
Expand Down Expand Up @@ -40,6 +42,10 @@ describe('Identity', () => {
};

identity = new Identity(rawIdentity);

metadataFixture = new Metadata(42, 0);

identity.setMetadata(metadataFixture);
});

describe('#constructor', () => {
Expand Down Expand Up @@ -172,4 +178,20 @@ describe('Identity', () => {
expect(identity.balance).to.equal(40);
});
});

describe('#setMetadata', () => {
it('should set metadata', () => {
const otherMetadata = new Metadata(43, 1);

identity.setMetadata(otherMetadata);

expect(identity.metadata).to.deep.equal(otherMetadata);
});
});

describe('#getMetadata', () => {
it('should get metadata', () => {
expect(identity.getMetadata()).to.deep.equal(metadataFixture);
});
});
});