Skip to content

Commit

Permalink
fix: add validations to TimestampInfo (#871)
Browse files Browse the repository at this point in the history
Signed-off-by: FabioPinheiro <[email protected]>
  • Loading branch information
FabioPinheiro authored Oct 7, 2024
1 parent b5b9035 commit e7850e6
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/main/scala/io/iohk/atala/prism/node/grpc/ProtoCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down

0 comments on commit e7850e6

Please sign in to comment.