2021-03-17 12:53:30 +01:00
|
|
|
import std/hashes
|
2021-03-09 14:02:01 +01:00
|
|
|
import pkg/questionable
|
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
|
import ./ethaddress
|
|
|
|
|
|
|
|
|
|
include questionable/errorban
|
|
|
|
|
|
|
|
|
|
type Destination* = distinct array[32, byte]
|
|
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func toArray*(destination: Destination): array[32, byte] =
|
2021-03-09 14:02:01 +01:00
|
|
|
array[32, byte](destination)
|
|
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func `$`*(destination: Destination): string =
|
2021-03-09 14:02:01 +01:00
|
|
|
destination.toArray().toHex()
|
|
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func parse*(_: type Destination, s: string): ?Destination =
|
2021-03-15 11:23:02 +01:00
|
|
|
Destination(array[32, byte].fromHex(s)).catch.option
|
2021-03-09 14:02:01 +01:00
|
|
|
|
2021-03-17 15:34:55 +01:00
|
|
|
proc `==`*(a, b: Destination): bool {.borrow.}
|
|
|
|
|
proc hash*(destination: Destination): Hash {.borrow.}
|
2021-03-09 14:02:01 +01:00
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func toDestination*(address: EthAddress): Destination =
|
2021-03-09 14:02:01 +01:00
|
|
|
var bytes: array[32, byte]
|
|
|
|
|
for i in 0..<20:
|
|
|
|
|
bytes[12 + i] = array[20, byte](address)[i]
|
|
|
|
|
Destination(bytes)
|