From e7850e6d17f8d61a445f6d9390a18c54837685a5 Mon Sep 17 00:00:00 2001 From: Fabio Pinheiro Date: Mon, 7 Oct 2024 13:10:12 +0100 Subject: [PATCH] fix: add validations to TimestampInfo (#871) Signed-off-by: FabioPinheiro --- .../atala/prism/node/grpc/ProtoCodecs.scala | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/scala/io/iohk/atala/prism/node/grpc/ProtoCodecs.scala b/src/main/scala/io/iohk/atala/prism/node/grpc/ProtoCodecs.scala index db837d5d1..81707acd9 100644 --- a/src/main/scala/io/iohk/atala/prism/node/grpc/ProtoCodecs.scala +++ b/src/main/scala/io/iohk/atala/prism/node/grpc/ProtoCodecs.scala @@ -120,14 +120,26 @@ object ProtoCodecs { def fromTimestampInfoProto( timestampInfoProto: node_models.TimestampInfo ): TimestampInfo = { - new TimestampInfo( - timestampInfoProto.blockTimestamp - .getOrElse(throw new RuntimeException("Missing timestamp")) - .toInstant - .toEpochMilli, - timestampInfoProto.blockSequenceNumber, - timestampInfoProto.operationSequenceNumber - ) + val atalaBlockTimestamp = timestampInfoProto.blockTimestamp + .getOrElse(throw new RuntimeException("Missing timestamp")) + .toInstant + + // https://beta.explorer.cardano.org/en/block/5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb + val firstCardanoBlock = java.time.Instant.ofEpochSecond(1506203091) // -> 2017-09-23T21:44:51Z + + val timestampEpochMilli = + if (!firstCardanoBlock.isAfter(atalaBlockTimestamp)) atalaBlockTimestamp.toEpochMilli + else throw new RuntimeException("TimestampInfo's atalaBlockTimestamp MUST NOT BE before the first Cardano Block") + + val blockSequenceNumber = + if (timestampInfoProto.blockSequenceNumber >= 1) timestampInfoProto.blockSequenceNumber + else throw new RuntimeException("TimestampInfo's blockSequenceNumber MUST be equal or bigger than 1") + + val operationSequenceNumber = + if (timestampInfoProto.operationSequenceNumber >= 0) timestampInfoProto.operationSequenceNumber + else throw new RuntimeException("TimestampInfo's operationSequenceNumber MUST be equal or bigger than 0") + + new TimestampInfo(timestampEpochMilli, blockSequenceNumber, operationSequenceNumber) } def fromProtoKey(protoKey: node_models.PublicKey): Option[SecpPublicKey] = {