nim-nitro/tests/nitro/testChannel.nim
Mark Spanbroek 34072f4749 Fix ABI encoding of Nitro state to match javascript implementation
- rewrite ABI encoding to properly handle dynamic tuples
- surround Nitro types with extra tuples to match javascript
- test with examples of encoding extracted from javascript
2021-02-25 09:11:46 +01:00

35 lines
935 B
Nim

import std/unittest
import pkg/nitro/channel
import pkg/nitro/abi
import pkg/nimcrypto
import pkg/stew/byteutils
import ./examples
suite "channel":
let channel = Channel.example
test "calculates channel id":
var encoder= AbiEncoder.init()
encoder.startTuple()
encoder.write(channel.chainId)
encoder.write(channel.participants)
encoder.write(channel.nonce)
encoder.finishTuple()
let encoded = encoder.finish()
let hashed = keccak256.digest(encoded).data
check getChannelId(channel) == hashed
test "produces same id as javascript implementation":
let channel = Channel(
chainId: 9001.u256,
nonce: 1,
participants: @[
EthAddress.fromHex("24b905Dcc8A11C0FE57C2592f3D25f0447402C10")
]
)
let expected = array[32, byte].fromHex(
"4f8cce57e9fe88edaab05234972eaf0c2d183e4f6b175aff293375fbe4d5d7cc"
)
check getChannelId(channel) == expected