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

[+] Anchoring schemas to CORD Network #245

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

VenuChoudhary001
Copy link

@VenuChoudhary001 VenuChoudhary001 commented Aug 24, 2023

C4GT 2023 | CORD Network #Issue

  • This PR contains the 1st milestone of the project -

Anchoring schemas to the cord network

How It Works

  • ENV VARIABLES. Add these in the application.yml file
  registry.cord.issuer_schema_url=http://localhost:5106/api/v1/schema
  registry.cord.issuer_registry_url=http://localhost:5106/api/v1/registry
  • If you are using docker, then instead of localhost, either you can try adding dhiagent or docker gateway url

    • In order to find docker gateway url, in a terminal, enter the command

      $  docker network inspect bridge
  • application.yml should be like this
    Screenshot from 2023-09-06 18-44-15

  • In order to create schemas in sunbird-rc, we are required to hit this endpoint : /api/v1/Schema .

  • I have added an environment variable anchor_to_cord, which needs to be set to true if you want to anchor the schema to the chain

  • Just when you hit the endpoint api/v1/Schema, once the definitionsManager of SunbirdRc confirms that no other schema with same name exists, then we go for anchoring it to the cord chain.

  • In order to anchor it to the chain, we need to hit the api/v1/schema endpoint of Issuer Agent, which accepts the following schema :

  {
       "title":"",
       "description":" ",
       "property":{
           // schema 
       }
  }
  • So, whenever you are creating a schema and want to anchor it to chain, you must pass a description property as well.

  • Major changes have been made in these two files :
 java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java 
 java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java 
  • In the RegistryHelper.java
    a. I have defined this function

     public JsonNode anchorToCord(JsonNode rootNode) throws Exception{
       try{
    
       ObjectMapper objectMapper=new ObjectMapper();
           /* anchor schema */
       JsonNode schemaProperty=rootNode.get("schema");
       JsonNode convertedSchema=objectMapper.readTree(schemaProperty.asText());
       
       JsonNode schemaNode=convertedSchema.get("definitions");
       
       JsonNode properties=schemaNode.get(rootNode.get("name").asText());
       JsonNode getProperties=properties.get("properties");
       
       JsonNode createSchema=objectMapper.createObjectNode()
       .put("title",convertedSchema.get("title").asText())
       .put("description",convertedSchema.get("description").asText())
       .set("properties",properties.get("properties"));
       
       JsonNode schemaToBeAnchored=objectMapper.createObjectNode()
       .set("schema",createSchema);
       
       JsonNode anchoredSchema=anchorSchemaAPI(schemaToBeAnchored);
       logger.info("ANCHORED SCHEMA TO CORD CHAIN  {}",anchoredSchema);
       /* Anchoring registry to CORD */
       JsonNode registrySchema=objectMapper.createObjectNode()
       .put("title",convertedSchema.get("title").asText())
       .put("description",convertedSchema.get("description").asText())
       .put("schemaId",anchoredSchema.get("schemaId").asText());
       
       JsonNode registryId=anchorRegistryAPI(registrySchema);
       logger.info("REGISTRY ID GENERATED ON CORD {} ",registryId);
       
       ((ObjectNode)rootNode).set("cord_registry_id", registryId.get("registryId"));
       ((ObjectNode)rootNode).set("cord_schema_id", anchoredSchema.get("schemaId"));
       
           return rootNode;
       }catch(Exception e){
           logger.error("ERROR {}",e);
           return objectMapper.createObjectNode().put("ERROR","Exception occurred");
       }
    
    }
    

    b. This function creates a proper json structure to pass as a request body to the api/v1/schema endpoint of Issuer
    and calls the anchorSchemaAPI() and anchorRegistryAPI() defined in the same file.

      public JsonNode anchorSchemaAPI(JsonNode obj) throws Exception{
         JsonNode schema=apiHelper(obj,issuer_schema_url);
         return schema;
     }
     /** Anchors registry to the CORD NETWORK ,*/
     public JsonNode anchorRegistryAPI(JsonNode obj) throws Exception{
         JsonNode registryDetails=apiHelper(obj,issuer_registry_url);
         return registryDetails;
     }
    
  • In the RegistryEntityController, under the mapping api/v1/{entityName}, I have added the following code

   if (anchorToCord) { 
       if("Schema".equals(entityName)){
         JsonNode getRootNode=registryHelper.anchorToCord(rootNode);
         newRootNode.set(entityName, getRootNode);
       }else{
         newRootNode.set(entityName,rootNode);
       }
     }else{
         newRootNode.set(entityName, rootNode);
     }

Screenshots

Schema creation & anchoring to chain

Screenshot from 2023-09-03 01-51-17


If you hit the api/v1/Schema/{id} endpoint, this is the response and you can see it works just fine.
Screenshot from 2023-09-04 15-37-54

Copy link

@CyberCitizen01 CyberCitizen01 left a comment

Choose a reason for hiding this comment

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

I think unnecessary spaces, comments and debug print statements should be removed.

Copy link

@amarts amarts left a comment

Choose a reason for hiding this comment

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

LGTM.

I feel someone with more Java experience should review it once. Need a reference to documentation on this in commit log (or as a comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants