diff --git a/nitro/wallet/signedstate.nim b/nitro/wallet/signedstate.nim index 265f08a..3b1d9f4 100644 --- a/nitro/wallet/signedstate.nim +++ b/nitro/wallet/signedstate.nim @@ -6,22 +6,23 @@ include questionable/errorban type SignedState* = object state*: State - signatures*: Signatures - Signatures* = seq[(EthAddress, Signature)] + signatures*: seq[Signature] func hasParticipant*(signed: SignedState, participant: EthAddress): bool = signed.state.channel.participants.contains(participant) func isSignedBy*(signed: SignedState, account: EthAddress): bool = - for (signer, signature) in signed.signatures: - if signer == account and signature.verify(signed.state, signer): - return true + for signature in signed.signatures: + if signer =? signature.recover(signed.state): + if signer == account: + return true false func verifySignatures*(signed: SignedState): bool = - for (participant, signature) in signed.signatures: - if not signed.hasParticipant(participant): - return false - if not signature.verify(signed.state, participant): + for signature in signed.signatures: + if signer =? signature.recover(signed.state): + if not signed.hasParticipant(signer): + return false + else: return false true diff --git a/nitro/wallet/wallet.nim b/nitro/wallet/wallet.nim index 7f51dc9..ca84f1c 100644 --- a/nitro/wallet/wallet.nim +++ b/nitro/wallet/wallet.nim @@ -33,7 +33,7 @@ func destination*(wallet: Wallet): Destination = func sign(wallet: Wallet, state: SignedState): SignedState = var signed = state - signed.signatures &= @{wallet.address: wallet.key.sign(state.state)} + signed.signatures &= wallet.key.sign(state.state) signed func createChannel(wallet: var Wallet, state: SignedState): ChannelId = @@ -71,16 +71,17 @@ func latestSignedState*(wallet: Wallet, channel: ChannelId): ?SignedState = func state*(wallet: Wallet, channel: ChannelId): ?State = wallet.latestSignedState(channel)?.state -func signatures*(wallet: Wallet, channel: ChannelId): ?Signatures = +func signatures*(wallet: Wallet, channel: ChannelId): ?seq[Signature] = wallet.latestSignedState(channel)?.signatures func signature*(wallet: Wallet, channel: ChannelId, address: EthAddress): ?Signature = - if signatures =? wallet.signatures(channel): - for (signer, signature) in signatures: - if signer == address: - return signature.some + if signed =? wallet.latestSignedState(channel): + for signature in signed.signatures: + if signer =? signature.recover(signed.state): + if signer == address: + return signature.some Signature.none func balance(state: State, diff --git a/tests/nitro/testWallet.nim b/tests/nitro/testWallet.nim index d74532e..64852fd 100644 --- a/tests/nitro/testWallet.nim +++ b/tests/nitro/testWallet.nim @@ -36,7 +36,7 @@ suite "wallet: opening ledger channel": test "signs the state": let state = wallet.state(channel).get let signatures = wallet.signatures(channel).get - check signatures == @{wallet.address: key.sign(state)} + check signatures == @[key.sign(state)] test "sets app definition and app data to zero": check wallet.state(channel).get.appDefinition == EthAddress.zero @@ -59,7 +59,7 @@ suite "wallet: accepting incoming channel": test "signs the channel state": let channel = wallet.acceptChannel(signed).get - let expectedSignatures = @{wallet.address: key.sign(signed.state)} + let expectedSignatures = @[key.sign(signed.state)] check wallet.signatures(channel).get == expectedSignatures test "fails when wallet address is not a participant": @@ -68,11 +68,7 @@ suite "wallet: accepting incoming channel": check wallet.acceptChannel(signed).isErr test "fails when signatures are incorrect": - let otherKey = PrivateKey.random() - let otherWallet = Wallet.init(otherKey) - let wrongAddress = EthAddress.example - signed.state.channel.participants &= @[otherWallet.address] - signed.signatures = @{wrongAddress: otherKey.sign(signed.state)} + signed.signatures = @[key.sign(State.example)] check wallet.acceptChannel(signed).isErr suite "wallet: making payments": @@ -183,7 +179,7 @@ suite "wallet: accepting payments": test "does not accept a payment with an incorrect signature": var payment = payer.pay(channel, asset, receiver.address, 10.u256).get - payment.signatures = @{payer.address: Signature.example} + payment.signatures = @[Signature.example] check receiver.acceptPayment(channel, asset, payer.address, payment).isErr test "does not accept a payment for an unknown channel":