Skip to content

Commit

Permalink
ATL-6924: Simplify types
Browse files Browse the repository at this point in the history
This commit replaces some uses of Array and uses Vector instead to allow
more transparent equality checks
  • Loading branch information
EzequielPostan committed Apr 17, 2024
1 parent 01967cc commit a8afcd6
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ object CryptoUtils {
def checkECDSASignature(msg: Array[Byte], sig: Array[Byte], pubKey: SecpPublicKey): Boolean = {
val ecdsaVerify = Signature.getInstance("SHA256withECDSA", provider)
ecdsaVerify.initVerify(pubKey.publicKey)
ecdsaVerify.update(msg)
ecdsaVerify.verify(sig)
ecdsaVerify.update(msg.toArray)
ecdsaVerify.verify(sig.toArray)
}

def unsafeToSecpPublicKeyFromByteCoordinates(x: Array[Byte], y: Array[Byte]): SecpPublicKey = {
Expand Down Expand Up @@ -88,12 +88,12 @@ object CryptoUtils {
SecpPublicKey.fromPublicKey(fact.generatePublic(keySpec))
}

def unsafeToSecpPublicKeyFromCompressed(com: Array[Byte]): SecpPublicKey = {
def unsafeToSecpPublicKeyFromCompressed(com: Vector[Byte]): SecpPublicKey = {
val params = ECNamedCurveTable.getParameterSpec("secp256k1")
val fact = KeyFactory.getInstance("ECDSA", provider)
val curve = params.getCurve
val ellipticCurve = EC5Util.convertCurve(curve, params.getSeed)
val point = ECPointUtil.decodePoint(ellipticCurve, com)
val point = ECPointUtil.decodePoint(ellipticCurve, com.toArray)
val params2 = EC5Util.convertSpec(ellipticCurve, params)
val keySpec = new ECPublicKeySpec(point, params2)
SecpPublicKey.fromPublicKey(fact.generatePublic(keySpec))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object ProtoCodecs {
node_models
.CompressedECKeyData()
.withCurve(key.curveName)
.withData(ByteString.copyFrom(key.compressedKey))
.withData(ByteString.copyFrom(key.compressedKey.toArray))
}

def toProtoKeyUsage(keyUsage: models.KeyUsage): node_models.KeyUsage = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ package object models {
}

case class PublicKeyData(
curveName: String,
compressedKey: Array[Byte]
curveName: String,
compressedKey: Vector[Byte]
)

object nodeState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ package io.iohk.atala.prism.node.operations
import cats.implicits._
import com.google.protobuf.ByteString
import io.iohk.atala.prism.crypto.Sha256Digest
import io.iohk.atala.prism.node.models.{
PublicKeyData,
DIDPublicKey,
DIDService,
DidSuffix,
KeyUsage,
ProtocolConstants,
}
import io.iohk.atala.prism.node.models.{PublicKeyData, DIDPublicKey, DIDService, DidSuffix, KeyUsage, ProtocolConstants}
import io.iohk.atala.prism.node.operations.ValidationError.{InvalidValue, MissingValue}
import io.iohk.atala.prism.node.operations.path.ValueAtPath
import io.iohk.atala.prism.protos.node_models
Expand Down Expand Up @@ -58,7 +51,7 @@ object ParsingUtils {
ecData(_.x.toByteArray),
ecData(_.y.toByteArray)
)
PublicKeyData(key.curveName, key.compressed)
PublicKeyData(key.curveName, key.compressed.toVector)
}.toEither.left
.map(ex =>
InvalidValue(
Expand All @@ -83,7 +76,7 @@ object ParsingUtils {
Left(ecData.child(_.curve, "curve").invalid(s"Unsupported curve - $curve"))
} else {
Right(
PublicKeyData(curve, ecData(_.data.toByteArray))
PublicKeyData(curve, ecData(_.data.toByteArray).toVector)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ package object daos extends BaseDAO {
)
].contramap { key =>
val curveName = key.key.curveName
val compressed = key.key.compressedKey
val compressed = key.key.compressedKey.toArray
(
key.didSuffix,
key.keyId,
Expand Down Expand Up @@ -130,7 +130,7 @@ package object daos extends BaseDAO {
) =>
val publicKeyData: PublicKeyData = PublicKeyData(
curveId,
compressed
compressed.toVector
)
val revokeLedgerData =
for (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ class BlockProcessingServiceImpl(applyOperationConfig: ApplyOperationConfig) ext
}

def verifySignature(
key: SecpPublicKey,
protoOperation: node_models.SignedAtalaOperation
key: SecpPublicKey,
protoOperation: node_models.SignedAtalaOperation
): Either[StateError, Unit] = {
try {
Either.cond(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ class NodeServiceSpec
publicKey.addedOn.value mustBe ProtoCodecs.toLedgerData(dummyLedgerData)
publicKey.revokedOn mustBe empty

ParsingUtils.parseCompressedECKey(
ValueAtPath(publicKey.getCompressedEcKeyData, Path.root)
).map(_.compressedKey.toVector) must beRight(key.key.compressedKey.toVector)
ParsingUtils
.parseCompressedECKey(
ValueAtPath(publicKey.getCompressedEcKeyData, Path.root)
)
.map(_.compressedKey.toVector) must beRight(key.key.compressedKey.toVector)

response.lastSyncedBlockTimestamp must be(
Some(dummySyncTimestamp.toProtoTimestamp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.iohk.atala.prism.node.crypto
import io.iohk.atala.prism.crypto.EC
import io.iohk.atala.prism.crypto.keys.{ECKeyPair, ECPublicKey}
import io.iohk.atala.prism.node.crypto.CryptoUtils.SecpPublicKey
import io.iohk.atala.prism.node.models.{DIDPublicKey, PublicKeyData}
import io.iohk.atala.prism.node.models.PublicKeyData

object CryptoTestUtils {

Expand All @@ -13,20 +13,13 @@ object CryptoTestUtils {

def generatePublicKeyData(): PublicKeyData = toPublicKeyData(generatePublicKey())

def compareDIDPubKeys(k1: DIDPublicKey, k2: DIDPublicKey): Boolean = {
k1.didSuffix == k2.didSuffix &&
k1.keyId == k2.keyId &&
k1.keyUsage == k2.keyUsage &&
k1.key.compressedKey.toVector == k2.key.compressedKey.toVector
}

def getUnderlyingKey(secpKey: SecpPublicKey): ECPublicKey = EC.INSTANCE.toPublicKeyFromCompressed(secpKey.compressed)

def toPublicKeyData(ecKey: ECPublicKey): PublicKeyData = {
val ecK = CryptoUtils.unsafeToSecpPublicKeyFromCompressed(ecKey.getEncodedCompressed)
val ecK = CryptoUtils.unsafeToSecpPublicKeyFromCompressed(ecKey.getEncodedCompressed.toVector)
PublicKeyData(
ecK.curveName,
ecK.compressed
ecK.compressed.toVector
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CryptoTestsSpec extends AnyWordSpec {
"Must generate the same key from different encodings" in {
val pair = EC.INSTANCE.generateKeyPair()
val compressedPub = pair.getPublicKey.getEncodedCompressed
val secpKeyFromCompressed = CryptoUtils.unsafeToSecpPublicKeyFromCompressed(compressedPub)
val secpKeyFromCompressed = CryptoUtils.unsafeToSecpPublicKeyFromCompressed(compressedPub.toVector)
val x = secpKeyFromCompressed.x
val y = secpKeyFromCompressed.y
val secpFromCoordinates = CryptoUtils.unsafeToSecpPublicKeyFromByteCoordinates(x, y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,28 +1004,21 @@ class CreateDIDOperationSpec extends AtalaWithPostgresSpec {

didState.didSuffix mustBe parsedOperation.id
didState.lastOperation mustBe parsedOperation.digest

val foundDIDs = didState.keys.map(toDIDPublicKey).sortBy(_.keyId)
val originalKeys = parsedOperation.keys.sortBy(_.keyId)

foundDIDs.lazyZip(originalKeys).foreach { case (d1, d2) =>
CryptoTestUtils.compareDIDPubKeys(d1, d2) mustBe true
}
didState.keys.map(
toDIDPublicKey
) must contain theSameElementsAs parsedOperation.keys

didState.context.sorted mustBe parsedOperation.context.sorted

for (key <- parsedOperation.keys) {
val keyState =
DataPreparation.findKey(parsedOperation.id, key.keyId).value
CryptoTestUtils.compareDIDPubKeys(
DIDPublicKey(
keyState.didSuffix,
keyState.keyId,
keyState.keyUsage,
keyState.key
),
key
) mustBe true
DIDPublicKey(
keyState.didSuffix,
keyState.keyId,
keyState.keyUsage,
keyState.key
) mustBe key
keyState.addedOn.timestampInfo mustBe dummyLedgerData.timestampInfo
keyState.revokedOn mustBe None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import io.iohk.atala.prism.node.{AtalaWithPostgresSpec, DataPreparation}
import io.iohk.atala.prism.node.DataPreparation.{dummyApplyOperationConfig, dummyLedgerData, dummyTimestampInfo}
import io.iohk.atala.prism.node.crypto.CryptoTestUtils
import io.iohk.atala.prism.node.grpc.ProtoCodecs
import io.iohk.atala.prism.node.models.{DIDService, KeyUsage, ProtocolConstants}
import io.iohk.atala.prism.node.operations.CreateDIDOperationSpec.{issuingEcKeyData, masterEcKeyData, randomCompressedECKeyData, randomECKeyData}
import io.iohk.atala.prism.node.models.{DIDPublicKey, DIDService, KeyUsage, ProtocolConstants}
import io.iohk.atala.prism.node.operations.CreateDIDOperationSpec.{
issuingEcKeyData,
masterEcKeyData,
randomCompressedECKeyData,
randomECKeyData
}
import io.iohk.atala.prism.node.repositories.daos.{ContextDAO, PublicKeysDAO, ServicesDAO}
import io.iohk.atala.prism.node.services.BlockProcessingServiceSpec
import io.iohk.atala.prism.protos.node_models
Expand Down Expand Up @@ -551,15 +556,15 @@ class UpdateDIDOperationSpec extends AtalaWithPostgresSpec with ProtoParsingTest
newKey.keyUsage mustBe KeyUsage.MasterKey
newKey.didSuffix mustBe createDidOperation.id

val actionKey = parsedOperation.actions.head
DIDPublicKey(
newKey.didSuffix,
newKey.keyId,
newKey.keyUsage,
newKey.key
) mustBe parsedOperation.actions.head
.asInstanceOf[AddKeyAction]
.key

newKey.didSuffix mustBe actionKey.didSuffix
newKey.keyId mustBe actionKey.keyId
newKey.keyUsage mustBe actionKey.keyUsage
newKey.key.compressedKey mustBe actionKey.key.compressedKey

newKey.addedOn.timestampInfo mustBe dummyLedgerData.timestampInfo
newKey.revokedOn mustBe None
didInfo.lastOperation mustBe Sha256.compute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,12 @@ class PublicKeysDAOSpec extends AtalaWithPostgresSpec {
DataPreparation.createDID(didData, dummyLedgerData)
val result = DataPreparation.findKey(didSuffix, "issuing").value

CryptoTestUtils.compareDIDPubKeys(
DIDPublicKey(
result.didSuffix,
result.keyId,
result.keyUsage,
result.key
),
keys.tail.head
) mustBe true
DIDPublicKey(
result.didSuffix,
result.keyId,
result.keyUsage,
result.key
) mustBe keys.tail.head

result.addedOn mustBe dummyLedgerData
result.revokedOn mustBe None
Expand All @@ -92,7 +89,7 @@ class PublicKeysDAOSpec extends AtalaWithPostgresSpec {
DataPreparation.createDID(didData, dummyLedgerData)
val results = DataPreparation.findByDidSuffix(didSuffix).keys

val resultDIDKeys = results
results
.map(didPublicKeyState =>
DIDPublicKey(
didPublicKeyState.didSuffix,
Expand All @@ -101,13 +98,7 @@ class PublicKeysDAOSpec extends AtalaWithPostgresSpec {
didPublicKeyState.key
)
)
.sortWith((l, r) => l.keyId < r.keyId)

val originalDIDKeysSorted = keys.sortWith((l, r) => l.keyId < r.keyId)

resultDIDKeys.lazyZip(originalDIDKeysSorted).foreach { case (x, y) =>
CryptoTestUtils.compareDIDPubKeys(x, y) mustBe true
}
.sortWith((l, r) => l.keyId < r.keyId) mustBe keys.sortWith((l, r) => l.keyId < r.keyId)

results.foreach(didPublicKeyState => didPublicKeyState.addedOn mustBe dummyLedgerData)
results.foreach(didPublicKeyState => didPublicKeyState.revokedOn mustBe None)
Expand Down

0 comments on commit a8afcd6

Please sign in to comment.