Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create Party Object from a String?

Tags:

corda

Given mystring = "O=PartyB,L=New York,C=US", is there an easy method to convert the string into a real Party object? Have been looking at CordaRPCOps.wellKnownPartyFromX500Name() but cannot get it to work... Have tried variations of the following:

val rpcOps = new CordaRPCOps()
val otherParty: Party = rpcOps.wellKnownPartyFromX500Name(partyString) ?: throw Exception("Party not recognised.")
like image 597
TonyBy Avatar asked Sep 11 '25 15:09

TonyBy


2 Answers

There are several ways you can do this:

Parsing the string into an X500 name

val x500Name = CordaX500Name.parse("O=PartyB,L=New York,C=US")
val party = rpcOps.wellKnownPartyFromX500Name(x500Name)

Constructing the X500 name directly

val x500Name = CordaX500Name(organisation = "PartyB", locality = "New York", country = "US")
val party = rpcOps.wellKnownPartyFromX500Name(x500Name)

Fuzzy matching

val matchingParties = rpcOps.partiesFromName("PartyB", false)
if (matchingParties.size != 1) {
    throw IllegalArgumentException()
}
val party2 = matchingParties.single()

Getting an CordaRPCOps instance

Here's an example of how you'd get a CordaRPCOps instance in the first place:

val rpcAddress = NetworkHostAndPort(host, rpcPort)
val rpcClient = CordaRPCClient(rpcAddress)
val rpcConnection = rpcClient.start(username, password)
val cordaRPCOps = rpcConnection.proxy
like image 65
Joel Avatar answered Sep 14 '25 19:09

Joel


You could also get the Party instance without using CordaRPCOps. But this solution requires you to have access to the ServiceHub instance. This means you can access it from a CordaService. Let's say serviceHub is a CordaService instance. Then, try the following:

val x500Name = CordaX500Name.parse("O=PartyB,L=New York,C=US")
val party: Party? = serviceHub.networkMapCache.getPeerByLegalName(x500Name)

This will handy in case you require Party instance from String in a Corda service, where it is unnecessary to create an RPC connection.

like image 34
Koshik Raj Avatar answered Sep 14 '25 20:09

Koshik Raj