2023-03-10 00:33:52 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import std/options, stew/results, stew/shims/net, testutils/unittests
|
|
|
|
import ../../waku/common/enr, ../testlib/wakucore
|
2023-03-10 00:33:52 +00:00
|
|
|
|
2023-03-21 16:27:51 +00:00
|
|
|
suite "nim-eth ENR - builder and typed record":
|
2023-03-10 00:33:52 +00:00
|
|
|
test "Non-supported private key (ECDSA)":
|
|
|
|
## Given
|
|
|
|
let privateKey = generateEcdsaKey()
|
|
|
|
|
|
|
|
## Then
|
|
|
|
expect Defect:
|
|
|
|
discard EnrBuilder.init(privateKey)
|
|
|
|
|
|
|
|
test "Supported private key (Secp256k1)":
|
|
|
|
let
|
|
|
|
seqNum = 1u64
|
|
|
|
privateKey = generateSecp256k1Key()
|
|
|
|
|
|
|
|
let expectedPubKey = privateKey.getPublicKey().get().getRawBytes().get()
|
|
|
|
|
|
|
|
## When
|
|
|
|
var builder = EnrBuilder.init(privateKey, seqNum)
|
|
|
|
let enrRes = builder.build()
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check enrRes.isOk()
|
|
|
|
|
2023-03-21 16:27:51 +00:00
|
|
|
let record = enrRes.tryGet().toTyped().get()
|
|
|
|
|
|
|
|
let id = record.id
|
2023-03-10 00:33:52 +00:00
|
|
|
check:
|
2023-03-21 16:27:51 +00:00
|
|
|
id == some(RecordId.V4)
|
|
|
|
|
|
|
|
let publicKey = record.secp256k1
|
|
|
|
check:
|
|
|
|
publicKey.isSome()
|
|
|
|
@(publicKey.get()) == expectedPubKey
|
2023-03-10 00:33:52 +00:00
|
|
|
|
2023-03-21 16:27:51 +00:00
|
|
|
suite "nim-eth ENR - Ext: IP address and TCP/UDP ports":
|
2023-03-10 00:33:52 +00:00
|
|
|
test "EIP-778 test vector":
|
|
|
|
## Given
|
|
|
|
# Test vector from EIP-778
|
|
|
|
# See: https://eips.ethereum.org/EIPS/eip-778#test-vectors
|
2024-03-15 23:08:47 +00:00
|
|
|
let expectedEnr =
|
|
|
|
"-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04j" &
|
|
|
|
"RzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJ" &
|
|
|
|
"c2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0x" & "OIN1ZHCCdl8"
|
2023-03-10 00:33:52 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
seqNum = 1u64
|
2024-03-15 23:08:47 +00:00
|
|
|
privateKey = ethSecp256k1Key(
|
|
|
|
"b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
|
|
|
|
)
|
2023-03-10 00:33:52 +00:00
|
|
|
|
2023-12-14 06:16:39 +00:00
|
|
|
enrIpAddr = parseIpAddress("127.0.0.1")
|
2023-03-10 00:33:52 +00:00
|
|
|
enrUdpPort = Port(30303)
|
|
|
|
|
|
|
|
## When
|
|
|
|
var builder = EnrBuilder.init(privateKey, seqNum)
|
2024-03-15 23:08:47 +00:00
|
|
|
builder.withIpAddressAndPorts(ipAddr = some(enrIpAddr), udpPort = some(enrUdpPort))
|
2023-03-10 00:33:52 +00:00
|
|
|
|
|
|
|
let enrRes = builder.build()
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check enrRes.isOk()
|
|
|
|
|
|
|
|
let record = enrRes.tryGet().toBase64()
|
|
|
|
check:
|
|
|
|
record == expectedEnr
|
|
|
|
|
|
|
|
test "IPv4 and TCP port":
|
|
|
|
let
|
|
|
|
seqNum = 1u64
|
|
|
|
privateKey = generateSecp256k1Key()
|
|
|
|
|
2023-12-14 06:16:39 +00:00
|
|
|
enrIpAddr = parseIpAddress("127.0.0.1")
|
2023-03-10 00:33:52 +00:00
|
|
|
enrTcpPort = Port(30301)
|
|
|
|
|
|
|
|
let expectedPubKey = privateKey.getPublicKey().get().getRawBytes().get()
|
|
|
|
|
|
|
|
## When
|
|
|
|
var builder = EnrBuilder.init(privateKey, seqNum)
|
2024-03-15 23:08:47 +00:00
|
|
|
builder.withIpAddressAndPorts(ipAddr = some(enrIpAddr), tcpPort = some(enrTcpPort))
|
2023-03-10 00:33:52 +00:00
|
|
|
|
|
|
|
let enrRes = builder.build()
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check enrRes.isOk()
|
|
|
|
|
2023-03-21 16:27:51 +00:00
|
|
|
let record = enrRes.tryGet().toTyped().get()
|
2023-03-10 00:33:52 +00:00
|
|
|
check:
|
|
|
|
@(record.secp256k1.get()) == expectedPubKey
|
|
|
|
record.ip == some(enrIpAddr.address_v4)
|
2023-03-21 16:27:51 +00:00
|
|
|
record.tcp == some(enrTcpPort.uint16)
|
|
|
|
record.udp == none(uint16)
|
|
|
|
record.ip6 == none(array[16, byte])
|
2023-03-10 00:33:52 +00:00
|
|
|
|
|
|
|
test "IPv6 and UDP port":
|
|
|
|
let
|
|
|
|
seqNum = 1u64
|
|
|
|
privateKey = generateSecp256k1Key()
|
|
|
|
|
2023-12-14 06:16:39 +00:00
|
|
|
enrIpAddr = parseIpAddress("::1")
|
2023-03-10 00:33:52 +00:00
|
|
|
enrUdpPort = Port(30301)
|
|
|
|
|
|
|
|
let expectedPubKey = privateKey.getPublicKey().get().getRawBytes().get()
|
|
|
|
|
|
|
|
## When
|
|
|
|
var builder = EnrBuilder.init(privateKey, seqNum)
|
2024-03-15 23:08:47 +00:00
|
|
|
builder.withIpAddressAndPorts(ipAddr = some(enrIpAddr), udpPort = some(enrUdpPort))
|
2023-03-10 00:33:52 +00:00
|
|
|
|
|
|
|
let enrRes = builder.build()
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check enrRes.isOk()
|
|
|
|
|
2023-03-21 16:27:51 +00:00
|
|
|
let record = enrRes.tryGet().toTyped().get()
|
2023-03-10 00:33:52 +00:00
|
|
|
check:
|
|
|
|
@(record.secp256k1.get()) == expectedPubKey
|
2023-03-21 16:27:51 +00:00
|
|
|
record.ip == none(array[4, byte])
|
|
|
|
record.tcp == none(uint16)
|
|
|
|
record.udp == none(uint16)
|
2023-03-10 00:33:52 +00:00
|
|
|
record.ip6 == some(enrIpAddr.address_v6)
|
2023-03-21 16:27:51 +00:00
|
|
|
record.tcp6 == none(uint16)
|
|
|
|
record.udp6 == some(enrUdpPort.uint16)
|