diff --git a/core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt b/core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt index a0e6c80f49..8c014cb9a5 100644 --- a/core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt +++ b/core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt @@ -1,11 +1,11 @@ package net.corda.core.schemas +import net.corda.core.crypto.SecureHash import net.corda.core.identity.PartyAndCertificate import net.corda.core.node.NodeInfo import net.corda.core.serialization.deserialize import net.corda.core.serialization.serialize import net.corda.core.utilities.NetworkHostAndPort -import net.corda.core.utilities.toBase58String import java.io.Serializable import javax.persistence.* @@ -85,8 +85,8 @@ object NodeInfoSchemaV1 : MappedSchema( @Table(name = "node_info_party_cert") data class DBPartyAndCertificate( @Id - @Column(name = "owning_key", length = 65535, nullable = false) - val owningKey: String, + @Column(name = "owning_key_hash") + val owningKeyHash: String, //@Id // TODO Do we assume that names are unique? Note: We can't have it as Id, because our toString on X500 is inconsistent. @Column(name = "party_name", nullable = false) @@ -102,10 +102,10 @@ object NodeInfoSchemaV1 : MappedSchema( private val persistentNodeInfos: Set = emptySet() ) { constructor(partyAndCert: PartyAndCertificate, isMain: Boolean = false) - : this(partyAndCert.party.owningKey.toBase58String(), partyAndCert.party.name.toString(), partyAndCert.serialize().bytes, isMain) + : this(SecureHash.sha256(partyAndCert.owningKey.encoded).toString(), partyAndCert.party.name.toString(), partyAndCert.serialize().bytes, isMain) fun toLegalIdentityAndCert(): PartyAndCertificate { - return partyCertBinary.deserialize() + return partyCertBinary.deserialize() } } } 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 4645c1d8e0..c1957696ab 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,6 +8,7 @@ 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 @@ -34,29 +35,28 @@ class PersistentKeyManagementService(val identityService: IdentityService, @Entity @javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}our_key_pairs") class PersistentKey( - @Id - @Column(length = 6000, name = "public_key") - var publicKey: String = "", + @Column(name = "public_key_hash") + var publicKeyHash: String, @Lob @Column(name = "private_key") var privateKey: ByteArray = ByteArray(0) - ) + ) { + constructor(publicKey: PublicKey, privateKey: ByteArray) + : this(SecureHash.sha256(publicKey.encoded).toString(), privateKey) + } private companion object { fun createKeyMap(): AppendOnlyPersistentMap { return AppendOnlyPersistentMap( toPersistentEntityKey = { it.toBase58String() }, fromPersistentEntity = { - Pair(parsePublicKeyBase58(it.publicKey), - it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) + Pair(parsePublicKeyBase58(it.publicKeyHash.hexToBase58()), + it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) }, toPersistentEntity = { key: PublicKey, value: PrivateKey -> - PersistentKey().apply { - publicKey = key.toBase58String() - privateKey = value.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes - } + PersistentKey(key, value.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes) }, persistentEntityClass = PersistentKey::class.java ) diff --git a/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapCache.kt b/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapCache.kt index eb45e7a0df..a6dffc54c1 100644 --- a/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapCache.kt +++ b/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapCache.kt @@ -1,6 +1,7 @@ package net.corda.node.services.network import net.corda.core.concurrent.CordaFuture +import net.corda.core.crypto.SecureHash import net.corda.core.identity.AbstractParty import net.corda.core.identity.CordaX500Name import net.corda.core.identity.Party @@ -21,7 +22,6 @@ import net.corda.core.serialization.deserialize import net.corda.core.serialization.serialize import net.corda.core.utilities.NetworkHostAndPort import net.corda.core.utilities.loggerFor -import net.corda.core.utilities.toBase58String import net.corda.node.services.api.NetworkCacheException import net.corda.node.services.api.NetworkMapCacheInternal import net.corda.node.services.api.ServiceHubInternal @@ -310,9 +310,9 @@ open class PersistentNetworkMapCache(private val serviceHub: ServiceHubInternal) private fun findByIdentityKey(session: Session, identityKey: PublicKey): List { val query = session.createQuery( - "SELECT n FROM ${NodeInfoSchemaV1.PersistentNodeInfo::class.java.name} n JOIN n.legalIdentitiesAndCerts l WHERE l.owningKey = :owningKey", + "SELECT n FROM ${NodeInfoSchemaV1.PersistentNodeInfo::class.java.name} n JOIN n.legalIdentitiesAndCerts l WHERE l.owningKeyHash = :owningKeyHash", NodeInfoSchemaV1.PersistentNodeInfo::class.java) - query.setParameter("owningKey", identityKey.toBase58String()) + query.setParameter("owningKeyHash", SecureHash.sha256(identityKey.encoded).toString()) return query.resultList } 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 62269a1ba3..8dcce158ac 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 @@ -31,8 +31,9 @@ class PersistentNetworkMapService(network: MessagingService, networkMapCache: Ne @Entity @Table(name = "${NODE_DATABASE_PREFIX}network_map_nodes") class NetworkNode( - @Id @Column(name = "node_party_key_hash") - var publicKeyHash: String = "", + @Id + @Column(name = "node_party_key_hash") + var publicKeyHash: String, @Column var nodeParty: NodeParty = NodeParty(),