nim-nitro/nitro/basics/destination.nim

29 lines
772 B
Nim
Raw Normal View History

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