@libp2p/kad-dht 9.3.6-3dfc236e → 9.3.6-6fdaa7dc

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 (137) hide show
  1. package/dist/index.min.js +19 -19
  2. package/dist/src/content-fetching/index.d.ts +2 -2
  3. package/dist/src/content-fetching/index.d.ts.map +1 -1
  4. package/dist/src/content-fetching/index.js +4 -4
  5. package/dist/src/content-fetching/index.js.map +1 -1
  6. package/dist/src/dual-kad-dht.d.ts +5 -5
  7. package/dist/src/dual-kad-dht.d.ts.map +1 -1
  8. package/dist/src/dual-kad-dht.js +5 -5
  9. package/dist/src/dual-kad-dht.js.map +1 -1
  10. package/dist/src/index.d.ts +9 -10
  11. package/dist/src/index.d.ts.map +1 -1
  12. package/dist/src/index.js.map +1 -1
  13. package/dist/src/kad-dht.d.ts +4 -4
  14. package/dist/src/kad-dht.d.ts.map +1 -1
  15. package/dist/src/kad-dht.js +3 -3
  16. package/dist/src/kad-dht.js.map +1 -1
  17. package/dist/src/message/index.d.ts +2 -2
  18. package/dist/src/message/index.d.ts.map +1 -1
  19. package/dist/src/message/index.js +1 -1
  20. package/dist/src/message/index.js.map +1 -1
  21. package/dist/src/network.d.ts +5 -5
  22. package/dist/src/network.d.ts.map +1 -1
  23. package/dist/src/network.js +2 -2
  24. package/dist/src/network.js.map +1 -1
  25. package/dist/src/peer-list/index.d.ts +1 -1
  26. package/dist/src/peer-list/peer-distance-list.d.ts +1 -1
  27. package/dist/src/peer-routing/index.d.ts +3 -3
  28. package/dist/src/peer-routing/index.d.ts.map +1 -1
  29. package/dist/src/peer-routing/index.js +3 -3
  30. package/dist/src/peer-routing/index.js.map +1 -1
  31. package/dist/src/providers.d.ts +2 -2
  32. package/dist/src/providers.d.ts.map +1 -1
  33. package/dist/src/query/events.d.ts +3 -3
  34. package/dist/src/query/events.d.ts.map +1 -1
  35. package/dist/src/query/events.js +1 -1
  36. package/dist/src/query/events.js.map +1 -1
  37. package/dist/src/query/manager.d.ts +3 -3
  38. package/dist/src/query/manager.d.ts.map +1 -1
  39. package/dist/src/query/manager.js +2 -2
  40. package/dist/src/query/manager.js.map +1 -1
  41. package/dist/src/query/query-path.d.ts +2 -2
  42. package/dist/src/query/query-path.d.ts.map +1 -1
  43. package/dist/src/query/query-path.js +1 -1
  44. package/dist/src/query/query-path.js.map +1 -1
  45. package/dist/src/query/types.d.ts +1 -1
  46. package/dist/src/query-self.d.ts +2 -2
  47. package/dist/src/query-self.d.ts.map +1 -1
  48. package/dist/src/record/index.d.ts +22 -0
  49. package/dist/src/record/index.d.ts.map +1 -0
  50. package/dist/src/record/index.js +53 -0
  51. package/dist/src/record/index.js.map +1 -0
  52. package/dist/src/record/record.d.ts +13 -0
  53. package/dist/src/record/record.d.ts.map +1 -0
  54. package/dist/src/record/record.js +67 -0
  55. package/dist/src/record/record.js.map +1 -0
  56. package/dist/src/record/selectors.d.ts +7 -0
  57. package/dist/src/record/selectors.d.ts.map +1 -0
  58. package/dist/src/record/selectors.js +38 -0
  59. package/dist/src/record/selectors.js.map +1 -0
  60. package/dist/src/record/utils.d.ts +11 -0
  61. package/dist/src/record/utils.d.ts.map +1 -0
  62. package/dist/src/record/utils.js +41 -0
  63. package/dist/src/record/utils.js.map +1 -0
  64. package/dist/src/record/validators.d.ts +10 -0
  65. package/dist/src/record/validators.d.ts.map +1 -0
  66. package/dist/src/record/validators.js +54 -0
  67. package/dist/src/record/validators.js.map +1 -0
  68. package/dist/src/routing-table/index.d.ts +6 -6
  69. package/dist/src/routing-table/index.d.ts.map +1 -1
  70. package/dist/src/routing-table/index.js +1 -1
  71. package/dist/src/routing-table/index.js.map +1 -1
  72. package/dist/src/routing-table/k-bucket.d.ts +2 -2
  73. package/dist/src/routing-table/k-bucket.d.ts.map +1 -1
  74. package/dist/src/routing-table/k-bucket.js +1 -1
  75. package/dist/src/routing-table/k-bucket.js.map +1 -1
  76. package/dist/src/routing-table/refresh.d.ts +1 -1
  77. package/dist/src/rpc/handlers/add-provider.d.ts +1 -1
  78. package/dist/src/rpc/handlers/add-provider.js +1 -1
  79. package/dist/src/rpc/handlers/add-provider.js.map +1 -1
  80. package/dist/src/rpc/handlers/find-node.d.ts +2 -2
  81. package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
  82. package/dist/src/rpc/handlers/get-providers.d.ts +3 -3
  83. package/dist/src/rpc/handlers/get-providers.js +1 -1
  84. package/dist/src/rpc/handlers/get-providers.js.map +1 -1
  85. package/dist/src/rpc/handlers/get-value.d.ts +3 -3
  86. package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
  87. package/dist/src/rpc/handlers/get-value.js +2 -2
  88. package/dist/src/rpc/handlers/get-value.js.map +1 -1
  89. package/dist/src/rpc/handlers/ping.d.ts +1 -1
  90. package/dist/src/rpc/handlers/put-value.d.ts +1 -1
  91. package/dist/src/rpc/handlers/put-value.js +2 -2
  92. package/dist/src/rpc/handlers/put-value.js.map +1 -1
  93. package/dist/src/rpc/index.d.ts +2 -2
  94. package/dist/src/rpc/index.d.ts.map +1 -1
  95. package/dist/src/topology-listener.d.ts +3 -3
  96. package/dist/src/topology-listener.d.ts.map +1 -1
  97. package/dist/src/topology-listener.js +2 -4
  98. package/dist/src/topology-listener.js.map +1 -1
  99. package/dist/src/utils.d.ts +2 -2
  100. package/dist/src/utils.d.ts.map +1 -1
  101. package/dist/src/utils.js +1 -1
  102. package/dist/src/utils.js.map +1 -1
  103. package/package.json +16 -29
  104. package/src/content-fetching/index.ts +5 -5
  105. package/src/content-routing/index.ts +1 -1
  106. package/src/dual-kad-dht.ts +7 -7
  107. package/src/index.ts +9 -10
  108. package/src/kad-dht.ts +6 -6
  109. package/src/message/index.ts +2 -2
  110. package/src/network.ts +7 -7
  111. package/src/peer-list/index.ts +1 -1
  112. package/src/peer-list/peer-distance-list.ts +1 -1
  113. package/src/peer-routing/index.ts +6 -6
  114. package/src/providers.ts +2 -2
  115. package/src/query/events.ts +4 -4
  116. package/src/query/manager.ts +5 -5
  117. package/src/query/query-path.ts +3 -3
  118. package/src/query/types.ts +1 -1
  119. package/src/query-self.ts +2 -2
  120. package/src/record/index.ts +70 -0
  121. package/src/record/record.proto +20 -0
  122. package/src/record/record.ts +87 -0
  123. package/src/record/selectors.ts +50 -0
  124. package/src/record/utils.ts +46 -0
  125. package/src/record/validators.ts +69 -0
  126. package/src/routing-table/index.ts +6 -6
  127. package/src/routing-table/k-bucket.ts +2 -2
  128. package/src/routing-table/refresh.ts +1 -1
  129. package/src/rpc/handlers/add-provider.ts +2 -2
  130. package/src/rpc/handlers/find-node.ts +3 -3
  131. package/src/rpc/handlers/get-providers.ts +4 -4
  132. package/src/rpc/handlers/get-value.ts +4 -4
  133. package/src/rpc/handlers/ping.ts +1 -1
  134. package/src/rpc/handlers/put-value.ts +3 -3
  135. package/src/rpc/index.ts +2 -2
  136. package/src/topology-listener.ts +4 -6
  137. package/src/utils.ts +3 -3
