From a9508b393cb12f6f15202c52e19f7495a1ea6a8d Mon Sep 17 00:00:00 2001 From: Viktor Kolomeyko Date: Mon, 9 Oct 2017 11:22:11 +0100 Subject: [PATCH] CORDA-540: AMQP Private key serializer (#1838) --- .../serialization/AMQPSerializationScheme.kt | 1 + .../amqp/custom/PrivateKeySerializer.kt | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/amqp/custom/PrivateKeySerializer.kt diff --git a/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/AMQPSerializationScheme.kt b/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/AMQPSerializationScheme.kt index bbc3be72a87..501840e3df6 100644 --- a/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/AMQPSerializationScheme.kt +++ b/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/AMQPSerializationScheme.kt @@ -33,6 +33,7 @@ abstract class AbstractAMQPSerializationScheme : SerializationScheme { fun registerCustomSerializers(factory: SerializerFactory) { with(factory) { register(net.corda.nodeapi.internal.serialization.amqp.custom.PublicKeySerializer) + register(net.corda.nodeapi.internal.serialization.amqp.custom.PrivateKeySerializer) register(net.corda.nodeapi.internal.serialization.amqp.custom.ThrowableSerializer(this)) register(net.corda.nodeapi.internal.serialization.amqp.custom.X500NameSerializer) register(net.corda.nodeapi.internal.serialization.amqp.custom.BigDecimalSerializer) diff --git a/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/amqp/custom/PrivateKeySerializer.kt b/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/amqp/custom/PrivateKeySerializer.kt new file mode 100644 index 00000000000..f9a2d1817df --- /dev/null +++ b/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/amqp/custom/PrivateKeySerializer.kt @@ -0,0 +1,27 @@ +package net.corda.nodeapi.internal.serialization.amqp.custom + +import net.corda.core.crypto.Crypto +import net.corda.core.serialization.SerializationContext.UseCase.* +import net.corda.nodeapi.internal.serialization.amqp.* +import net.corda.nodeapi.internal.serialization.checkUseCase +import org.apache.qpid.proton.codec.Data +import java.lang.reflect.Type +import java.security.PrivateKey +import java.util.* + +object PrivateKeySerializer : CustomSerializer.Implements(PrivateKey::class.java) { + + private val allowedUseCases = EnumSet.of(Storage, Checkpoint) + + override val schemaForDocumentation = Schema(listOf(RestrictedType(type.toString(), "", listOf(type.toString()), SerializerFactory.primitiveTypeName(ByteArray::class.java)!!, descriptor, emptyList()))) + + override fun writeDescribedObject(obj: PrivateKey, data: Data, type: Type, output: SerializationOutput) { + checkUseCase(allowedUseCases) + output.writeObject(obj.encoded, data, clazz) + } + + override fun readObject(obj: Any, schema: Schema, input: DeserializationInput): PrivateKey { + val bits = input.readObject(obj, schema, ByteArray::class.java) as ByteArray + return Crypto.decodePrivateKey(bits) + } +} \ No newline at end of file