Explorer

definy.verify-signed-event

description
Read a SignedEvent (Base64) and verify its Ed25519 signature with account_id (verify_ed25519) and that its time is at most 10 minutes in the future (read_time). The event hash is the SHA-256 of the bytes (hash_contents)
type
Fn(
  signed_event_bytes: String,
  then: Fn(result: @definy.signed-event-verification) -> @definy.service-io
) -> @definy.service-io
expression
fn(signed_event_bytes$0, then$1) =>
  match @std.base64-to-bytes(text: $0) {
    #none(_) =>
      $1(result: #invalid<@definy.signed-event-verification>("signedEventBytes must be Base64")),
    #some(bytes$2) =>
      match @definy.decode-signed-event(bytes: $2) {
        #invalid(message$3) =>
          $1(result: #invalid<@definy.signed-event-verification>($3)),
        #decoded(decoded$4) =>
          #verify_ed25519<@definy.service-io>({
            public_key: $4.account_id_bytes,
            signature: $4.signature,
            message: $4.message,
            then: fn(valid$5) =>
              match $5 {
                false =>
                  $1(result: #invalid<@definy.signed-event-verification>("Ed25519 signature verification failed")),
                true =>
                  #read_time<@definy.service-io>({
                    then: fn(now_ms$6) =>
                      match @core.less-than(left: @core.plus(left: $6, right: 600000), right: $4.time_ms) {
                        true =>
                          $1(result: #invalid<@definy.signed-event-verification>("event time is too far in the future")),
                        false =>
                          #hash_contents<@definy.service-io>({
                            contents_bytes: [$0],
                            then: fn(hashes$7) =>
                              $1(
                                result: #valid<@definy.signed-event-verification>({
                                  event_hash: match @core.list-get(list: $7, index: 0) {
                                    #some(hash$8) =>
                                      match $8 {
                                        #some(text$9) =>
                                          $9,
                                        #none(_) =>
                                          ""
                                      },
                                    #none(_) =>
                                      ""
                                  },
                                  account_id: @core.bytes-to-base64url(bytes: $4.account_id_bytes),
                                  time_ms: $4.time_ms,
                                  content: $4.content,
                                  referenced_content_hashes: match $4.content {
                                    #project_commit(commit$10) =>
                                      @definy.snapshot-content-hashes(modules: $10.modules, index: 0),
                                    _ =>
                                      []
                                  }
                                })
                              )
                          })
                      }
                  })
              }
          })
      }
  }
visibility
public
definy.verify-signed-event (project definy, branch release-time-compare, snapshot JJrRgQW_LZN6tN8CbYr1dN7HosqNTuIWWYx7ACbt1n0)
id: IS3J0drNcD1OTyZFGpDRpMMONpb-tX7hExQ8edqYQaI
content hash: mS_unTIP2qC8CNXcQww8v01KScYqiw6qpNjM2VSYSnY