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.")
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With