package/src/index.ts CHANGED
@@ -1,15 +1,14 @@
1
1
  import { DefaultDualKadDHT } from './dual-kad-dht.js'
2
2
  import type { ProvidersInit } from './providers.js'
3
- import type { AddressManager } from '@libp2p/interface-address-manager'
4
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
5
- import type { Libp2pEvents } from '@libp2p/interface-libp2p'
6
- import type { Metrics } from '@libp2p/interface-metrics'
7
- import type { PeerId } from '@libp2p/interface-peer-id'
8
- import type { PeerInfo } from '@libp2p/interface-peer-info'
9
- import type { PeerStore } from '@libp2p/interface-peer-store'
10
- import type { Registrar } from '@libp2p/interface-registrar'
11
- import type { AbortOptions } from '@libp2p/interfaces'
12
- import type { EventEmitter } from '@libp2p/interfaces/events'
3
+ import type { Libp2pEvents, AbortOptions } from '@libp2p/interface'
4
+ import type { EventEmitter } from '@libp2p/interface/events'
5
+ import type { Metrics } from '@libp2p/interface/metrics'
6
+ import type { PeerId } from '@libp2p/interface/peer-id'
7
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
8
+ import type { PeerStore } from '@libp2p/interface/peer-store'
9
+ import type { AddressManager } from '@libp2p/interface-internal/address-manager'
10
+ import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
11
+ import type { Registrar } from '@libp2p/interface-internal/registrar'
13
12
  import type { Datastore } from 'interface-datastore'
