mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-05 07:03:07 +00:00
Rename: ChannelUpdate -> SignedState
This commit is contained in:
parent
f498228bb9
commit
e798964ba5
@ -1,20 +0,0 @@
|
||||
import ./basics
|
||||
import ./protocol
|
||||
|
||||
include questionable/errorban
|
||||
|
||||
type
|
||||
ChannelUpdate* = object
|
||||
state*: State
|
||||
signatures*: seq[(EthAddress, Signature)]
|
||||
|
||||
func participants*(update: ChannelUpdate): seq[EthAddress] =
|
||||
update.state.channel.participants
|
||||
|
||||
func verifySignatures*(update: ChannelUpdate): bool =
|
||||
for (participant, signature) in update.signatures:
|
||||
if not update.participants.contains(participant):
|
||||
return false
|
||||
if not signature.verify(update.state, participant):
|
||||
return false
|
||||
true
|
||||
@ -1,5 +1,5 @@
|
||||
import ./basics
|
||||
import ./channelupdate
|
||||
import ./signedstate
|
||||
import ./protocol
|
||||
|
||||
func startLedger*(me: EthAddress,
|
||||
@ -7,8 +7,8 @@ func startLedger*(me: EthAddress,
|
||||
chainId: UInt256,
|
||||
nonce: UInt48,
|
||||
asset: EthAddress,
|
||||
amount: UInt256): ChannelUpdate =
|
||||
ChannelUpdate(
|
||||
amount: UInt256): SignedState =
|
||||
SignedState(
|
||||
state: State(
|
||||
channel: ChannelDefinition(
|
||||
chainId: chainId,
|
||||
|
||||
20
nitro/signedstate.nim
Normal file
20
nitro/signedstate.nim
Normal file
@ -0,0 +1,20 @@
|
||||
import ./basics
|
||||
import ./protocol
|
||||
|
||||
include questionable/errorban
|
||||
|
||||
type
|
||||
SignedState* = object
|
||||
state*: State
|
||||
signatures*: seq[(EthAddress, Signature)]
|
||||
|
||||
func participants*(signed: SignedState): seq[EthAddress] =
|
||||
signed.state.channel.participants
|
||||
|
||||
func verifySignatures*(signed: SignedState): bool =
|
||||
for (participant, signature) in signed.signatures:
|
||||
if not signed.participants.contains(participant):
|
||||
return false
|
||||
if not signature.verify(signed.state, participant):
|
||||
return false
|
||||
true
|
||||
@ -2,19 +2,19 @@ import std/tables
|
||||
import ./basics
|
||||
import ./keys
|
||||
import ./protocol
|
||||
import ./channelupdate
|
||||
import ./signedstate
|
||||
import ./ledger
|
||||
|
||||
include questionable/errorban
|
||||
|
||||
export basics
|
||||
export keys
|
||||
export channelupdate
|
||||
export signedstate
|
||||
|
||||
type
|
||||
Wallet* = object
|
||||
key: PrivateKey
|
||||
channels: Table[ChannelId, ChannelUpdate]
|
||||
channels: Table[ChannelId, SignedState]
|
||||
ChannelId* = Destination
|
||||
|
||||
func init*(_: type Wallet, key: PrivateKey): Wallet =
|
||||
@ -23,16 +23,16 @@ func init*(_: type Wallet, key: PrivateKey): Wallet =
|
||||
func address*(wallet: Wallet): EthAddress =
|
||||
wallet.key.toPublicKey.toAddress
|
||||
|
||||
func `[]`*(wallet: Wallet, channel: ChannelId): ?ChannelUpdate =
|
||||
func `[]`*(wallet: Wallet, channel: ChannelId): ?SignedState =
|
||||
wallet.channels[channel].catch.option
|
||||
|
||||
func sign(wallet: Wallet, update: ChannelUpdate): ChannelUpdate =
|
||||
var signed = update
|
||||
signed.signatures &= @{wallet.address: wallet.key.sign(update.state)}
|
||||
func sign(wallet: Wallet, state: SignedState): SignedState =
|
||||
var signed = state
|
||||
signed.signatures &= @{wallet.address: wallet.key.sign(state.state)}
|
||||
signed
|
||||
|
||||
func createChannel(wallet: var Wallet, update: ChannelUpdate): ChannelId =
|
||||
let signed = wallet.sign(update)
|
||||
func createChannel(wallet: var Wallet, state: SignedState): ChannelId =
|
||||
let signed = wallet.sign(state)
|
||||
let id = getChannelId(signed.state.channel)
|
||||
wallet.channels[id] = signed
|
||||
id
|
||||
@ -43,14 +43,14 @@ func openLedgerChannel*(wallet: var Wallet,
|
||||
nonce: UInt48,
|
||||
asset: EthAddress,
|
||||
amount: UInt256): ChannelId =
|
||||
let update = startLedger(wallet.address, hub, chainId, nonce, asset, amount)
|
||||
wallet.createChannel(update)
|
||||
let state = startLedger(wallet.address, hub, chainId, nonce, asset, amount)
|
||||
wallet.createChannel(state)
|
||||
|
||||
func acceptChannel*(wallet: var Wallet, update: ChannelUpdate): ?!ChannelId =
|
||||
if not update.participants.contains(wallet.address):
|
||||
func acceptChannel*(wallet: var Wallet, state: SignedState): ?!ChannelId =
|
||||
if not state.participants.contains(wallet.address):
|
||||
return ChannelId.failure "wallet owner is not a participant"
|
||||
|
||||
if not verifySignatures(update):
|
||||
if not verifySignatures(state):
|
||||
return ChannelId.failure "incorrect signatures"
|
||||
|
||||
wallet.createChannel(update).success
|
||||
wallet.createChannel(state).success
|
||||
|
||||
@ -47,31 +47,31 @@ suite "wallet: accepting incoming channel":
|
||||
|
||||
let key = PrivateKey.random()
|
||||
var wallet: Wallet
|
||||
var update: ChannelUpdate
|
||||
var signed: SignedState
|
||||
|
||||
setup:
|
||||
wallet = Wallet.init(key)
|
||||
update = ChannelUpdate(state: State.example)
|
||||
update.state.channel.participants &= @[wallet.address]
|
||||
signed = SignedState(state: State.example)
|
||||
signed.state.channel.participants &= @[wallet.address]
|
||||
|
||||
test "returns the new channel id":
|
||||
let channel = wallet.acceptChannel(update).get
|
||||
check wallet[channel].get.state == update.state
|
||||
let channel = wallet.acceptChannel(signed).get
|
||||
check wallet[channel].get.state == signed.state
|
||||
|
||||
test "signs the channel state":
|
||||
let channel = wallet.acceptChannel(update).get
|
||||
let expectedSignatures = @{wallet.address: key.sign(update.state)}
|
||||
let channel = wallet.acceptChannel(signed).get
|
||||
let expectedSignatures = @{wallet.address: key.sign(signed.state)}
|
||||
check wallet[channel].get.signatures == expectedSignatures
|
||||
|
||||
test "fails when wallet address is not a participant":
|
||||
let wrongParticipants = seq[EthAddress].example
|
||||
update.state.channel.participants = wrongParticipants
|
||||
check wallet.acceptChannel(update).isErr
|
||||
signed.state.channel.participants = wrongParticipants
|
||||
check wallet.acceptChannel(signed).isErr
|
||||
|
||||
test "fails when signatures are incorrect":
|
||||
let otherKey = PrivateKey.random()
|
||||
let otherWallet = Wallet.init(otherKey)
|
||||
let wrongAddress = EthAddress.example
|
||||
update.state.channel.participants &= @[otherWallet.address]
|
||||
update.signatures = @{wrongAddress: otherKey.sign(update.state)}
|
||||
check wallet.acceptChannel(update).isErr
|
||||
signed.state.channel.participants &= @[otherWallet.address]
|
||||
signed.signatures = @{wrongAddress: otherKey.sign(signed.state)}
|
||||
check wallet.acceptChannel(signed).isErr
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user