Skip to content

Commit

Permalink
Standardisation of Public Keys in Schema entities. (corda#68) (corda#…
Browse files Browse the repository at this point in the history
…1936)

* Standardisation of Public Keys in Schema entities. (corda#68)

* Standardisation in usage of Public Keys in Schema entities.
Use PK Hash where optimal, otherwise use ByteArray/LOB representation of PK.

* Redundant after rebase.

* Use .encoded and Crypto.decode<Public|Private>Key(bytes) instead of Corda serialization.

* Optimize DBPartyAndCertificate entity to store and query on ownerKeyHash.

* Updated API stability check for schema attribute change.
  • Loading branch information
josecoll authored Oct 24, 2017
1 parent 2fe3fbb commit 5349d4f
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .ci/api-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ public static final class net.corda.core.schemas.NodeInfoSchemaV1$DBHostAndPort$
@org.jetbrains.annotations.NotNull public final net.corda.core.schemas.NodeInfoSchemaV1$DBPartyAndCertificate copy(String, String, byte[], boolean, Set)
public boolean equals(Object)
@org.jetbrains.annotations.NotNull public final String getName()
@org.jetbrains.annotations.NotNull public final String getOwningKey()
@org.jetbrains.annotations.NotNull public final String getOwningKeyHash()
@org.jetbrains.annotations.NotNull public final byte[] getPartyCertBinary()
public int hashCode()
public final boolean isMain()
Expand Down
16 changes: 11 additions & 5 deletions core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.corda.core.schemas

import net.corda.core.crypto.toStringShort
import net.corda.core.identity.PartyAndCertificate
import net.corda.core.node.NodeInfo
import net.corda.core.serialization.SerializationDefaults
import net.corda.core.serialization.deserialize
import net.corda.core.serialization.serialize
import net.corda.core.utilities.MAX_HASH_HEX_SIZE
import net.corda.core.utilities.NetworkHostAndPort
import net.corda.core.utilities.toBase58String
import java.io.Serializable
import javax.persistence.*

Expand Down Expand Up @@ -88,20 +90,24 @@ object NodeInfoSchemaV1 : MappedSchema(
@Column(name = "party_name", nullable = false)
val name: String,

@Column(name = "owning_key", length = 65535, nullable = false)
val owningKey: String,
@Lob
@Column(name = "owning_key_hash", length = MAX_HASH_HEX_SIZE)
val owningKeyHash: String,

@Column(name = "party_cert_binary")
@Lob
@Column(name = "party_cert_binary")
val partyCertBinary: ByteArray,


val isMain: Boolean,

@ManyToMany(mappedBy = "legalIdentitiesAndCerts", cascade = arrayOf(CascadeType.ALL)) // ManyToMany because of distributed services.
private val persistentNodeInfos: Set<PersistentNodeInfo> = emptySet()
) {
constructor(partyAndCert: PartyAndCertificate, isMain: Boolean = false)
: this(partyAndCert.name.toString(), partyAndCert.party.owningKey.toBase58String(), partyAndCert.serialize().bytes, isMain)
: this(partyAndCert.name.toString(),
partyAndCert.party.owningKey.toStringShort(),
partyAndCert.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes, isMain)

fun toLegalIdentityAndCert(): PartyAndCertificate {
return partyCertBinary.deserialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import javax.xml.bind.DatatypeConverter

// This file includes useful encoding methods and extension functions for the most common encoding/decoding operations.

/**
* 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

// [ByteArray] encoders

/** Convert a byte array to a Base58 encoded [String]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.corda.finance.contracts
import co.paralleluniverse.fibers.Suspendable
import net.corda.core.contracts.*
import net.corda.core.crypto.NullKeys.NULL_PARTY
import net.corda.core.utilities.toBase58String
import net.corda.core.crypto.toStringShort
import net.corda.core.identity.AbstractParty
import net.corda.core.identity.Party
import net.corda.core.identity.PartyAndCertificate
Expand Down Expand Up @@ -76,13 +76,13 @@ class CommercialPaper : Contract {
override fun generateMappedObject(schema: MappedSchema): PersistentState {
return when (schema) {
is CommercialPaperSchemaV1 -> CommercialPaperSchemaV1.PersistentCommercialPaperState(
issuanceParty = this.issuance.party.owningKey.toBase58String(),
issuancePartyHash = this.issuance.party.owningKey.toStringShort(),
issuanceRef = this.issuance.reference.bytes,
owner = this.owner.owningKey.toBase58String(),
ownerHash = this.owner.owningKey.toStringShort(),
maturity = this.maturityDate,
faceValue = this.faceValue.quantity,
currency = this.faceValue.token.product.currencyCode,
faceValueIssuerParty = this.faceValue.token.issuer.party.owningKey.toBase58String(),
faceValueIssuerPartyHash = this.faceValue.token.issuer.party.owningKey.toStringShort(),
faceValueIssuerRef = this.faceValue.token.issuer.reference.bytes
)
/** Additional schema mappings would be added here (eg. CommercialPaperV2, ...) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.corda.core.contracts.*
import net.corda.core.contracts.Amount.Companion.sumOrThrow
import net.corda.core.crypto.NullKeys.NULL_PARTY
import net.corda.core.crypto.entropyToKeyPair
import net.corda.core.crypto.toStringShort
import net.corda.core.identity.AbstractParty
import net.corda.core.identity.CordaX500Name
import net.corda.core.identity.Party
Expand All @@ -19,7 +20,6 @@ import net.corda.core.schemas.PersistentState
import net.corda.core.schemas.QueryableState
import net.corda.core.transactions.LedgerTransaction
import net.corda.core.transactions.TransactionBuilder
import net.corda.core.utilities.toBase58String
import net.corda.finance.contracts.asset.cash.selection.AbstractCashSelection
import net.corda.finance.schemas.CashSchemaV1
import net.corda.finance.utils.sumCash
Expand Down Expand Up @@ -83,7 +83,7 @@ class Cash : OnLedgerAsset<Currency, Cash.Commands, Cash.State>() {
owner = this.owner,
pennies = this.amount.quantity,
currency = this.amount.token.product.currencyCode,
issuerParty = this.amount.token.issuer.party.owningKey.toBase58String(),
issuerPartyHash = this.amount.token.issuer.party.owningKey.toStringShort(),
issuerRef = this.amount.token.issuer.reference.bytes
)
/** Additional schema mappings would be added here (eg. CashSchemaV2, CashSchemaV3, ...) */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.corda.finance.contracts.asset.cash.selection

import net.corda.core.contracts.Amount
import net.corda.core.crypto.toStringShort
import net.corda.core.identity.AbstractParty
import net.corda.core.identity.Party
import net.corda.core.utilities.*
Expand Down Expand Up @@ -44,7 +45,7 @@ class CashSelectionH2Impl : AbstractCashSelection() {
(if (notary != null)
" AND vs.notary_name = ?" else "") +
(if (onlyFromIssuerParties.isNotEmpty())
" AND ccs.issuer_key IN (?)" else "") +
" AND ccs.issuer_key_hash IN (?)" else "") +
(if (withIssuerRefs.isNotEmpty())
" AND ccs.issuer_ref IN (?)" else "")

Expand All @@ -57,7 +58,7 @@ class CashSelectionH2Impl : AbstractCashSelection() {
if (notary != null)
psSelectJoin.setString(++pIndex, notary.name.toString())
if (onlyFromIssuerParties.isNotEmpty())
psSelectJoin.setObject(++pIndex, onlyFromIssuerParties.map { it.owningKey.toBase58String() as Any}.toTypedArray() )
psSelectJoin.setObject(++pIndex, onlyFromIssuerParties.map { it.owningKey.toStringShort() as Any}.toTypedArray() )
if (withIssuerRefs.isNotEmpty())
psSelectJoin.setObject(++pIndex, withIssuerRefs.map { it.bytes.toHexString() as Any }.toTypedArray())
log.debug { psSelectJoin.toString() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import net.corda.core.identity.AbstractParty
import net.corda.core.schemas.MappedSchema
import net.corda.core.schemas.PersistentState
import net.corda.core.serialization.CordaSerializable
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Index
import javax.persistence.Table
import net.corda.core.utilities.MAX_HASH_HEX_SIZE
import javax.persistence.*

/**
* An object used to fully qualify the [CashSchema] family name (i.e. independent of version).
Expand Down Expand Up @@ -35,8 +33,9 @@ object CashSchemaV1 : MappedSchema(schemaFamily = CashSchema.javaClass, version
@Column(name = "ccy_code", length = 3)
var currency: String,

@Column(name = "issuer_key")
var issuerParty: String,
@Lob
@Column(name = "issuer_key_hash", length = MAX_HASH_HEX_SIZE)
var issuerPartyHash: String,

@Column(name = "issuer_ref")
var issuerRef: ByteArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.corda.finance.schemas
import net.corda.core.schemas.MappedSchema
import net.corda.core.schemas.PersistentState
import net.corda.core.serialization.CordaSerializable
import net.corda.core.utilities.MAX_HASH_HEX_SIZE
import java.time.Instant
import javax.persistence.Column
import javax.persistence.Entity
Expand All @@ -26,14 +27,14 @@ object CommercialPaperSchemaV1 : MappedSchema(schemaFamily = CommercialPaperSche
Index(name = "maturity_index", columnList = "maturity_instant"),
Index(name = "face_value_index", columnList = "face_value")))
class PersistentCommercialPaperState(
@Column(name = "issuance_key")
var issuanceParty: String,
@Column(name = "issuance_key_hash", length = MAX_HASH_HEX_SIZE)
var issuancePartyHash: String,

@Column(name = "issuance_ref")
var issuanceRef: ByteArray,

@Column(name = "owner_key")
var owner: String,
@Column(name = "owner_key_hash", length = MAX_HASH_HEX_SIZE)
var ownerHash: String,

@Column(name = "maturity_instant")
var maturity: Instant,
Expand All @@ -44,8 +45,8 @@ object CommercialPaperSchemaV1 : MappedSchema(schemaFamily = CommercialPaperSche
@Column(name = "ccy_code", length = 3)
var currency: String,

@Column(name = "face_value_issuer_key")
var faceValueIssuerParty: String,
@Column(name = "face_value_issuer_key_hash", length = MAX_HASH_HEX_SIZE)
var faceValueIssuerPartyHash: String,

@Column(name = "face_value_issuer_ref")
var faceValueIssuerRef: ByteArray
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package net.corda.finance.contracts.asset

import net.corda.core.contracts.*
import net.corda.core.utilities.toBase58String
import net.corda.core.crypto.toStringShort
import net.corda.core.identity.AbstractParty
import net.corda.core.internal.Emoji
import net.corda.core.schemas.MappedSchema
import net.corda.core.schemas.PersistentState
import net.corda.core.schemas.QueryableState
import net.corda.core.transactions.LedgerTransaction
import net.corda.finance.utils.sumCash
import net.corda.finance.utils.sumCashOrNull
import net.corda.finance.utils.sumCashOrZero
import net.corda.finance.schemas.SampleCashSchemaV1
import net.corda.finance.schemas.SampleCashSchemaV2
import net.corda.finance.schemas.SampleCashSchemaV3
import net.corda.finance.utils.sumCash
import net.corda.finance.utils.sumCashOrNull
import net.corda.finance.utils.sumCashOrZero
import java.security.PublicKey
import java.util.*

Expand Down Expand Up @@ -43,10 +43,10 @@ class DummyFungibleContract : OnLedgerAsset<Currency, DummyFungibleContract.Comm
override fun generateMappedObject(schema: MappedSchema): PersistentState {
return when (schema) {
is SampleCashSchemaV1 -> SampleCashSchemaV1.PersistentCashState(
owner = this.owner.owningKey.toBase58String(),
ownerHash = this.owner.owningKey.toStringShort(),
pennies = this.amount.quantity,
currency = this.amount.token.product.currencyCode,
issuerParty = this.amount.token.issuer.party.owningKey.toBase58String(),
issuerPartyHash = this.amount.token.issuer.party.owningKey.toStringShort(),
issuerRef = this.amount.token.issuer.reference.bytes
)
is SampleCashSchemaV2 -> SampleCashSchemaV2.PersistentCashState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.corda.finance.schemas

import net.corda.core.schemas.MappedSchema
import net.corda.core.schemas.PersistentState
import net.corda.core.utilities.MAX_HASH_HEX_SIZE
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Index
Expand All @@ -22,17 +23,17 @@ object SampleCashSchemaV1 : MappedSchema(schemaFamily = CashSchema.javaClass, ve
indexes = arrayOf(Index(name = "ccy_code_idx", columnList = "ccy_code"),
Index(name = "pennies_idx", columnList = "pennies")))
class PersistentCashState(
@Column(name = "owner_key")
var owner: String,
@Column(name = "owner_key_hash", length = MAX_HASH_HEX_SIZE)
var ownerHash: String,

@Column(name = "pennies")
var pennies: Long,

@Column(name = "ccy_code", length = 3)
var currency: String,

@Column(name = "issuer_key")
var issuerParty: String,
@Column(name = "issuer_key_hash", length = MAX_HASH_HEX_SIZE)
var issuerPartyHash: String,

@Column(name = "issuer_ref")
var issuerRef: ByteArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.corda.finance.schemas

import net.corda.core.schemas.MappedSchema
import net.corda.core.schemas.PersistentState
import net.corda.core.utilities.MAX_HASH_HEX_SIZE
import java.time.Instant
import javax.persistence.Column
import javax.persistence.Entity
Expand All @@ -24,14 +25,14 @@ object SampleCommercialPaperSchemaV1 : MappedSchema(schemaFamily = CommercialPap
Index(name = "maturity_index", columnList = "maturity_instant"),
Index(name = "face_value_index", columnList = "face_value")))
class PersistentCommercialPaperState(
@Column(name = "issuance_key")
var issuanceParty: String,
@Column(name = "issuance_key_hash", length = MAX_HASH_HEX_SIZE)
var issuancePartyHash: String,

@Column(name = "issuance_ref")
var issuanceRef: ByteArray,

@Column(name = "owner_key")
var owner: String,
@Column(name = "owner_key_hash", length = MAX_HASH_HEX_SIZE)
var ownerHash: String,

@Column(name = "maturity_instant")
var maturity: Instant,
Expand All @@ -42,8 +43,8 @@ object SampleCommercialPaperSchemaV1 : MappedSchema(schemaFamily = CommercialPap
@Column(name = "ccy_code", length = 3)
var currency: String,

@Column(name = "face_value_issuer_key")
var faceValueIssuerParty: String,
@Column(name = "face_value_issuer_key_hash", length = MAX_HASH_HEX_SIZE)
var faceValueIssuerPartyHash: String,

@Column(name = "face_value_issuer_ref")
var faceValueIssuerRef: ByteArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.corda.finance.schemas
import net.corda.core.identity.AbstractParty
import net.corda.core.schemas.CommonSchemaV1
import net.corda.core.schemas.MappedSchema
import net.corda.core.utilities.MAX_HASH_HEX_SIZE
import java.time.Instant
import javax.persistence.Column
import javax.persistence.Entity
Expand All @@ -26,8 +27,8 @@ object SampleCommercialPaperSchemaV2 : MappedSchema(schemaFamily = CommercialPap
@Column(name = "ccy_code", length = 3)
var currency: String,

@Column(name = "face_value_issuer_key")
var faceValueIssuerParty: String,
@Column(name = "face_value_issuer_key_hash", length = MAX_HASH_HEX_SIZE)
var faceValueIssuerPartyHash: String,

@Column(name = "face_value_issuer_ref")
var faceValueIssuerRef: ByteArray,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import net.corda.core.node.services.UnknownAnonymousPartyException
import net.corda.core.serialization.SingletonSerializeAsToken
import net.corda.core.utilities.debug
import net.corda.core.utilities.loggerFor
import net.corda.core.utilities.MAX_HASH_HEX_SIZE
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import net.corda.core.crypto.*
import net.corda.core.identity.PartyAndCertificate
import net.corda.core.node.services.IdentityService
import net.corda.core.node.services.KeyManagementService
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.MAX_HASH_HEX_SIZE
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
Expand Down Expand Up @@ -47,17 +44,15 @@ class PersistentKeyManagementService(val identityService: IdentityService,
var privateKey: ByteArray = ByteArray(0)
) {
constructor(publicKey: PublicKey, privateKey: PrivateKey)
: this(publicKey.toStringShort(),
publicKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes,
privateKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes)
: this(publicKey.toStringShort(), publicKey.encoded, privateKey.encoded)
}

private companion object {
fun createKeyMap(): AppendOnlyPersistentMap<PublicKey, PrivateKey, PersistentKey, String> {
return AppendOnlyPersistentMap(
toPersistentEntityKey = { it.toStringShort() },
fromPersistentEntity = { Pair(it.publicKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT),
it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) },
fromPersistentEntity = { Pair(Crypto.decodePublicKey(it.publicKey), Crypto.decodePrivateKey(
it.privateKey)) },
toPersistentEntity = { key: PublicKey, value: PrivateKey ->
PersistentKey(key, value)
},
Expand Down
Loading

0 comments on commit 5349d4f

Please sign in to comment.