nim-dagger/dagger/bitswap/protobuf/payments.nim

52 lines
1.4 KiB
Nim
Raw Normal View History

2021-04-07 10:19:00 +00:00
import pkg/protobuf_serialization
2021-04-08 08:09:59 +00:00
import pkg/stew/byteutils
import pkg/stint
2021-04-07 10:19:00 +00:00
import pkg/nitro
import pkg/questionable
import pkg/upraises
import ./bitswap
2021-04-07 10:19:00 +00:00
export PricingMessage
export StateChannelUpdate
2021-04-07 10:19:00 +00:00
export stint
2021-04-07 10:19:00 +00:00
export nitro
push: {.upraises: [].}
2021-04-07 10:19:00 +00:00
type
Pricing* = object
address*: EthAddress
price*: UInt256
func init*(_: type PricingMessage, pricing: Pricing): PricingMessage =
PricingMessage(
address: @(pricing.address.toArray),
price: @(pricing.price.toBytesBE)
)
func parse(_: type EthAddress, bytes: seq[byte]): ?EthAddress =
var address: array[20, byte]
if bytes.len != address.len:
return EthAddress.none
for i in 0..<address.len:
address[i] = bytes[i]
EthAddress(address).some
func parse(_: type UInt256, bytes: seq[byte]): ?UInt256 =
if bytes.len > 32:
return UInt256.none
UInt256.fromBytesBE(bytes).some
func init*(_: type Pricing, message: PricingMessage): ?Pricing =
without address =? EthAddress.parse(message.address) and
price =? UInt256.parse(message.price):
2021-04-07 10:19:00 +00:00
return Pricing.none
Pricing(address: address, price: price).some
func init*(_: type StateChannelUpdate, state: SignedState): StateChannelUpdate =
StateChannelUpdate(update: state.toJson.toBytes)
proc init*(_: type SignedState, update: StateChannelUpdate): ?SignedState =
SignedState.fromJson(string.fromBytes(update.update))