diff --git a/libp2p/peer.nim b/libp2p/peer.nim index fbbc9c944..40f364c9f 100644 --- a/libp2p/peer.nim +++ b/libp2p/peer.nim @@ -8,7 +8,7 @@ ## those terms. ## This module implementes API for libp2p peer. -import hashes +import hashes, options import nimcrypto/utils import crypto/crypto, multicodec, multihash, base58, vbuffer import protobuf/minprotobuf @@ -21,16 +21,16 @@ const type PeerID* = object data*: seq[byte] - privateKey*: PrivateKey - publicKey: PublicKey + privateKey*: Option[PrivateKey] + publicKey: Option[PublicKey] PeerIDError* = object of CatchableError -proc publicKey*(pid: PeerID): PublicKey {.inline.} = - if len(pid.publicKey.getBytes()) > 0: +proc publicKey*(pid: PeerID): Option[PublicKey] {.inline.} = + if pid.publicKey.isSome and len(pid.publicKey.get().getBytes()) > 0: result = pid.publicKey - elif len(pid.privateKey.getBytes()) > 0: - result = pid.privateKey.getKey() + elif pid.privateKey.isSome and len(pid.privateKey.get().getBytes()) > 0: + result = some(pid.privateKey.get().getKey()) proc pretty*(pid: PeerID): string {.inline.} = ## Return base58 encoded ``pid`` representation. @@ -171,12 +171,12 @@ proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID = else: mh = MultiHash.digest("sha2-256", pubraw) result.data = mh.data.buffer - result.publicKey = pubkey + result.publicKey = some(pubkey) proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} = ## Create new peer id from private key ``seckey``. result = PeerID.init(seckey.getKey()) - result.privateKey = seckey + result.privateKey = some(seckey) proc match*(pid: PeerID, pubkey: PublicKey): bool {.inline.} = ## Returns ``true`` if ``pid`` matches public key ``pubkey``. diff --git a/libp2p/protocols/identify.nim b/libp2p/protocols/identify.nim index d9f03cccc..6e6518be5 100644 --- a/libp2p/protocols/identify.nim +++ b/libp2p/protocols/identify.nim @@ -46,7 +46,7 @@ proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer = result = initProtoBuffer() if peerInfo.peerId.isSome: - result.write(initProtoField(1, peerInfo.peerId.get().publicKey.getBytes())) + result.write(initProtoField(1, peerInfo.peerId.get().publicKey.get().getBytes())) for ma in peerInfo.addrs: result.write(initProtoField(2, ma.data.buffer)) @@ -123,14 +123,12 @@ proc identify*(p: Identify, result = decodeMsg(message) trace "Identify for remote peer succeded" - # TODO: To enable the blow code, the private and public - # keys in PeerID need to be wrapped with Option[T] - # if remotePeerInfo.peerId.isSome and - # result.pubKey.isSome and - # result.pubKey.get() != remotePeerInfo.peerId.get().publicKey: - # trace "identify: Peer's remote public key doesn't match" - # raise newException(IdentityNoMatchError, - # "Peer's remote public key doesn't match") + if remotePeerInfo.peerId.isSome and + result.pubKey.isSome and + result.pubKey != remotePeerInfo.peerId.get().publicKey: + trace "identify: Peer's remote public key doesn't match" + raise newException(IdentityNoMatchError, + "Peer's remote public key doesn't match") proc push*(p: Identify, conn: Connection) {.async.} = await conn.write(IdentifyPushCodec) diff --git a/libp2p/protocols/pubsub/rpcmsg.nim b/libp2p/protocols/pubsub/rpcmsg.nim index 4109b61da..0fb746b01 100644 --- a/libp2p/protocols/pubsub/rpcmsg.nim +++ b/libp2p/protocols/pubsub/rpcmsg.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import sequtils +import sequtils, options import chronos, nimcrypto/sysrand, chronicles import ../../peerinfo, ../../peer, @@ -150,20 +150,27 @@ proc sign*(peerId: PeerID, msg: Message): Message = encodeMessage(msg, buff) if buff.buffer.len > 0: result = msg - result.signature = peerId. - privateKey. - sign(getPreix() & buff.buffer). - getBytes() + if peerId.privateKey.isSome: + result.signature = peerId. + privateKey. + get(). + sign(getPreix() & buff.buffer). + getBytes() proc makeMessage*(peerId: PeerID, data: seq[byte], name: string): Message {.gcsafe.} = var seqno: seq[byte] = newSeq[byte](20) if randomBytes(addr seqno[0], 20) > 0: + var key: seq[byte] = @[] + + if peerId.publicKey.isSome: + key = peerId.publicKey.get().getRawBytes() + result = Message(fromPeer: peerId.getBytes(), data: data, seqno: seqno, topicIDs: @[name], signature: @[], - key: peerId.publicKey.getRawBytes()) + key: key) result = sign(peerId, result)