From 491b1abd4a8d752ace3f2c288a3e661188030f3e Mon Sep 17 00:00:00 2001 From: Andras Slemmer Date: Wed, 31 Aug 2016 13:59:48 +0100 Subject: [PATCH] contracts, core, node: Use entropyToKeyPair in Generators instead of manually creating keys --- .../r3corda/contracts/testing/Generators.kt | 3 ++- .../com/r3corda/core/testing/Generators.kt | 26 +++++++------------ .../com/r3corda/node/JsonSupportTest.kt | 3 ++- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/contracts/src/main/kotlin/com/r3corda/contracts/testing/Generators.kt b/contracts/src/main/kotlin/com/r3corda/contracts/testing/Generators.kt index 95f3977aac..e8aa3c1524 100644 --- a/contracts/src/main/kotlin/com/r3corda/contracts/testing/Generators.kt +++ b/contracts/src/main/kotlin/com/r3corda/contracts/testing/Generators.kt @@ -7,6 +7,7 @@ import com.pholser.junit.quickcheck.random.SourceOfRandomness import com.r3corda.contracts.asset.Cash import com.r3corda.core.contracts.* import com.r3corda.core.crypto.DigitalSignature +import com.r3corda.core.crypto.NullSignature import com.r3corda.core.crypto.SecureHash import com.r3corda.core.testing.* import java.util.* @@ -81,7 +82,7 @@ class SignedTransactionGenerator: Generator(SignedTransaction val wireTransaction = WiredTransactionGenerator().generate(random, status) return SignedTransaction( txBits = wireTransaction.serialized, - sigs = wireTransaction.signers.map { DigitalSignature.WithKey(it, random.nextBytes(16)) } + sigs = listOf(NullSignature) ) } } diff --git a/core/src/main/kotlin/com/r3corda/core/testing/Generators.kt b/core/src/main/kotlin/com/r3corda/core/testing/Generators.kt index 7313288f72..20714593cb 100644 --- a/core/src/main/kotlin/com/r3corda/core/testing/Generators.kt +++ b/core/src/main/kotlin/com/r3corda/core/testing/Generators.kt @@ -6,15 +6,10 @@ import com.pholser.junit.quickcheck.generator.java.lang.StringGenerator import com.pholser.junit.quickcheck.generator.java.util.ArrayListGenerator import com.pholser.junit.quickcheck.random.SourceOfRandomness import com.r3corda.core.contracts.* -import com.r3corda.core.crypto.DigitalSignature -import com.r3corda.core.crypto.Party -import com.r3corda.core.crypto.SecureHash -import com.r3corda.core.crypto.ed25519Curve +import com.r3corda.core.crypto.* import com.r3corda.core.serialization.OpaqueBytes -import net.i2p.crypto.eddsa.EdDSAPrivateKey -import net.i2p.crypto.eddsa.EdDSAPublicKey -import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec -import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec +import java.security.PrivateKey +import java.security.PublicKey import java.time.Duration import java.time.Instant import java.util.* @@ -32,18 +27,15 @@ fun Generator.generateList(random: SourceOfRandomness, status: Generation return arrayGenerator.generate(random, status) as List } -class PrivateKeyGenerator: Generator(EdDSAPrivateKey::class.java) { - override fun generate(random: SourceOfRandomness, status: GenerationStatus): EdDSAPrivateKey { - val seed = random.nextBytes(32) - val privateKeySpec = EdDSAPrivateKeySpec(seed, ed25519Curve) - return EdDSAPrivateKey(privateKeySpec) +class PrivateKeyGenerator: Generator(PrivateKey::class.java) { + override fun generate(random: SourceOfRandomness, status: GenerationStatus): PrivateKey { + return entropyToKeyPair(random.nextBigInteger(32)).private } } -class PublicKeyGenerator: Generator(EdDSAPublicKey::class.java) { - override fun generate(random: SourceOfRandomness, status: GenerationStatus): EdDSAPublicKey { - val privateKey = PrivateKeyGenerator().generate(random, status) - return EdDSAPublicKey(EdDSAPublicKeySpec(privateKey.a, ed25519Curve)) +class PublicKeyGenerator: Generator(PublicKey::class.java) { + override fun generate(random: SourceOfRandomness, status: GenerationStatus): PublicKey { + return entropyToKeyPair(random.nextBigInteger(32)).public } } diff --git a/node/src/test/kotlin/com/r3corda/node/JsonSupportTest.kt b/node/src/test/kotlin/com/r3corda/node/JsonSupportTest.kt index b5621be543..d4cbce8dc5 100644 --- a/node/src/test/kotlin/com/r3corda/node/JsonSupportTest.kt +++ b/node/src/test/kotlin/com/r3corda/node/JsonSupportTest.kt @@ -5,6 +5,7 @@ import com.r3corda.core.testing.PublicKeyGenerator import com.r3corda.node.utilities.JsonSupport import net.i2p.crypto.eddsa.EdDSAPublicKey import org.junit.runner.RunWith +import java.security.PublicKey import kotlin.test.assertEquals @RunWith(com.pholser.junit.quickcheck.runner.JUnitQuickcheck::class) @@ -15,7 +16,7 @@ class JsonSupportTest { } @com.pholser.junit.quickcheck.Property - fun publicKeySerializingWorks(@com.pholser.junit.quickcheck.From(PublicKeyGenerator::class) publicKey: EdDSAPublicKey) { + fun publicKeySerializingWorks(@com.pholser.junit.quickcheck.From(PublicKeyGenerator::class) publicKey: PublicKey) { val serialized = mapper.writeValueAsString(publicKey) val parsedKey = mapper.readValue(serialized, EdDSAPublicKey::class.java) assertEquals(publicKey, parsedKey)