mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-01-23 11:20:02 +00:00
make peerId an Option[T]
This commit is contained in:
parent
177eb71ffa
commit
41aad2e7e6
@ -7,9 +7,10 @@
|
|||||||
## This file may not be copied, modified, or distributed except according to
|
## This file may not be copied, modified, or distributed except according to
|
||||||
## those terms.
|
## those terms.
|
||||||
|
|
||||||
|
import options
|
||||||
import peer, multiaddress
|
import peer, multiaddress
|
||||||
|
|
||||||
type PeerInfo* = object of RootObj
|
type PeerInfo* = object of RootObj
|
||||||
peerId*: PeerID
|
peerId*: Option[PeerID]
|
||||||
addrs*: seq[MultiAddress]
|
addrs*: seq[MultiAddress]
|
||||||
protocols*: seq[string]
|
protocols*: seq[string]
|
||||||
|
@ -32,11 +32,11 @@ type
|
|||||||
IdentityInvalidMsgError* = object of CatchableError
|
IdentityInvalidMsgError* = object of CatchableError
|
||||||
|
|
||||||
IdentifyInfo* = object
|
IdentifyInfo* = object
|
||||||
pubKey*: PublicKey
|
pubKey*: Option[PublicKey]
|
||||||
addrs*: seq[MultiAddress]
|
addrs*: seq[MultiAddress]
|
||||||
observedAddr*: MultiAddress
|
observedAddr*: Option[MultiAddress]
|
||||||
protoVersion*: string
|
protoVersion*: Option[string]
|
||||||
agentVersion*: string
|
agentVersion*: Option[string]
|
||||||
protos*: seq[string]
|
protos*: seq[string]
|
||||||
|
|
||||||
Identify* = ref object of LPProtocol
|
Identify* = ref object of LPProtocol
|
||||||
@ -45,7 +45,9 @@ type
|
|||||||
proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer =
|
proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer =
|
||||||
result = initProtoBuffer()
|
result = initProtoBuffer()
|
||||||
|
|
||||||
result.write(initProtoField(1, peerInfo.peerId.publicKey.getBytes()))
|
if peerInfo.peerId.isSome:
|
||||||
|
result.write(initProtoField(1, peerInfo.peerId.get().publicKey.getBytes()))
|
||||||
|
|
||||||
for ma in peerInfo.addrs:
|
for ma in peerInfo.addrs:
|
||||||
result.write(initProtoField(2, ma.data.buffer))
|
result.write(initProtoField(2, ma.data.buffer))
|
||||||
|
|
||||||
@ -64,7 +66,10 @@ proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer =
|
|||||||
proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
|
proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
|
||||||
var pb = initProtoBuffer(buf)
|
var pb = initProtoBuffer(buf)
|
||||||
|
|
||||||
discard pb.getValue(1, result.pubKey)
|
result.pubKey = none(PublicKey)
|
||||||
|
var pubKey: PublicKey
|
||||||
|
if pb.getValue(1, pubKey) > 0:
|
||||||
|
result.pubKey = some(pubKey)
|
||||||
|
|
||||||
result.addrs = newSeq[MultiAddress]()
|
result.addrs = newSeq[MultiAddress]()
|
||||||
var address = newSeq[byte]()
|
var address = newSeq[byte]()
|
||||||
@ -81,10 +86,15 @@ proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
|
|||||||
|
|
||||||
var observableAddr = newSeq[byte]()
|
var observableAddr = newSeq[byte]()
|
||||||
if pb.getBytes(4, observableAddr) > 0: # attempt to read the observed addr
|
if pb.getBytes(4, observableAddr) > 0: # attempt to read the observed addr
|
||||||
result.observedAddr = MultiAddress.init(observableAddr)
|
result.observedAddr = some(MultiAddress.init(observableAddr))
|
||||||
|
|
||||||
discard pb.getString(5, result.protoVersion)
|
var protoVersion = ""
|
||||||
discard pb.getString(6, result.agentVersion)
|
if pb.getString(5, protoVersion) > 0:
|
||||||
|
result.protoVersion = some(protoVersion)
|
||||||
|
|
||||||
|
var agentVersion = ""
|
||||||
|
if pb.getString(6, agentVersion) > 0:
|
||||||
|
result.agentVersion = some(protoVersion)
|
||||||
|
|
||||||
proc newIdentify*(peerInfo: PeerInfo): Identify =
|
proc newIdentify*(peerInfo: PeerInfo): Identify =
|
||||||
new result
|
new result
|
||||||
@ -101,7 +111,7 @@ method init*(p: Identify) =
|
|||||||
|
|
||||||
proc identify*(p: Identify,
|
proc identify*(p: Identify,
|
||||||
conn: Connection,
|
conn: Connection,
|
||||||
remotePeerInfo: Option[PeerInfo] = none(PeerInfo)):
|
remotePeerInfo: PeerInfo):
|
||||||
Future[IdentifyInfo] {.async.} =
|
Future[IdentifyInfo] {.async.} =
|
||||||
var message = await conn.readLp()
|
var message = await conn.readLp()
|
||||||
if len(message) == 0:
|
if len(message) == 0:
|
||||||
@ -111,8 +121,10 @@ proc identify*(p: Identify,
|
|||||||
|
|
||||||
result = decodeMsg(message)
|
result = decodeMsg(message)
|
||||||
debug "identify: Identify for remote peer succeded"
|
debug "identify: Identify for remote peer succeded"
|
||||||
if remotePeerInfo.isSome and
|
|
||||||
remotePeerInfo.get().peerId.publicKey != result.pubKey:
|
if remotePeerInfo.peerId.isSome and
|
||||||
|
result.pubKey.isSome and
|
||||||
|
result.pubKey.get() != remotePeerInfo.peerId.get().publicKey:
|
||||||
debug "identify: Peer's remote public key doesn't match"
|
debug "identify: Peer's remote public key doesn't match"
|
||||||
raise newException(IdentityNoMatchError,
|
raise newException(IdentityNoMatchError,
|
||||||
"Peer's remote public key doesn't match")
|
"Peer's remote public key doesn't match")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user