mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-07 16:13:09 +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 ./basics
|
||||||
import ./channelupdate
|
import ./signedstate
|
||||||
import ./protocol
|
import ./protocol
|
||||||
|
|
||||||
func startLedger*(me: EthAddress,
|
func startLedger*(me: EthAddress,
|
||||||
@ -7,8 +7,8 @@ func startLedger*(me: EthAddress,
|
|||||||
chainId: UInt256,
|
chainId: UInt256,
|
||||||
nonce: UInt48,
|
nonce: UInt48,
|
||||||
asset: EthAddress,
|
asset: EthAddress,
|
||||||
amount: UInt256): ChannelUpdate =
|
amount: UInt256): SignedState =
|
||||||
ChannelUpdate(
|
SignedState(
|
||||||
state: State(
|
state: State(
|
||||||
channel: ChannelDefinition(
|
channel: ChannelDefinition(
|
||||||
chainId: chainId,
|
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 ./basics
|
||||||
import ./keys
|
import ./keys
|
||||||
import ./protocol
|
import ./protocol
|
||||||
import ./channelupdate
|
import ./signedstate
|
||||||
import ./ledger
|
import ./ledger
|
||||||
|
|
||||||
include questionable/errorban
|
include questionable/errorban
|
||||||
|
|
||||||
export basics
|
export basics
|
||||||
export keys
|
export keys
|
||||||
export channelupdate
|
export signedstate
|
||||||
|
|
||||||
type
|
type
|
||||||
Wallet* = object
|
Wallet* = object
|
||||||
key: PrivateKey
|
key: PrivateKey
|
||||||
channels: Table[ChannelId, ChannelUpdate]
|
channels: Table[ChannelId, SignedState]
|
||||||
ChannelId* = Destination
|
ChannelId* = Destination
|
||||||
|
|
||||||
func init*(_: type Wallet, key: PrivateKey): Wallet =
|
func init*(_: type Wallet, key: PrivateKey): Wallet =
|
||||||
@ -23,16 +23,16 @@ func init*(_: type Wallet, key: PrivateKey): Wallet =
|
|||||||
func address*(wallet: Wallet): EthAddress =
|
func address*(wallet: Wallet): EthAddress =
|
||||||
wallet.key.toPublicKey.toAddress
|
wallet.key.toPublicKey.toAddress
|
||||||
|
|
||||||
func `[]`*(wallet: Wallet, channel: ChannelId): ?ChannelUpdate =
|
func `[]`*(wallet: Wallet, channel: ChannelId): ?SignedState =
|
||||||
wallet.channels[channel].catch.option
|
wallet.channels[channel].catch.option
|
||||||
|
|
||||||
func sign(wallet: Wallet, update: ChannelUpdate): ChannelUpdate =
|
func sign(wallet: Wallet, state: SignedState): SignedState =
|
||||||
var signed = update
|
var signed = state
|
||||||
signed.signatures &= @{wallet.address: wallet.key.sign(update.state)}
|
signed.signatures &= @{wallet.address: wallet.key.sign(state.state)}
|
||||||
signed
|
signed
|
||||||
|
|
||||||
func createChannel(wallet: var Wallet, update: ChannelUpdate): ChannelId =
|
func createChannel(wallet: var Wallet, state: SignedState): ChannelId =
|
||||||
let signed = wallet.sign(update)
|
let signed = wallet.sign(state)
|
||||||
let id = getChannelId(signed.state.channel)
|
let id = getChannelId(signed.state.channel)
|
||||||
wallet.channels[id] = signed
|
wallet.channels[id] = signed
|
||||||
id
|
id
|
||||||
@ -43,14 +43,14 @@ func openLedgerChannel*(wallet: var Wallet,
|
|||||||
nonce: UInt48,
|
nonce: UInt48,
|
||||||
asset: EthAddress,
|
asset: EthAddress,
|
||||||
amount: UInt256): ChannelId =
|
amount: UInt256): ChannelId =
|
||||||
let update = startLedger(wallet.address, hub, chainId, nonce, asset, amount)
|
let state = startLedger(wallet.address, hub, chainId, nonce, asset, amount)
|
||||||
wallet.createChannel(update)
|
wallet.createChannel(state)
|
||||||
|
|
||||||
func acceptChannel*(wallet: var Wallet, update: ChannelUpdate): ?!ChannelId =
|
func acceptChannel*(wallet: var Wallet, state: SignedState): ?!ChannelId =
|
||||||
if not update.participants.contains(wallet.address):
|
if not state.participants.contains(wallet.address):
|
||||||
return ChannelId.failure "wallet owner is not a participant"
|
return ChannelId.failure "wallet owner is not a participant"
|
||||||
|
|
||||||
if not verifySignatures(update):
|
if not verifySignatures(state):
|
||||||
return ChannelId.failure "incorrect signatures"
|
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()
|
let key = PrivateKey.random()
|
||||||
var wallet: Wallet
|
var wallet: Wallet
|
||||||
var update: ChannelUpdate
|
var signed: SignedState
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
wallet = Wallet.init(key)
|
wallet = Wallet.init(key)
|
||||||
update = ChannelUpdate(state: State.example)
|
signed = SignedState(state: State.example)
|
||||||
update.state.channel.participants &= @[wallet.address]
|
signed.state.channel.participants &= @[wallet.address]
|
||||||
|
|
||||||
test "returns the new channel id":
|
test "returns the new channel id":
|
||||||
let channel = wallet.acceptChannel(update).get
|
let channel = wallet.acceptChannel(signed).get
|
||||||
check wallet[channel].get.state == update.state
|
check wallet[channel].get.state == signed.state
|
||||||
|
|
||||||
test "signs the channel state":
|
test "signs the channel state":
|
||||||
let channel = wallet.acceptChannel(update).get
|
let channel = wallet.acceptChannel(signed).get
|
||||||
let expectedSignatures = @{wallet.address: key.sign(update.state)}
|
let expectedSignatures = @{wallet.address: key.sign(signed.state)}
|
||||||
check wallet[channel].get.signatures == expectedSignatures
|
check wallet[channel].get.signatures == expectedSignatures
|
||||||
|
|
||||||
test "fails when wallet address is not a participant":
|
test "fails when wallet address is not a participant":
|
||||||
let wrongParticipants = seq[EthAddress].example
|
let wrongParticipants = seq[EthAddress].example
|
||||||
update.state.channel.participants = wrongParticipants
|
signed.state.channel.participants = wrongParticipants
|
||||||
check wallet.acceptChannel(update).isErr
|
check wallet.acceptChannel(signed).isErr
|
||||||
|
|
||||||
test "fails when signatures are incorrect":
|
test "fails when signatures are incorrect":
|
||||||
let otherKey = PrivateKey.random()
|
let otherKey = PrivateKey.random()
|
||||||
let otherWallet = Wallet.init(otherKey)
|
let otherWallet = Wallet.init(otherKey)
|
||||||
let wrongAddress = EthAddress.example
|
let wrongAddress = EthAddress.example
|
||||||
update.state.channel.participants &= @[otherWallet.address]
|
signed.state.channel.participants &= @[otherWallet.address]
|
||||||
update.signatures = @{wrongAddress: otherKey.sign(update.state)}
|
signed.signatures = @{wrongAddress: otherKey.sign(signed.state)}
|
||||||
check wallet.acceptChannel(update).isErr
|
check wallet.acceptChannel(signed).isErr
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user