14
13
  import type { CID } from 'multiformats/cid'
15
14
  import type { ProgressOptions, ProgressEvent } from 'progress-events'
package/src/kad-dht.ts CHANGED
@@ -1,7 +1,5 @@
1
- import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
1
+ import { CustomEvent, EventEmitter } from '@libp2p/interface/events'
2
2
  import { type Logger, logger } from '@libp2p/logger'
3
- import { selectors as recordSelectors } from '@libp2p/record/selectors'
4
- import { validators as recordValidators } from '@libp2p/record/validators'
5
3
  import pDefer from 'p-defer'
6
4
  import { PROTOCOL_DHT, PROTOCOL_PREFIX, LAN_PREFIX } from './constants.js'
7
5
  import { ContentFetching } from './content-fetching/index.js'
@@ -11,6 +9,8 @@ import { PeerRouting } from './peer-routing/index.js'
11
9
  import { Providers } from './providers.js'
12
10
  import { QueryManager } from './query/manager.js'
13
11
  import { QuerySelf } from './query-self.js'
12
+ import { selectors as recordSelectors } from './record/selectors.js'
13
+ import { validators as recordValidators } from './record/validators.js'
14
14
  import { RoutingTable } from './routing-table/index.js'
15
15
  import { RoutingTableRefresh } from './routing-table/refresh.js'
16
16
  import { RPC } from './rpc/index.js'
@@ -20,9 +20,9 @@ import {
20
20
  removePublicAddresses
21
21
  } from './utils.js'
22
22
  import type { KadDHTComponents, KadDHTInit, QueryOptions, Validators, Selectors, KadDHT, QueryEvent } from './index.js'
23
- import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
24
- import type { PeerId } from '@libp2p/interface-peer-id'
25
- import type { PeerInfo } from '@libp2p/interface-peer-info'
23
+ import type { PeerDiscoveryEvents } from '@libp2p/interface/peer-discovery'
24
+ import type { PeerId } from '@libp2p/interface/peer-id'
25
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
26
26
  import type { CID } from 'multiformats/cid'
27
27
 
28
28
  export const DEFAULT_MAX_INBOUND_STREAMS = 32
@@ -1,8 +1,8 @@
1
1
  import { peerIdFromBytes } from '@libp2p/peer-id'
2
- import { Libp2pRecord } from '@libp2p/record'
3
2
  import { multiaddr } from '@multiformats/multiaddr'
3
+ import { Libp2pRecord } from '../record/index.js'
4
4
  import { Message as PBMessage } from './dht.js'
5
- import type { PeerInfo } from '@libp2p/interface-peer-info'
5
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
6
6
  import type { Uint8ArrayList } from 'uint8arraylist'
7
7
 
8
8
  export const MESSAGE_TYPE = PBMessage.MessageType
package/src/network.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { CodeError } from '@libp2p/interfaces/errors'
2
- import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
1
+ import { CodeError } from '@libp2p/interface/errors'
2
+ import { EventEmitter, CustomEvent } from '@libp2p/interface/events'
3
3
  import { logger } from '@libp2p/logger'
4
4
  import { abortableDuplex } from 'abortable-iterator'
5
5
  import drain from 'it-drain'
@@ -14,11 +14,11 @@ import {
14
14
  queryErrorEvent
15
15
  } from './query/events.js'
16
16
  import type { KadDHTComponents, QueryEvent, QueryOptions } from './index.js'
17
- import type { Stream } from '@libp2p/interface-connection'
18
- import type { PeerId } from '@libp2p/interface-peer-id'
19
- import type { PeerInfo } from '@libp2p/interface-peer-info'
20
- import type { AbortOptions } from '@libp2p/interfaces'
21
- import type { Startable } from '@libp2p/interfaces/startable'
17
+ import type { AbortOptions } from '@libp2p/interface'
18
+ import type { Stream } from '@libp2p/interface/connection'
19
+ import type { PeerId } from '@libp2p/interface/peer-id'
20
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
21
+ import type { Startable } from '@libp2p/interface/startable'
22
22
  import type { Logger } from '@libp2p/logger'
23
23
  import type { Duplex, Source } from 'it-stream-types'
24
24
  import type { Uint8ArrayList } from 'uint8arraylist'
@@ -1,4 +1,4 @@
1
- import type { PeerId } from '@libp2p/interface-peer-id'
1
+ import type { PeerId } from '@libp2p/interface/peer-id'
2
2
 
