protobuf message for state channel updates
This commit is contained in:
parent
f60df42c88
commit
c819ba1be1
|
@ -6,6 +6,7 @@ import pkg/upraises
|
||||||
import_proto3 "payments.proto"
|
import_proto3 "payments.proto"
|
||||||
|
|
||||||
export PricingMessage
|
export PricingMessage
|
||||||
|
export StateChannelUpdate
|
||||||
|
|
||||||
export nitro
|
export nitro
|
||||||
|
|
||||||
|
@ -44,3 +45,9 @@ func init*(_: type Pricing, message: PricingMessage): ?Pricing =
|
||||||
if address.isNone or asset.isNone or price.isNone:
|
if address.isNone or asset.isNone or price.isNone:
|
||||||
return Pricing.none
|
return Pricing.none
|
||||||
Pricing(address: address.get, asset: asset.get, price: price.get).some
|
Pricing(address: address.get, asset: asset.get, price: price.get).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))
|
||||||
|
|
|
@ -7,3 +7,7 @@ message PricingMessage {
|
||||||
bytes asset = 2; // Asset (coin) with which to pay
|
bytes asset = 2; // Asset (coin) with which to pay
|
||||||
bytes price = 3; // Amount of assets to pay per byte (UInt256)
|
bytes price = 3; // Amount of assets to pay per byte (UInt256)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message StateChannelUpdate {
|
||||||
|
bytes update = 1; // Signed Nitro state, serialized as JSON
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import pkg/asynctest
|
import pkg/asynctest
|
||||||
import pkg/chronos
|
import pkg/chronos
|
||||||
|
import pkg/stew/byteutils
|
||||||
import ../../examples
|
import ../../examples
|
||||||
import ../../../../dagger/bitswap/protobuf/payments
|
import ../../../../dagger/bitswap/protobuf/payments
|
||||||
|
|
||||||
|
@ -43,3 +44,19 @@ suite "pricing protobuf messages":
|
||||||
var incorrect = message
|
var incorrect = message
|
||||||
incorrect.price = newSeq[byte](33)
|
incorrect.price = newSeq[byte](33)
|
||||||
check Pricing.init(incorrect).isNone
|
check Pricing.init(incorrect).isNone
|
||||||
|
|
||||||
|
suite "channel update messages":
|
||||||
|
|
||||||
|
let state = SignedState.example
|
||||||
|
let update = StateChannelUpdate.init(state)
|
||||||
|
|
||||||
|
test "encodes a nitro signed state":
|
||||||
|
check update.update == state.toJson.toBytes
|
||||||
|
|
||||||
|
test "decodes a channel update":
|
||||||
|
check SignedState.init(update) == state.some
|
||||||
|
|
||||||
|
test "fails to decode incorrect channel update":
|
||||||
|
var incorrect = update
|
||||||
|
incorrect.update.del(0)
|
||||||
|
check SignedState.init(incorrect).isNone
|
||||||
|
|
|
@ -9,3 +9,11 @@ proc example*(_: type UInt256): UInt256 =
|
||||||
for b in bytes.mitems:
|
for b in bytes.mitems:
|
||||||
b = rand(byte)
|
b = rand(byte)
|
||||||
UInt256.fromBytes(bytes)
|
UInt256.fromBytes(bytes)
|
||||||
|
|
||||||
|
proc example*(_: type SignedState): SignedState =
|
||||||
|
var wallet = Wallet.init(EthPrivateKey.random())
|
||||||
|
let hub, asset, receiver = EthAddress.example
|
||||||
|
let chainId, amount = UInt256.example
|
||||||
|
let nonce = UInt48.rand()
|
||||||
|
let channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, amount).get
|
||||||
|
wallet.pay(channel, asset, receiver, amount).get
|
||||||
|
|
Loading…
Reference in New Issue