2020-05-06 16:31:47 +00:00
|
|
|
{.used.}
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
import options
|
2022-03-14 08:39:30 +00:00
|
|
|
import chronos, stew/byteutils
|
2019-12-07 16:36:39 +00:00
|
|
|
import ../libp2p/crypto/crypto,
|
2022-03-14 08:39:30 +00:00
|
|
|
../libp2p/multicodec,
|
2019-12-07 16:36:39 +00:00
|
|
|
../libp2p/peerinfo,
|
2022-03-14 08:39:30 +00:00
|
|
|
../libp2p/peerid,
|
|
|
|
../libp2p/routing_record
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
import ./helpers
|
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
suite "PeerInfo":
|
|
|
|
test "Should init with private key":
|
2020-07-07 11:14:11 +00:00
|
|
|
let seckey = PrivateKey.random(ECDSA, rng[]).get()
|
2021-10-25 08:26:32 +00:00
|
|
|
var peerInfo = PeerInfo.new(seckey)
|
2021-12-16 10:05:20 +00:00
|
|
|
var peerId = PeerId.init(seckey).get()
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
check peerId == peerInfo.peerId
|
2021-09-08 09:07:46 +00:00
|
|
|
check seckey.getPublicKey().get() == peerInfo.publicKey
|
2022-03-14 08:39:30 +00:00
|
|
|
|
|
|
|
test "Signed peer record":
|
|
|
|
const
|
|
|
|
ExpectedDomain = $multiCodec("libp2p-peer-record")
|
|
|
|
ExpectedPayloadType = @[(byte) 0x03, (byte) 0x01]
|
|
|
|
|
|
|
|
let
|
|
|
|
seckey = PrivateKey.random(rng[]).tryGet()
|
2022-07-27 11:47:50 +00:00
|
|
|
peerId = PeerId.init(seckey).get()
|
2022-03-14 08:39:30 +00:00
|
|
|
multiAddresses = @[MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()]
|
|
|
|
peerInfo = PeerInfo.new(seckey, multiAddresses)
|
|
|
|
|
|
|
|
let
|
2022-04-13 07:35:28 +00:00
|
|
|
env = peerInfo.signedPeerRecord.envelope
|
2022-03-14 08:39:30 +00:00
|
|
|
rec = PeerRecord.decode(env.payload()).tryGet()
|
|
|
|
|
|
|
|
# Check envelope fields
|
|
|
|
check:
|
|
|
|
env.publicKey == peerInfo.publicKey
|
|
|
|
env.domain == ExpectedDomain
|
|
|
|
env.payloadType == ExpectedPayloadType
|
|
|
|
|
|
|
|
# Check payload (routing record)
|
|
|
|
check:
|
|
|
|
rec.peerId == peerId
|
|
|
|
rec.seqNo > 0
|
|
|
|
rec.addresses.len == 2
|
|
|
|
rec.addresses[0].address == multiAddresses[0]
|
|
|
|
rec.addresses[1].address == multiAddresses[1]
|