Store missing pubkey in PeerStore [KeyBook] (#804)
This commit is contained in:
parent
cc3c637c22
commit
192cac6254
|
@ -184,6 +184,11 @@ func init*(t: typedesc[PeerId], seckey: PrivateKey): Result[PeerId, cstring] =
|
|||
## Create new peer id from private key ``seckey``.
|
||||
PeerId.init(? seckey.getPublicKey().orError(cstring("invalid private key")))
|
||||
|
||||
proc random*(t: typedesc[PeerId], rng = newRng()): Result[PeerId, cstring] =
|
||||
## Create new peer id with random public key.
|
||||
let randomKey = PrivateKey.random(Secp256k1, rng[])[]
|
||||
PeerId.init(randomKey).orError(cstring("failed to generate random key"))
|
||||
|
||||
func match*(pid: PeerId, pubkey: PublicKey): bool =
|
||||
## Returns ``true`` if ``pid`` matches public key ``pubkey``.
|
||||
let p = PeerId.init(pubkey)
|
||||
|
|
|
@ -16,8 +16,7 @@ runnableExamples:
|
|||
# Create a custom book type
|
||||
type MoodBook = ref object of PeerBook[string]
|
||||
|
||||
var somePeerId: PeerId
|
||||
discard somePeerId.init("")
|
||||
var somePeerId = PeerId.random().get()
|
||||
|
||||
peerStore[MoodBook][somePeerId] = "Happy"
|
||||
doAssert peerStore[MoodBook][somePeerId] == "Happy"
|
||||
|
@ -153,6 +152,9 @@ proc updatePeerInfo*(
|
|||
if info.addrs.len > 0:
|
||||
peerStore[AddressBook][info.peerId] = info.addrs
|
||||
|
||||
if info.pubkey.isSome:
|
||||
peerStore[KeyBook][info.peerId] = info.pubkey.get()
|
||||
|
||||
if info.agentVersion.isSome:
|
||||
peerStore[AgentBook][info.peerId] = info.agentVersion.get().string
|
||||
|
||||
|
|
|
@ -152,8 +152,8 @@ suite "Identify":
|
|||
identifyPush2 {.threadvar.}: IdentifyPush
|
||||
conn {.threadvar.}: Connection
|
||||
asyncSetup:
|
||||
switch1 = newStandardSwitch()
|
||||
switch2 = newStandardSwitch()
|
||||
switch1 = newStandardSwitch(sendSignedPeerRecord=true)
|
||||
switch2 = newStandardSwitch(sendSignedPeerRecord=true)
|
||||
|
||||
proc updateStore1(peerId: PeerId, info: IdentifyInfo) {.async.} =
|
||||
switch1.peerStore.updatePeerInfo(info)
|
||||
|
@ -178,12 +178,20 @@ suite "Identify":
|
|||
switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs
|
||||
switch2.peerStore[AddressBook][switch1.peerInfo.peerId] == switch1.peerInfo.addrs
|
||||
|
||||
switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs
|
||||
switch2.peerStore[AddressBook][switch1.peerInfo.peerId] == switch1.peerInfo.addrs
|
||||
switch1.peerStore[KeyBook][switch2.peerInfo.peerId] == switch2.peerInfo.publicKey
|
||||
switch2.peerStore[KeyBook][switch1.peerInfo.peerId] == switch1.peerInfo.publicKey
|
||||
|
||||
#switch1.peerStore.signedPeerRecordBook.get(switch2.peerInfo.peerId) == switch2.peerInfo.signedPeerRecord.get()
|
||||
#switch2.peerStore.signedPeerRecordBook.get(switch1.peerInfo.peerId) == switch1.peerInfo.signedPeerRecord.get()
|
||||
# no longer sent by default
|
||||
switch1.peerStore[AgentBook][switch2.peerInfo.peerId] == switch2.peerInfo.agentVersion
|
||||
switch2.peerStore[AgentBook][switch1.peerInfo.peerId] == switch1.peerInfo.agentVersion
|
||||
|
||||
switch1.peerStore[ProtoVersionBook][switch2.peerInfo.peerId] == switch2.peerInfo.protoVersion
|
||||
switch2.peerStore[ProtoVersionBook][switch1.peerInfo.peerId] == switch1.peerInfo.protoVersion
|
||||
|
||||
switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] == switch2.peerInfo.protocols
|
||||
switch2.peerStore[ProtoBook][switch1.peerInfo.peerId] == switch1.peerInfo.protocols
|
||||
|
||||
switch1.peerStore[SPRBook][switch2.peerInfo.peerId] == switch2.peerInfo.signedPeerRecord.envelope
|
||||
switch2.peerStore[SPRBook][switch1.peerInfo.peerId] == switch1.peerInfo.signedPeerRecord.envelope
|
||||
|
||||
proc closeAll() {.async.} =
|
||||
await conn.close()
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
import unittest2
|
||||
import nimcrypto/utils, stew/base58
|
||||
import ../libp2p/crypto/crypto, ../libp2p/peerid
|
||||
|
||||
import bearssl/hash
|
||||
when defined(nimHasUsed): {.used.}
|
||||
|
||||
const
|
||||
|
@ -236,3 +236,15 @@ suite "Peer testing suite":
|
|||
ekey2 == pubkey
|
||||
ekey3 == pubkey
|
||||
ekey4 == pubkey
|
||||
test "Test PeerId.random() proc":
|
||||
# generate a random peer with a deterministic ssed
|
||||
var rng = (ref HmacDrbgContext)()
|
||||
hmacDrbgInit(rng[], addr sha256Vtable, nil, 0)
|
||||
var randomPeer1 = PeerId.random(rng)
|
||||
check:
|
||||
$randomPeer1.get() == "16Uiu2HAmCxpSTFDNdWiu1MLScu7inPhcbbGfPvuvRPD1e51gw1Xr"
|
||||
|
||||
# generate a random peer with a new random seed
|
||||
var randomPeer2 = PeerId.random()
|
||||
check:
|
||||
randomPeer2.isErr() != true
|
Loading…
Reference in New Issue