Added a json serializer for PublicKeyTree

Typo fixes, other minor refactorings
This commit is contained in:
Andrius Dagys 2016-09-23 16:19:49 +01:00
parent ecb1acdb8d
commit b3f3ee0562
3 changed files with 34 additions and 2 deletions

View File

@ -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.

View File

@ -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)
}
}

View File

@ -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<PublicKeyTree>() {
override fun serialize(obj: PublicKeyTree, generator: JsonGenerator, provider: SerializerProvider) {
generator.writeString(obj.toBase58String())
}
}
object PublicKeyTreeDeserializer : JsonDeserializer<PublicKeyTree>() {
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}")
}
}
}
}