Update to version 0.9.1 of questionable

This commit is contained in:
Mark Spanbroek 2021-05-10 09:12:46 +02:00
parent 2a8e4e5bf4
commit adcd534eb5
4 changed files with 22 additions and 34 deletions

View File

@ -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"

View File

@ -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.}

View File

@ -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

View File

@ -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,