mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-07 16:13:09 +00:00
Remove superfluous tuple from signatures
This commit is contained in:
parent
24c83838c7
commit
28c0f9b076
@ -6,22 +6,23 @@ include questionable/errorban
|
|||||||
type
|
type
|
||||||
SignedState* = object
|
SignedState* = object
|
||||||
state*: State
|
state*: State
|
||||||
signatures*: Signatures
|
signatures*: seq[Signature]
|
||||||
Signatures* = seq[(EthAddress, Signature)]
|
|
||||||
|
|
||||||
func hasParticipant*(signed: SignedState, participant: EthAddress): bool =
|
func hasParticipant*(signed: SignedState, participant: EthAddress): bool =
|
||||||
signed.state.channel.participants.contains(participant)
|
signed.state.channel.participants.contains(participant)
|
||||||
|
|
||||||
func isSignedBy*(signed: SignedState, account: EthAddress): bool =
|
func isSignedBy*(signed: SignedState, account: EthAddress): bool =
|
||||||
for (signer, signature) in signed.signatures:
|
for signature in signed.signatures:
|
||||||
if signer == account and signature.verify(signed.state, signer):
|
if signer =? signature.recover(signed.state):
|
||||||
|
if signer == account:
|
||||||
return true
|
return true
|
||||||
false
|
false
|
||||||
|
|
||||||
func verifySignatures*(signed: SignedState): bool =
|
func verifySignatures*(signed: SignedState): bool =
|
||||||
for (participant, signature) in signed.signatures:
|
for signature in signed.signatures:
|
||||||
if not signed.hasParticipant(participant):
|
if signer =? signature.recover(signed.state):
|
||||||
|
if not signed.hasParticipant(signer):
|
||||||
return false
|
return false
|
||||||
if not signature.verify(signed.state, participant):
|
else:
|
||||||
return false
|
return false
|
||||||
true
|
true
|
||||||
|
|||||||
@ -33,7 +33,7 @@ func destination*(wallet: Wallet): Destination =
|
|||||||
|
|
||||||
func sign(wallet: Wallet, state: SignedState): SignedState =
|
func sign(wallet: Wallet, state: SignedState): SignedState =
|
||||||
var signed = state
|
var signed = state
|
||||||
signed.signatures &= @{wallet.address: wallet.key.sign(state.state)}
|
signed.signatures &= wallet.key.sign(state.state)
|
||||||
signed
|
signed
|
||||||
|
|
||||||
func createChannel(wallet: var Wallet, state: SignedState): ChannelId =
|
func createChannel(wallet: var Wallet, state: SignedState): ChannelId =
|
||||||
@ -71,14 +71,15 @@ func latestSignedState*(wallet: Wallet, channel: ChannelId): ?SignedState =
|
|||||||
func state*(wallet: Wallet, channel: ChannelId): ?State =
|
func state*(wallet: Wallet, channel: ChannelId): ?State =
|
||||||
wallet.latestSignedState(channel)?.state
|
wallet.latestSignedState(channel)?.state
|
||||||
|
|
||||||
func signatures*(wallet: Wallet, channel: ChannelId): ?Signatures =
|
func signatures*(wallet: Wallet, channel: ChannelId): ?seq[Signature] =
|
||||||
wallet.latestSignedState(channel)?.signatures
|
wallet.latestSignedState(channel)?.signatures
|
||||||
|
|
||||||
func signature*(wallet: Wallet,
|
func signature*(wallet: Wallet,
|
||||||
channel: ChannelId,
|
channel: ChannelId,
|
||||||
address: EthAddress): ?Signature =
|
address: EthAddress): ?Signature =
|
||||||
if signatures =? wallet.signatures(channel):
|
if signed =? wallet.latestSignedState(channel):
|
||||||
for (signer, signature) in signatures:
|
for signature in signed.signatures:
|
||||||
|
if signer =? signature.recover(signed.state):
|
||||||
if signer == address:
|
if signer == address:
|
||||||
return signature.some
|
return signature.some
|
||||||
Signature.none
|
Signature.none
|
||||||
|
|||||||
@ -36,7 +36,7 @@ suite "wallet: opening ledger channel":
|
|||||||
test "signs the state":
|
test "signs the state":
|
||||||
let state = wallet.state(channel).get
|
let state = wallet.state(channel).get
|
||||||
let signatures = wallet.signatures(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":
|
test "sets app definition and app data to zero":
|
||||||
check wallet.state(channel).get.appDefinition == EthAddress.zero
|
check wallet.state(channel).get.appDefinition == EthAddress.zero
|
||||||
@ -59,7 +59,7 @@ suite "wallet: accepting incoming channel":
|
|||||||
|
|
||||||
test "signs the channel state":
|
test "signs the channel state":
|
||||||
let channel = wallet.acceptChannel(signed).get
|
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
|
check wallet.signatures(channel).get == expectedSignatures
|
||||||
|
|
||||||
test "fails when wallet address is not a participant":
|
test "fails when wallet address is not a participant":
|
||||||
@ -68,11 +68,7 @@ suite "wallet: accepting incoming channel":
|
|||||||
check wallet.acceptChannel(signed).isErr
|
check wallet.acceptChannel(signed).isErr
|
||||||
|
|
||||||
test "fails when signatures are incorrect":
|
test "fails when signatures are incorrect":
|
||||||
let otherKey = PrivateKey.random()
|
signed.signatures = @[key.sign(State.example)]
|
||||||
let otherWallet = Wallet.init(otherKey)
|
|
||||||
let wrongAddress = EthAddress.example
|
|
||||||
signed.state.channel.participants &= @[otherWallet.address]
|
|
||||||
signed.signatures = @{wrongAddress: otherKey.sign(signed.state)}
|
|
||||||
check wallet.acceptChannel(signed).isErr
|
check wallet.acceptChannel(signed).isErr
|
||||||
|
|
||||||
suite "wallet: making payments":
|
suite "wallet: making payments":
|
||||||
@ -183,7 +179,7 @@ suite "wallet: accepting payments":
|
|||||||
|
|
||||||
test "does not accept a payment with an incorrect signature":
|
test "does not accept a payment with an incorrect signature":
|
||||||
var payment = payer.pay(channel, asset, receiver.address, 10.u256).get
|
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
|
check receiver.acceptPayment(channel, asset, payer.address, payment).isErr
|
||||||
|
|
||||||
test "does not accept a payment for an unknown channel":
|
test "does not accept a payment for an unknown channel":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user