libp2p 0.36.2 → 0.37.0

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 (430) hide show
  1. package/LICENSE +3 -21
  2. package/README.md +13 -10
  3. package/dist/src/address-manager/index.d.ts +34 -52
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/index.js +81 -0
  6. package/dist/src/address-manager/index.js.map +1 -0
  7. package/dist/src/circuit/auto-relay.d.ts +24 -85
  8. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  9. package/dist/src/circuit/auto-relay.js +216 -0
  10. package/dist/src/circuit/auto-relay.js.map +1 -0
  11. package/dist/src/circuit/circuit/hop.d.ts +26 -53
  12. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  13. package/dist/src/circuit/circuit/hop.js +138 -0
  14. package/dist/src/circuit/circuit/hop.js.map +1 -0
  15. package/dist/src/circuit/circuit/stop.d.ts +18 -10
  16. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  17. package/dist/src/circuit/circuit/stop.js +49 -0
  18. package/dist/src/circuit/circuit/stop.js.map +1 -0
  19. package/dist/src/circuit/circuit/stream-handler.d.ts +19 -38
  20. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  21. package/dist/src/circuit/circuit/stream-handler.js +59 -0
  22. package/dist/src/circuit/circuit/stream-handler.js.map +1 -0
  23. package/dist/src/circuit/circuit/utils.d.ts +3 -6
  24. package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
  25. package/dist/src/circuit/circuit/utils.js +43 -0
  26. package/dist/src/circuit/circuit/utils.js.map +1 -0
  27. package/dist/src/circuit/constants.d.ts +24 -6
  28. package/dist/src/circuit/constants.d.ts.map +1 -1
  29. package/dist/src/circuit/constants.js +26 -0
  30. package/dist/src/circuit/constants.js.map +1 -0
  31. package/dist/src/circuit/index.d.ts +41 -74
  32. package/dist/src/circuit/index.d.ts.map +1 -1
  33. package/dist/src/circuit/index.js +69 -0
  34. package/dist/src/circuit/index.js.map +1 -0
  35. package/dist/src/circuit/listener.d.ts +8 -3
  36. package/dist/src/circuit/listener.d.ts.map +1 -1
  37. package/dist/src/circuit/listener.js +62 -0
  38. package/dist/src/circuit/listener.js.map +1 -0
  39. package/dist/src/circuit/multicodec.d.ts +1 -1
  40. package/dist/src/circuit/multicodec.d.ts.map +1 -1
  41. package/dist/src/circuit/multicodec.js +2 -0
  42. package/dist/src/circuit/multicodec.js.map +1 -0
  43. package/dist/src/circuit/pb/index.d.ts +52 -0
  44. package/dist/src/circuit/pb/index.d.ts.map +1 -0
  45. package/dist/src/circuit/pb/index.js +98 -0
  46. package/dist/src/circuit/pb/index.js.map +1 -0
  47. package/dist/src/circuit/transport.d.ts +18 -78
  48. package/dist/src/circuit/transport.d.ts.map +1 -1
  49. package/dist/src/circuit/transport.js +192 -0
  50. package/dist/src/circuit/transport.js.map +1 -0
  51. package/dist/src/circuit/utils.d.ts +5 -2
  52. package/dist/src/circuit/utils.d.ts.map +1 -1
  53. package/dist/src/circuit/utils.js +11 -0
  54. package/dist/src/circuit/utils.js.map +1 -0
  55. package/dist/src/config.d.ts +3 -85
  56. package/dist/src/config.d.ts.map +1 -1
  57. package/dist/src/config.js +91 -0
  58. package/dist/src/config.js.map +1 -0
  59. package/dist/src/connection-manager/auto-dialler.d.ts +24 -41
  60. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  61. package/dist/src/connection-manager/auto-dialler.js +105 -0
  62. package/dist/src/connection-manager/auto-dialler.js.map +1 -0
  63. package/dist/src/connection-manager/dialer/auto-dialer.d.ts +16 -0
  64. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
  65. package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
  66. package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
  67. package/dist/src/connection-manager/dialer/dial-request.d.ts +27 -0
  68. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
  69. package/dist/src/connection-manager/dialer/dial-request.js +83 -0
  70. package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
  71. package/dist/src/connection-manager/dialer/index.d.ts +104 -0
  72. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
  73. package/dist/src/connection-manager/dialer/index.js +267 -0
  74. package/dist/src/connection-manager/dialer/index.js.map +1 -0
  75. package/dist/src/connection-manager/index.d.ts +102 -136
  76. package/dist/src/connection-manager/index.d.ts.map +1 -1
  77. package/dist/src/connection-manager/index.js +333 -0
  78. package/dist/src/connection-manager/index.js.map +1 -0
  79. package/dist/src/connection-manager/latency-monitor.d.ts +72 -97
  80. package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
  81. package/dist/src/connection-manager/latency-monitor.js +220 -0
  82. package/dist/src/connection-manager/latency-monitor.js.map +1 -0
  83. package/dist/src/connection-manager/visibility-change-emitter.d.ts +15 -16
  84. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
  85. package/dist/src/connection-manager/visibility-change-emitter.js +118 -0
  86. package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -0
  87. package/dist/src/constants.d.ts +22 -10
  88. package/dist/src/constants.d.ts.map +1 -1
  89. package/dist/src/constants.js +27 -0
  90. package/dist/src/constants.js.map +1 -0
  91. package/dist/src/content-routing/index.d.ts +27 -81
  92. package/dist/src/content-routing/index.d.ts.map +1 -1
  93. package/dist/src/content-routing/index.js +99 -0
  94. package/dist/src/content-routing/index.js.map +1 -0
  95. package/dist/src/content-routing/utils.d.ts +6 -49
  96. package/dist/src/content-routing/utils.d.ts.map +1 -1
  97. package/dist/src/content-routing/utils.js +42 -0
  98. package/dist/src/content-routing/utils.js.map +1 -0
  99. package/dist/src/dht/dht-content-routing.d.ts +10 -24
  100. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  101. package/dist/src/dht/dht-content-routing.js +32 -0
  102. package/dist/src/dht/dht-content-routing.js.map +1 -0
  103. package/dist/src/dht/dht-peer-routing.d.ts +9 -24
  104. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  105. package/dist/src/dht/dht-peer-routing.js +26 -0
  106. package/dist/src/dht/dht-peer-routing.js.map +1 -0
  107. package/dist/src/dht/dummy-dht.d.ts +20 -0
  108. package/dist/src/dht/dummy-dht.d.ts.map +1 -0
  109. package/dist/src/dht/dummy-dht.js +46 -0
  110. package/dist/src/dht/dummy-dht.js.map +1 -0
  111. package/dist/src/errors.d.ts +70 -63
  112. package/dist/src/errors.d.ts.map +1 -1
  113. package/dist/src/errors.js +75 -0
  114. package/dist/src/errors.js.map +1 -0
  115. package/dist/src/fetch/constants.d.ts +1 -1
  116. package/dist/src/fetch/constants.d.ts.map +1 -1
  117. package/dist/src/fetch/constants.js +3 -0
  118. package/dist/src/fetch/constants.js.map +1 -0
  119. package/dist/src/fetch/index.d.ts +30 -49
  120. package/dist/src/fetch/index.d.ts.map +1 -1
  121. package/dist/src/fetch/index.js +129 -0
  122. package/dist/src/fetch/index.js.map +1 -0
  123. package/dist/src/fetch/pb/proto.d.ts +27 -0
  124. package/dist/src/fetch/pb/proto.d.ts.map +1 -0
  125. package/dist/src/fetch/pb/proto.js +50 -0
  126. package/dist/src/fetch/pb/proto.js.map +1 -0
  127. package/dist/src/get-peer.d.ts +4 -11
  128. package/dist/src/get-peer.d.ts.map +1 -1
  129. package/dist/src/get-peer.js +43 -0
  130. package/dist/src/get-peer.js.map +1 -0
  131. package/dist/src/identify/consts.d.ts +9 -9
  132. package/dist/src/identify/consts.d.ts.map +1 -1
  133. package/dist/src/identify/consts.js +11 -0
  134. package/dist/src/identify/consts.js.map +1 -0
  135. package/dist/src/identify/index.d.ts +42 -96
  136. package/dist/src/identify/index.d.ts.map +1 -1
  137. package/dist/src/identify/index.js +322 -0
  138. package/dist/src/identify/index.js.map +1 -0
  139. package/dist/src/identify/pb/message.d.ts +16 -0
  140. package/dist/src/identify/pb/message.d.ts.map +1 -0
  141. package/dist/src/identify/pb/message.js +24 -0
  142. package/dist/src/identify/pb/message.js.map +1 -0
  143. package/dist/src/index.d.ts +150 -496
  144. package/dist/src/index.d.ts.map +1 -1
  145. package/dist/src/index.js +9 -0
  146. package/dist/src/index.js.map +1 -0
  147. package/dist/src/insecure/index.d.ts +9 -0
  148. package/dist/src/insecure/index.d.ts.map +1 -0
  149. package/dist/src/insecure/index.js +81 -0
  150. package/dist/src/insecure/index.js.map +1 -0
  151. package/dist/src/insecure/pb/proto.d.ts +29 -0
  152. package/dist/src/insecure/pb/proto.d.ts.map +1 -0
  153. package/dist/src/insecure/pb/proto.js +53 -0
  154. package/dist/src/insecure/pb/proto.js.map +1 -0
  155. package/dist/src/keychain/cms.d.ts +6 -14
  156. package/dist/src/keychain/cms.d.ts.map +1 -1
  157. package/dist/src/keychain/cms.js +129 -0
  158. package/dist/src/keychain/cms.js.map +1 -0
  159. package/dist/src/keychain/index.d.ts +60 -81
  160. package/dist/src/keychain/index.d.ts.map +1 -1
  161. package/dist/src/keychain/index.js +492 -0
  162. package/dist/src/keychain/index.js.map +1 -0
  163. package/dist/src/keychain/util.d.ts +3 -6
  164. package/dist/src/keychain/util.d.ts.map +1 -1
  165. package/dist/src/keychain/util.js +79 -0
  166. package/dist/src/keychain/util.js.map +1 -0
  167. package/dist/src/libp2p.d.ts +80 -0
  168. package/dist/src/libp2p.d.ts.map +1 -0
  169. package/dist/src/libp2p.js +397 -0
  170. package/dist/src/libp2p.js.map +1 -0
  171. package/dist/src/metrics/index.d.ts +47 -104
  172. package/dist/src/metrics/index.d.ts.map +1 -1
  173. package/dist/src/metrics/index.js +231 -0
  174. package/dist/src/metrics/index.js.map +1 -0
  175. package/dist/src/metrics/moving-average.d.ts +14 -0
  176. package/dist/src/metrics/moving-average.d.ts.map +1 -0
  177. package/dist/src/metrics/moving-average.js +40 -0
  178. package/dist/src/metrics/moving-average.js.map +1 -0
  179. package/dist/src/metrics/stats.d.ts +41 -93
  180. package/dist/src/metrics/stats.d.ts.map +1 -1
  181. package/dist/src/metrics/stats.js +183 -0
  182. package/dist/src/metrics/stats.js.map +1 -0
  183. package/dist/src/nat-manager.d.ts +42 -99
  184. package/dist/src/nat-manager.d.ts.map +1 -1
  185. package/dist/src/nat-manager.js +114 -0
  186. package/dist/src/nat-manager.js.map +1 -0
  187. package/dist/src/peer-record-updater.d.ts +15 -0
  188. package/dist/src/peer-record-updater.d.ts.map +1 -0
  189. package/dist/src/peer-record-updater.js +44 -0
  190. package/dist/src/peer-record-updater.js.map +1 -0
  191. package/dist/src/peer-routing.d.ts +40 -84
  192. package/dist/src/peer-routing.d.ts.map +1 -1
  193. package/dist/src/peer-routing.js +108 -0
  194. package/dist/src/peer-routing.js.map +1 -0
  195. package/dist/src/ping/constants.d.ts +4 -4
  196. package/dist/src/ping/constants.d.ts.map +1 -1
  197. package/dist/src/ping/constants.js +5 -0
  198. package/dist/src/ping/constants.js.map +1 -0
  199. package/dist/src/ping/index.d.ts +17 -31
  200. package/dist/src/ping/index.d.ts.map +1 -1
  201. package/dist/src/ping/index.js +57 -0
  202. package/dist/src/ping/index.js.map +1 -0
  203. package/dist/src/pnet/crypto.d.ts +15 -5
  204. package/dist/src/pnet/crypto.d.ts.map +1 -1
  205. package/dist/src/pnet/crypto.js +60 -0
  206. package/dist/src/pnet/crypto.js.map +1 -0
  207. package/dist/src/pnet/errors.d.ts +5 -5
  208. package/dist/src/pnet/errors.d.ts.map +1 -1
  209. package/dist/src/pnet/errors.js +6 -0
  210. package/dist/src/pnet/errors.js.map +1 -0
  211. package/dist/src/pnet/index.d.ts +12 -20
  212. package/dist/src/pnet/index.d.ts.map +1 -1
  213. package/dist/src/pnet/index.js +67 -0
  214. package/dist/src/pnet/index.js.map +1 -0
  215. package/dist/src/pnet/key-generator.d.ts +3 -7
  216. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  217. package/dist/src/pnet/key-generator.js +25 -0
  218. package/dist/src/pnet/key-generator.js.map +1 -0
  219. package/dist/src/pubsub/dummy-pubsub.d.ts +17 -0
  220. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
  221. package/dist/src/pubsub/dummy-pubsub.js +37 -0
  222. package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
  223. package/dist/src/registrar.d.ts +26 -65
  224. package/dist/src/registrar.d.ts.map +1 -1
  225. package/dist/src/registrar.js +144 -0
  226. package/dist/src/registrar.js.map +1 -0
  227. package/dist/src/transport-manager.d.ts +36 -82
  228. package/dist/src/transport-manager.d.ts.map +1 -1
  229. package/dist/src/transport-manager.js +223 -0
  230. package/dist/src/transport-manager.js.map +1 -0
  231. package/dist/src/upgrader.d.ts +49 -129
  232. package/dist/src/upgrader.d.ts.map +1 -1
  233. package/dist/src/upgrader.js +402 -0
  234. package/dist/src/upgrader.js.map +1 -0
  235. package/dist/src/version.d.ts +3 -0
  236. package/dist/src/version.d.ts.map +1 -0
  237. package/dist/src/version.js +3 -0
  238. package/dist/src/version.js.map +1 -0
  239. package/package.json +144 -201
  240. package/src/address-manager/index.ts +130 -0
  241. package/src/circuit/README.md +16 -11
  242. package/src/circuit/auto-relay.ts +286 -0
  243. package/src/circuit/circuit/hop.ts +211 -0
  244. package/src/circuit/circuit/stop.ts +78 -0
  245. package/src/circuit/circuit/stream-handler.ts +86 -0
  246. package/src/circuit/circuit/{utils.js → utils.ts} +9 -25
  247. package/src/circuit/constants.ts +31 -0
  248. package/src/circuit/index.ts +120 -0
  249. package/src/circuit/listener.ts +82 -0
  250. package/src/circuit/multicodec.ts +2 -0
  251. package/src/circuit/{protocol → pb}/index.proto +1 -1
  252. package/src/circuit/pb/index.ts +117 -0
  253. package/src/circuit/transport.ts +219 -0
  254. package/src/circuit/utils.ts +12 -0
  255. package/src/config.ts +99 -0
  256. package/src/connection-manager/auto-dialler.ts +154 -0
  257. package/src/connection-manager/dialer/auto-dialer.ts +59 -0
  258. package/src/{dialer/dial-request.js → connection-manager/dialer/dial-request.ts} +60 -51
  259. package/src/connection-manager/dialer/index.ts +382 -0
  260. package/src/connection-manager/index.ts +508 -0
  261. package/src/connection-manager/latency-monitor.ts +319 -0
  262. package/src/connection-manager/{visibility-change-emitter.js → visibility-change-emitter.ts} +48 -38
  263. package/src/constants.ts +31 -0
  264. package/src/content-routing/index.ts +144 -0
  265. package/src/content-routing/utils.ts +54 -0
  266. package/src/dht/dht-content-routing.ts +43 -0
  267. package/src/dht/dht-peer-routing.ts +35 -0
  268. package/src/dht/dummy-dht.ts +60 -0
  269. package/src/errors.ts +73 -0
  270. package/src/fetch/README.md +1 -1
  271. package/src/fetch/constants.ts +3 -0
  272. package/src/fetch/index.ts +167 -0
  273. package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
  274. package/src/fetch/pb/proto.ts +65 -0
  275. package/src/get-peer.ts +57 -0
  276. package/src/identify/consts.ts +13 -0
  277. package/src/identify/index.ts +445 -0
  278. package/src/identify/{message.proto → pb/message.proto} +0 -0
  279. package/src/identify/pb/message.ts +37 -0
  280. package/src/index.ts +220 -0
  281. package/src/insecure/index.ts +101 -0
  282. package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
  283. package/src/insecure/pb/proto.ts +68 -0
  284. package/src/keychain/{cms.js → cms.ts} +62 -58
  285. package/src/keychain/index.ts +587 -0
  286. package/src/keychain/{util.js → util.ts} +7 -17
  287. package/src/libp2p.ts +538 -0
  288. package/src/metrics/index.ts +310 -0
  289. package/src/metrics/moving-average.ts +53 -0
  290. package/src/metrics/stats.ts +238 -0
  291. package/src/nat-manager.ts +194 -0
  292. package/src/peer-record-updater.ts +55 -0
  293. package/src/peer-routing.ts +186 -0
  294. package/src/ping/README.md +1 -1
  295. package/src/ping/constants.ts +5 -0
  296. package/src/ping/index.ts +84 -0
  297. package/src/pnet/README.md +2 -2
  298. package/src/pnet/crypto.ts +67 -0
  299. package/src/pnet/errors.ts +5 -0
  300. package/src/pnet/index.ts +97 -0
  301. package/src/pnet/key-generator.ts +28 -0
  302. package/src/pubsub/dummy-pubsub.ts +51 -0
  303. package/src/registrar.ts +189 -0
  304. package/src/transport-manager.ts +281 -0
  305. package/src/upgrader.ts +499 -0
  306. package/src/version.ts +3 -0
  307. package/dist/index.min.js +0 -55
  308. package/dist/src/circuit/protocol/index.d.ts +0 -173
  309. package/dist/src/dialer/dial-request.d.ts +0 -55
  310. package/dist/src/dialer/dial-request.d.ts.map +0 -1
  311. package/dist/src/dialer/index.d.ts +0 -182
  312. package/dist/src/dialer/index.d.ts.map +0 -1
  313. package/dist/src/fetch/proto.d.ts +0 -134
  314. package/dist/src/identify/message.d.ts +0 -95
  315. package/dist/src/insecure/plaintext.d.ts +0 -12
  316. package/dist/src/insecure/plaintext.d.ts.map +0 -1
  317. package/dist/src/insecure/proto.d.ts +0 -128
  318. package/dist/src/metrics/old-peers.d.ts +0 -3
  319. package/dist/src/metrics/old-peers.d.ts.map +0 -1
  320. package/dist/src/metrics/tracked-map.d.ts +0 -8
  321. package/dist/src/metrics/tracked-map.d.ts.map +0 -1
  322. package/dist/src/peer-store/address-book.d.ts +0 -67
  323. package/dist/src/peer-store/address-book.d.ts.map +0 -1
  324. package/dist/src/peer-store/index.d.ts +0 -58
  325. package/dist/src/peer-store/index.d.ts.map +0 -1
  326. package/dist/src/peer-store/key-book.d.ts +0 -40
  327. package/dist/src/peer-store/key-book.d.ts.map +0 -1
  328. package/dist/src/peer-store/metadata-book.d.ts +0 -58
  329. package/dist/src/peer-store/metadata-book.d.ts.map +0 -1
  330. package/dist/src/peer-store/pb/peer.d.ts +0 -222
  331. package/dist/src/peer-store/proto-book.d.ts +0 -43
  332. package/dist/src/peer-store/proto-book.d.ts.map +0 -1
  333. package/dist/src/peer-store/store.d.ts +0 -73
  334. package/dist/src/peer-store/store.d.ts.map +0 -1
  335. package/dist/src/peer-store/types.d.ts +0 -202
  336. package/dist/src/peer-store/types.d.ts.map +0 -1
  337. package/dist/src/ping/util.d.ts +0 -5
  338. package/dist/src/ping/util.d.ts.map +0 -1
  339. package/dist/src/pubsub-adapter.d.ts +0 -22
  340. package/dist/src/pubsub-adapter.d.ts.map +0 -1
  341. package/dist/src/record/envelope/envelope.d.ts +0 -77
  342. package/dist/src/record/envelope/index.d.ts +0 -80
  343. package/dist/src/record/envelope/index.d.ts.map +0 -1
  344. package/dist/src/record/peer-record/consts.d.ts +0 -4
  345. package/dist/src/record/peer-record/consts.d.ts.map +0 -1
  346. package/dist/src/record/peer-record/index.d.ts +0 -60
  347. package/dist/src/record/peer-record/index.d.ts.map +0 -1
  348. package/dist/src/record/peer-record/peer-record.d.ts +0 -133
  349. package/dist/src/record/utils.d.ts +0 -12
  350. package/dist/src/record/utils.d.ts.map +0 -1
  351. package/dist/src/types.d.ts +0 -90
  352. package/dist/src/types.d.ts.map +0 -1
  353. package/src/address-manager/index.js +0 -96
  354. package/src/circuit/auto-relay.js +0 -302
  355. package/src/circuit/circuit/hop.js +0 -205
  356. package/src/circuit/circuit/stop.js +0 -81
  357. package/src/circuit/circuit/stream-handler.js +0 -94
  358. package/src/circuit/constants.js +0 -12
  359. package/src/circuit/index.js +0 -102
  360. package/src/circuit/listener.js +0 -75
  361. package/src/circuit/multicodec.js +0 -5
  362. package/src/circuit/protocol/index.d.ts +0 -173
  363. package/src/circuit/protocol/index.js +0 -530
  364. package/src/circuit/transport.js +0 -229
  365. package/src/circuit/utils.js +0 -17
  366. package/src/config.js +0 -114
  367. package/src/connection-manager/auto-dialler.js +0 -132
  368. package/src/connection-manager/index.js +0 -374
  369. package/src/connection-manager/latency-monitor.js +0 -264
  370. package/src/constants.js +0 -18
  371. package/src/content-routing/index.js +0 -163
  372. package/src/content-routing/utils.js +0 -89
  373. package/src/dht/dht-content-routing.js +0 -44
  374. package/src/dht/dht-peer-routing.js +0 -51
  375. package/src/dialer/index.js +0 -376
  376. package/src/errors.js +0 -66
  377. package/src/fetch/constants.js +0 -6
  378. package/src/fetch/index.js +0 -159
  379. package/src/fetch/proto.d.ts +0 -134
  380. package/src/fetch/proto.js +0 -333
  381. package/src/get-peer.js +0 -49
  382. package/src/identify/consts.js +0 -15
  383. package/src/identify/index.js +0 -384
  384. package/src/identify/message.d.ts +0 -95
  385. package/src/identify/message.js +0 -328
  386. package/src/index.js +0 -813
  387. package/src/insecure/plaintext.js +0 -95
  388. package/src/insecure/proto.d.ts +0 -128
  389. package/src/insecure/proto.js +0 -371
  390. package/src/keychain/index.js +0 -561
  391. package/src/metrics/index.js +0 -290
  392. package/src/metrics/old-peers.js +0 -16
  393. package/src/metrics/stats.js +0 -270
  394. package/src/metrics/tracked-map.js +0 -94
  395. package/src/nat-manager.js +0 -197
  396. package/src/peer-routing.js +0 -176
  397. package/src/peer-store/README.md +0 -145
  398. package/src/peer-store/address-book.js +0 -382
  399. package/src/peer-store/index.js +0 -121
  400. package/src/peer-store/key-book.js +0 -141
  401. package/src/peer-store/metadata-book.js +0 -250
  402. package/src/peer-store/pb/peer.d.ts +0 -222
  403. package/src/peer-store/pb/peer.js +0 -643
  404. package/src/peer-store/pb/peer.proto +0 -31
  405. package/src/peer-store/proto-book.js +0 -237
  406. package/src/peer-store/store.js +0 -263
  407. package/src/peer-store/types.ts +0 -245
  408. package/src/ping/constants.js +0 -8
  409. package/src/ping/index.js +0 -84
  410. package/src/ping/util.js +0 -18
  411. package/src/pnet/crypto.js +0 -84
  412. package/src/pnet/errors.js +0 -7
  413. package/src/pnet/index.js +0 -86
  414. package/src/pnet/key-generator.js +0 -33
  415. package/src/pubsub-adapter.js +0 -61
  416. package/src/record/README.md +0 -130
  417. package/src/record/envelope/envelope.d.ts +0 -77
  418. package/src/record/envelope/envelope.js +0 -243
  419. package/src/record/envelope/envelope.proto +0 -19
  420. package/src/record/envelope/index.js +0 -183
  421. package/src/record/peer-record/consts.js +0 -14
  422. package/src/record/peer-record/index.js +0 -113
  423. package/src/record/peer-record/peer-record.d.ts +0 -133
  424. package/src/record/peer-record/peer-record.js +0 -367
  425. package/src/record/peer-record/peer-record.proto +0 -18
  426. package/src/record/utils.js +0 -25
  427. package/src/registrar.js +0 -127
  428. package/src/transport-manager.js +0 -269
  429. package/src/types.ts +0 -98
  430. package/src/upgrader.js +0 -492
