nim-ethers/ethers/address.nim

24 lines
541 B
Nim

import pkg/stew/byteutils
import pkg/questionable
type
Address* = distinct array[20, byte]
func init*(_: type Address, bytes: array[20, byte]): Address =
Address(bytes)
func init*(_: type Address, hex: string): ?Address =
try:
let bytes = array[20, byte].fromHex(hex)
some Address.init(bytes)
except ValueError:
none Address
func toArray(address: Address): array[20, byte] =
array[20, byte](address)
func `$`*(address: Address): string =
"0x" & toHex(address.toArray)
func `==`*(a, b: Address): bool {.borrow.}