From e798964ba57d936a8ddb8387ca84e5b064580496 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 17 Mar 2021 13:10:49 +0100 Subject: [PATCH] Rename: ChannelUpdate -> SignedState --- nitro/channelupdate.nim | 20 -------------------- nitro/ledger.nim | 6 +++--- nitro/signedstate.nim | 20 ++++++++++++++++++++ nitro/wallet.nim | 30 +++++++++++++++--------------- tests/nitro/testWallet.nim | 24 ++++++++++++------------ 5 files changed, 50 insertions(+), 50 deletions(-) delete mode 100644 nitro/channelupdate.nim create mode 100644 nitro/signedstate.nim diff --git a/nitro/channelupdate.nim b/nitro/channelupdate.nim deleted file mode 100644 index 539421a..0000000 --- a/nitro/channelupdate.nim +++ /dev/null @@ -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 diff --git a/nitro/ledger.nim b/nitro/ledger.nim index a7b053b..42fa11a 100644 --- a/nitro/ledger.nim +++ b/nitro/ledger.nim @@ -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, diff --git a/nitro/signedstate.nim b/nitro/signedstate.nim new file mode 100644 index 0000000..0c0db0a --- /dev/null +++ b/nitro/signedstate.nim @@ -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 diff --git a/nitro/wallet.nim b/nitro/wallet.nim index a2a91e3..6ccd9be 100644 --- a/nitro/wallet.nim +++ b/nitro/wallet.nim @@ -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 diff --git a/tests/nitro/testWallet.nim b/tests/nitro/testWallet.nim index 759ed9c..721ccaa 100644 --- a/tests/nitro/testWallet.nim +++ b/tests/nitro/testWallet.nim @@ -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