mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-03 14:13:09 +00:00
78 lines
2.5 KiB
Nim
78 lines
2.5 KiB
Nim
import ./basics
|
|
|
|
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":
|
|
|
|
let key = PrivateKey.random()
|
|
let asset = EthAddress.example
|
|
let amount = 42.u256
|
|
let hub = EthAddress.example
|
|
let chainId = UInt256.example
|
|
let nonce = UInt48.example
|
|
|
|
var wallet: Wallet
|
|
var channel: ChannelId
|
|
|
|
setup:
|
|
wallet = Wallet.init(key)
|
|
channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, amount)
|
|
|
|
test "sets correct channel definition":
|
|
let definition = wallet[channel].get.state.channel
|
|
check definition.chainId == chainId
|
|
check definition.nonce == nonce
|
|
check definition.participants == @[wallet.address, hub]
|
|
|
|
test "provides correct outcome":
|
|
let outcome = wallet[channel].get.state.outcome
|
|
let destination = wallet.address.toDestination
|
|
check outcome == Outcome.init(asset, {destination: amount})
|
|
|
|
test "signs the state":
|
|
let state = wallet[channel].get.state
|
|
let signatures = wallet[channel].get.signatures
|
|
check signatures == @{wallet.address: key.sign(state)}
|
|
|
|
test "sets app definition and app data to zero":
|
|
check wallet[channel].get.state.appDefinition == EthAddress.zero
|
|
check wallet[channel].get.state.appData.len == 0
|
|
|
|
suite "wallet: accepting incoming channel":
|
|
|
|
let key = PrivateKey.random()
|
|
var wallet: Wallet
|
|
var update: ChannelUpdate
|
|
|
|
setup:
|
|
wallet = Wallet.init(key)
|
|
update = ChannelUpdate(state: State.example)
|
|
update.state.channel.participants &= @[wallet.address]
|
|
|
|
test "returns the new channel id":
|
|
let channel = wallet.acceptChannel(update).get
|
|
check wallet[channel].get.state == update.state
|
|
|
|
test "signs the channel state":
|
|
let channel = wallet.acceptChannel(update).get
|
|
let expectedSignatures = @{wallet.address: key.sign(update.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
|
|
|
|
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
|