mirror of
https://github.com/corda/corda.git
synced 2025-06-22 17:09:00 +00:00
Added a json serializer for PublicKeyTree
Typo fixes, other minor refactorings
This commit is contained in:
@ -9,9 +9,9 @@ import java.security.PublicKey
|
|||||||
/**
|
/**
|
||||||
* A tree data structure that enables the representation of composite public keys.
|
* 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 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
|
* 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.
|
* (e.g. the minimum number of child signatures required) to satisfy the tree signature requirement.
|
||||||
|
@ -2,6 +2,7 @@ package com.r3corda.core.crypto
|
|||||||
|
|
||||||
import com.r3corda.core.serialization.OpaqueBytes
|
import com.r3corda.core.serialization.OpaqueBytes
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
class PublicKeyTreeTests {
|
class PublicKeyTreeTests {
|
||||||
@ -46,4 +47,15 @@ class PublicKeyTreeTests {
|
|||||||
|
|
||||||
assertTrue { aliceAndBobOrCharlie.isFulfilledBy(signatures.byKeys()) }
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
@ -50,6 +50,10 @@ object JsonSupport {
|
|||||||
cordaModule.addSerializer(EdDSAPublicKey::class.java, PublicKeySerializer)
|
cordaModule.addSerializer(EdDSAPublicKey::class.java, PublicKeySerializer)
|
||||||
cordaModule.addDeserializer(EdDSAPublicKey::class.java, PublicKeyDeserializer)
|
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(timeModule)
|
||||||
mapper.registerModule(cordaModule)
|
mapper.registerModule(cordaModule)
|
||||||
mapper.registerModule(KotlinModule())
|
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}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user