|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 ONIXLabs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.onixlabs.corda.identityframework.workflow |
| 18 | + |
| 19 | +import io.onixlabs.corda.identityframework.contract.accounts.Account |
| 20 | +import io.onixlabs.corda.identityframework.contract.accounts.AccountParty |
| 21 | +import net.corda.core.contracts.UniqueIdentifier |
| 22 | +import net.corda.core.identity.AbstractParty |
| 23 | +import net.corda.core.identity.AnonymousParty |
| 24 | +import net.corda.core.identity.CordaX500Name |
| 25 | +import net.corda.core.node.ServiceHub |
| 26 | +import net.corda.core.utilities.parsePublicKeyBase58 |
| 27 | + |
| 28 | +fun ServiceHub.ownsLegalIdentity(party: AbstractParty): Boolean { |
| 29 | + val partyToResolve = if (party is AccountParty) party.owner else party |
| 30 | + val wellKnownParty = identityService.wellKnownPartyFromAnonymous(partyToResolve) |
| 31 | + |
| 32 | + return wellKnownParty in myInfo.legalIdentities |
| 33 | +} |
| 34 | + |
| 35 | +fun ServiceHub.resolveParty(value: String, type: Class<out Account> = Account::class.java): AbstractParty { |
| 36 | + return if (AccountParty.DELIMITER in value) resolveAccountParty(value, type) else resolveAbstractParty(value) |
| 37 | +} |
| 38 | + |
| 39 | +fun ServiceHub.resolveAbstractParty(value: String): AbstractParty { |
| 40 | + return try { |
| 41 | + val parsedCordaX500Name = CordaX500Name.parse(value) |
| 42 | + identityService.wellKnownPartyFromX500Name(parsedCordaX500Name) |
| 43 | + } catch (ex: Exception) { |
| 44 | + val parsedPublicKey = parsePublicKeyBase58(value) |
| 45 | + AnonymousParty(parsedPublicKey) |
| 46 | + } ?: throw IllegalArgumentException("Failed to resolve '$value' to party.") |
| 47 | +} |
| 48 | + |
| 49 | +fun ServiceHub.resolveAccountParty(value: String, type: Class<out Account> = Account::class.java): AccountParty { |
| 50 | + val identityComponent = value.substringAfter(AccountParty.DELIMITER) |
| 51 | + val linearIdComponent = value.substringBefore(AccountParty.DELIMITER) |
| 52 | + |
| 53 | + val identity = resolveAbstractParty(identityComponent) |
| 54 | + val linearId = UniqueIdentifier.fromString(linearIdComponent) |
| 55 | + |
| 56 | + return AccountParty(identity, linearId, type) |
| 57 | +} |
0 commit comments