From b3f3ee0562c268fa4d9886c912c155af1575df56 Mon Sep 17 00:00:00 2001 From: Andrius Dagys Date: Fri, 23 Sep 2016 16:19:49 +0100 Subject: [PATCH] Added a json serializer for PublicKeyTree Typo fixes, other minor refactorings --- .../com/r3corda/core/crypto/PublicKeyTree.kt | 4 ++-- .../r3corda/core/crypto/PublicKeyTreeTests.kt | 12 +++++++++++ .../com/r3corda/node/utilities/JsonSupport.kt | 20 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/core/src/main/kotlin/com/r3corda/core/crypto/PublicKeyTree.kt b/core/src/main/kotlin/com/r3corda/core/crypto/PublicKeyTree.kt index d8e1c1514a..fa9590db38 100644 --- a/core/src/main/kotlin/com/r3corda/core/crypto/PublicKeyTree.kt +++ b/core/src/main/kotlin/com/r3corda/core/crypto/PublicKeyTree.kt @@ -9,9 +9,9 @@ import java.security.PublicKey /** * A tree data structure that enables the representation of composite public keys. * - * It the simplest case it may just contain a single node encapsulating a [PublicKey] – a [Leaf]. + * In the simplest case it may just contain a single node encapsulating a [PublicKey] – a [Leaf]. * - * For more complex scenarios, such as *"Both Alice and Bob need to sign to consume a sate S"*, we can represent + * For more complex scenarios, such as *"Both Alice and Bob need to sign to consume a state S"*, we can represent * the requirement by creating a tree with a root [Node], and Alice and Bob as children – [Leaf]s. * The root node would specify *weights* for each of its children and a *threshold* – the minimum total weight required * (e.g. the minimum number of child signatures required) to satisfy the tree signature requirement. diff --git a/core/src/test/kotlin/com/r3corda/core/crypto/PublicKeyTreeTests.kt b/core/src/test/kotlin/com/r3corda/core/crypto/PublicKeyTreeTests.kt index 4ce89761a0..27215e906b 100644 --- a/core/src/test/kotlin/com/r3corda/core/crypto/PublicKeyTreeTests.kt +++ b/core/src/test/kotlin/com/r3corda/core/crypto/PublicKeyTreeTests.kt @@ -2,6 +2,7 @@ package com.r3corda.core.crypto import com.r3corda.core.serialization.OpaqueBytes import org.junit.Test +import kotlin.test.assertEquals import kotlin.test.assertTrue class PublicKeyTreeTests { @@ -46,4 +47,15 @@ class PublicKeyTreeTests { assertTrue { aliceAndBobOrCharlie.isFulfilledBy(signatures.byKeys()) } } + + @Test + fun `encoded tree decodes correctly`() { + val aliceAndBob = PublicKeyTree.Builder().addKeys(alicePublicKey, bobPublicKey).build() + val aliceAndBobOrCharlie = PublicKeyTree.Builder().addKeys(aliceAndBob, charliePublicKey).build(threshold = 1) + + val encoded = aliceAndBobOrCharlie.toBase58String() + val decoded = PublicKeyTree.parseFromBase58(encoded) + + assertEquals(decoded, aliceAndBobOrCharlie) + } } \ No newline at end of file diff --git a/node/src/main/kotlin/com/r3corda/node/utilities/JsonSupport.kt b/node/src/main/kotlin/com/r3corda/node/utilities/JsonSupport.kt index a35ef0e1cd..07c492b0f2 100644 --- a/node/src/main/kotlin/com/r3corda/node/utilities/JsonSupport.kt +++ b/node/src/main/kotlin/com/r3corda/node/utilities/JsonSupport.kt @@ -50,6 +50,10 @@ object JsonSupport { cordaModule.addSerializer(EdDSAPublicKey::class.java, PublicKeySerializer) cordaModule.addDeserializer(EdDSAPublicKey::class.java, PublicKeyDeserializer) + // For public key trees + cordaModule.addSerializer(PublicKeyTree::class.java, PublicKeyTreeSerializer) + cordaModule.addDeserializer(PublicKeyTree::class.java, PublicKeyTreeDeserializer) + mapper.registerModule(timeModule) mapper.registerModule(cordaModule) mapper.registerModule(KotlinModule()) @@ -148,4 +152,20 @@ object JsonSupport { } } } + + object PublicKeyTreeSerializer : JsonSerializer() { + override fun serialize(obj: PublicKeyTree, generator: JsonGenerator, provider: SerializerProvider) { + generator.writeString(obj.toBase58String()) + } + } + + object PublicKeyTreeDeserializer : JsonDeserializer() { + override fun deserialize(parser: JsonParser, context: DeserializationContext): PublicKeyTree { + return try { + PublicKeyTree.parseFromBase58(parser.text) + } catch (e: Exception) { + throw JsonParseException(parser, "Invalid public key tree ${parser.text}: ${e.message}") + } + } + } }