feat: make private/public keys Option[T]
This commit is contained in:
parent
663ce6c589
commit
b270515bb3
|
@ -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``.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,8 +150,10 @@ proc sign*(peerId: PeerID, msg: Message): Message =
|
|||
encodeMessage(msg, buff)
|
||||
if buff.buffer.len > 0:
|
||||
result = msg
|
||||
if peerId.privateKey.isSome:
|
||||
result.signature = peerId.
|
||||
privateKey.
|
||||
get().
|
||||
sign(getPreix() & buff.buffer).
|
||||
getBytes()
|
||||
|
||||
|
@ -160,10 +162,15 @@ proc makeMessage*(peerId: PeerID,
|
|||
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)
|
||||
|
|
Loading…
Reference in New Issue