diff --git a/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt b/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt index 7d3151c63b..18d074cd68 100644 --- a/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt +++ b/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt @@ -12,6 +12,7 @@ import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.utilities.debug import net.corda.core.utilities.loggerFor import net.corda.node.utilities.AppendOnlyPersistentMap +import net.corda.node.utilities.MAX_HASH_HEX_SIZE import net.corda.node.utilities.NODE_DATABASE_PREFIX import org.bouncycastle.cert.X509CertificateHolder import java.io.ByteArrayInputStream @@ -72,8 +73,8 @@ class PersistentIdentityService(identities: Iterable = empt @javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}identities") class PersistentIdentity( @Id - @Column(name = "pk_hash", length = 64) - var publicKeyHash: String = "", + @Column(name = "pk_hash", length = MAX_HASH_HEX_SIZE) + var publicKeyHash: String, @Lob @Column @@ -87,7 +88,7 @@ class PersistentIdentityService(identities: Iterable = empt @Column(name = "name", length = 128) var name: String = "", - @Column(name = "pk_hash", length = 64) + @Column(name = "pk_hash", length = MAX_HASH_HEX_SIZE) var publicKeyHash: String = "" ) diff --git a/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt b/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt index 8eaf8fc0c9..9983976146 100644 --- a/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt +++ b/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt @@ -8,10 +8,8 @@ import net.corda.core.serialization.SerializationDefaults import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.serialization.deserialize import net.corda.core.serialization.serialize -import net.corda.core.utilities.hexToBase58 -import net.corda.core.utilities.parsePublicKeyBase58 -import net.corda.core.utilities.toBase58String import net.corda.node.utilities.AppendOnlyPersistentMap +import net.corda.node.utilities.MAX_HASH_HEX_SIZE import net.corda.node.utilities.NODE_DATABASE_PREFIX import org.bouncycastle.operator.ContentSigner import java.security.KeyPair @@ -36,27 +34,31 @@ class PersistentKeyManagementService(val identityService: IdentityService, @javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}our_key_pairs") class PersistentKey( @Id - @Column(name = "public_key_hash", length = 64) + @Column(name = "public_key_hash", length = MAX_HASH_HEX_SIZE) var publicKeyHash: String, + @Lob + @Column(name = "public_key") + var publicKey: ByteArray = ByteArray(0), + @Lob @Column(name = "private_key") var privateKey: ByteArray = ByteArray(0) ) { - constructor(publicKey: PublicKey, privateKey: ByteArray) - : this(publicKey.encoded.sha256().toString(), privateKey) + constructor(publicKey: PublicKey, privateKey: PrivateKey) + : this(publicKey.toStringShort(), + publicKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes, + privateKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes) } private companion object { fun createKeyMap(): AppendOnlyPersistentMap { return AppendOnlyPersistentMap( - toPersistentEntityKey = { it.toBase58String() }, - fromPersistentEntity = { - Pair(parsePublicKeyBase58(it.publicKeyHash.hexToBase58()), - it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) - }, + toPersistentEntityKey = { it.toStringShort() }, + fromPersistentEntity = { Pair(it.publicKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT), + it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) }, toPersistentEntity = { key: PublicKey, value: PrivateKey -> - PersistentKey(key, value.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes) + PersistentKey(key, value) }, persistentEntityClass = PersistentKey::class.java ) diff --git a/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapService.kt b/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapService.kt index f89f66c84e..df03e8c1e4 100644 --- a/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapService.kt +++ b/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapService.kt @@ -1,7 +1,6 @@ package net.corda.node.services.network -import net.corda.core.crypto.SecureHash -import net.corda.core.crypto.sha256 +import net.corda.core.crypto.toStringShort import net.corda.core.identity.PartyAndCertificate import net.corda.core.internal.ThreadBox import net.corda.core.messaging.SingleMessageRecipient @@ -10,6 +9,7 @@ import net.corda.core.serialization.deserialize import net.corda.core.serialization.serialize import net.corda.node.services.api.NetworkMapCacheInternal import net.corda.node.services.messaging.MessagingService +import net.corda.node.utilities.MAX_HASH_HEX_SIZE import net.corda.node.utilities.NODE_DATABASE_PREFIX import net.corda.node.utilities.PersistentMap import net.corda.nodeapi.ArtemisMessagingComponent @@ -34,7 +34,7 @@ class PersistentNetworkMapService(network: MessagingService, networkMapCache: Ne @Table(name = "${NODE_DATABASE_PREFIX}network_map_nodes") class NetworkNode( @Id - @Column(name = "node_party_key_hash", length = 64) + @Column(name = "node_party_key_hash", length = MAX_HASH_HEX_SIZE) var publicKeyHash: String, @Column @@ -61,14 +61,14 @@ class PersistentNetworkMapService(network: MessagingService, networkMapCache: Ne fun createNetworkNodesMap(): PersistentMap { return PersistentMap( - toPersistentEntityKey = { SecureHash.sha256(it.owningKey.encoded).toString() }, + toPersistentEntityKey = { it.owningKey.toStringShort() }, fromPersistentEntity = { Pair(PartyAndCertificate(factory.generateCertPath(ByteArrayInputStream(it.nodeParty.certPath))), it.registrationInfo.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) }, toPersistentEntity = { key: PartyAndCertificate, value: NodeRegistrationInfo -> NetworkNode( - publicKeyHash = key.owningKey.encoded.sha256().toString(), + publicKeyHash = key.owningKey.toStringShort(), nodeParty = NodeParty( key.name.toString(), key.certificate.encoded, diff --git a/node/src/main/kotlin/net/corda/node/utilities/CordaPersistence.kt b/node/src/main/kotlin/net/corda/node/utilities/CordaPersistence.kt index 77055c6073..30c3402c49 100644 --- a/node/src/main/kotlin/net/corda/node/utilities/CordaPersistence.kt +++ b/node/src/main/kotlin/net/corda/node/utilities/CordaPersistence.kt @@ -20,6 +20,14 @@ import java.util.concurrent.CopyOnWriteArrayList */ const val NODE_DATABASE_PREFIX = "node_" +/** + * The maximum supported field-size for hash HEX-encoded outputs (e.g. database fields). + * This value is enough to support hash functions with outputs up to 512 bits (e.g. SHA3-512), in which + * case 128 HEX characters are required. + * 130 was selected instead of 128, to allow for 2 extra characters that will be used as hash-scheme identifiers. + */ +const val MAX_HASH_HEX_SIZE = 130 + //HikariDataSource implements Closeable which allows CordaPersistence to be Closeable class CordaPersistence(var dataSource: HikariDataSource, private val schemaService: SchemaService, private val createIdentityService: () -> IdentityService, databaseProperties: Properties) : Closeable {