-
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.
✨ added static hex word class and constraint
- Loading branch information
1 parent
f3448aa
commit d8fecbc
Showing
10 changed files
with
100 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package cleareth.model | ||
|
||
import cleareth.model.constraint.HexWord | ||
import cleareth.model.constraint.IsHexWordStrict | ||
import io.github.iltotore.iron.* | ||
import scodec.bits.ByteVector | ||
|
||
/** This object represents an Ethereum Public address, which must be an hexadecimal string of 20 bytes | ||
*/ | ||
opaque type Address = ByteVector :| HexWord[20] | ||
opaque type Address = ByteVector :| IsHexWordStrict[20] | ||
|
||
object Address extends HexWord.Utils[20, Address]: | ||
object Address extends IsHexWordStrict.Utils[20, Address]: | ||
val mint: Address = Address("0x0000000000000000000000000000000000000000") |
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,11 +1,11 @@ | ||
package cleareth.model | ||
|
||
import cleareth.model.constraint.HexWord | ||
import cleareth.model.constraint.IsHexWordStrict | ||
import io.github.iltotore.iron.* | ||
import scodec.bits.ByteVector | ||
|
||
/** This object represents an Ethereum Block hash, which must be an hexadecimal string of 32 bytes | ||
*/ | ||
opaque type BlockHash = ByteVector :| HexWord[32] | ||
opaque type BlockHash = ByteVector :| IsHexWordStrict[32] | ||
|
||
object BlockHash extends HexWord.Utils[32, BlockHash] | ||
object BlockHash extends IsHexWordStrict.Utils[32, BlockHash] |
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,28 @@ | ||
package cleareth.model | ||
|
||
import cleareth.model.constraint.IsHexWord | ||
import io.github.iltotore.iron.* | ||
import scodec.bits.ByteVector | ||
|
||
/** This object represents a static hex word, which must be an hexadecimal string of at most 32 bytes | ||
*/ | ||
opaque type HexWord = ByteVector :| IsHexWord | ||
|
||
object HexWord extends RefinedTypeOpsImpl[ByteVector, IsHexWord, HexWord]: | ||
|
||
inline def apply(inline bv: ByteVector): HexWord = applyUnsafe(bv) | ||
inline def apply(inline str: String): HexWord = applyUnsafe(ByteVector.fromValidHex(str)) | ||
|
||
inline def from(inline bv: ByteVector): Option[HexWord] = option(bv) | ||
inline def from(inline str: String): Option[HexWord] = | ||
for | ||
bv <- ByteVector.fromHex(str) | ||
word <- option(bv) | ||
yield word | ||
|
||
inline def fromDescriptive(inline bv: ByteVector): Either[String, HexWord] = either(bv) | ||
inline def fromDescriptive(inline str: String): Either[String, HexWord] = | ||
for | ||
bv <- ByteVector.fromHexDescriptive(str) | ||
word <- either(bv.padTo(32)) | ||
yield word |
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,11 +1,11 @@ | ||
package cleareth.model | ||
|
||
import cleareth.model.constraint.HexWord | ||
import cleareth.model.constraint.IsHexWordStrict | ||
import io.github.iltotore.iron.* | ||
import scodec.bits.ByteVector | ||
|
||
/** This object represents an Ethereum Transaction hash, which must be an hexadecimal string of 32 bytes | ||
*/ | ||
opaque type TxHash = ByteVector :| HexWord[32] | ||
opaque type TxHash = ByteVector :| IsHexWordStrict[32] | ||
|
||
object TxHash extends HexWord.Utils[32, TxHash] | ||
object TxHash extends IsHexWordStrict.Utils[32, TxHash] |
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,9 +1,9 @@ | ||
package cleareth.model | ||
|
||
import cleareth.model.constraint.UInt | ||
import cleareth.model.constraint.IsUInt | ||
import io.github.iltotore.iron.* | ||
import scodec.bits.ByteVector | ||
|
||
opaque type Wei = ByteVector :| UInt[256] | ||
opaque type Wei = ByteVector :| IsUInt[256] | ||
|
||
object Wei extends UInt.Utils[256, Wei] | ||
object Wei extends IsUInt.Utils[256, Wei] |
14 changes: 14 additions & 0 deletions
14
model/src/main/scala/cleareth/model/constraint/IsHexWord.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,14 @@ | ||
package cleareth.model.constraint | ||
|
||
import io.github.iltotore.iron.* | ||
import scala.compiletime.constValue | ||
import scodec.bits.ByteVector | ||
|
||
final class IsHexWord | ||
|
||
object IsHexWord: | ||
trait Utils[L <: ByteLength, IT <: ByteVector :| IsHexWord] extends ByteVectorUtils[IsHexWord, IT] | ||
|
||
given [L <: ByteLength]: Constraint[ByteVector, IsHexWord] with | ||
inline def test(value: ByteVector): Boolean = value.length <= EVM_WORD_LENGTH | ||
inline def message: String = s"Expecting at most $EVM_WORD_LENGTH bytes" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cleareth.model | ||
|
||
import munit.FunSuite | ||
import scodec.bits.ByteVector | ||
|
||
class HexWordTest extends FunSuite: | ||
|
||
test(s"creation from a valid hexadecimal string should succeed (length < 64)") { | ||
HexWord("0xdeadbeefdeadbeef") | ||
} | ||
|
||
test(s"creation from a valid hexadecimal string should succeed (`0x` string)") { | ||
HexWord("0x") | ||
} | ||
|
||
test(s"creation from a valid hexadecimal string should succeed (length = 64)") { | ||
HexWord("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef") | ||
} | ||
|
||
test(s"creation from a valid hexadecimal string should fail (length > 64)") { | ||
interceptMessage[IllegalArgumentException]( | ||
"Expecting at most 32 bytes" | ||
)(HexWord("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef")) | ||
} |