feat: make private/public keys Option[T]

This commit is contained in:
Dmitriy Ryajov 2019-09-23 17:00:55 -06:00
parent 663ce6c589
commit b270515bb3
3 changed files with 29 additions and 24 deletions

View File

@ -8,7 +8,7 @@
## those terms. ## those terms.
## This module implementes API for libp2p peer. ## This module implementes API for libp2p peer.
import hashes import hashes, options
import nimcrypto/utils import nimcrypto/utils
import crypto/crypto, multicodec, multihash, base58, vbuffer import crypto/crypto, multicodec, multihash, base58, vbuffer
import protobuf/minprotobuf import protobuf/minprotobuf
@ -21,16 +21,16 @@ const
type type
PeerID* = object PeerID* = object
data*: seq[byte] data*: seq[byte]
privateKey*: PrivateKey privateKey*: Option[PrivateKey]
publicKey: PublicKey publicKey: Option[PublicKey]
PeerIDError* = object of CatchableError PeerIDError* = object of CatchableError
proc publicKey*(pid: PeerID): PublicKey {.inline.} = proc publicKey*(pid: PeerID): Option[PublicKey] {.inline.} =
if len(pid.publicKey.getBytes()) > 0: if pid.publicKey.isSome and len(pid.publicKey.get().getBytes()) > 0:
result = pid.publicKey result = pid.publicKey
elif len(pid.privateKey.getBytes()) > 0: elif pid.privateKey.isSome and len(pid.privateKey.get().getBytes()) > 0:
result = pid.privateKey.getKey() result = some(pid.privateKey.get().getKey())
proc pretty*(pid: PeerID): string {.inline.} = proc pretty*(pid: PeerID): string {.inline.} =
## Return base58 encoded ``pid`` representation. ## Return base58 encoded ``pid`` representation.
@ -171,12 +171,12 @@ proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID =
else: else:
mh = MultiHash.digest("sha2-256", pubraw) mh = MultiHash.digest("sha2-256", pubraw)
result.data = mh.data.buffer result.data = mh.data.buffer
result.publicKey = pubkey result.publicKey = some(pubkey)
proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} = proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} =
## Create new peer id from private key ``seckey``. ## Create new peer id from private key ``seckey``.
result = PeerID.init(seckey.getKey()) result = PeerID.init(seckey.getKey())
result.privateKey = seckey result.privateKey = some(seckey)
proc match*(pid: PeerID, pubkey: PublicKey): bool {.inline.} = proc match*(pid: PeerID, pubkey: PublicKey): bool {.inline.} =
## Returns ``true`` if ``pid`` matches public key ``pubkey``. ## Returns ``true`` if ``pid`` matches public key ``pubkey``.

View File

@ -46,7 +46,7 @@ proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer =
result = initProtoBuffer() result = initProtoBuffer()
if peerInfo.peerId.isSome: 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: for ma in peerInfo.addrs:
result.write(initProtoField(2, ma.data.buffer)) result.write(initProtoField(2, ma.data.buffer))
@ -123,14 +123,12 @@ proc identify*(p: Identify,
result = decodeMsg(message) result = decodeMsg(message)
trace "Identify for remote peer succeded" trace "Identify for remote peer succeded"
# TODO: To enable the blow code, the private and public if remotePeerInfo.peerId.isSome and
# keys in PeerID need to be wrapped with Option[T] result.pubKey.isSome and
# if remotePeerInfo.peerId.isSome and result.pubKey != remotePeerInfo.peerId.get().publicKey:
# result.pubKey.isSome and trace "identify: Peer's remote public key doesn't match"
# result.pubKey.get() != remotePeerInfo.peerId.get().publicKey: raise newException(IdentityNoMatchError,
# trace "identify: Peer's remote public key doesn't match" "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.} = proc push*(p: Identify, conn: Connection) {.async.} =
await conn.write(IdentifyPushCodec) await conn.write(IdentifyPushCodec)

View File

@ -7,7 +7,7 @@
## 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 sequtils import sequtils, options
import chronos, nimcrypto/sysrand, chronicles import chronos, nimcrypto/sysrand, chronicles
import ../../peerinfo, import ../../peerinfo,
../../peer, ../../peer,
@ -150,8 +150,10 @@ proc sign*(peerId: PeerID, msg: Message): Message =
encodeMessage(msg, buff) encodeMessage(msg, buff)
if buff.buffer.len > 0: if buff.buffer.len > 0:
result = msg result = msg
if peerId.privateKey.isSome:
result.signature = peerId. result.signature = peerId.
privateKey. privateKey.
get().
sign(getPreix() & buff.buffer). sign(getPreix() & buff.buffer).
getBytes() getBytes()
@ -160,10 +162,15 @@ proc makeMessage*(peerId: PeerID,
name: string): Message {.gcsafe.} = name: string): Message {.gcsafe.} =
var seqno: seq[byte] = newSeq[byte](20) var seqno: seq[byte] = newSeq[byte](20)
if randomBytes(addr seqno[0], 20) > 0: 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(), result = Message(fromPeer: peerId.getBytes(),
data: data, data: data,
seqno: seqno, seqno: seqno,
topicIDs: @[name], topicIDs: @[name],
signature: @[], signature: @[],
key: peerId.publicKey.getRawBytes()) key: key)
result = sign(peerId, result) result = sign(peerId, result)