@libp2p/peer-store
Advanced tools
Comparing version 11.0.21 to 11.0.22
import { publicKeyFromProtobuf } from '@libp2p/crypto/keys'; | ||
import { peerIdFromPublicKey } from '@libp2p/peer-id'; | ||
import { multiaddr } from '@multiformats/multiaddr'; | ||
import { base58btc } from 'multiformats/bases/base58'; | ||
import * as Digest from 'multiformats/hashes/digest'; | ||
import { Peer as PeerPB } from '../pb/peer.js'; | ||
function populatePublicKey(peerId, protobuf) { | ||
if (peerId.publicKey != null || protobuf.publicKey == null) { | ||
return peerId; | ||
} | ||
let digest; | ||
if (peerId.type === 'RSA') { | ||
// avoid hashing public key | ||
const multihash = base58btc.decode(`z${peerId}`); | ||
digest = Digest.decode(multihash); | ||
} | ||
const publicKey = publicKeyFromProtobuf(protobuf.publicKey, digest); | ||
return peerIdFromPublicKey(publicKey); | ||
} | ||
export function bytesToPeer(peerId, buf) { | ||
const peer = PeerPB.decode(buf); | ||
if (peer.publicKey != null && peerId.publicKey == null) { | ||
const publicKey = publicKeyFromProtobuf(peer.publicKey); | ||
peerId = peerIdFromPublicKey(publicKey); | ||
} | ||
const tags = new Map(); | ||
@@ -22,3 +33,3 @@ // remove any expired tags | ||
...peer, | ||
id: peerId, | ||
id: populatePublicKey(peerId, peer), | ||
addresses: peer.addresses.map(({ multiaddr: ma, isCertified }) => { | ||
@@ -25,0 +36,0 @@ return { |
{ | ||
"name": "@libp2p/peer-store", | ||
"version": "11.0.21", | ||
"version": "11.0.22", | ||
"description": "Stores information about peers libp2p knows on the network", | ||
@@ -31,3 +31,3 @@ "license": "Apache-2.0 OR MIT", | ||
".": { | ||
"types": "./src/index.d.ts", | ||
"types": "./dist/src/index.d.ts", | ||
"import": "./dist/src/index.js" | ||
@@ -63,6 +63,6 @@ } | ||
"dependencies": { | ||
"@libp2p/crypto": "^5.0.13", | ||
"@libp2p/crypto": "^5.0.14", | ||
"@libp2p/interface": "^2.6.1", | ||
"@libp2p/peer-id": "^5.0.14", | ||
"@libp2p/peer-record": "^8.0.21", | ||
"@libp2p/peer-id": "^5.0.15", | ||
"@libp2p/peer-record": "^8.0.22", | ||
"@multiformats/multiaddr": "^12.3.3", | ||
@@ -78,5 +78,6 @@ "interface-datastore": "^8.3.1", | ||
"devDependencies": { | ||
"@libp2p/logger": "^5.1.10", | ||
"@libp2p/logger": "^5.1.11", | ||
"@types/sinon": "^17.0.3", | ||
"aegir": "^45.1.1", | ||
"benchmark": "^2.1.4", | ||
"datastore-core": "^10.0.2", | ||
@@ -83,0 +84,0 @@ "delay": "^6.0.0", |
import { publicKeyFromProtobuf } from '@libp2p/crypto/keys' | ||
import { peerIdFromPublicKey } from '@libp2p/peer-id' | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { base58btc } from 'multiformats/bases/base58' | ||
import * as Digest from 'multiformats/hashes/digest' | ||
import { Peer as PeerPB } from '../pb/peer.js' | ||
import type { PeerId, Peer, Tag } from '@libp2p/interface' | ||
export function bytesToPeer (peerId: PeerId, buf: Uint8Array): Peer { | ||
const peer = PeerPB.decode(buf) | ||
function populatePublicKey (peerId: PeerId, protobuf: PeerPB): PeerId { | ||
if (peerId.publicKey != null || protobuf.publicKey == null) { | ||
return peerId | ||
} | ||
if (peer.publicKey != null && peerId.publicKey == null) { | ||
const publicKey = publicKeyFromProtobuf(peer.publicKey) | ||
peerId = peerIdFromPublicKey(publicKey) | ||
let digest: any | ||
if (peerId.type === 'RSA') { | ||
// avoid hashing public key | ||
const multihash = base58btc.decode(`z${peerId}`) | ||
digest = Digest.decode(multihash) | ||
} | ||
const publicKey = publicKeyFromProtobuf(protobuf.publicKey, digest) | ||
return peerIdFromPublicKey(publicKey) | ||
} | ||
export function bytesToPeer (peerId: PeerId, buf: Uint8Array): Peer { | ||
const peer = PeerPB.decode(buf) | ||
const tags = new Map<string, Tag>() | ||
@@ -30,3 +43,3 @@ | ||
...peer, | ||
id: peerId, | ||
id: populatePublicKey(peerId, peer), | ||
addresses: peer.addresses.map(({ multiaddr: ma, isCertified }) => { | ||
@@ -33,0 +46,0 @@ return { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
270320
10
2774
Updated@libp2p/crypto@^5.0.14
Updated@libp2p/peer-id@^5.0.15
Updated@libp2p/peer-record@^8.0.22