@libp2p/peer-store 10.1.5 → 11.0.0-18dd3cb26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +4 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +6 -4
- package/dist/src/index.js.map +1 -1
- package/dist/src/pb/peer.d.ts +6 -6
- package/dist/src/pb/peer.d.ts.map +1 -1
- package/dist/src/pb/peer.js +76 -38
- package/dist/src/pb/peer.js.map +1 -1
- package/dist/src/store.js +7 -7
- package/dist/src/store.js.map +1 -1
- package/dist/src/utils/bytes-to-peer.d.ts.map +1 -1
- package/dist/src/utils/bytes-to-peer.js +4 -5
- package/dist/src/utils/bytes-to-peer.js.map +1 -1
- package/dist/src/utils/dedupe-addresses.d.ts.map +1 -1
- package/dist/src/utils/dedupe-addresses.js +2 -3
- package/dist/src/utils/dedupe-addresses.js.map +1 -1
- package/dist/src/utils/peer-data-to-datastore-peer.d.ts.map +1 -1
- package/dist/src/utils/peer-data-to-datastore-peer.js +14 -15
- package/dist/src/utils/peer-data-to-datastore-peer.js.map +1 -1
- package/dist/src/utils/peer-id-to-datastore-key.d.ts.map +1 -1
- package/dist/src/utils/peer-id-to-datastore-key.js +2 -3
- package/dist/src/utils/peer-id-to-datastore-key.js.map +1 -1
- package/dist/src/utils/to-peer-pb.d.ts.map +1 -1
- package/dist/src/utils/to-peer-pb.js +24 -15
- package/dist/src/utils/to-peer-pb.js.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +6 -4
- package/src/pb/peer.ts +80 -39
- package/src/store.ts +7 -7
- package/src/utils/bytes-to-peer.ts +4 -5
- package/src/utils/dedupe-addresses.ts +2 -3
- package/src/utils/peer-data-to-datastore-peer.ts +14 -15
- package/src/utils/peer-id-to-datastore-key.ts +2 -3
- package/src/utils/to-peer-pb.ts +24 -16
- package/dist/src/errors.d.ts +0 -4
- package/dist/src/errors.d.ts.map +0 -1
- package/dist/src/errors.js +0 -4
- package/dist/src/errors.js.map +0 -1
- package/dist/typedoc-urls.json +0 -10
- package/src/errors.ts +0 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { publicKeyFromProtobuf } from '@libp2p/crypto/keys'
|
|
2
|
+
import { peerIdFromPublicKey } from '@libp2p/peer-id'
|
|
2
3
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
3
4
|
import { Peer as PeerPB } from '../pb/peer.js'
|
|
4
5
|
import type { PeerId, Peer, Tag } from '@libp2p/interface'
|
|
@@ -7,10 +8,8 @@ export function bytesToPeer (peerId: PeerId, buf: Uint8Array): Peer {
|
|
|
7
8
|
const peer = PeerPB.decode(buf)
|
|
8
9
|
|
|
9
10
|
if (peer.publicKey != null && peerId.publicKey == null) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
publicKey: peerId.publicKey
|
|
13
|
-
})
|
|
11
|
+
const publicKey = publicKeyFromProtobuf(peer.publicKey)
|
|
12
|
+
peerId = peerIdFromPublicKey(publicKey)
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
const tags = new Map<string, Tag>()
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InvalidParametersError } from '@libp2p/interface'
|
|
2
2
|
import { isMultiaddr, multiaddr } from '@multiformats/multiaddr'
|
|
3
|
-
import { codes } from '../errors.js'
|
|
4
3
|
import type { AddressFilter } from '../index.js'
|
|
5
4
|
import type { Address as AddressPB } from '../pb/peer.js'
|
|
6
5
|
import type { PeerId, Address } from '@libp2p/interface'
|
|
@@ -18,7 +17,7 @@ export async function dedupeFilterAndSortAddresses (peerId: PeerId, filter: Addr
|
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
if (!isMultiaddr(addr.multiaddr)) {
|
|
21
|
-
throw new
|
|
20
|
+
throw new InvalidParametersError('Multiaddr was invalid')
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
if (!(await filter(peerId, addr.multiaddr))) {
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { publicKeyToProtobuf } from '@libp2p/crypto/keys'
|
|
2
|
+
import { InvalidParametersError } from '@libp2p/interface'
|
|
2
3
|
import { isMultiaddr } from '@multiformats/multiaddr'
|
|
3
|
-
import { equals as uint8arrayEquals } from 'uint8arrays/equals'
|
|
4
|
-
import { codes } from '../errors.js'
|
|
5
4
|
import type { Peer as PeerPB } from '../pb/peer.js'
|
|
6
5
|
import type { PeerId, PeerData } from '@libp2p/interface'
|
|
7
6
|
|
|
8
7
|
export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
9
8
|
if (data == null) {
|
|
10
|
-
throw new
|
|
9
|
+
throw new InvalidParametersError('Invalid PeerData')
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
if (data.publicKey != null && peerId.publicKey != null && !
|
|
14
|
-
throw new
|
|
12
|
+
if (data.publicKey != null && peerId.publicKey != null && !data.publicKey.equals(peerId.publicKey)) {
|
|
13
|
+
throw new InvalidParametersError('publicKey bytes do not match peer id publicKey bytes')
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
// merge addresses and multiaddrs, and dedupe
|
|
@@ -22,7 +21,7 @@ export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
|
22
21
|
.concat((data.multiaddrs ?? []).map(multiaddr => ({ multiaddr, isCertified: false })))
|
|
23
22
|
.filter(address => {
|
|
24
23
|
if (!isMultiaddr(address.multiaddr)) {
|
|
25
|
-
throw new
|
|
24
|
+
throw new InvalidParametersError('Invalid mulitaddr')
|
|
26
25
|
}
|
|
27
26
|
|
|
28
27
|
if (addressSet.has(address.multiaddr.toString())) {
|
|
@@ -42,7 +41,7 @@ export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
|
42
41
|
protocols: (data.protocols ?? []).sort(),
|
|
43
42
|
metadata: new Map(),
|
|
44
43
|
tags: new Map(),
|
|
45
|
-
publicKey: data.publicKey,
|
|
44
|
+
publicKey: data.publicKey != null ? publicKeyToProtobuf(data.publicKey) : undefined,
|
|
46
45
|
peerRecordEnvelope: data.peerRecordEnvelope
|
|
47
46
|
}
|
|
48
47
|
|
|
@@ -52,7 +51,7 @@ export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
|
52
51
|
|
|
53
52
|
for (const [key, value] of metadataEntries) {
|
|
54
53
|
if (typeof key !== 'string') {
|
|
55
|
-
throw new
|
|
54
|
+
throw new InvalidParametersError('Peer metadata keys must be strings')
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
if (value == null) {
|
|
@@ -60,7 +59,7 @@ export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
if (!(value instanceof Uint8Array)) {
|
|
63
|
-
throw new
|
|
62
|
+
throw new InvalidParametersError('Peer metadata values must be Uint8Arrays')
|
|
64
63
|
}
|
|
65
64
|
|
|
66
65
|
output.metadata.set(key, value)
|
|
@@ -72,7 +71,7 @@ export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
|
72
71
|
|
|
73
72
|
for (const [key, value] of tagsEntries) {
|
|
74
73
|
if (typeof key !== 'string') {
|
|
75
|
-
throw new
|
|
74
|
+
throw new InvalidParametersError('Peer tag keys must be strings')
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
if (value == null) {
|
|
@@ -86,20 +85,20 @@ export function toDatastorePeer (peerId: PeerId, data: PeerData): PeerPB {
|
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
if (tag.value < 0 || tag.value > 100) {
|
|
89
|
-
throw new
|
|
88
|
+
throw new InvalidParametersError('Tag value must be between 0-100')
|
|
90
89
|
}
|
|
91
90
|
|
|
92
91
|
if (parseInt(`${tag.value}`, 10) !== tag.value) {
|
|
93
|
-
throw new
|
|
92
|
+
throw new InvalidParametersError('Tag value must be an integer')
|
|
94
93
|
}
|
|
95
94
|
|
|
96
95
|
if (tag.ttl != null) {
|
|
97
96
|
if (tag.ttl < 0) {
|
|
98
|
-
throw new
|
|
97
|
+
throw new InvalidParametersError('Tag ttl must be between greater than 0')
|
|
99
98
|
}
|
|
100
99
|
|
|
101
100
|
if (parseInt(`${tag.ttl}`, 10) !== tag.ttl) {
|
|
102
|
-
throw new
|
|
101
|
+
throw new InvalidParametersError('Tag ttl must be an integer')
|
|
103
102
|
}
|
|
104
103
|
}
|
|
105
104
|
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InvalidParametersError } from '@libp2p/interface'
|
|
2
2
|
import { isPeerId, type PeerId } from '@libp2p/interface'
|
|
3
3
|
import { Key } from 'interface-datastore/key'
|
|
4
|
-
import { codes } from '../errors.js'
|
|
5
4
|
|
|
6
5
|
export const NAMESPACE_COMMON = '/peers/'
|
|
7
6
|
|
|
8
7
|
export function peerIdToDatastoreKey (peerId: PeerId): Key {
|
|
9
8
|
if (!isPeerId(peerId) || peerId.type == null) {
|
|
10
|
-
throw new
|
|
9
|
+
throw new InvalidParametersError('Invalid PeerId')
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
const b32key = peerId.toCID().toString()
|
package/src/utils/to-peer-pb.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { codes } from '../errors.js'
|
|
1
|
+
import { publicKeyToProtobuf } from '@libp2p/crypto/keys'
|
|
2
|
+
import { InvalidParametersError } from '@libp2p/interface'
|
|
4
3
|
import { dedupeFilterAndSortAddresses } from './dedupe-addresses.js'
|
|
5
4
|
import type { AddressFilter } from '../index.js'
|
|
6
5
|
import type { Tag, Peer as PeerPB } from '../pb/peer.js'
|
|
@@ -13,17 +12,17 @@ export interface ToPBPeerOptions {
|
|
|
13
12
|
|
|
14
13
|
export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strategy: 'merge' | 'patch', options: ToPBPeerOptions): Promise<PeerPB> {
|
|
15
14
|
if (data == null) {
|
|
16
|
-
throw new
|
|
15
|
+
throw new InvalidParametersError('Invalid PeerData')
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
if (data.publicKey != null && peerId.publicKey != null && !
|
|
20
|
-
throw new
|
|
18
|
+
if (data.publicKey != null && peerId.publicKey != null && !data.publicKey.equals(peerId.publicKey)) {
|
|
19
|
+
throw new InvalidParametersError('publicKey bytes do not match peer id publicKey bytes')
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
const existingPeer = options.existingPeer
|
|
24
23
|
|
|
25
24
|
if (existingPeer != null && !peerId.equals(existingPeer.id)) {
|
|
26
|
-
throw new
|
|
25
|
+
throw new InvalidParametersError('peer id did not match existing peer id')
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
let addresses: Address[] = existingPeer?.addresses ?? []
|
|
@@ -131,6 +130,16 @@ export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strateg
|
|
|
131
130
|
}
|
|
132
131
|
}
|
|
133
132
|
|
|
133
|
+
let publicKey: Uint8Array | undefined
|
|
134
|
+
|
|
135
|
+
if (existingPeer?.id.publicKey != null) {
|
|
136
|
+
publicKey = publicKeyToProtobuf(existingPeer.id.publicKey)
|
|
137
|
+
} else if (data.publicKey != null) {
|
|
138
|
+
publicKey = publicKeyToProtobuf(data.publicKey)
|
|
139
|
+
} else if (peerId.publicKey != null) {
|
|
140
|
+
publicKey = publicKeyToProtobuf(peerId.publicKey)
|
|
141
|
+
}
|
|
142
|
+
|
|
134
143
|
const output: PeerPB = {
|
|
135
144
|
addresses: await dedupeFilterAndSortAddresses(peerId, options.addressFilter ?? (async () => true), addresses),
|
|
136
145
|
protocols: [...protocols.values()].sort((a, b) => {
|
|
@@ -138,8 +147,7 @@ export async function toPeerPB (peerId: PeerId, data: Partial<PeerData>, strateg
|
|
|
138
147
|
}),
|
|
139
148
|
metadata,
|
|
140
149
|
tags,
|
|
141
|
-
|
|
142
|
-
publicKey: existingPeer?.id.publicKey ?? data.publicKey ?? peerId.publicKey,
|
|
150
|
+
publicKey,
|
|
143
151
|
peerRecordEnvelope
|
|
144
152
|
}
|
|
145
153
|
|
|
@@ -184,36 +192,36 @@ function createSortedMap <V, R = V> (entries: Array<[string, V | undefined]>, op
|
|
|
184
192
|
|
|
185
193
|
function validateMetadata (key: string, value: Uint8Array): void {
|
|
186
194
|
if (typeof key !== 'string') {
|
|
187
|
-
throw new
|
|
195
|
+
throw new InvalidParametersError('Metadata key must be a string')
|
|
188
196
|
}
|
|
189
197
|
|
|
190
198
|
if (!(value instanceof Uint8Array)) {
|
|
191
|
-
throw new
|
|
199
|
+
throw new InvalidParametersError('Metadata value must be a Uint8Array')
|
|
192
200
|
}
|
|
193
201
|
}
|
|
194
202
|
|
|
195
203
|
function validateTag (key: string, tag: TagOptions): void {
|
|
196
204
|
if (typeof key !== 'string') {
|
|
197
|
-
throw new
|
|
205
|
+
throw new InvalidParametersError('Tag name must be a string')
|
|
198
206
|
}
|
|
199
207
|
|
|
200
208
|
if (tag.value != null) {
|
|
201
209
|
if (parseInt(`${tag.value}`, 10) !== tag.value) {
|
|
202
|
-
throw new
|
|
210
|
+
throw new InvalidParametersError('Tag value must be an integer')
|
|
203
211
|
}
|
|
204
212
|
|
|
205
213
|
if (tag.value < 0 || tag.value > 100) {
|
|
206
|
-
throw new
|
|
214
|
+
throw new InvalidParametersError('Tag value must be between 0-100')
|
|
207
215
|
}
|
|
208
216
|
}
|
|
209
217
|
|
|
210
218
|
if (tag.ttl != null) {
|
|
211
219
|
if (parseInt(`${tag.ttl}`, 10) !== tag.ttl) {
|
|
212
|
-
throw new
|
|
220
|
+
throw new InvalidParametersError('Tag ttl must be an integer')
|
|
213
221
|
}
|
|
214
222
|
|
|
215
223
|
if (tag.ttl < 0) {
|
|
216
|
-
throw new
|
|
224
|
+
throw new InvalidParametersError('Tag ttl must be between greater than 0')
|
|
217
225
|
}
|
|
218
226
|
}
|
|
219
227
|
}
|
package/dist/src/errors.d.ts
DELETED
package/dist/src/errors.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK;;CAEjB,CAAA"}
|
package/dist/src/errors.js
DELETED
package/dist/src/errors.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,sBAAsB,EAAE,wBAAwB;CACjD,CAAA"}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"PersistentPeerStore": "https://libp2p.github.io/js-libp2p/classes/_libp2p_peer_store.PersistentPeerStore.html",
|
|
3
|
-
".:PersistentPeerStore": "https://libp2p.github.io/js-libp2p/classes/_libp2p_peer_store.PersistentPeerStore.html",
|
|
4
|
-
"AddressFilter": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.AddressFilter.html",
|
|
5
|
-
".:AddressFilter": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.AddressFilter.html",
|
|
6
|
-
"PersistentPeerStoreComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreComponents.html",
|
|
7
|
-
".:PersistentPeerStoreComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreComponents.html",
|
|
8
|
-
"PersistentPeerStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreInit.html",
|
|
9
|
-
".:PersistentPeerStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_peer_store.PersistentPeerStoreInit.html"
|
|
10
|
-
}
|
package/src/errors.ts
DELETED