mirror of
https://github.com/vacp2p/nim-libp2p-experimental.git
synced 2025-01-13 03:44:26 +00:00
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``.
|
## Create new peer id from private key ``seckey``.
|
||||||
PeerId.init(? seckey.getPublicKey().orError(cstring("invalid private key")))
|
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 =
|
func match*(pid: PeerId, pubkey: PublicKey): bool =
|
||||||
## Returns ``true`` if ``pid`` matches public key ``pubkey``.
|
## Returns ``true`` if ``pid`` matches public key ``pubkey``.
|
||||||
let p = PeerId.init(pubkey)
|
let p = PeerId.init(pubkey)
|
||||||
|
@ -16,8 +16,7 @@ runnableExamples:
|
|||||||
# Create a custom book type
|
# Create a custom book type
|
||||||
type MoodBook = ref object of PeerBook[string]
|
type MoodBook = ref object of PeerBook[string]
|
||||||
|
|
||||||
var somePeerId: PeerId
|
var somePeerId = PeerId.random().get()
|
||||||
discard somePeerId.init("")
|
|
||||||
|
|
||||||
peerStore[MoodBook][somePeerId] = "Happy"
|
peerStore[MoodBook][somePeerId] = "Happy"
|
||||||
doAssert peerStore[MoodBook][somePeerId] == "Happy"
|
doAssert peerStore[MoodBook][somePeerId] == "Happy"
|
||||||
@ -153,6 +152,9 @@ proc updatePeerInfo*(
|
|||||||
if info.addrs.len > 0:
|
if info.addrs.len > 0:
|
||||||
peerStore[AddressBook][info.peerId] = info.addrs
|
peerStore[AddressBook][info.peerId] = info.addrs
|
||||||
|
|
||||||
|
if info.pubkey.isSome:
|
||||||
|
peerStore[KeyBook][info.peerId] = info.pubkey.get()
|
||||||
|
|
||||||
if info.agentVersion.isSome:
|
if info.agentVersion.isSome:
|
||||||
peerStore[AgentBook][info.peerId] = info.agentVersion.get().string
|
peerStore[AgentBook][info.peerId] = info.agentVersion.get().string
|
||||||
|
|
||||||
|
@ -152,8 +152,8 @@ suite "Identify":
|
|||||||
identifyPush2 {.threadvar.}: IdentifyPush
|
identifyPush2 {.threadvar.}: IdentifyPush
|
||||||
conn {.threadvar.}: Connection
|
conn {.threadvar.}: Connection
|
||||||
asyncSetup:
|
asyncSetup:
|
||||||
switch1 = newStandardSwitch()
|
switch1 = newStandardSwitch(sendSignedPeerRecord=true)
|
||||||
switch2 = newStandardSwitch()
|
switch2 = newStandardSwitch(sendSignedPeerRecord=true)
|
||||||
|
|
||||||
proc updateStore1(peerId: PeerId, info: IdentifyInfo) {.async.} =
|
proc updateStore1(peerId: PeerId, info: IdentifyInfo) {.async.} =
|
||||||
switch1.peerStore.updatePeerInfo(info)
|
switch1.peerStore.updatePeerInfo(info)
|
||||||
@ -178,12 +178,20 @@ suite "Identify":
|
|||||||
switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs
|
switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs
|
||||||
switch2.peerStore[AddressBook][switch1.peerInfo.peerId] == switch1.peerInfo.addrs
|
switch2.peerStore[AddressBook][switch1.peerInfo.peerId] == switch1.peerInfo.addrs
|
||||||
|
|
||||||
switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs
|
switch1.peerStore[KeyBook][switch2.peerInfo.peerId] == switch2.peerInfo.publicKey
|
||||||
switch2.peerStore[AddressBook][switch1.peerInfo.peerId] == switch1.peerInfo.addrs
|
switch2.peerStore[KeyBook][switch1.peerInfo.peerId] == switch1.peerInfo.publicKey
|
||||||
|
|
||||||
#switch1.peerStore.signedPeerRecordBook.get(switch2.peerInfo.peerId) == switch2.peerInfo.signedPeerRecord.get()
|
switch1.peerStore[AgentBook][switch2.peerInfo.peerId] == switch2.peerInfo.agentVersion
|
||||||
#switch2.peerStore.signedPeerRecordBook.get(switch1.peerInfo.peerId) == switch1.peerInfo.signedPeerRecord.get()
|
switch2.peerStore[AgentBook][switch1.peerInfo.peerId] == switch1.peerInfo.agentVersion
|
||||||
# no longer sent by default
|
|
||||||
|
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.} =
|
proc closeAll() {.async.} =
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
import unittest2
|
import unittest2
|
||||||
import nimcrypto/utils, stew/base58
|
import nimcrypto/utils, stew/base58
|
||||||
import ../libp2p/crypto/crypto, ../libp2p/peerid
|
import ../libp2p/crypto/crypto, ../libp2p/peerid
|
||||||
|
import bearssl/hash
|
||||||
when defined(nimHasUsed): {.used.}
|
when defined(nimHasUsed): {.used.}
|
||||||
|
|
||||||
const
|
const
|
||||||
@ -236,3 +236,15 @@ suite "Peer testing suite":
|
|||||||
ekey2 == pubkey
|
ekey2 == pubkey
|
||||||
ekey3 == pubkey
|
ekey3 == pubkey
|
||||||
ekey4 == 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…
x
Reference in New Issue
Block a user