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

Anchor Credentials / Entries on to the CORD blockchain #249

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

Conversation

VenuChoudhary001
Copy link

C4GT 2023 | CORD NETWORK

Issue Link

  • This PR contains the final milestone of my C4GT 2023 project-

Integrate APIs to create credentials, or entries on to this registry

How It Works

  • In order to create credentials on the cord network, we need to send the document schema to the credentials api of the Issuer Agent.
  • The schema and document creation API endpoint on sunbird rc are same - api/v1/{entityName}
  • When the endpoint api/v1/Schema is hit, the schema is created and anchored to the chain. Refer this PR for schema and registry anchoring.
  • I have created an environment variable anchor_to_cord, which must be set to true for the connecting with chain and 3 other variables
  @Value("${registry.cord.anchor_to_cord}")
  boolean anchorToCord;
  @Value("${registry.cord.issuer_schema_url}") String issuer_schema_url;
  @Value("${registry.cord.issuer_registry_url}") String issuer_registry_url;
  @Value("${registry.cord.issuer_credential_url}") String issuer_credential_url;
  • The major changes are done in these files :

      /java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java 
    
      /java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java 
    
  • In the RegistryHelper.java, I have created a private function

appendCredentialsToCord(String schemaId,String registryId,JsonNode document) {
 try {
           ObjectNode documentObject = (ObjectNode) document;
           if(documentObject.has("osid")) documentObject.remove("osid");
           if(documentObject.has("osOwner")) documentObject.remove("osOwner");
           JsonNode rootNode=new ObjectMapper().createObjectNode()
           .put("schemaId",schemaId)
           .put("registryId",registryId)
           .put("holderDid","did:cord:3yxVe6KTeexjYkK43q1SZxthbBVhnBdxwh1SYLTxzWxPobSS")
           .set("property",documentObject);
           
           JsonNode anchorVC=apiHelper(rootNode,issuer_credential_url);
       } catch (Exception e) {
           logger.error("EXCEPTION OCCURRED WHILE APPENDING TO CORD");
       }

}

and a public function anchorCredentialsToCord(), which internally calls the above function to anchor credentials.

  • The anchorCredentialsToCord() is used for the following :
    a. Getting the schemaId and registryId of the anchored schema

    b. In order to get the schemaId and registryId, this function calls the predefined searchEntity() function which is used by the /api/v1/{entityName}/search endpoint internally, to get schema details.

    c. We are making this call, to get back the schemaId and registryId we received when we anchored the schema to chain.At that moment, we also anchored these values to the schema.

    The response from the search schema api - api/v1/Schema/search looks like :

Screenshot from 2023-09-04 15-37-54

d. After extracting the schemaId and registryId, we create a json structure to send to the credentials api, in the appendCredentialsToCord() function

Helper functions

  • In the RegistryHelper, I created a helper function for making fetch api calls :
   public JsonNode apiHelper(JsonNode obj,String url) throws Exception{
        try{
            Mono<JsonNode> responseMono = this.builder.build()
                    .post()
                    .uri(url)
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .bodyValue(obj)
                    .retrieve()
                    .bodyToMono(JsonNode.class)
                    .onErrorResume(throwable -> {
                        throwable.printStackTrace();
                        return Mono.empty();
                    });

            JsonNode response = responseMono.block();
            return response;
        }catch(Exception e){
            logger.error("Exception occurred !" , e);
            return new ObjectMapper().createObjectNode().put("ERROR ","exception caught");
        }
    }
NOTE : 
- Appending credentials to the chain happens after it is created on sunbird RC.
- Where as anchoring schema and registry takes place before the addEntity() method of sunbird-rc is called.

@amarts
Copy link

amarts commented Sep 13, 2023

@VenuChoudhary001 One request, I see #245 and #246 also open. Can you please keep only the relevant PR open and close other things? That way, it is easier for the maintainers to look at only one PR.

@coolbung @tejash-jl would like some eyes and feedback on this, so PR can be taken to a conclusion. Happy to provide more information, or take up the work on this if more is pending according to you.

@Vikastc FYI

@ gamemaker1 (adding you as a reference as you contributed last time) (not adding him yet as the handle shows as busy)

@VenuChoudhary001
Copy link
Author

@VenuChoudhary001 One request, I see #245 and #246 also open. Can you please keep only the relevant PR open and close other things? That way, it is easier for the maintainers to look at only one PR.

@coolbung @tejash-jl would like some eyes and feedback on this, so PR can be taken to a conclusion. Happy to provide more information, or take up the work on this if more is pending according to you.

@Vikastc FYI

@ gamemaker1 (adding you as a reference as you contributed last time) (not adding him yet as the handle shows as busy)

@amarts I have closed the #246. In this #249 itself all the changes are present, should i close #245 as well?

@amarts
Copy link

amarts commented Sep 26, 2023

@srprasanna @surendrasinghs some review comments on this would help to take it to next level.

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