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 12c8ca7
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 53 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 @@ -118,7 +118,7 @@ package object models {

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

object nodeState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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 +83,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 @@ -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 @@ -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 12c8ca7

Please sign in to comment.