@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.
Files changed (40) hide show
  1. package/dist/index.min.js +4 -5
  2. package/dist/src/index.d.ts.map +1 -1
  3. package/dist/src/index.js +6 -4
  4. package/dist/src/index.js.map +1 -1
  5. package/dist/src/pb/peer.d.ts +6 -6
  6. package/dist/src/pb/peer.d.ts.map +1 -1
  7. package/dist/src/pb/peer.js +76 -38
  8. package/dist/src/pb/peer.js.map +1 -1
  9. package/dist/src/store.js +7 -7
  10. package/dist/src/store.js.map +1 -1
  11. package/dist/src/utils/bytes-to-peer.d.ts.map +1 -1
  12. package/dist/src/utils/bytes-to-peer.js +4 -5
  13. package/dist/src/utils/bytes-to-peer.js.map +1 -1
  14. package/dist/src/utils/dedupe-addresses.d.ts.map +1 -1
  15. package/dist/src/utils/dedupe-addresses.js +2 -3
  16. package/dist/src/utils/dedupe-addresses.js.map +1 -1
  17. package/dist/src/utils/peer-data-to-datastore-peer.d.ts.map +1 -1
  18. package/dist/src/utils/peer-data-to-datastore-peer.js +14 -15
  19. package/dist/src/utils/peer-data-to-datastore-peer.js.map +1 -1
  20. package/dist/src/utils/peer-id-to-datastore-key.d.ts.map +1 -1
  21. package/dist/src/utils/peer-id-to-datastore-key.js +2 -3
  22. package/dist/src/utils/peer-id-to-datastore-key.js.map +1 -1
  23. package/dist/src/utils/to-peer-pb.d.ts.map +1 -1
  24. package/dist/src/utils/to-peer-pb.js +24 -15
  25. package/dist/src/utils/to-peer-pb.js.map +1 -1
  26. package/package.json +9 -9
  27. package/src/index.ts +6 -4
  28. package/src/pb/peer.ts +80 -39
  29. package/src/store.ts +7 -7
  30. package/src/utils/bytes-to-peer.ts +4 -5
  31. package/src/utils/dedupe-addresses.ts +2 -3
  32. package/src/utils/peer-data-to-datastore-peer.ts +14 -15
  33. package/src/utils/peer-id-to-datastore-key.ts +2 -3
  34. package/src/utils/to-peer-pb.ts +24 -16
  35. package/dist/src/errors.d.ts +0 -4
  36. package/dist/src/errors.d.ts.map +0 -1
  37. package/dist/src/errors.js +0 -4
  38. package/dist/src/errors.js.map +0 -1
  39. package/dist/typedoc-urls.json +0 -10
  40. package/src/errors.ts +0 -3
@@ -1,4 +1,5 @@
1
- import { peerIdFromPeerId } from '@libp2p/peer-id'
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
- peerId = peerIdFromPeerId({
11
- ...peerId,
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 { CodeError } from '@libp2p/interface'
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 CodeError('Multiaddr was invalid', codes.ERR_INVALID_PARAMETERS)
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 { CodeError } from '@libp2p/interface'
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 CodeError('Invalid PeerData', codes.ERR_INVALID_PARAMETERS)
9
+ throw new InvalidParametersError('Invalid PeerData')
11
10
  }
12
11
 
13
- if (data.publicKey != null && peerId.publicKey != null && !uint8arrayEquals(data.publicKey, peerId.publicKey)) {
14
- throw new CodeError('publicKey bytes do not match peer id publicKey bytes', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Invalid mulitaddr', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Peer metadata keys must be strings', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Peer metadata values must be Uint8Arrays', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Peer tag keys must be strings', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Tag value must be between 0-100', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Tag value must be an integer', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Tag ttl must be between greater than 0', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Tag ttl must be an integer', codes.ERR_INVALID_PARAMETERS)
101
+ throw new InvalidParametersError('Tag ttl must be an integer')
103
102
  }
104
103
  }
105
104
 
@@ -1,13 +1,12 @@
1
- import { CodeError } from '@libp2p/interface'
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 CodeError('Invalid PeerId', codes.ERR_INVALID_PARAMETERS)
9
+ throw new InvalidParametersError('Invalid PeerId')
11
10
  }
12
11
 
13
12
  const b32key = peerId.toCID().toString()
@@ -1,6 +1,5 @@
1
- import { CodeError } from '@libp2p/interface'
2
- import { equals as uint8arrayEquals } from 'uint8arrays/equals'
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 CodeError('Invalid PeerData', codes.ERR_INVALID_PARAMETERS)
15
+ throw new InvalidParametersError('Invalid PeerData')
17
16
  }
18
17
 
19
- if (data.publicKey != null && peerId.publicKey != null && !uint8arrayEquals(data.publicKey, peerId.publicKey)) {
20
- throw new CodeError('publicKey bytes do not match peer id publicKey bytes', codes.ERR_INVALID_PARAMETERS)
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 CodeError('peer id did not match existing peer id', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Metadata key must be a string', codes.ERR_INVALID_PARAMETERS)
195
+ throw new InvalidParametersError('Metadata key must be a string')
188
196
  }
189
197
 
190
198
  if (!(value instanceof Uint8Array)) {
191
- throw new CodeError('Metadata value must be a Uint8Array', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Tag name must be a string', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Tag value must be an integer', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Tag value must be between 0-100', codes.ERR_INVALID_PARAMETERS)
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 CodeError('Tag ttl must be an integer', codes.ERR_INVALID_PARAMETERS)
220
+ throw new InvalidParametersError('Tag ttl must be an integer')
213
221
  }
214
222
 
215
223
  if (tag.ttl < 0) {
216
- throw new CodeError('Tag ttl must be between greater than 0', codes.ERR_INVALID_PARAMETERS)
224
+ throw new InvalidParametersError('Tag ttl must be between greater than 0')
217
225
  }
218
226
  }
219
227
  }
@@ -1,4 +0,0 @@
1
- export declare const codes: {
2
- ERR_INVALID_PARAMETERS: string;
3
- };
4
- //# sourceMappingURL=errors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK;;CAEjB,CAAA"}
@@ -1,4 +0,0 @@
1
- export const codes = {
2
- ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS'
3
- };
4
- //# sourceMappingURL=errors.js.map
@@ -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"}
@@ -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
@@ -1,3 +0,0 @@
1
- export const codes = {
2
- ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS'
3
- }