Skip to content

Commit

Permalink
Merge pull request #109 from OriginTrail/feature/network-and-assertio…
Browse files Browse the repository at this point in the history
…n-modules

Network + Assertion Modules
  • Loading branch information
u-hubar authored Oct 3, 2023
2 parents 8e9725e + aefde5f commit 5de5c56
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 102 deletions.
2 changes: 1 addition & 1 deletion dist/dkg.min.js

Large diffs are not rendered by default.

105 changes: 75 additions & 30 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ function divider() {
}

(async () => {
const content = {
public: {
'@context': ['https://schema.org'],
'@id': 'uuid:1',
company: 'OT',
user: {
'@id': 'uuid:user:1',
},
city: {
'@id': 'uuid:belgrade',
},
},
private: {
'@context': ['https://schema.org'],
'@graph': [
{
'@id': 'uuid:user:1',
name: 'Adam',
lastname: 'Smith',
},
{
'@id': 'uuid:belgrade',
title: 'Belgrade',
postCode: '11000',
},
],
},
};

divider();

const nodeInfo = await DkgClient.node.info();
Expand All @@ -34,37 +63,53 @@ function divider() {

divider();

const createAssetResult = await DkgClient.asset.create(
{
public: {
'@context': ['https://schema.org'],
'@id': 'uuid:1',
company: 'OT',
user: {
'@id': 'uuid:user:1',
},
city: {
'@id': 'uuid:belgrade',
},
},
private: {
'@context': ['https://schema.org'],
'@graph': [
{
'@id': 'uuid:user:1',
name: 'Adam',
lastname: 'Smith',
},
{
'@id': 'uuid:belgrade',
title: 'Belgrade',
postCode: '11000',
},
],
},
},
const assertions = await DkgClient.assertion.formatGraph(content);
console.log('======================== ASSERTIONS FORMATTED');
console.log(JSON.stringify(assertions));

divider();

const publicAssertionId = await DkgClient.assertion.getPublicAssertionId(content);
console.log('======================== PUBLIC ASSERTION ID (MERKLE ROOT) CALCULATED');
console.log(publicAssertionId);

divider();

const publicAssertionSize = await DkgClient.assertion.getSizeInBytes(content);
console.log('======================== PUBLIC ASSERTION SIZE CALCULATED');
console.log(publicAssertionSize);

divider();

const bidSuggestion = await DkgClient.network.getBidSuggestion(
publicAssertionId,
publicAssertionSize,
{ epochsNum: 2 },
);
console.log('======================== BID SUGGESTION CALCULATED');
console.log(bidSuggestion);

divider();

const increaseAllowanceResult = await DkgClient.asset.increaseAllowance(bidSuggestion);
console.log('======================== ALLOWANCE INCREASED');
console.log(increaseAllowanceResult);

divider();

const decreaseAllowanceResult = await DkgClient.asset.decreaseAllowance(bidSuggestion);
console.log('======================== ALLOWANCE DECREASED');
console.log(decreaseAllowanceResult);

divider();

const setAllowanceResult = await DkgClient.asset.setAllowance(bidSuggestion);
console.log('======================== ALLOWANCE SET');
console.log(setAllowanceResult);

divider();

const createAssetResult = await DkgClient.asset.create(content, { epochsNum: 2 });
console.log('======================== ASSET CREATED');
console.log(createAssetResult);

Expand Down Expand Up @@ -187,7 +232,7 @@ function divider() {
divider();

queryResult = await DkgClient.graph.query(
'construct { ?s ?p ?o } where { ?s ?p ?o . <uuid:1> ?p ?o }',
'construct { ?s ?p ?o } where { ?s ?p ?o . <uuid:user:1> ?p ?o }',
'CONSTRUCT',
{ graphState: 'HISTORICAL', graphLocation: 'LOCAL_KG' },
);
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// managers
const AssertionOperationsManager = require('./managers/assertion-operations-manager.js');
const AssetOperationsManager = require('./managers/asset-operations-manager.js');
const BlockchainOperationsManager = require('./managers/blockchain-operations-manager');
const GraphOperationsManager = require('./managers/graph-operations-manager.js');
const NetworkOperationsManager = require('./managers/network-operations-manager.js');
const NodeOperationsManager = require('./managers/node-operations-manager.js');

const BaseServiceManager = require('./services/base-service-manager.js');
Expand All @@ -11,10 +13,12 @@ class DkgClient {
const baseServiceManager = new BaseServiceManager(config);
const services = baseServiceManager.getServices();

this.asset = new AssetOperationsManager(config, services);
this.blockchain = new BlockchainOperationsManager(config, services);
this.graph = new GraphOperationsManager(config, services);
this.node = new NodeOperationsManager(config, services);
this.assertion = new AssertionOperationsManager(services);
this.asset = new AssetOperationsManager(services);
this.blockhain = new BlockchainOperationsManager(services);
this.node = new NodeOperationsManager(services);
this.graph = new GraphOperationsManager(services);
this.network = new NetworkOperationsManager(services);
}
}
module.exports = DkgClient;
68 changes: 68 additions & 0 deletions managers/assertion-operations-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { assertionMetadata, calculateRoot, formatGraph } = require('assertion-tools');

class AssertionOperationsManager {
constructor(services) {
this.nodeApiService = services.nodeApiService;
this.inputService = services.inputService;
}

/**
* Formats the content provided, producing both a public and, if available, a private assertion.
*
* @param {Object} content - The content object containing optional public and private properties.
* @returns {Promise<Object>} a promise that resolves with an object containing the
* formatted public assertion and, if available, the private assertion.
*/
async formatGraph(content) {
return formatGraph(content);
}

/**
* Calculates and returns the Merkle root of the public assertion from the provided content.
*
* @param {Object} content - The content object containing optional public and private properties.
* @returns {Promise<String>} a promise that resolves with a string representing the
* Merkle root of the formatted public assertion.
*/
async getPublicAssertionId(content) {
const assertions = await formatGraph(content);
return calculateRoot(assertions.public);
}

/**
* Calculates and returns the size in bytes of the public assertion from the provided content.
*
* @param {Object} content - The content object containing optional public and private properties.
* @returns {Promise<Number>} a promise that resolves with a number representing the
* size in bytes of the formatted public assertion.
*/
async getSizeInBytes(content) {
const assertions = await formatGraph(content);
return assertionMetadata.getAssertionSizeInBytes(assertions.public);
}

/**
* Calculates and returns the number of triples of the public assertion from the provided content.
*
* @param {Object} content - The content object containing optional public and private properties.
* @returns {Promise<Number>} a promise that resolves with a number representing the
* number of triples of the formatted public assertion.
*/
async getTriplesNumber(content) {
const assertions = await formatGraph(content);
return assertionMetadata.getAssertionTriplesNumber(assertions.public);
}

/**
* Calculates and returns the number of chunks of the public assertion from the provided content.
*
* @param {Object} content - The content object containing optional public and private properties.
* @returns {Promise<Number>} a promise that resolves with a number representing the
* number of chunks of the formatted public assertion.
*/
async getChunksNumber(content) {
const assertions = await formatGraph(content);
return assertionMetadata.getAssertionChunksNumber(assertions.public);
}
}
module.exports = AssertionOperationsManager;
Loading

0 comments on commit 5de5c56

Please sign in to comment.