-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add JsonCanonicalizer and use in serialization
- Loading branch information
Showing
27 changed files
with
1,240 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 14 additions & 5 deletions
19
src/main/scala/io/constellationnetwork/metagraph_sdk/std/JsonBinaryHasher.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
package io.constellationnetwork.metagraph_sdk.std | ||
|
||
import cats.Functor | ||
import cats.implicits.toFunctorOps | ||
import cats.MonadThrow | ||
import cats.syntax.functor._ | ||
|
||
import org.tessellation.security.hash.Hash | ||
|
||
trait JsonBinaryHasher[F[_]] { | ||
def hash[A](data: A): F[Hash] | ||
def hash[A](data: A)(implicit codec: JsonBinaryCodec[F, A]): F[Hash] | ||
} | ||
|
||
object JsonBinaryHasher { | ||
def apply[F[_]](implicit ev: JsonBinaryHasher[F]): JsonBinaryHasher[F] = ev | ||
|
||
implicit class FromJsonBinaryCodec[F[_]: Functor, A](obj: A)(implicit bin: JsonBinaryCodec[F, A]) { | ||
def hash: F[Hash] = bin.serialize(obj).map(Hash.fromBytes) | ||
implicit def deriveFromCodec[F[_]: MonadThrow]: JsonBinaryHasher[F] = | ||
new JsonBinaryHasher[F] { | ||
|
||
def hash[A](data: A)(implicit codec: JsonBinaryCodec[F, A]): F[Hash] = | ||
codec.serialize(data).map(Hash.fromBytes) | ||
} | ||
|
||
implicit class HasherOps[F[_], A](private val _v: A) extends AnyVal { | ||
|
||
def hash(implicit mt: MonadThrow[F], codec: JsonBinaryCodec[F, A]): F[Hash] = | ||
JsonBinaryHasher[F].hash(_v) | ||
} | ||
} |
Oops, something went wrong.