Use questionable library for handling Option and Result

This commit is contained in:
Mark Spanbroek 2021-03-09 09:37:27 +01:00
parent 2e9896c13a
commit 4afe88965c
9 changed files with 18 additions and 39 deletions

View File

@ -5,6 +5,7 @@ description = "Nitro state channels"
requires "nim >= 1.2.6 & < 2.0.0"
requires "nimcrypto >= 0.5.4 & < 0.6.0"
requires "questionable >= 0.2.0 & < 0.3.0"
requires "secp256k1"
requires "stint"
requires "stew"

View File

@ -1,13 +0,0 @@
import std/options
import pkg/stew/results
include ./noerrors
export options
export results
proc toOption*[T, E](res: Result[T, E]): Option[T] =
if res.isOk:
res.unsafeGet().some
else:
T.none

View File

@ -1,8 +0,0 @@
## Include this file to indicate that your module does not raise Errors.
## Disables compiler hints about unused declarations in Nim < 1.4.0
when (NimMajor, NimMinor, NimPatch) >= (1, 4, 0):
{.push raises:[].}
else:
{.push raises: [Defect].}
{.hint[XDeclaredButNotUsed]: off.}

View File

@ -2,7 +2,7 @@ import pkg/stew/endians2
import pkg/stint
import ../types
include ../noerrors
include questionable/errorban
export types

View File

@ -2,7 +2,7 @@ import pkg/nimcrypto
import ../types
import ./abi
include ../noerrors
include questionable/errorban
export types

View File

@ -2,7 +2,7 @@ import pkg/nimcrypto
import ../types
import ./abi
include ../noerrors
include questionable/errorban
export types

View File

@ -1,13 +1,13 @@
import std/options
import pkg/questionable
import pkg/questionable/results
import pkg/nimcrypto
import pkg/secp256k1
import pkg/stew/byteutils
import ../helpers
import ./state
include ../noerrors
include questionable/errorban
export options
export questionable
export toPublicKey
type
@ -24,7 +24,7 @@ proc random*(_: type PrivateKey): PrivateKey =
proc `$`*(key: PrivateKey): string =
key.toHex()
proc parse*(_: type PrivateKey, s: string): Option[PrivateKey] =
proc parse*(_: type PrivateKey, s: string): ?PrivateKey =
SkSecretKey.fromHex(s).toOption()
proc sign(key: PrivateKey, data: openArray[byte]): Signature =
@ -47,10 +47,9 @@ proc `$`*(signature: Signature): string =
bytes[64] += 27
bytes.toHex()
proc parse*(_: type Signature, s: string): Option[Signature] =
try:
proc parse*(_: type Signature, s: string): ?Signature =
let signature = catch:
var bytes = array[65, byte].fromHex(s)
bytes[64] = bytes[64] - 27
SkRecoverableSignature.fromRaw(bytes).toOption()
except ValueError:
Signature.none
SkRecoverableSignature.fromRaw(bytes).get()
signature.toOption()

View File

@ -4,7 +4,7 @@ import ./channel
import ./outcome
import ./abi
include ../noerrors
include questionable/errorban
export types
export channel

View File

@ -1,12 +1,12 @@
import std/math
import std/options
import pkg/questionable
import pkg/stint
import pkg/stew/byteutils
include ./noerrors
include questionable/errorban
export stint
export options
export questionable
type
UInt48* = range[0'u64..2'u64^48-1]
@ -15,7 +15,7 @@ type
proc toArray*(address: EthAddress): array[20, byte] =
array[20, byte](address)
proc fromHex*(_: type EthAddress, hex: string): Option[EthAddress] =
proc fromHex*(_: type EthAddress, hex: string): ?EthAddress =
try:
EthAddress(array[20, byte].fromHex(hex)).some
except ValueError: