-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TSDK-729 Support for Sha256 Digest Locks (#172)
* Added the Sha256 implementation to Quivr Context * implemented tests * Mentioned the new routine in the docs * Updated Changelog
- Loading branch information
1 parent
d1e238a
commit 46bfed0
Showing
8 changed files
with
105 additions
and
6 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
37 changes: 37 additions & 0 deletions
37
brambl-sdk/src/main/scala/co/topl/brambl/validation/Sha256DigestInterpreter.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package co.topl.brambl.validation | ||
|
||
import cats.Monad | ||
import cats.implicits.{catsSyntaxApplicativeId, catsSyntaxEitherId} | ||
import co.topl.crypto.hash.implicits.sha256Hash | ||
import co.topl.quivr.algebras.DigestVerifier | ||
import co.topl.quivr.runtime.QuivrRuntimeError | ||
import co.topl.quivr.runtime.QuivrRuntimeErrors.ValidationError.{ | ||
LockedPropositionIsUnsatisfiable, | ||
UserProvidedInterfaceFailure | ||
} | ||
import quivr.models.{Digest, DigestVerification, Preimage} | ||
|
||
/** | ||
* Validates that a Sha256 digest is valid. | ||
*/ | ||
object Sha256DigestInterpreter { | ||
|
||
def make[F[_]: Monad](): DigestVerifier[F] = new DigestVerifier[F] { | ||
|
||
/** | ||
* Validates that an Sha256 digest is valid. | ||
* @param t DigestVerification object containing the digest and preimage | ||
* @return The DigestVerification object if the digest is valid, otherwise an error | ||
*/ | ||
override def validate(t: DigestVerification): F[Either[QuivrRuntimeError, DigestVerification]] = t match { | ||
case DigestVerification(Digest(expectedDigest, _), Preimage(p, salt, _), _) => | ||
val testHash: Array[Byte] = sha256Hash.hash(p.toByteArray ++ salt.toByteArray).value | ||
if (java.util.Arrays.equals(testHash, expectedDigest.toByteArray)) | ||
t.asRight[QuivrRuntimeError].pure[F] | ||
else | ||
(LockedPropositionIsUnsatisfiable: QuivrRuntimeError).asLeft[DigestVerification].pure[F] | ||
case _ => | ||
(UserProvidedInterfaceFailure: QuivrRuntimeError).asLeft[DigestVerification].pure[F] | ||
} | ||
} | ||
} |
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
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