Updated DB schemas from using string'ified key to use hash of key.

This commit is contained in:
josecoll 2017-10-16 17:23:12 +01:00
parent 20e0e63eed
commit 0e5346caa1
4 changed files with 21 additions and 20 deletions

View File

@ -1,11 +1,11 @@
package net.corda.core.schemas package net.corda.core.schemas
import net.corda.core.crypto.SecureHash
import net.corda.core.identity.PartyAndCertificate import net.corda.core.identity.PartyAndCertificate
import net.corda.core.node.NodeInfo import net.corda.core.node.NodeInfo
import net.corda.core.serialization.deserialize import net.corda.core.serialization.deserialize
import net.corda.core.serialization.serialize import net.corda.core.serialization.serialize
import net.corda.core.utilities.NetworkHostAndPort import net.corda.core.utilities.NetworkHostAndPort
import net.corda.core.utilities.toBase58String
import java.io.Serializable import java.io.Serializable
import javax.persistence.* import javax.persistence.*
@ -85,8 +85,8 @@ object NodeInfoSchemaV1 : MappedSchema(
@Table(name = "node_info_party_cert") @Table(name = "node_info_party_cert")
data class DBPartyAndCertificate( data class DBPartyAndCertificate(
@Id @Id
@Column(name = "owning_key", length = 65535, nullable = false) @Column(name = "owning_key_hash")
val owningKey: String, 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. //@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) @Column(name = "party_name", nullable = false)
@ -102,10 +102,10 @@ object NodeInfoSchemaV1 : MappedSchema(
private val persistentNodeInfos: Set<PersistentNodeInfo> = emptySet() private val persistentNodeInfos: Set<PersistentNodeInfo> = emptySet()
) { ) {
constructor(partyAndCert: PartyAndCertificate, isMain: Boolean = false) 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 { fun toLegalIdentityAndCert(): PartyAndCertificate {
return partyCertBinary.deserialize<PartyAndCertificate>() return partyCertBinary.deserialize()
} }
} }
} }

View File

@ -8,6 +8,7 @@ import net.corda.core.serialization.SerializationDefaults
import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.serialization.SingletonSerializeAsToken
import net.corda.core.serialization.deserialize import net.corda.core.serialization.deserialize
import net.corda.core.serialization.serialize import net.corda.core.serialization.serialize
import net.corda.core.utilities.hexToBase58
import net.corda.core.utilities.parsePublicKeyBase58 import net.corda.core.utilities.parsePublicKeyBase58
import net.corda.core.utilities.toBase58String import net.corda.core.utilities.toBase58String
import net.corda.node.utilities.AppendOnlyPersistentMap import net.corda.node.utilities.AppendOnlyPersistentMap
@ -34,29 +35,28 @@ class PersistentKeyManagementService(val identityService: IdentityService,
@Entity @Entity
@javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}our_key_pairs") @javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}our_key_pairs")
class PersistentKey( class PersistentKey(
@Id @Id
@Column(length = 6000, name = "public_key") @Column(name = "public_key_hash")
var publicKey: String = "", var publicKeyHash: String,
@Lob @Lob
@Column(name = "private_key") @Column(name = "private_key")
var privateKey: ByteArray = ByteArray(0) var privateKey: ByteArray = ByteArray(0)
) ) {
constructor(publicKey: PublicKey, privateKey: ByteArray)
: this(SecureHash.sha256(publicKey.encoded).toString(), privateKey)
}
private companion object { private companion object {
fun createKeyMap(): AppendOnlyPersistentMap<PublicKey, PrivateKey, PersistentKey, String> { fun createKeyMap(): AppendOnlyPersistentMap<PublicKey, PrivateKey, PersistentKey, String> {
return AppendOnlyPersistentMap( return AppendOnlyPersistentMap(
toPersistentEntityKey = { it.toBase58String() }, toPersistentEntityKey = { it.toBase58String() },
fromPersistentEntity = { fromPersistentEntity = {
Pair(parsePublicKeyBase58(it.publicKey), Pair(parsePublicKeyBase58(it.publicKeyHash.hexToBase58()),
it.privateKey.deserialize<PrivateKey>(context = SerializationDefaults.STORAGE_CONTEXT)) it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT))
}, },
toPersistentEntity = { key: PublicKey, value: PrivateKey -> toPersistentEntity = { key: PublicKey, value: PrivateKey ->
PersistentKey().apply { PersistentKey(key, value.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes)
publicKey = key.toBase58String()
privateKey = value.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes
}
}, },
persistentEntityClass = PersistentKey::class.java persistentEntityClass = PersistentKey::class.java
) )

View File

@ -1,6 +1,7 @@
package net.corda.node.services.network package net.corda.node.services.network
import net.corda.core.concurrent.CordaFuture import net.corda.core.concurrent.CordaFuture
import net.corda.core.crypto.SecureHash
import net.corda.core.identity.AbstractParty import net.corda.core.identity.AbstractParty
import net.corda.core.identity.CordaX500Name import net.corda.core.identity.CordaX500Name
import net.corda.core.identity.Party 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.serialization.serialize
import net.corda.core.utilities.NetworkHostAndPort import net.corda.core.utilities.NetworkHostAndPort
import net.corda.core.utilities.loggerFor 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.NetworkCacheException
import net.corda.node.services.api.NetworkMapCacheInternal import net.corda.node.services.api.NetworkMapCacheInternal
import net.corda.node.services.api.ServiceHubInternal 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<NodeInfoSchemaV1.PersistentNodeInfo> { private fun findByIdentityKey(session: Session, identityKey: PublicKey): List<NodeInfoSchemaV1.PersistentNodeInfo> {
val query = session.createQuery( 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) NodeInfoSchemaV1.PersistentNodeInfo::class.java)
query.setParameter("owningKey", identityKey.toBase58String()) query.setParameter("owningKeyHash", SecureHash.sha256(identityKey.encoded).toString())
return query.resultList return query.resultList
} }

View File

@ -31,8 +31,9 @@ class PersistentNetworkMapService(network: MessagingService, networkMapCache: Ne
@Entity @Entity
@Table(name = "${NODE_DATABASE_PREFIX}network_map_nodes") @Table(name = "${NODE_DATABASE_PREFIX}network_map_nodes")
class NetworkNode( class NetworkNode(
@Id @Column(name = "node_party_key_hash") @Id
var publicKeyHash: String = "", @Column(name = "node_party_key_hash")
var publicKeyHash: String,
@Column @Column
var nodeParty: NodeParty = NodeParty(), var nodeParty: NodeParty = NodeParty(),