@@ -0,0 +1,445 @@
1
+ import { logger } from '@libp2p/logger'
2
+ import errCode from 'err-code'
3
+ import * as lp from 'it-length-prefixed'
4
+ import { pipe } from 'it-pipe'
5
+ import all from 'it-all'
6
+ import take from 'it-take'
7
+ import drain from 'it-drain'
8
+ import first from 'it-first'
9
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
+ import { Multiaddr, protocols } from '@multiformats/multiaddr'
11
+ import { Identify } from './pb/message.js'
12
+ import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
13
+ import {
14
+ MULTICODEC_IDENTIFY,
15
+ MULTICODEC_IDENTIFY_PUSH,
16
+ IDENTIFY_PROTOCOL_VERSION,
17
+ MULTICODEC_IDENTIFY_PROTOCOL_NAME,
18
+ MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME,
19
+ MULTICODEC_IDENTIFY_PROTOCOL_VERSION,
20
+ MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
21
+ } from './consts.js'
22
+ import { codes } from '../errors.js'
23
+ import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
24
+ import type { Connection } from '@libp2p/interfaces/connection'
25
+ import type { Startable } from '@libp2p/interfaces/startable'
26
+ import { peerIdFromKeys } from '@libp2p/peer-id'
27
+ import type { Components } from '@libp2p/interfaces/components'
28
+
29
+ const log = logger('libp2p:identify')
30
+
31
+ export interface HostProperties {
32
+ agentVersion: string
33
+ }
34
+
35
+ export interface IdentifyServiceInit {
36
+ protocolPrefix: string
37
+ host: HostProperties
38
+ }
39
+
40
+ export class IdentifyService implements Startable {
41
+ private readonly components: Components
42
+ private readonly identifyProtocolStr: string
43
+ private readonly identifyPushProtocolStr: string
44
+ private readonly host: {
45
+ protocolVersion: string
46
+ agentVersion: string
47
+ }
48
+
49
+ private started: boolean
50
+
51
+ constructor (components: Components, init: IdentifyServiceInit) {
52
+ this.components = components
53
+ this.started = false
54
+
55
+ this.handleMessage = this.handleMessage.bind(this)
56
+
57
+ this.identifyProtocolStr = `/${init.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`
58
+ this.identifyPushProtocolStr = `/${init.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
59
+
60
+ // Store self host metadata
61
+ this.host = {
62
+ protocolVersion: `${init.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
63
+ ...init.host
64
+ }
65
+
66
+ // When a new connection happens, trigger identify
67
+ this.components.getConnectionManager().addEventListener('peer:connect', (evt) => {
68
+ const connection = evt.detail
69
+ this.identify(connection).catch(log.error)
70
+ })
71
+
72
+ // When self multiaddrs change, trigger identify-push
73
+ this.components.getPeerStore().addEventListener('change:multiaddrs', (evt) => {
74
+ const { peerId } = evt.detail
75
+
76
+ if (this.components.getPeerId().equals(peerId)) {
77
+ void this.pushToPeerStore().catch(err => log.error(err))
78
+ }
79
+ })
80
+
81
+ // When self protocols change, trigger identify-push
82
+ this.components.getPeerStore().addEventListener('change:protocols', (evt) => {
83
+ const { peerId } = evt.detail
84
+
85
+ if (this.components.getPeerId().equals(peerId)) {
86
+ void this.pushToPeerStore().catch(err => log.error(err))
87
+ }
88
+ })
89
+ }
90
+
91
+ isStarted () {
92
+ return this.started
93
+ }
94
+
95
+ async start () {
96
+ if (this.started) {
97
+ return
98
+ }
99
+
100
+ await this.components.getPeerStore().metadataBook.setValue(this.components.getPeerId(), 'AgentVersion', uint8ArrayFromString(this.host.agentVersion))
101
+ await this.components.getPeerStore().metadataBook.setValue(this.components.getPeerId(), 'ProtocolVersion', uint8ArrayFromString(this.host.protocolVersion))
102
+
103
+ await this.components.getRegistrar().handle([
104
+ this.identifyProtocolStr,
105
+ this.identifyPushProtocolStr
106
+ ], (data) => {
107
+ void this.handleMessage(data)?.catch(err => {
108
+ log.error(err)
109
+ })
110
+ })
111
+
112
+ this.started = true
113
+ }
114
+
115
+ async stop () {
116
+ await this.components.getRegistrar().unhandle(this.identifyProtocolStr)
117
+ await this.components.getRegistrar().unhandle(this.identifyPushProtocolStr)
118
+
119
+ this.started = false
120
+ }
121
+
122
+ /**
123
+ * Send an Identify Push update to the list of connections
124
+ */
125
+ async push (connections: Connection[]): Promise<void> {
126
+ const signedPeerRecord = await this.components.getPeerStore().addressBook.getRawEnvelope(this.components.getPeerId())
127
+ const listenAddrs = this.components.getAddressManager().getAddresses().map((ma) => ma.bytes)
128
+ const protocols = await this.components.getPeerStore().protoBook.get(this.components.getPeerId())
129
+
130
+ const pushes = connections.map(async connection => {
131
+ try {
132
+ const { stream } = await connection.newStream([this.identifyPushProtocolStr])
133
+
134
+ await pipe(
135
+ [Identify.encode({
136
+ listenAddrs,
137
+ signedPeerRecord,
138
+ protocols
139
+ })],
140
+ lp.encode(),
141
+ stream,
142
+ drain
143
+ )
144
+ } catch (err: any) {
145
+ // Just log errors
146
+ log.error('could not push identify update to peer', err)
147
+ }
148
+ })
149
+
150
+ await Promise.all(pushes)
151
+ }
152
+
153
+ /**
154
+ * Calls `push` on all peer connections
155
+ */
156
+ async pushToPeerStore () {
157
+ // Do not try to push if we are not running
158
+ if (!this.isStarted()) {
159
+ return
160
+ }
161
+
162
+ const connections: Connection[] = []
163
+
164
+ for (const conn of this.components.getConnectionManager().getConnections()) {
165
+ const peerId = conn.remotePeer
166
+ const peer = await this.components.getPeerStore().get(peerId)
167
+
168
+ if (!peer.protocols.includes(this.identifyPushProtocolStr)) {
169
+ continue
170
+ }
171
+
172
+ connections.push(conn)
173
+ }
174
+
175
+ await this.push(connections)
176
+ }
177
+
178
+ /**
179
+ * Requests the `Identify` message from peer associated with the given `connection`.
180
+ * If the identified peer does not match the `PeerId` associated with the connection,
181
+ * an error will be thrown.
182
+ */
183
+ async identify (connection: Connection): Promise<void> {
184
+ const { stream } = await connection.newStream([this.identifyProtocolStr])
185
+ const [data] = await pipe(
186
+ [],
187
+ stream,
188
+ lp.decode(),
189
+ (source) => take(source, 1),
190
+ async (source) => await all(source)
191
+ )
192
+
193
+ if (data == null) {
194
+ throw errCode(new Error('No data could be retrieved'), codes.ERR_CONNECTION_ENDED)
195
+ }
196
+
197
+ let message: Identify
198
+ try {
199
+ message = Identify.decode(data)
200
+ } catch (err: any) {
201
+ throw errCode(err, codes.ERR_INVALID_MESSAGE)
202
+ }
203
+
204
+ const {
205
+ publicKey,
206
+ listenAddrs,
207
+ protocols,
208
+ observedAddr,
209
+ signedPeerRecord,
210
+ agentVersion,
211
+ protocolVersion
212
+ } = message
213
+
214
+ if (publicKey == null) {
215
+ throw errCode(new Error('public key was missing from identify message'), codes.ERR_MISSING_PUBLIC_KEY)
216
+ }
217
+
218
+ const id = await peerIdFromKeys(publicKey)
219
+
220
+ if (!connection.remotePeer.equals(id)) {
221
+ throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
222
+ }
223
+
224
+ if (this.components.getPeerId().equals(id)) {
225
+ throw errCode(new Error('identified peer is our own peer id?'), codes.ERR_INVALID_PEER)
226
+ }
227
+
228
+ // Get the observedAddr if there is one
229
+ const cleanObservedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
230
+
231
+ if (signedPeerRecord != null) {
232
+ log('received signed peer record from %p', id)
233
+
234
+ try {
235
+ const envelope = await RecordEnvelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
236
+
237
+ if (!envelope.peerId.equals(id)) {
238
+ throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
239
+ }
240
+
241
+ if (await this.components.getPeerStore().addressBook.consumePeerRecord(envelope)) {
242
+ await this.components.getPeerStore().protoBook.set(id, protocols)
243
+
244
+ if (agentVersion != null) {
245
+ await this.components.getPeerStore().metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(agentVersion))
246
+ }
247
+
248
+ if (protocolVersion != null) {
249
+ await this.components.getPeerStore().metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(protocolVersion))
250
+ }
251
+
252
+ log('identify completed for peer %p and protocols %o', id, protocols)
253
+
254
+ return
255
+ }
256
+ } catch (err: any) {
257
+ log('received invalid envelope, discard it and fallback to listenAddrs is available', err)
258
+ }
259
+ } else {
260
+ log('no signed peer record received from %p', id)
261
+ }
262
+
263
+ log('falling back to legacy addresses from %p', id)
264
+
265
+ // LEGACY: Update peers data in PeerStore
266
+ try {
267
+ await this.components.getPeerStore().addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
268
+ } catch (err: any) {
269
+ log.error('received invalid addrs', err)
270
+ }
271
+
272
+ await this.components.getPeerStore().protoBook.set(id, protocols)
273
+
274
+ if (agentVersion != null) {
275
+ await this.components.getPeerStore().metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(agentVersion))
276
+ }
277
+
278
+ if (protocolVersion != null) {
279
+ await this.components.getPeerStore().metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(protocolVersion))
280
+ }
281
+
282
+ log('identify completed for peer %p and protocols %o', id, protocols)
283
+
284
+ // TODO: Add and score our observed addr
285
+ log('received observed address of %s', cleanObservedAddr?.toString())
286
+ // this.components.getAddressManager().addObservedAddr(observedAddr)
287
+ }
288
+
289
+ /**
290
+ * A handler to register with Libp2p to process identify messages
291
+ */
292
+ handleMessage (data: IncomingStreamData) {
293
+ const { protocol } = data
294
+
295
+ switch (protocol) {
296
+ case this.identifyProtocolStr:
297
+ return this._handleIdentify(data)
298
+ case this.identifyPushProtocolStr:
299
+ return this._handlePush(data)
300
+ default:
301
+ log.error('cannot handle unknown protocol %s', protocol)
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Sends the `Identify` response with the Signed Peer Record
307
+ * to the requesting peer over the given `connection`
308
+ */
309
+ async _handleIdentify (data: IncomingStreamData) {
310
+ const { connection, stream } = data
311
+ try {
312
+ const publicKey = this.components.getPeerId().publicKey ?? new Uint8Array(0)
313
+ const peerData = await this.components.getPeerStore().get(this.components.getPeerId())
314
+ const multiaddrs = this.components.getAddressManager().getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
315
+ let signedPeerRecord = peerData.peerRecordEnvelope
316
+
317
+ if (multiaddrs.length > 0 && signedPeerRecord == null) {
318
+ const peerRecord = new PeerRecord({
319
+ peerId: this.components.getPeerId(),
320
+ multiaddrs
321
+ })
322
+
323
+ const envelope = await RecordEnvelope.seal(peerRecord, this.components.getPeerId())
324
+ await this.components.getPeerStore().addressBook.consumePeerRecord(envelope)
325
+ signedPeerRecord = envelope.marshal()
326
+ }
327
+
328
+ const message = Identify.encode({
329
+ protocolVersion: this.host.protocolVersion,
330
+ agentVersion: this.host.agentVersion,
331
+ publicKey,
332
+ listenAddrs: multiaddrs.map(addr => addr.bytes),
333
+ signedPeerRecord,
334
+ observedAddr: connection.remoteAddr.bytes,
335
+ protocols: peerData.protocols
336
+ })
337
+
338
+ await pipe(
339
+ [message],
340
+ lp.encode(),
341
+ stream,
342
+ drain
343
+ )
344
+ } catch (err: any) {
345
+ log.error('could not respond to identify request', err)
346
+ }
347
+ }
348
+
349
+ /**
350
+ * Reads the Identify Push message from the given `connection`
351
+ */
352
+ async _handlePush (data: IncomingStreamData) {
353
+ const { connection, stream } = data
354
+
355
+ let message: Identify | undefined
356
+ try {
357
+ const data = await pipe(
358
+ [],
359
+ stream,
360
+ lp.decode(),
361
+ async (source) => await first(source)
362
+ )
363
+
364
+ if (data != null) {
365
+ message = Identify.decode(data)
366
+ }
367
+ } catch (err: any) {
368
+ return log.error('received invalid message', err)
369
+ }
370
+
371
+ if (message == null) {
372
+ return log.error('received invalid message')
373
+ }
374
+
375
+ const id = connection.remotePeer
376
+
377
+ if (this.components.getPeerId().equals(id)) {
378
+ log('received push from ourselves?')
379
+ return
380
+ }
381
+
382
+ log('received push from %p', id)
383
+
384
+ if (message.signedPeerRecord != null) {
385
+ log('received signedPeerRecord in push')
386
+
387
+ try {
388
+ const envelope = await RecordEnvelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
389
+
390
+ if (await this.components.getPeerStore().addressBook.consumePeerRecord(envelope)) {
391
+ log('consumed signedPeerRecord sent in push')
392
+
393
+ await this.components.getPeerStore().protoBook.set(id, message.protocols)
394
+ return
395
+ } else {
396
+ log('failed to consume signedPeerRecord sent in push')
397
+ }
398
+ } catch (err: any) {
399
+ log('received invalid envelope, discard it and fallback to listenAddrs is available', err)
400
+ }
401
+ } else {
402
+ log('did not receive signedPeerRecord in push')
403
+ }
404
+
405
+ // LEGACY: Update peers data in PeerStore
406
+ try {
407
+ await this.components.getPeerStore().addressBook.set(id,
408
+ message.listenAddrs.map((addr) => new Multiaddr(addr)))
409
+ } catch (err: any) {
410
+ log.error('received invalid addrs', err)
411
+ }
412
+
413
+ // Update the protocols
414
+ try {
415
+ await this.components.getPeerStore().protoBook.set(id, message.protocols)
416
+ } catch (err: any) {
417
+ log.error('received invalid protocols', err)
418
+ }
419
+
420
+ log('handled push from %p', id)
421
+ }
422
+
423
+ /**
424
+ * Takes the `addr` and converts it to a Multiaddr if possible
425
+ */
426
+ static getCleanMultiaddr (addr: Uint8Array | string | null | undefined) {
427
+ if (addr != null && addr.length > 0) {
428
+ try {
429
+ return new Multiaddr(addr)
430
+ } catch {
431
+
432
+ }
433
+ }
434
+ }
435
+ }
436
+
437
+ /**
438
+ * The protocols the IdentifyService supports
439
+ */
440
+ export const multicodecs = {
441
+ IDENTIFY: MULTICODEC_IDENTIFY,
442
+ IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
443
+ }
444
+
445
+ export const Message = { Identify }
File without changes
@@ -0,0 +1,37 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+
4
+ import { encodeMessage, decodeMessage, message, string, bytes } from 'protons-runtime'
5
+ import type { Codec } from 'protons-runtime'
6
+
7
+ export interface Identify {
8
+ protocolVersion?: string
9
+ agentVersion?: string
10
+ publicKey?: Uint8Array
11
+ listenAddrs: Uint8Array[]
12
+ observedAddr?: Uint8Array
13
+ protocols: string[]
14
+ signedPeerRecord?: Uint8Array
15
+ }
16
+
17
+ export namespace Identify {
18
+ export const codec = (): Codec<Identify> => {
19
+ return message<Identify>({
20
+ 5: { name: 'protocolVersion', codec: string, optional: true },
21
+ 6: { name: 'agentVersion', codec: string, optional: true },
22
+ 1: { name: 'publicKey', codec: bytes, optional: true },
23
+ 2: { name: 'listenAddrs', codec: bytes, repeats: true },
24
+ 4: { name: 'observedAddr', codec: bytes, optional: true },
25
+ 3: { name: 'protocols', codec: string, repeats: true },
26
+ 8: { name: 'signedPeerRecord', codec: bytes, optional: true }
27
+ })
28
+ }
29
+
30
+ export const encode = (obj: Identify): Uint8Array => {
31
+ return encodeMessage(obj, Identify.codec())
32
+ }
33
+
34
+ export const decode = (buf: Uint8Array): Identify => {
35
+ return decodeMessage(buf, Identify.codec())
36
+ }
37
+ }
package/src/index.ts ADDED
@@ -0,0 +1,220 @@
1
+ import { createLibp2pNode } from './libp2p.js'
2
+ import type { AbortOptions, RecursivePartial } from '@libp2p/interfaces'
3
+ import type { EventEmitter } from '@libp2p/interfaces/events'
4
+ import type { Startable } from '@libp2p/interfaces/startable'
5
+ import type { Multiaddr } from '@multiformats/multiaddr'
6
+ import type { FaultTolerance } from './transport-manager.js'
7
+ import type { HostProperties } from './identify/index.js'
8
+ import type { DualDHT } from '@libp2p/interfaces/dht'
9
+ import type { Datastore } from 'interface-datastore'
10
+ import type { PeerStore, PeerStoreInit } from '@libp2p/interfaces/peer-store'
11
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
12
+ import type { AutoRelayConfig, RelayAdvertiseConfig } from './circuit/index.js'
13
+ import type { PeerDiscovery } from '@libp2p/interfaces/peer-discovery'
14
+ import type { Connection, ConnectionGater, ConnectionProtector, ProtocolStream } from '@libp2p/interfaces/connection'
15
+ import type { Transport } from '@libp2p/interfaces/transport'
16
+ import type { StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer'
17
+ import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
18
+ import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
19
+ import type { ContentRouting } from '@libp2p/interfaces/content-routing'
20
+ import type { PubSub } from '@libp2p/interfaces/pubsub'
21
+ import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
22
+ import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
23
+ import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
24
+ import type { PeerInfo } from '@libp2p/interfaces/peer-info'
25
+ import type { KeyChain } from './keychain/index.js'
26
+ import type { ConnectionManagerInit } from './connection-manager/index.js'
27
+
28
+ export interface PersistentPeerStoreOptions {
29
+ threshold?: number
30
+ }
31
+
32
+ export interface DEKConfig {
33
+ keyLength: number
34
+ iterationCount: number
35
+ salt: string
36
+ hash: string
37
+ }
38
+
39
+ export interface KeychainConfig {
40
+ pass?: string
41
+ dek?: DEKConfig
42
+ }
43
+
44
+ export interface MetricsConfig {
45
+ enabled?: boolean
46
+ }
47
+
48
+ export interface HopConfig {
49
+ enabled?: boolean
50
+ active?: boolean
51
+ }
52
+
53
+ export interface RelayConfig {
54
+ enabled: boolean
55
+ advertise: RelayAdvertiseConfig
56
+ hop: HopConfig
57
+ autoRelay: AutoRelayConfig
58
+ }
59
+
60
+ export interface NatManagerConfig {
61
+ enabled: boolean
62
+ externalAddress?: string
63
+ localAddress?: string
64
+ description?: string
65
+ ttl?: number
66
+ keepAlive: boolean
67
+ gateway?: string
68
+ }
69
+
70
+ export interface AddressesConfig {
71
+ listen: string[]
72
+ announce: string[]
73
+ noAnnounce: string[]
74
+ announceFilter: (multiaddrs: Multiaddr[]) => Multiaddr[]
75
+ }
76
+
77
+ export interface TransportManagerConfig {
78
+ faultTolerance?: FaultTolerance
79
+ }
80
+
81
+ export interface PeerStoreConfig {
82
+ persistence?: boolean
83
+ threshold?: number
84
+ }
85
+
86
+ export interface PeerRoutingConfig {
87
+ refreshManager: RefreshManagerConfig
88
+ }
89
+
90
+ export interface RefreshManagerConfig {
91
+ enabled?: boolean
92
+ interval: number
93
+ bootDelay: number
94
+ }
95
+
96
+ export interface Libp2pInit {
97
+ peerId: PeerId
98
+ host: HostProperties
99
+ addresses: AddressesConfig
100
+ connectionManager: ConnectionManagerInit
101
+ connectionGater: Partial<ConnectionGater>
102
+ transportManager: TransportManagerConfig
103
+ datastore: Datastore
104
+ metrics: MetricsInit
105
+ peerStore: PeerStoreInit
106
+ peerRouting: PeerRoutingConfig
107
+ keychain: KeychainConfig
108
+ protocolPrefix: string
109
+ nat: NatManagerConfig
110
+ relay: RelayConfig
111
+
112
+ transports: Transport[]
113
+ streamMuxers?: StreamMuxerFactory[]
114
+ connectionEncryption?: ConnectionEncrypter[]
115
+ peerDiscovery?: PeerDiscovery[]
116
+ peerRouters?: PeerRouting[]
117
+ contentRouters?: ContentRouting[]
118
+ dht?: DualDHT
119
+ pubsub?: PubSub
120
+ connectionProtector?: ConnectionProtector
121
+ }
122
+
123
+ export interface Libp2pEvents {
124
+ 'peer:discovery': CustomEvent<PeerInfo>
125
+ }
126
+
127
+ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
128
+ peerId: PeerId
129
+ peerStore: PeerStore
130
+ peerRouting: PeerRouting
131
+ contentRouting: ContentRouting
132
+ keychain: KeyChain
133
+ connectionManager: ConnectionManager
134
+ registrar: Registrar
135
+ metrics?: Metrics
136
+ pubsub: PubSub
137
+ dht: DualDHT
138
+
139
+ /**
140
+ * Load keychain keys from the datastore.
141
+ * Imports the private key as 'self', if needed.
142
+ */
143
+ loadKeychain: () => Promise<void>
144
+
145
+ /**
146
+ * Get a deduplicated list of peer advertising multiaddrs by concatenating
147
+ * the listen addresses used by transports with any configured
148
+ * announce addresses as well as observed addresses reported by peers.
149
+ *
150
+ * If Announce addrs are specified, configured listen addresses will be
151
+ * ignored though observed addresses will still be included.
152
+ */
153
+ getMultiaddrs: () => Multiaddr[]
154
+
155
+ /**
156
+ * Return a list of all connections this node has open, optionally filtering
157
+ * by a PeerId
158
+ */
159
+ getConnections: (peerId?: PeerId) => Connection[]
160
+
161
+ /**
162
+ * Return a list of all peers we currently have a connection open to
163
+ */
164
+ getPeers: () => PeerId[]
165
+
166
+ /**
167
+ * Dials to the provided peer. If successful, the known metadata of the
168
+ * peer will be added to the nodes `peerStore`
169
+ */
170
+ dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
171
+
172
+ /**
173
+ * Dials to the provided peer and tries to handshake with the given protocols in order.
174
+ * If successful, the known metadata of the peer will be added to the nodes `peerStore`,
175
+ * and the `MuxedStream` will be returned together with the successful negotiated protocol.
176
+ */
177
+ dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<ProtocolStream>
178
+
179
+ /**
180
+ * Disconnects all connections to the given `peer`
181
+ */
182
+ hangUp: (peer: PeerId | Multiaddr | string) => Promise<void>
183
+
184
+ /**
185
+ * Registers the `handler` for each protocol
186
+ */
187
+ handle: (protocol: string | string[], handler: StreamHandler) => Promise<void>
188
+
189
+ /**
190
+ * Removes the handler for each protocol. The protocol
191
+ * will no longer be supported on streams.
192
+ */
193
+ unhandle: (protocols: string[] | string) => Promise<void>
194
+
195
+ /**
196
+ * Pings the given peer in order to obtain the operation latency
197
+ */
198
+ ping: (peer: Multiaddr |PeerId) => Promise<number>
199
+
200
+ /**
201
+ * Sends a request to fetch the value associated with the given key from the given peer.
202
+ */
203
+ fetch: (peer: PeerId | Multiaddr | string, key: string) => Promise<Uint8Array | null>
204
+
205
+ /**
206
+ * Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
207
+ * this may mean searching the DHT if the key is not present in the KeyStore.
208
+ */
209
+ getPublicKey: (peer: PeerId, options?: AbortOptions) => Promise<Uint8Array>
210
+ }
211
+
212
+ export type Libp2pOptions = RecursivePartial<Libp2pInit>
213
+
214
+ /**
215
+ * Returns a new instance of the Libp2p interface, generating a new PeerId
216
+ * if one is not passed as part of the options.
217
+ */
218
+ export async function createLibp2p (options: Libp2pOptions): Promise<Libp2p> {
219
+ return await createLibp2pNode(options)
220
+ }