Metanet Objects

Published on
January 15, 2022
Published by
G2C Team

The Metanet is a protocol that allows data to be saved in a directed graph structure using the Blockchain as the underlying storage that fits perfectly into the base bitcoin protocol and its consensus rules. As a result, any internet-like data can be part of the Metanet, so this protocol goes beyond the idea of simple transactions to exchange payments among peers.

The G2C Suite provides different functions to make it easier to interact with Metanet objects from a developer perspective. The Suite facilitates basic CRUD operations, that combined, allow developers to create whatever they want.

Create user objects

The first call allows to create and store objects on the Metanet, providing path, name, permissions, encoding, object properties and object data itself.

The following are the supported data encodings:

  • OBJECT_ENCODING_RAWHEX = 1 // For raw binary content (HEX)
  • OBJECT_ENCODING_RAWTEXT = 3 // For raw text string (text)
  • OBJECT_ENCODING_JSON = 5 // For Objects (data as an object)

The next list enumerates the currently supported permissions:

  • OBJECT_PERMISSION_PUBLIC = 1 // Cleartext, publicly visible. Object content is base64 encoded
  • OBJECT_PERMISSION_USER = 2 // Encrypted. Only owner can see object's content
  • Future release will add application level permission

Each created object can be linked with one or more payment transaction id by using the relatedpayments array parameter.

createUserObject() {
   return new Promise(async (resolve, reject) => {
     try {
       // createUserObjectParameters object contains the following fields:
       // const createUserObjectParameters = {
       //    tokenid,
       //    tokens1,
       //    tokenc1,
       //    application,
       //    nick,
       //    type,
       //    path,
       //    name,
       //    data,
       //    encoding,
       //    permissions,
       //    relatedpayments,
       // }
       g2cclient_createUserObject(createUserObjectParameters, async (response) => {
           if (response.hasOwnProperty('error')) {
             if (
               response.error.includes('Trying to create an existing object/tree') === true ) {
               // Deal with an existing object exception: maybe call function to update object
             }
           } else {
             const object = response.data.object; // Metanet object txid
             const objectdata = response.data.objectdata; // array of txids containing the object (depending on the size of data, it could be splitted into various chuncks)
             const costs = response.data.costs; // matrix of costs
             resolve(response.data);
           }
       });
     } catch (error) {
       reject(new Error(error));
     }
   });
}

Once created, object will be owned by the user who called the function.

Update user object

Once stored, the G2C Suite can update a user object calling the function g2cclient_updateUserObject(). Input and output parameters are the same as defined by the creation object function.

updateUserObject() {
   return new Promise(async (resolve, reject) => {
     try {
       // updateUserObjectParameters object contains the following fields:
       // const updateUserObjectParameters = {
       //    tokenid,
       //    tokens1,
       //    tokenc1,
       //    application,
       //    nick,
       //    type,
       //    path,
       //    name,
       //    data,
       //    encoding,
       //    permissions,
       //    relatedpayments,
       // }
       g2cclient_updateUserObject(updateUserObjectParameters, async (response) => {
           if (response.hasOwnProperty('error')) {
             // Deal with errors on application layer
             reject(new Error(response.error));
           } else {
             const object = response.data.object; // Metanet object txid
             const objectdata = response.data.objectdata; // array of txids containing the object (depending on the size of data, it could be splitted into various chuncks)
             const costs = response.data.costs; // matrix of costs
             resolve(response.data);
           }
       });
     } catch (error) {
       reject(new Error(error));
     }
   });
}

As well as creating and updating Metanet objects, the G2C Suite allows developers to create tree structures starting from an owned object by the user who calls the function. So, as a result of calling g2cclient_createUserObjectTree() function, a tree branch is created at the treePath parameter.  

createUserObjectTree() {
   return new Promise(async (resolve, reject) => {
     try {
       // createUserObjectTreeParameters object contains the following fields:
       // const createUserObjectTreeParameters = {
       //    tokenid,
       //    tokens1,
       //    tokenc1,
       //    application,
       //    nick,
       //    treePath,
       //    treeName,
       // }
       g2cclient_createUserObjectTree(createUserObjectTreeParameters, async (response) => {
           if (response.hasOwnProperty('error')) {
             // Deal with errors on application layer
             reject(new Error(response.error));
           } else {
             resolve(response.data);
           }
       });
     } catch (error) {
       reject(new Error(error));
     }
   });
}

As part of CRUD operations, function g2c_getUserObjectList() is able to retrieve a list of all the objects owned by user (nick) from the provided application parameter.

userObjectList() {
   return new Promise(async (resolve, reject) => {
     try {
       // userObjectListParameters object contains the following fields:
       // const userObjectListParameters = {
       //    tokenid,
       //    tokens1,
       //    tokenc1,
       //    application,
       //    nick,
       //    path,
       //    objectType,
       // }
       g2c_getUserObjectList(userObjectListParameters, async (response) => {
           if (response.hasOwnProperty('error')) {
             // Deal with errors on application layer
             reject(new Error(response.error));
           } else {
             const objects = response.data.objects; // array of objects
             // objects is an array of objects with the following structure:
             // const object = {
             //   fullpath, // Path relative to user root folder with the object name included
             //   type, // Object's type
             //   version, // Object's revision
             //   creation, // Object's creation timestamp
             //   updation, // Object's updation timestamp
             // }
             resolve(response.data);
           }
       });
     } catch (error) {
       reject(new Error(error));
     }
   });
}