From adcd534eb57be1c2535c2b34c62c8dd45e9e8252 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 10 May 2021 09:12:46 +0200 Subject: [PATCH] Update to version 0.9.1 of questionable --- nitro.nimble | 2 +- nitro/json.nim | 21 ++++++++------------- nitro/protocol/signature.nim | 9 ++++----- nitro/wallet/wallet.nim | 24 +++++++++--------------- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/nitro.nimble b/nitro.nimble index 1ebd15a..6ec45e9 100644 --- a/nitro.nimble +++ b/nitro.nimble @@ -5,7 +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.7.0 & < 0.8.0" +requires "questionable >= 0.9.1 & < 0.10.0" requires "upraises >= 0.1.0 & < 0.2.0" requires "secp256k1" requires "stint" diff --git a/nitro/json.nim b/nitro/json.nim index c6b46e8..87330dd 100644 --- a/nitro/json.nim +++ b/nitro/json.nim @@ -46,25 +46,21 @@ func expectKind(node: JsonNode, kind: JsonNodeKind) = func initFromJson*(bytes: var seq[byte], node: JsonNode, _: var string) = node.expectKind(JString) - let parsed = seq[byte].fromHex(node.getStr) - if parsed.isOk: - bytes = parsed.get - else: + without parsed =? seq[byte].fromHex(node.getStr): raise newException(ValueError, "invalid hex string") + bytes = parsed func initFromJson*(address: var EthAddress, node: JsonNode, _: var string) = node.expectKind(JString) - if parsed =? EthAddress.parse(node.getStr): - address = parsed - else: + without parsed =? EthAddress.parse(node.getStr): raise newException(ValueError, "invalid ethereum address") + address = parsed func initFromJson*(dest: var Destination, node: JsonNode, _: var string) = node.expectKind(JString) - if parsed =? Destination.parse(node.getStr): - dest = parsed - else: + without parsed =? Destination.parse(node.getStr): raise newException(ValueError, "invalid nitro destination") + dest = parsed func initFromJson*(number: var UInt256, node: JsonNode, _: var string) = node.expectKind(JString) @@ -72,10 +68,9 @@ func initFromJson*(number: var UInt256, node: JsonNode, _: var string) = func initFromJson*(signature: var Signature, node: JsonNode, _: var string) = node.expectKind(JString) - if parsed =? Signature.parse(node.getStr): - signature = parsed - else: + without parsed =? Signature.parse(node.getStr): raise newException(ValueError, "invalid signature") + signature = parsed {.pop.} diff --git a/nitro/protocol/signature.nim b/nitro/protocol/signature.nim index 843502a..5abb2f5 100644 --- a/nitro/protocol/signature.nim +++ b/nitro/protocol/signature.nim @@ -43,8 +43,7 @@ func `$`*(signature: Signature): string = bytes.toHex() func parse*(_: type Signature, s: string): ?Signature = - let signature = catch: - var bytes = array[65, byte].fromHex(s) - bytes[64] = bytes[64] - 27 - SkRecoverableSignature.fromRaw(bytes).get() - signature.option + without var bytes =? array[65, byte].fromHex(s).catch: + return Signature.none + bytes[64] = bytes[64] - 27 + SkRecoverableSignature.fromRaw(bytes).option diff --git a/nitro/wallet/wallet.nim b/nitro/wallet/wallet.nim index e173358..781b94a 100644 --- a/nitro/wallet/wallet.nim +++ b/nitro/wallet/wallet.nim @@ -115,22 +115,17 @@ func signature*(wallet: Wallet, func balance(state: State, asset: EthAddress, destination: Destination): UInt256 = - if balances =? state.outcome.balances(asset): - if balance =? balances.?[destination]: - balance - else: - 0.u256 - else: - 0.u256 + without balance =? state.outcome.balances(asset).?[destination]: + return 0.u256 + balance func balance*(wallet: Wallet, channel: ChannelId, asset: EthAddress, destination: Destination): UInt256 {.deref.} = - if state =? wallet.state(channel): - state.balance(asset, destination) - else: - 0.u256 + without state =? wallet.state(channel): + return 0.u256 + state.balance(asset, destination) func balance*(wallet: Wallet, channel: ChannelId, @@ -151,10 +146,9 @@ func total(state: State, asset: EthAddress): UInt256 = total func total(wallet: Wallet, channel: ChannelId, asset: EthAddress): UInt256 = - if state =? wallet.state(channel): - state.total(asset) - else: - 0.u256 + without state =? wallet.state(channel): + return 0.u256 + state.total(asset) func pay*(wallet: var Wallet, channel: ChannelId,