From 34a480d7e3cecced82b3d02556d7cf77210c3e44 Mon Sep 17 00:00:00 2001 From: Shams Asari Date: Thu, 31 May 2018 17:40:49 +0100 Subject: [PATCH] Move checkPayloadIs out of the public API (#3279) --- .../net/corda/core/internal/InternalUtils.kt | 19 ++++++++++++++----- .../corda/core/utilities/UntrustworthyData.kt | 11 ----------- .../services/statemachine/FlowSessionImpl.kt | 2 +- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt b/core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt index f5a2fdbae1..e329838949 100644 --- a/core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt +++ b/core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt @@ -10,14 +10,12 @@ import net.corda.core.cordapp.CordappContext import net.corda.core.crypto.* import net.corda.core.identity.CordaX500Name import net.corda.core.node.ServicesForResolution -import net.corda.core.serialization.SerializationContext -import net.corda.core.serialization.SerializedBytes -import net.corda.core.serialization.deserialize -import net.corda.core.serialization.serialize +import net.corda.core.serialization.* import net.corda.core.transactions.SignedTransaction import net.corda.core.transactions.TransactionBuilder import net.corda.core.transactions.WireTransaction import net.corda.core.utilities.OpaqueBytes +import net.corda.core.utilities.UntrustworthyData import org.bouncycastle.asn1.x500.X500Name import org.bouncycastle.asn1.x500.X500NameBuilder import org.bouncycastle.asn1.x500.style.BCStyle @@ -505,4 +503,15 @@ fun Iterable.sumByLong(selector: (T) -> Long): Long = this.map { selector */ internal fun SignedTransaction.pushToLoggingContext() { MDC.put("tx_id", id.toString()) -} \ No newline at end of file +} + +fun SerializedBytes.checkPayloadIs(type: Class): UntrustworthyData { + val payloadData: T = try { + val serializer = SerializationDefaults.SERIALIZATION_FACTORY + serializer.deserialize(this, type, SerializationDefaults.P2P_CONTEXT) + } catch (ex: Exception) { + throw IllegalArgumentException("Payload invalid", ex) + } + return type.castIfPossible(payloadData)?.let { UntrustworthyData(it) } + ?: throw IllegalArgumentException("We were expecting a ${type.name} but we instead got a ${payloadData.javaClass.name} ($payloadData)") +} diff --git a/core/src/main/kotlin/net/corda/core/utilities/UntrustworthyData.kt b/core/src/main/kotlin/net/corda/core/utilities/UntrustworthyData.kt index da3efc2020..bb693e99c0 100644 --- a/core/src/main/kotlin/net/corda/core/utilities/UntrustworthyData.kt +++ b/core/src/main/kotlin/net/corda/core/utilities/UntrustworthyData.kt @@ -32,14 +32,3 @@ class UntrustworthyData(@PublishedApi internal val fromUntrustedWorld: T) } inline fun UntrustworthyData.unwrap(validator: (T) -> R): R = validator(fromUntrustedWorld) - -fun SerializedBytes.checkPayloadIs(type: Class): UntrustworthyData { - val payloadData: T = try { - val serializer = SerializationDefaults.SERIALIZATION_FACTORY - serializer.deserialize(this, type, SerializationDefaults.P2P_CONTEXT) - } catch (ex: Exception) { - throw IllegalArgumentException("Payload invalid", ex) - } - return type.castIfPossible(payloadData)?.let { UntrustworthyData(it) } ?: throw IllegalArgumentException("We were expecting a ${type.name} but we instead got a " + - "${payloadData.javaClass.name} ($payloadData)") -} diff --git a/node/src/main/kotlin/net/corda/node/services/statemachine/FlowSessionImpl.kt b/node/src/main/kotlin/net/corda/node/services/statemachine/FlowSessionImpl.kt index a0c3359aca..2e259c2207 100644 --- a/node/src/main/kotlin/net/corda/node/services/statemachine/FlowSessionImpl.kt +++ b/node/src/main/kotlin/net/corda/node/services/statemachine/FlowSessionImpl.kt @@ -12,7 +12,7 @@ import net.corda.core.serialization.SerializedBytes import net.corda.core.serialization.serialize import net.corda.core.utilities.NonEmptySet import net.corda.core.utilities.UntrustworthyData -import net.corda.core.utilities.checkPayloadIs +import net.corda.core.internal.checkPayloadIs class FlowSessionImpl( override val counterparty: Party,