mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-07 16:13:09 +00:00
Return Option[T] instead of raising exceptions
This commit is contained in:
parent
8ddd78ed68
commit
72ba624cdc
@ -13,6 +13,8 @@ type
|
|||||||
head: Slice[int]
|
head: Slice[int]
|
||||||
tail: seq[byte]
|
tail: seq[byte]
|
||||||
|
|
||||||
|
{.push raises:[].}
|
||||||
|
|
||||||
proc write*[T](encoder: var AbiEncoder, value: T)
|
proc write*[T](encoder: var AbiEncoder, value: T)
|
||||||
proc encode*[T](_: type AbiEncoder, value: T): seq[byte]
|
proc encode*[T](_: type AbiEncoder, value: T): seq[byte]
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,8 @@ type
|
|||||||
participants*: seq[EthAddress]
|
participants*: seq[EthAddress]
|
||||||
chainId*: UInt256
|
chainId*: UInt256
|
||||||
|
|
||||||
|
{.push raises:[].}
|
||||||
|
|
||||||
proc getChannelId*(channel: Channel): array[32, byte] =
|
proc getChannelId*(channel: Channel): array[32, byte] =
|
||||||
var encoder= AbiEncoder.init()
|
var encoder= AbiEncoder.init()
|
||||||
encoder.startTuple()
|
encoder.startTuple()
|
||||||
|
|||||||
13
nitro/helpers.nim
Normal file
13
nitro/helpers.nim
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import std/options
|
||||||
|
import pkg/stew/results
|
||||||
|
|
||||||
|
export options
|
||||||
|
export results
|
||||||
|
|
||||||
|
{.push raises:[].}
|
||||||
|
|
||||||
|
proc toOption*[T, E](res: Result[T, E]): Option[T] =
|
||||||
|
if res.isOk:
|
||||||
|
res.value.some
|
||||||
|
else:
|
||||||
|
T.none
|
||||||
@ -25,6 +25,8 @@ type
|
|||||||
targetChannelId*: array[32, byte]
|
targetChannelId*: array[32, byte]
|
||||||
destinations*: seq[array[32, byte]]
|
destinations*: seq[array[32, byte]]
|
||||||
|
|
||||||
|
{.push raises:[].}
|
||||||
|
|
||||||
proc encode*(encoder: var AbiEncoder, guarantee: Guarantee) =
|
proc encode*(encoder: var AbiEncoder, guarantee: Guarantee) =
|
||||||
encoder.startTuple()
|
encoder.startTuple()
|
||||||
encoder.startTuple()
|
encoder.startTuple()
|
||||||
|
|||||||
@ -1,8 +1,11 @@
|
|||||||
|
import std/options
|
||||||
import pkg/nimcrypto
|
import pkg/nimcrypto
|
||||||
import pkg/secp256k1
|
import pkg/secp256k1
|
||||||
import pkg/stew/byteutils
|
import pkg/stew/byteutils
|
||||||
import ./state
|
import ./state
|
||||||
|
import ./helpers
|
||||||
|
|
||||||
|
export options
|
||||||
export toPublicKey
|
export toPublicKey
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -10,6 +13,8 @@ type
|
|||||||
PublicKey* = SkPublicKey
|
PublicKey* = SkPublicKey
|
||||||
Signature* = SkRecoverableSignature
|
Signature* = SkRecoverableSignature
|
||||||
|
|
||||||
|
{.push raises:[].}
|
||||||
|
|
||||||
proc rng(data: var openArray[byte]): bool =
|
proc rng(data: var openArray[byte]): bool =
|
||||||
randomBytes(data) == data.len
|
randomBytes(data) == data.len
|
||||||
|
|
||||||
@ -19,8 +24,8 @@ proc random*(_: type PrivateKey): PrivateKey =
|
|||||||
proc `$`*(key: PrivateKey): string =
|
proc `$`*(key: PrivateKey): string =
|
||||||
key.toHex()
|
key.toHex()
|
||||||
|
|
||||||
proc parse*(_: type PrivateKey, s: string): PrivateKey =
|
proc parse*(_: type PrivateKey, s: string): Option[PrivateKey] =
|
||||||
SkSecretKey.fromHex(s).tryGet()
|
SkSecretKey.fromHex(s).toOption()
|
||||||
|
|
||||||
proc sign(key: PrivateKey, data: openArray[byte]): Signature =
|
proc sign(key: PrivateKey, data: openArray[byte]): Signature =
|
||||||
let hash = keccak256.digest(data).data
|
let hash = keccak256.digest(data).data
|
||||||
@ -42,7 +47,10 @@ proc `$`*(signature: Signature): string =
|
|||||||
bytes[64] += 27
|
bytes[64] += 27
|
||||||
bytes.toHex()
|
bytes.toHex()
|
||||||
|
|
||||||
proc parse*(_: type Signature, s: string): Signature =
|
proc parse*(_: type Signature, s: string): Option[Signature] =
|
||||||
var bytes = array[65, byte].fromHex(s)
|
try:
|
||||||
bytes[64] -= 27
|
var bytes = array[65, byte].fromHex(s)
|
||||||
SkRecoverableSignature.fromRaw(bytes).tryGet()
|
bytes[64] = bytes[64] - 27
|
||||||
|
SkRecoverableSignature.fromRaw(bytes).toOption()
|
||||||
|
except ValueError:
|
||||||
|
Signature.none
|
||||||
|
|||||||
@ -27,6 +27,8 @@ type
|
|||||||
outcome*: seq[byte]
|
outcome*: seq[byte]
|
||||||
appdata*: seq[byte]
|
appdata*: seq[byte]
|
||||||
|
|
||||||
|
{.push raises:[].}
|
||||||
|
|
||||||
proc fixedPart*(state: State): FixedPart =
|
proc fixedPart*(state: State): FixedPart =
|
||||||
FixedPart(
|
FixedPart(
|
||||||
chainId: state.channel.chainId,
|
chainId: state.channel.chainId,
|
||||||
|
|||||||
@ -1,17 +1,24 @@
|
|||||||
import std/math
|
import std/math
|
||||||
|
import std/options
|
||||||
import pkg/stint
|
import pkg/stint
|
||||||
import pkg/stew/byteutils
|
import pkg/stew/byteutils
|
||||||
|
|
||||||
export stint
|
export stint
|
||||||
|
export options
|
||||||
|
|
||||||
type
|
type
|
||||||
UInt48* = range[0'u64..2'u64^48-1]
|
UInt48* = range[0'u64..2'u64^48-1]
|
||||||
EthAddress* = distinct array[20, byte]
|
EthAddress* = distinct array[20, byte]
|
||||||
|
|
||||||
|
{.push raises:[].}
|
||||||
|
|
||||||
proc toArray*(address: EthAddress): array[20, byte] =
|
proc toArray*(address: EthAddress): array[20, byte] =
|
||||||
array[20, byte](address)
|
array[20, byte](address)
|
||||||
|
|
||||||
proc fromHex*(_: type EthAddress, hex: string): EthAddress =
|
proc fromHex*(_: type EthAddress, hex: string): Option[EthAddress] =
|
||||||
EthAddress(array[20, byte].fromHex(hex))
|
try:
|
||||||
|
EthAddress(array[20, byte].fromHex(hex)).some
|
||||||
|
except ValueError:
|
||||||
|
EthAddress.none
|
||||||
|
|
||||||
proc `==`*(a, b: EthAddress): bool {.borrow.}
|
proc `==`*(a, b: EthAddress): bool {.borrow.}
|
||||||
|
|||||||
@ -25,7 +25,7 @@ suite "channel":
|
|||||||
chainId: 9001.u256,
|
chainId: 9001.u256,
|
||||||
nonce: 1,
|
nonce: 1,
|
||||||
participants: @[
|
participants: @[
|
||||||
EthAddress.fromHex("24b905Dcc8A11C0FE57C2592f3D25f0447402C10")
|
EthAddress.fromHex("24b905Dcc8A11C0FE57C2592f3D25f0447402C10").get()
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
let expected = array[32, byte].fromHex(
|
let expected = array[32, byte].fromHex(
|
||||||
|
|||||||
@ -94,7 +94,7 @@ suite "outcome":
|
|||||||
kind: allocationType,
|
kind: allocationType,
|
||||||
assetHolder: EthAddress.fromHex(
|
assetHolder: EthAddress.fromHex(
|
||||||
"1E90B49563da16D2537CA1Ddd9b1285279103D93"
|
"1E90B49563da16D2537CA1Ddd9b1285279103D93"
|
||||||
),
|
).get(),
|
||||||
allocation: Allocation(@[
|
allocation: Allocation(@[
|
||||||
AllocationItem(
|
AllocationItem(
|
||||||
destination: array[32, byte].fromHex(
|
destination: array[32, byte].fromHex(
|
||||||
@ -108,7 +108,7 @@ suite "outcome":
|
|||||||
kind: guaranteeType,
|
kind: guaranteeType,
|
||||||
assetHolder: EthAddress.fromHex(
|
assetHolder: EthAddress.fromHex(
|
||||||
"1E90B49563da16D2537CA1Ddd9b1285279103D93"
|
"1E90B49563da16D2537CA1Ddd9b1285279103D93"
|
||||||
),
|
).get(),
|
||||||
guarantee: Guarantee(
|
guarantee: Guarantee(
|
||||||
targetChannelId: array[32, byte].fromHex(
|
targetChannelId: array[32, byte].fromHex(
|
||||||
"cac1bb71f0a97c8ac94ca9546b43178a9ad254c7b757ac07433aa6df35cd8089"
|
"cac1bb71f0a97c8ac94ca9546b43178a9ad254c7b757ac07433aa6df35cd8089"
|
||||||
|
|||||||
@ -26,7 +26,7 @@ suite "signature":
|
|||||||
chainId: 0x1.u256,
|
chainId: 0x1.u256,
|
||||||
nonce: 1,
|
nonce: 1,
|
||||||
participants: @[
|
participants: @[
|
||||||
EthAddress.fromHex("0x8a64E10FF40Bc9C90EA5750313dB5e036495c10E")
|
EthAddress.fromHex("0x8a64E10FF40Bc9C90EA5750313dB5e036495c10E").get()
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
outcome: Outcome(@[]),
|
outcome: Outcome(@[]),
|
||||||
@ -38,10 +38,10 @@ suite "signature":
|
|||||||
)
|
)
|
||||||
let seckey = PrivateKey.parse(
|
let seckey = PrivateKey.parse(
|
||||||
"41b0f5f91967dded8af487277874f95116094cc6004ac2b2169b5b6a87608f3e"
|
"41b0f5f91967dded8af487277874f95116094cc6004ac2b2169b5b6a87608f3e"
|
||||||
)
|
).get()
|
||||||
let expected = Signature.parse(
|
let expected = Signature.parse(
|
||||||
"9b966cf0065586d59c8b9eb475ac763c96ad8316b81061238f32968a631f9e21" &
|
"9b966cf0065586d59c8b9eb475ac763c96ad8316b81061238f32968a631f9e21" &
|
||||||
"251363c193c78c89b3eb2fec23f0ea5c3c72acff7d1f27430cfb84b9da9831fb" &
|
"251363c193c78c89b3eb2fec23f0ea5c3c72acff7d1f27430cfb84b9da9831fb" &
|
||||||
"1c"
|
"1c"
|
||||||
)
|
).get()
|
||||||
check seckey.sign(state) == expected
|
check seckey.sign(state) == expected
|
||||||
|
|||||||
@ -55,7 +55,7 @@ suite "state":
|
|||||||
chainId: 0x1.u256,
|
chainId: 0x1.u256,
|
||||||
nonce: 1,
|
nonce: 1,
|
||||||
participants: @[
|
participants: @[
|
||||||
EthAddress.fromHex("DBE821484648c73C1996Da25f2355342B9803eBD")
|
EthAddress.fromHex("DBE821484648c73C1996Da25f2355342B9803eBD").get()
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
outcome: Outcome(@[]),
|
outcome: Outcome(@[]),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user