nim-libp2p/tests/testpeerinfo.nim
Tanguy c7504d2446
Gossipsub peer exchange (#647)
* Signed envelopes and routing records
* Send signed peer record as part of identify (#649)
* Add SPR from identify to new peer book (#657)
* Send & receive gossipsub PX
* Add Signed Payload

Co-authored-by: Hanno Cornelius <68783915+jm-clius@users.noreply.github.com>
2022-03-14 09:39:30 +01:00

50 lines
1.4 KiB
Nim

{.used.}
import options, bearssl
import chronos, stew/byteutils
import ../libp2p/crypto/crypto,
../libp2p/multicodec,
../libp2p/peerinfo,
../libp2p/peerid,
../libp2p/routing_record
import ./helpers
suite "PeerInfo":
test "Should init with private key":
let seckey = PrivateKey.random(ECDSA, rng[]).get()
var peerInfo = PeerInfo.new(seckey)
var peerId = PeerId.init(seckey).get()
check peerId == peerInfo.peerId
check seckey.getPublicKey().get() == peerInfo.publicKey
test "Signed peer record":
const
ExpectedDomain = $multiCodec("libp2p-peer-record")
ExpectedPayloadType = @[(byte) 0x03, (byte) 0x01]
let
seckey = PrivateKey.random(rng[]).tryGet()
peerId = PeerID.init(seckey).get()
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
env = peerInfo.signedPeerRecord.get()
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]