Accounts Library in Corda

Home Blockchain Accounts Library in Corda

Part 2: Creating and Sharing Accounts in a sample CorDapp

Welcome back CorDappers!

I am grateful to every reader and the Corda community for their support. I have been working with accounts library with my team at Webmob and let me just say it is too much fun. You get to apply all the logics you want in implementing just one library.

I won’t waste time on introductions and will be sharing the how’s and why’s. Don’t worry this is going to be as descriptive as possible.

So let’s start with the fun part: Development.

I assume you have the account’s library set up in your existing CorDapp, if not please refer to my previous blog Part 1: Getting Started with Accounts in Corda.

We are going to create 2 flows

  1. One will let us create Accounts on nodes
  2. The second one to share those Accounts between nodes which is very useful if at any point in future you want to make a trade between accounts on different nodes or just simply make them communicate with each other.

Note: When it comes to using accounts library for your business logic, as a developer you need to know that flows are your best-friend here. Every piece of code related to Accounts let it be creating, querying, sharing and everything else, all needs to be done in Flows.

For the first step, open up the controller.kt file present in your “clients” directory. Create a function named CreateAccounts and annotate it with post mapping. This function will have the following declaration.

https://webmobinfo.ch/@PostMapping(value = ["/CreateAccount"], 
produces = arrayOf("text/plain"))
private fun CreateNewAccount(@RequestParam Accountname:String): String {    val result = proxy.startFlowDynamic(CreateAccount::class.java, Accountname).returnValue.get()
   
    return result
}

We will hit this API to create an account with the required name. The CreateAccount flow will be called which requires one parameter in its constructor.

The account will be created on the server that you hit the API on. For example, I gave localhost:10050/CreateAccounts in my postman and account name as Account1. This created my ‘Account1’ on my Trader server which is defined in my Build.gradle(in clients directory) as follows:

task runTraderServer(type: JavaExec, dependsOn: assemble) {classpath = sourceSets.main.runtimeClasspath
main = 'com.matterSphere.webserver.ServerKt'                       args '--server.port=10050', 
'--config.rpc.host=localhost', 
'--config.rpc.port=10006', 
'--config.rpc.username=user1', 
'--config.rpc.password=test'}

Similarly, I have defined my buyer server on port 10053. Unless I share my ‘Account1’, the buyer server will not know if this account even exits let alone communicate with it.

Let’s talk about the flow now. It will look as follows:

class CreateAccount(private val acctName:String):FlowLogic<String>() {
    @Suspendable
    override fun call(): String {
        //Create a new account
        val newAccount = accountService.createAccount(name = acctName).toCompletableFuture().getOrThrow()
        val acct = newAccount.state.data
        return  acct.identifier.toString()
    }
}

The account name you passed in the API will be stored in the acctName variable which is then passed in a pre-defined function: createAccount.

This will create an account with the given name, a unique identifier and map it with the host Party internally along with the verification of the name and id of the account to be unique.

This. Is. It.

Account created. Only the concept needs your true efforts but when it comes to development the Corda team has done most of the work.

In the same way, we will create an API shareAccounts which takes account name to be shared and Party name to share with.

@PostMapping(value = ["/ShareAccounts"], produces = arrayOf("text/plain"))
private fun ShareAccounts(
@RequestParam AccountName:String, @RequestParam Shareto: String): String { val Sharedto = proxy.partiesFromName(Shareto, false).singleOrNull()
            ?:throw  IllegalArgumentException("No exact match found for Party name ${Shareto}.")    val result = proxy.startFlowDynamic(ShareAccount::class.java, AccountName, Sharedto).returnValue.get()    return result
}

By using the pre-defined function proxy.partiesFromName we can use a variable representing a Party or node on Corda.

The corresponding flow will be as follows:

class ShareAccount(
        private val acctName: String,
        private val shareTo: Party
) : FlowLogic<String>(){

    @Suspendable
    override fun call(): String {
val AllmyAccounts = accountService.ourAccounts()
val SharedAccount = AllmyAccounts.single { it.state.data.name == acctName}.state.data.identifier.id
        accountService.shareAccountInfoWithParty(SharedAccount,shareTo)

return "Shared" + acctName + "with: " + shareTo.name.organisation
    }
}

Here, we store all accounts information, present on a node, in a variable and then iterate through those accounts while comparing them against the account name passed. On a successful match, we store the Unique ID of that account which is then passed in the pre-defined function “shareAccountInfoWithParty” which takes the account ID and party as arguments and performs the desired task.

If you go to the definition of these pre-defined functions you will find that there a sufficient number of other functions to help you with your Accounts integration in your CorDapp.

That’s it for now. You can try the following task to improve your understanding:

  1. Create an account on two nodes
  2. Share those accounts
  3. Send a message from the account on node 1 to the account on node 2.

The road so far is quite smooth however, you need to be prepared for a lot of debugging and brainstorming if you want to completely benefit from accounts depending on your project and I hope this helps in your initial understanding and development.

See you next time. Happy CorDapping 🙂

wpuser

wpuser

Leave A Comment

Your email address will not be published. Required fields are marked *