nim-nitro/tests/nitro/testWallet.nim

78 lines
2.5 KiB
Nim
Raw Normal View History

2021-03-09 16:53:15 +01:00
import ./basics
2021-03-15 16:19:29 +01:00
suite "wallet":
test "wallet can be created from private key":
let key = PrivateKey.random()
let wallet = Wallet.init(key)
check wallet.address == key.toPublicKey.toAddress
suite "wallet: opening ledger channel":
2021-03-09 16:53:15 +01:00
let key = PrivateKey.random()
let asset = EthAddress.example
let amount = 42.u256
2021-03-15 16:19:29 +01:00
let hub = EthAddress.example
let chainId = UInt256.example
let nonce = UInt48.example
2021-03-09 16:53:15 +01:00
2021-03-15 16:19:29 +01:00
var wallet: Wallet
2021-03-17 12:53:30 +01:00
var channel: ChannelId
2021-03-09 16:53:15 +01:00
2021-03-15 16:19:29 +01:00
setup:
wallet = Wallet.init(key)
channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, amount)
test "sets correct channel definition":
2021-03-17 12:53:30 +01:00
let definition = wallet[channel].get.state.channel
2021-03-16 12:50:46 +01:00
check definition.chainId == chainId
check definition.nonce == nonce
check definition.participants == @[wallet.address, hub]
2021-03-15 16:19:29 +01:00
test "provides correct outcome":
2021-03-17 12:53:30 +01:00
let outcome = wallet[channel].get.state.outcome
2021-03-15 16:19:29 +01:00
let destination = wallet.address.toDestination
2021-03-16 12:50:46 +01:00
check outcome == Outcome.init(asset, {destination: amount})
2021-03-15 16:19:29 +01:00
2021-03-17 12:53:30 +01:00
test "signs the state":
let state = wallet[channel].get.state
let signatures = wallet[channel].get.signatures
2021-03-16 12:50:46 +01:00
check signatures == @{wallet.address: key.sign(state)}
2021-03-15 16:19:29 +01:00
test "sets app definition and app data to zero":
2021-03-17 12:53:30 +01:00
check wallet[channel].get.state.appDefinition == EthAddress.zero
check wallet[channel].get.state.appData.len == 0
2021-03-16 12:50:46 +01:00
suite "wallet: accepting incoming channel":
let key = PrivateKey.random()
var wallet: Wallet
2021-03-17 13:10:49 +01:00
var signed: SignedState
2021-03-16 12:50:46 +01:00
setup:
wallet = Wallet.init(key)
2021-03-17 13:10:49 +01:00
signed = SignedState(state: State.example)
signed.state.channel.participants &= @[wallet.address]
2021-03-16 12:50:46 +01:00
2021-03-17 12:53:30 +01:00
test "returns the new channel id":
2021-03-17 13:10:49 +01:00
let channel = wallet.acceptChannel(signed).get
check wallet[channel].get.state == signed.state
2021-03-16 12:50:46 +01:00
test "signs the channel state":
2021-03-17 13:10:49 +01:00
let channel = wallet.acceptChannel(signed).get
let expectedSignatures = @{wallet.address: key.sign(signed.state)}
2021-03-17 12:53:30 +01:00
check wallet[channel].get.signatures == expectedSignatures
2021-03-16 12:50:46 +01:00
test "fails when wallet address is not a participant":
let wrongParticipants = seq[EthAddress].example
2021-03-17 13:10:49 +01:00
signed.state.channel.participants = wrongParticipants
check wallet.acceptChannel(signed).isErr
2021-03-16 12:50:46 +01:00
test "fails when signatures are incorrect":
let otherKey = PrivateKey.random()
let otherWallet = Wallet.init(otherKey)
let wrongAddress = EthAddress.example
2021-03-17 13:10:49 +01:00
signed.state.channel.participants &= @[otherWallet.address]
signed.signatures = @{wrongAddress: otherKey.sign(signed.state)}
check wallet.acceptChannel(signed).isErr