3
3
  /**
4
4
  * A list of unique peers.
@@ -1,7 +1,7 @@
1
1
  import { compare as uint8ArrayCompare } from 'uint8arrays/compare'
2
2
  import { xor as uint8ArrayXor } from 'uint8arrays/xor'
3
3
  import * as utils from '../utils.js'
4
- import type { PeerId } from '@libp2p/interface-peer-id'
4
+ import type { PeerId } from '@libp2p/interface/peer-id'
5
5
 
6
6
  interface PeerDistance {
7
7
  peerId: PeerId
@@ -1,9 +1,7 @@
1
1
  import { keys } from '@libp2p/crypto'
2
- import { CodeError } from '@libp2p/interfaces/errors'
2
+ import { CodeError } from '@libp2p/interface/errors'
3
3
  import { logger } from '@libp2p/logger'
4
4
  import { peerIdFromKeys } from '@libp2p/peer-id'
5
- import { Libp2pRecord } from '@libp2p/record'
6
- import { verifyRecord } from '@libp2p/record/validators'
7
5
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8
6
  import { Message, MESSAGE_TYPE } from '../message/index.js'
9
7
  import { PeerDistanceList } from '../peer-list/peer-distance-list.js'
@@ -12,15 +10,17 @@ import {
12
10
  finalPeerEvent,
13
11
  valueEvent
14
12
  } from '../query/events.js'
13
+ import { Libp2pRecord } from '../record/index.js'
14
+ import { verifyRecord } from '../record/validators.js'
15
15
  import * as utils from '../utils.js'
16
16
  import type { KadDHTComponents, DHTRecord, DialPeerEvent, FinalPeerEvent, QueryEvent, Validators } from '../index.js'
17
17
  import type { Network } from '../network.js'
18
18
  import type { QueryManager, QueryOptions } from '../query/manager.js'
19
19
  import type { QueryFunc } from '../query/types.js'
20
20
  import type { RoutingTable } from '../routing-table/index.js'
21
- import type { PeerId } from '@libp2p/interface-peer-id'
22
- import type { PeerInfo } from '@libp2p/interface-peer-info'
23
- import type { AbortOptions } from '@libp2p/interfaces'
21
+ import type { AbortOptions } from '@libp2p/interface'
22
+ import type { PeerId } from '@libp2p/interface/peer-id'
23
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
24
24
  import type { Logger } from '@libp2p/logger'
25
25
 
26
26
  export interface PeerRoutingInit {
package/src/providers.ts CHANGED
@@ -11,8 +11,8 @@ import {
11
11
  PROVIDERS_LRU_CACHE_SIZE,
12
12
  PROVIDER_KEY_PREFIX
13
13
  } from './constants.js'
14
- import type { PeerId } from '@libp2p/interface-peer-id'
15
- import type { Startable } from '@libp2p/interfaces/startable'
14
+ import type { PeerId } from '@libp2p/interface/peer-id'
15
+ import type { Startable } from '@libp2p/interface/startable'
16
16
  import type { Datastore } from 'interface-datastore'
17
17
  import type { CID } from 'multiformats'
18
18
 
@@ -1,10 +1,10 @@
1
- import { CustomEvent } from '@libp2p/interfaces/events'
1
+ import { CustomEvent } from '@libp2p/interface/events'
2
2
  import { MESSAGE_TYPE_LOOKUP } from '../message/index.js'
3
3
  import type { SendQueryEvent, PeerResponseEvent, DialPeerEvent, AddPeerEvent, ValueEvent, ProviderEvent, QueryErrorEvent, FinalPeerEvent, QueryOptions } from '../index.js'
4
4
  import type { Message } from '../message/dht.js'
5
- import type { PeerId } from '@libp2p/interface-peer-id'
6
- import type { PeerInfo } from '@libp2p/interface-peer-info'
7
- import type { Libp2pRecord } from '@libp2p/record'
5
+ import type { Libp2pRecord } from '../record/index.js'
6
+ import type { PeerId } from '@libp2p/interface/peer-id'
7
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
8
8
 
9
9
  export interface QueryEventFields {
10
10
  to: PeerId
@@ -1,6 +1,6 @@
1
1
  import { setMaxListeners } from 'events'
2
- import { AbortError } from '@libp2p/interfaces/errors'
3
- import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
2
+ import { AbortError } from '@libp2p/interface/errors'
3
+ import { EventEmitter, CustomEvent } from '@libp2p/interface/events'
4
4
  import { logger } from '@libp2p/logger'
5
5
  import { PeerSet } from '@libp2p/peer-collections'
6
6
  import { anySignal } from 'any-signal'
@@ -14,9 +14,9 @@ import { queryPath } from './query-path.js'
14
14
  import type { QueryFunc } from './types.js'
15
15
  import type { QueryEvent, QueryOptions as RootQueryOptions } from '../index.js'
16
16
  import type { RoutingTable } from '../routing-table/index.js'
17
- import type { Metric, Metrics } from '@libp2p/interface-metrics'
18
- import type { PeerId } from '@libp2p/interface-peer-id'
19
- import type { Startable } from '@libp2p/interfaces/startable'
17
+ import type { Metric, Metrics } from '@libp2p/interface/metrics'
18
+ import type { PeerId } from '@libp2p/interface/peer-id'
19
+ import type { Startable } from '@libp2p/interface/startable'
20
20
  import type { DeferredPromise } from 'p-defer'
21
21
 
22
22
  export interface CleanUpEvents {
@@ -1,4 +1,4 @@
1
- import { CodeError } from '@libp2p/interfaces/errors'
1
+ import { CodeError } from '@libp2p/interface/errors'
2
2
  import { anySignal } from 'any-signal'
3
3
  import defer from 'p-defer'
4
4
  import Queue from 'p-queue'
@@ -9,8 +9,8 @@ import { queryErrorEvent } from './events.js'
9
9
  import type { CleanUpEvents } from './manager.js'
10
10
  import type { QueryEvent, QueryOptions } from '../index.js'
11
11
  import type { QueryFunc } from '../query/types.js'
12
- import type { PeerId } from '@libp2p/interface-peer-id'
13
- import type { EventEmitter } from '@libp2p/interfaces/events'
12
+ import type { EventEmitter } from '@libp2p/interface/events'
13
+ import type { PeerId } from '@libp2p/interface/peer-id'
14
14
  import type { Logger } from '@libp2p/logger'
15
15
  import type { PeerSet } from '@libp2p/peer-collections'
16
16
 
@@ -1,5 +1,5 @@
1
1
  import type { QueryEvent } from '../index.js'
2
- import type { PeerId } from '@libp2p/interface-peer-id'
2
+ import type { PeerId } from '@libp2p/interface/peer-id'
3
3
 
4
4
  export interface QueryContext {
5
5
  // the key we are looking up
package/src/query-self.ts CHANGED
@@ -9,8 +9,8 @@ import { pEvent } from 'p-event'
9
9
  import { QUERY_SELF_INTERVAL, QUERY_SELF_TIMEOUT, K, QUERY_SELF_INITIAL_INTERVAL } from './constants.js'
10
10
  import type { PeerRouting } from './peer-routing/index.js'
11
11
  import type { RoutingTable } from './routing-table/index.js'
12
- import type { PeerId } from '@libp2p/interface-peer-id'
13
- import type { Startable } from '@libp2p/interfaces/startable'
12
+ import type { PeerId } from '@libp2p/interface/peer-id'
13
+ import type { Startable } from '@libp2p/interface/startable'
14
14
  import type { DeferredPromise } from 'p-defer'
15
15
 
16
16
  export interface QuerySelfInit {
@@ -0,0 +1,70 @@
1
+ import {
2
+ Record
3
+ } from './record.js'
4
+ import * as utils from './utils.js'
5
+ import type { Uint8ArrayList } from 'uint8arraylist'
6
+
7
+ export class Libp2pRecord {
8
+ public key: Uint8Array
9
+ public value: Uint8Array
10
+ public timeReceived: Date
11
+
12
+ constructor (key: Uint8Array, value: Uint8Array, timeReceived: Date) {
13
+ if (!(key instanceof Uint8Array)) {
14
+ throw new Error('key must be a Uint8Array')
15
+ }
16
+
17
+ if (!(value instanceof Uint8Array)) {
18
+ throw new Error('value must be a Uint8Array')
19
+ }
20
+
21
+ this.key = key
22
+ this.value = value
23
+ this.timeReceived = timeReceived
24
+ }
25
+
26
+ serialize (): Uint8Array {
27
+ return Record.encode(this.prepareSerialize())
28
+ }
29
+
30
+ /**
31
+ * Return the object format ready to be given to the protobuf library.
32
+ */
33
+ prepareSerialize (): Record {
34
+ return {
35
+ key: this.key,
36
+ value: this.value,
37
+ timeReceived: utils.toRFC3339(this.timeReceived)
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Decode a protobuf encoded record
43
+ */
44
+ static deserialize (raw: Uint8Array | Uint8ArrayList): Libp2pRecord {
45
+ const rec = Record.decode(raw)
46
+
47
+ return new Libp2pRecord(rec.key, rec.value, new Date(rec.timeReceived))
48
+ }
49
+
50
+ /**
51
+ * Create a record from the raw object returned from the protobuf library
52
+ */
53
+ static fromDeserialized (obj: Record): Libp2pRecord {
54
+ const recvtime = utils.parseRFC3339(obj.timeReceived)
55
+
56
+ if (obj.key == null) {
57
+ throw new Error('key missing from deserialized object')
58
+ }
59
+
60
+ if (obj.value == null) {
61
+ throw new Error('value missing from deserialized object')
62
+ }
63
+
64
+ const rec = new Libp2pRecord(
65
+ obj.key, obj.value, recvtime
66
+ )
67
+
68
+ return rec
69
+ }
70
+ }
@@ -0,0 +1,20 @@
1
+ syntax = "proto3";
2
+
3
+ // Record represents a dht record that contains a value
4
+ // for a key value pair
5
+ message Record {
6
+ // The key that references this record
7
+ bytes key = 1;
8
+
9
+ // The actual value this record is storing
10
+ bytes value = 2;
11
+
12
+ // Note: These fields were removed from the Record message
13
+ // hash of the authors public key
14
+ // optional bytes author = 3;
15
+ // A PKI signature for the key+value+author
16
+ // optional bytes signature = 4;
17
+
18
+ // Time the record was received, set by receiver
19
+ string timeReceived = 5;
20
+ }
@@ -0,0 +1,87 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable complexity */
3
+ /* eslint-disable @typescript-eslint/no-namespace */
4
+ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
6
+
7
+ import { encodeMessage, decodeMessage, message } from 'protons-runtime'
8
+ import type { Codec } from 'protons-runtime'
9
+ import type { Uint8ArrayList } from 'uint8arraylist'
10
+
11
+ export interface Record {
12
+ key: Uint8Array
13
+ value: Uint8Array
14
+ timeReceived: string
15
+ }
16
+
17
+ export namespace Record {
18
+ let _codec: Codec<Record>
19
+
20
+ export const codec = (): Codec<Record> => {
21
+ if (_codec == null) {
22
+ _codec = message<Record>((obj, w, opts = {}) => {
23
+ if (opts.lengthDelimited !== false) {
24
+ w.fork()
25
+ }
26
+
27
+ if ((obj.key != null && obj.key.byteLength > 0)) {
28
+ w.uint32(10)
29
+ w.bytes(obj.key)
30
+ }
31
+
32
+ if ((obj.value != null && obj.value.byteLength > 0)) {
33
+ w.uint32(18)
34
+ w.bytes(obj.value)
35
+ }
36
+
37
+ if ((obj.timeReceived != null && obj.timeReceived !== '')) {
38
+ w.uint32(42)
39
+ w.string(obj.timeReceived)
40
+ }
41
+
42
+ if (opts.lengthDelimited !== false) {
43
+ w.ldelim()
44
+ }
45
+ }, (reader, length) => {
46
+ const obj: any = {
47
+ key: new Uint8Array(0),
48
+ value: new Uint8Array(0),
49
+ timeReceived: ''
50
+ }
51
+
52
+ const end = length == null ? reader.len : reader.pos + length
53
+
54
+ while (reader.pos < end) {
55
+ const tag = reader.uint32()
56
+
57
+ switch (tag >>> 3) {
58
+ case 1:
59
+ obj.key = reader.bytes()
60
+ break
61
+ case 2:
62
+ obj.value = reader.bytes()
63
+ break
64
+ case 5:
65
+ obj.timeReceived = reader.string()
66
+ break
67
+ default:
68
+ reader.skipType(tag & 7)
69
+ break
70
+ }
71
+ }
72
+
73
+ return obj
74
+ })
75
+ }
76
+
77
+ return _codec
78
+ }
79
+
80
+ export const encode = (obj: Partial<Record>): Uint8Array => {
81
+ return encodeMessage(obj, Record.codec())
82
+ }
83
+
84
+ export const decode = (buf: Uint8Array | Uint8ArrayList): Record => {
85
+ return decodeMessage(buf, Record.codec())
86
+ }
87
+ }
@@ -0,0 +1,50 @@
1
+ import { CodeError } from '@libp2p/interface/errors'
2
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
3
+ import type { Selectors } from '../index.js'
4
+
5
+ /**
6
+ * Select the best record out of the given records
7
+ */
8
+ export function bestRecord (selectors: Selectors, k: Uint8Array, records: Uint8Array[]): number {
9
+ if (records.length === 0) {
10
+ const errMsg = 'No records given'
11
+
12
+ throw new CodeError(errMsg, 'ERR_NO_RECORDS_RECEIVED')
13
+ }
14
+
15
+ const kStr = uint8ArrayToString(k)
16
+ const parts = kStr.split('/')
17
+
18
+ if (parts.length < 3) {
19
+ const errMsg = 'Record key does not have a selector function'
20
+
21
+ throw new CodeError(errMsg, 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY')
22
+ }
23
+
24
+ const selector = selectors[parts[1].toString()]
25
+
26
+ if (selector == null) {
27
+ const errMsg = `Unrecognized key prefix: ${parts[1]}`
28
+
29
+ throw new CodeError(errMsg, 'ERR_UNRECOGNIZED_KEY_PREFIX')
30
+ }
31
+
32
+ if (records.length === 1) {
33
+ return 0
34
+ }
35
+
36
+ return selector(k, records)
37
+ }
38
+
39
+ /**
40
+ * Best record selector, for public key records.
41
+ * Simply returns the first record, as all valid public key
42
+ * records are equal
43
+ */
44
+ function publickKey (k: Uint8Array, records: Uint8Array[]): number {
45
+ return 0
46
+ }
47
+
48
+ export const selectors: Selectors = {
49
+ pk: publickKey
50
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Convert a JavaScript date into an `RFC3339Nano` formatted
3
+ * string
4
+ */
5
+ export function toRFC3339 (time: Date): string {
6
+ const year = time.getUTCFullYear()
7
+ const month = String(time.getUTCMonth() + 1).padStart(2, '0')
8
+ const day = String(time.getUTCDate()).padStart(2, '0')
9
+ const hour = String(time.getUTCHours()).padStart(2, '0')
10
+ const minute = String(time.getUTCMinutes()).padStart(2, '0')
11
+ const seconds = String(time.getUTCSeconds()).padStart(2, '0')
12
+ const milliseconds = time.getUTCMilliseconds()
13
+ const nanoseconds = String(milliseconds * 1000 * 1000).padStart(9, '0')
14
+
15
+ return `${year}-${month}-${day}T${hour}:${minute}:${seconds}.${nanoseconds}Z`
16
+ }
17
+
18
+ /**
19
+ * Parses a date string formatted as `RFC3339Nano` into a
20
+ * JavaScript Date object
21
+ */
22
+ export function parseRFC3339 (time: string): Date {
23
+ const rfc3339Matcher = new RegExp(
24
+ // 2006-01-02T
25
+ '(\\d{4})-(\\d{2})-(\\d{2})T' +
26
+ // 15:04:05
27
+ '(\\d{2}):(\\d{2}):(\\d{2})' +
28
+ // .999999999Z
29
+ '\\.(\\d+)Z'
30
+ )
31
+ const m = String(time).trim().match(rfc3339Matcher)
32
+
33
+ if (m == null) {
34
+ throw new Error('Invalid format')
35
+ }
36
+
37
+ const year = parseInt(m[1], 10)
38
+ const month = parseInt(m[2], 10) - 1
39
+ const date = parseInt(m[3], 10)
40
+ const hour = parseInt(m[4], 10)
41
+ const minute = parseInt(m[5], 10)
42
+ const second = parseInt(m[6], 10)
43
+ const millisecond = parseInt(m[7].slice(0, -6), 10)
44
+
45
+ return new Date(Date.UTC(year, month, date, hour, minute, second, millisecond))
46
+ }
@@ -0,0 +1,69 @@
1
+ import { CodeError } from '@libp2p/interface/errors'
2
+ import { sha256 } from 'multiformats/hashes/sha2'
3
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
4
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
5
+ import type { Libp2pRecord } from './index.js'
6
+ import type { Validators } from '../index.js'
7
+
8
+ /**
9
+ * Checks a record and ensures it is still valid.
10
+ * It runs the needed validators.
11
+ * If verification fails the returned Promise will reject with the error.
12
+ */
13
+ export async function verifyRecord (validators: Validators, record: Libp2pRecord): Promise<void> {
14
+ const key = record.key
15
+ const keyString = uint8ArrayToString(key)
16
+ const parts = keyString.split('/')
17
+
18
+ if (parts.length < 3) {
19
+ // No validator available
20
+ return
21
+ }
22
+
23
+ const validator = validators[parts[1].toString()]
24
+
25
+ if (validator == null) {
26
+ const errMsg = 'Invalid record keytype'
27
+
28
+ throw new CodeError(errMsg, 'ERR_INVALID_RECORD_KEY_TYPE')
29
+ }
30
+
31
+ await validator(key, record.value)
32
+ }
33
+
34
+ /**
35
+ * Validator for public key records.
36
+ * Verifies that the passed in record value is the PublicKey
37
+ * that matches the passed in key.
38
+ * If validation fails the returned Promise will reject with the error.
39
+ *
40
+ * @param {Uint8Array} key - A valid key is of the form `'/pk/<keymultihash>'`
41
+ * @param {Uint8Array} publicKey - The public key to validate against (protobuf encoded).
42
+ */
43
+ const validatePublicKeyRecord = async (key: Uint8Array, publicKey: Uint8Array): Promise<void> => {
44
+ if (!(key instanceof Uint8Array)) {
45
+ throw new CodeError('"key" must be a Uint8Array', 'ERR_INVALID_RECORD_KEY_NOT_BUFFER')
46
+ }
47
+
48
+ if (key.byteLength < 5) {
49
+ throw new CodeError('invalid public key record', 'ERR_INVALID_RECORD_KEY_TOO_SHORT')
50
+ }
51
+
52
+ const prefix = uint8ArrayToString(key.subarray(0, 4))
53
+
54
+ if (prefix !== '/pk/') {
55
+ throw new CodeError('key was not prefixed with /pk/', 'ERR_INVALID_RECORD_KEY_BAD_PREFIX')
56
+ }
57
+
58
+ const keyhash = key.slice(4)
59
+
60
+ const publicKeyHash = await sha256.digest(publicKey)
61
+
62
+ if (!uint8ArrayEquals(keyhash, publicKeyHash.bytes)) {
63
+ throw new CodeError('public key does not match passed in key', 'ERR_INVALID_RECORD_HASH_MISMATCH')
64
+ }
65
+ }
66
+
67
+ export const validators: Validators = {
68
+ pk: validatePublicKeyRecord
69
+ }
@@ -1,14 +1,14 @@
1
- import { EventEmitter } from '@libp2p/interfaces/events'
1
+ import { EventEmitter } from '@libp2p/interface/events'
2
2
  import { logger } from '@libp2p/logger'
3
3
  import { PeerSet } from '@libp2p/peer-collections'
4
4
  import Queue from 'p-queue'
5
5
  import * as utils from '../utils.js'
6
6
  import { KBucket, type PingEventDetails } from './k-bucket.js'
7
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
8
- import type { Metric, Metrics } from '@libp2p/interface-metrics'
9
- import type { PeerId } from '@libp2p/interface-peer-id'
10
- import type { PeerStore } from '@libp2p/interface-peer-store'
11
- import type { Startable } from '@libp2p/interfaces/startable'
7
+ import type { Metric, Metrics } from '@libp2p/interface/metrics'
8
+ import type { PeerId } from '@libp2p/interface/peer-id'
9
+ import type { PeerStore } from '@libp2p/interface/peer-store'
10
+ import type { Startable } from '@libp2p/interface/startable'
11
+ import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
12
12
  import type { Logger } from '@libp2p/logger'
13
13
 
14
14
  export const KAD_CLOSE_TAG_NAME = 'kad-close'
@@ -27,8 +27,8 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27
27
  OTHER DEALINGS IN THE SOFTWARE.
28
28
  */
29
29
 
30
- import { EventEmitter } from '@libp2p/interfaces/events'
31
- import type { PeerId } from '@libp2p/interface-peer-id'
30
+ import { EventEmitter } from '@libp2p/interface/events'
31
+ import type { PeerId } from '@libp2p/interface/peer-id'
32
32
 
33
33
  function arrayEquals (array1: Uint8Array, array2: Uint8Array): boolean {
34
34
  if (array1 === array2) {
@@ -8,7 +8,7 @@ import { TABLE_REFRESH_INTERVAL, TABLE_REFRESH_QUERY_TIMEOUT } from '../constant
8
8
  import GENERATED_PREFIXES from './generated-prefix-list.js'
9
9
  import type { RoutingTable } from './index.js'
10
10
  import type { PeerRouting } from '../peer-routing/index.js'
11
- import type { PeerId } from '@libp2p/interface-peer-id'
11
+ import type { PeerId } from '@libp2p/interface/peer-id'
12
12
  import type { Logger } from '@libp2p/logger'
13
13
 
14
14
  /**
@@ -1,10 +1,10 @@
1
- import { CodeError } from '@libp2p/interfaces/errors'
1
+ import { CodeError } from '@libp2p/interface/errors'
2
2
  import { logger } from '@libp2p/logger'
3
3
  import { CID } from 'multiformats/cid'
4
4
  import type { Message } from '../../message/index.js'
5
5
  import type { Providers } from '../../providers'
6
6
  import type { DHTMessageHandler } from '../index.js'
7
- import type { PeerId } from '@libp2p/interface-peer-id'
7
+ import type { PeerId } from '@libp2p/interface/peer-id'
8
8
 
9
9
  const log = logger('libp2p:kad-dht:rpc:handlers:add-provider')
10
10
 
@@ -8,9 +8,9 @@ import {
8
8
  } from '../../utils.js'
9
9
  import type { PeerRouting } from '../../peer-routing/index.js'
10
10
  import type { DHTMessageHandler } from '../index.js'
11
- import type { AddressManager } from '@libp2p/interface-address-manager'
12
- import type { PeerId } from '@libp2p/interface-peer-id'
13
- import type { PeerInfo } from '@libp2p/interface-peer-info'
11
+ import type { PeerId } from '@libp2p/interface/peer-id'
12
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
13
+ import type { AddressManager } from '@libp2p/interface-internal/address-manager'
14
14
 
15
15
  const log = logger('libp2p:kad-dht:rpc:handlers:find-node')
16
16