libp2p 0.36.0 → 0.36.2-199395d

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 (398) hide show
  1. package/LICENSE +3 -21
  2. package/README.md +13 -10
  3. package/dist/src/address-manager/index.d.ts +33 -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 +215 -0
  10. package/dist/src/circuit/auto-relay.js.map +1 -0
  11. package/dist/src/circuit/circuit/hop.d.ts +25 -52
  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 +16 -8
  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 +16 -35
  20. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  21. package/dist/src/circuit/circuit/stream-handler.js +60 -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 +54 -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/{protocol → pb}/index.d.ts +0 -0
  44. package/{src/circuit/protocol → dist/src/circuit/pb}/index.js +7 -9
  45. package/dist/src/circuit/transport.d.ts +18 -78
  46. package/dist/src/circuit/transport.d.ts.map +1 -1
  47. package/dist/src/circuit/transport.js +190 -0
  48. package/dist/src/circuit/transport.js.map +1 -0
  49. package/dist/src/circuit/utils.d.ts +5 -2
  50. package/dist/src/circuit/utils.d.ts.map +1 -1
  51. package/dist/src/circuit/utils.js +11 -0
  52. package/dist/src/circuit/utils.js.map +1 -0
  53. package/dist/src/config.d.ts +3 -85
  54. package/dist/src/config.d.ts.map +1 -1
  55. package/dist/src/config.js +93 -0
  56. package/dist/src/config.js.map +1 -0
  57. package/dist/src/connection-manager/auto-dialler.d.ts +24 -41
  58. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  59. package/dist/src/connection-manager/auto-dialler.js +105 -0
  60. package/dist/src/connection-manager/auto-dialler.js.map +1 -0
  61. package/dist/src/connection-manager/index.d.ts +79 -144
  62. package/dist/src/connection-manager/index.d.ts.map +1 -1
  63. package/dist/src/connection-manager/index.js +295 -0
  64. package/dist/src/connection-manager/index.js.map +1 -0
  65. package/dist/src/connection-manager/latency-monitor.d.ts +72 -97
  66. package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
  67. package/dist/src/connection-manager/latency-monitor.js +220 -0
  68. package/dist/src/connection-manager/latency-monitor.js.map +1 -0
  69. package/dist/src/connection-manager/visibility-change-emitter.d.ts +15 -16
  70. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
  71. package/dist/src/connection-manager/visibility-change-emitter.js +118 -0
  72. package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -0
  73. package/dist/src/constants.d.ts +22 -10
  74. package/dist/src/constants.d.ts.map +1 -1
  75. package/dist/src/constants.js +27 -0
  76. package/dist/src/constants.js.map +1 -0
  77. package/dist/src/content-routing/index.d.ts +26 -81
  78. package/dist/src/content-routing/index.d.ts.map +1 -1
  79. package/dist/src/content-routing/index.js +99 -0
  80. package/dist/src/content-routing/index.js.map +1 -0
  81. package/dist/src/content-routing/utils.d.ts +6 -49
  82. package/dist/src/content-routing/utils.d.ts.map +1 -1
  83. package/dist/src/content-routing/utils.js +42 -0
  84. package/dist/src/content-routing/utils.js.map +1 -0
  85. package/dist/src/dht/dht-content-routing.d.ts +10 -24
  86. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  87. package/dist/src/dht/dht-content-routing.js +32 -0
  88. package/dist/src/dht/dht-content-routing.js.map +1 -0
  89. package/dist/src/dht/dht-peer-routing.d.ts +9 -24
  90. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  91. package/dist/src/dht/dht-peer-routing.js +26 -0
  92. package/dist/src/dht/dht-peer-routing.js.map +1 -0
  93. package/dist/src/dialer/auto-dialer.d.ts +14 -0
  94. package/dist/src/dialer/auto-dialer.d.ts.map +1 -0
  95. package/dist/src/dialer/auto-dialer.js +24 -0
  96. package/dist/src/dialer/auto-dialer.js.map +1 -0
  97. package/dist/src/dialer/dial-request.d.ts +18 -46
  98. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  99. package/dist/src/dialer/dial-request.js +83 -0
  100. package/dist/src/dialer/dial-request.js.map +1 -0
  101. package/dist/src/dialer/index.d.ts +41 -148
  102. package/dist/src/dialer/index.d.ts.map +1 -1
  103. package/dist/src/dialer/index.js +281 -0
  104. package/dist/src/dialer/index.js.map +1 -0
  105. package/dist/src/errors.d.ts +68 -63
  106. package/dist/src/errors.d.ts.map +1 -1
  107. package/dist/src/errors.js +73 -0
  108. package/dist/src/errors.js.map +1 -0
  109. package/dist/src/fetch/constants.d.ts +1 -1
  110. package/dist/src/fetch/constants.d.ts.map +1 -1
  111. package/dist/src/fetch/constants.js +3 -0
  112. package/dist/src/fetch/constants.js.map +1 -0
  113. package/dist/src/fetch/index.d.ts +30 -49
  114. package/dist/src/fetch/index.d.ts.map +1 -1
  115. package/dist/src/fetch/index.js +130 -0
  116. package/dist/src/fetch/index.js.map +1 -0
  117. package/dist/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
  118. package/{src/fetch → dist/src/fetch/pb}/proto.js +7 -9
  119. package/dist/src/get-peer.d.ts +4 -11
  120. package/dist/src/get-peer.d.ts.map +1 -1
  121. package/dist/src/get-peer.js +43 -0
  122. package/dist/src/get-peer.js.map +1 -0
  123. package/dist/src/identify/consts.d.ts +9 -9
  124. package/dist/src/identify/consts.d.ts.map +1 -1
  125. package/dist/src/identify/consts.js +11 -0
  126. package/dist/src/identify/consts.js.map +1 -0
  127. package/dist/src/identify/index.d.ts +40 -96
  128. package/dist/src/identify/index.d.ts.map +1 -1
  129. package/dist/src/identify/index.js +322 -0
  130. package/dist/src/identify/index.js.map +1 -0
  131. package/{src/identify → dist/src/identify/pb}/message.d.ts +20 -5
  132. package/{src/identify → dist/src/identify/pb}/message.js +83 -42
  133. package/dist/src/index.d.ts +157 -583
  134. package/dist/src/index.d.ts.map +1 -1
  135. package/dist/src/index.js +9 -0
  136. package/dist/src/index.js.map +1 -0
  137. package/dist/src/insecure/index.d.ts +9 -0
  138. package/dist/src/insecure/index.d.ts.map +1 -0
  139. package/dist/src/insecure/index.js +78 -0
  140. package/dist/src/insecure/index.js.map +1 -0
  141. package/dist/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
  142. package/{src/insecure → dist/src/insecure/pb}/proto.js +39 -22
  143. package/dist/src/keychain/cms.d.ts +6 -14
  144. package/dist/src/keychain/cms.d.ts.map +1 -1
  145. package/dist/src/keychain/cms.js +129 -0
  146. package/dist/src/keychain/cms.js.map +1 -0
  147. package/dist/src/keychain/index.d.ts +62 -82
  148. package/dist/src/keychain/index.d.ts.map +1 -1
  149. package/dist/src/keychain/index.js +493 -0
  150. package/dist/src/keychain/index.js.map +1 -0
  151. package/dist/src/keychain/util.d.ts +3 -6
  152. package/dist/src/keychain/util.d.ts.map +1 -1
  153. package/dist/src/keychain/util.js +79 -0
  154. package/dist/src/keychain/util.js.map +1 -0
  155. package/dist/src/libp2p.d.ts +75 -0
  156. package/dist/src/libp2p.d.ts.map +1 -0
  157. package/dist/src/libp2p.js +368 -0
  158. package/dist/src/libp2p.js.map +1 -0
  159. package/dist/src/metrics/index.d.ts +47 -104
  160. package/dist/src/metrics/index.d.ts.map +1 -1
  161. package/dist/src/metrics/index.js +231 -0
  162. package/dist/src/metrics/index.js.map +1 -0
  163. package/dist/src/metrics/moving-average.d.ts +14 -0
  164. package/dist/src/metrics/moving-average.d.ts.map +1 -0
  165. package/dist/src/metrics/moving-average.js +40 -0
  166. package/dist/src/metrics/moving-average.js.map +1 -0
  167. package/dist/src/metrics/stats.d.ts +45 -93
  168. package/dist/src/metrics/stats.d.ts.map +1 -1
  169. package/dist/src/metrics/stats.js +183 -0
  170. package/dist/src/metrics/stats.js.map +1 -0
  171. package/dist/src/nat-manager.d.ts +42 -99
  172. package/dist/src/nat-manager.d.ts.map +1 -1
  173. package/dist/src/nat-manager.js +114 -0
  174. package/dist/src/nat-manager.js.map +1 -0
  175. package/dist/src/peer-record-updater.d.ts +15 -0
  176. package/dist/src/peer-record-updater.d.ts.map +1 -0
  177. package/dist/src/peer-record-updater.js +44 -0
  178. package/dist/src/peer-record-updater.js.map +1 -0
  179. package/dist/src/peer-routing.d.ts +39 -84
  180. package/dist/src/peer-routing.d.ts.map +1 -1
  181. package/dist/src/peer-routing.js +108 -0
  182. package/dist/src/peer-routing.js.map +1 -0
  183. package/dist/src/ping/constants.d.ts +4 -4
  184. package/dist/src/ping/constants.d.ts.map +1 -1
  185. package/dist/src/ping/constants.js +5 -0
  186. package/dist/src/ping/constants.js.map +1 -0
  187. package/dist/src/ping/index.d.ts +26 -33
  188. package/dist/src/ping/index.d.ts.map +1 -1
  189. package/dist/src/ping/index.js +56 -0
  190. package/dist/src/ping/index.js.map +1 -0
  191. package/dist/src/pnet/crypto.d.ts +15 -5
  192. package/dist/src/pnet/crypto.d.ts.map +1 -1
  193. package/dist/src/pnet/crypto.js +60 -0
  194. package/dist/src/pnet/crypto.js.map +1 -0
  195. package/dist/src/pnet/errors.d.ts +5 -5
  196. package/dist/src/pnet/errors.d.ts.map +1 -1
  197. package/dist/src/pnet/errors.js +6 -0
  198. package/dist/src/pnet/errors.js.map +1 -0
  199. package/dist/src/pnet/index.d.ts +11 -20
  200. package/dist/src/pnet/index.d.ts.map +1 -1
  201. package/dist/src/pnet/index.js +66 -0
  202. package/dist/src/pnet/index.js.map +1 -0
  203. package/dist/src/pnet/key-generator.d.ts +3 -7
  204. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  205. package/dist/src/pnet/key-generator.js +25 -0
  206. package/dist/src/pnet/key-generator.js.map +1 -0
  207. package/dist/src/registrar.d.ts +27 -65
  208. package/dist/src/registrar.d.ts.map +1 -1
  209. package/dist/src/registrar.js +159 -0
  210. package/dist/src/registrar.js.map +1 -0
  211. package/dist/src/transport-manager.d.ts +34 -82
  212. package/dist/src/transport-manager.d.ts.map +1 -1
  213. package/dist/src/transport-manager.js +223 -0
  214. package/dist/src/transport-manager.js.map +1 -0
  215. package/dist/src/upgrader.d.ts +49 -129
  216. package/dist/src/upgrader.d.ts.map +1 -1
  217. package/dist/src/upgrader.js +402 -0
  218. package/dist/src/upgrader.js.map +1 -0
  219. package/dist/src/version.d.ts +3 -0
  220. package/dist/src/version.d.ts.map +1 -0
  221. package/dist/src/version.js +3 -0
  222. package/dist/src/version.js.map +1 -0
  223. package/package.json +145 -197
  224. package/src/address-manager/index.ts +129 -0
  225. package/src/circuit/README.md +16 -11
  226. package/src/circuit/auto-relay.ts +284 -0
  227. package/src/circuit/circuit/hop.ts +211 -0
  228. package/src/circuit/circuit/stop.ts +78 -0
  229. package/src/circuit/circuit/stream-handler.ts +87 -0
  230. package/src/circuit/circuit/{utils.js → utils.ts} +9 -25
  231. package/src/circuit/constants.ts +31 -0
  232. package/src/circuit/index.ts +120 -0
  233. package/src/circuit/{listener.js → listener.ts} +21 -27
  234. package/src/circuit/multicodec.ts +2 -0
  235. package/src/circuit/{protocol → pb}/index.d.ts +0 -0
  236. package/src/circuit/pb/index.js +528 -0
  237. package/src/circuit/{protocol → pb}/index.proto +0 -0
  238. package/src/circuit/transport.ts +216 -0
  239. package/src/circuit/utils.ts +12 -0
  240. package/src/config.ts +101 -0
  241. package/src/connection-manager/auto-dialler.ts +154 -0
  242. package/src/connection-manager/index.ts +422 -0
  243. package/src/connection-manager/latency-monitor.ts +319 -0
  244. package/src/connection-manager/{visibility-change-emitter.js → visibility-change-emitter.ts} +48 -38
  245. package/src/constants.ts +31 -0
  246. package/src/content-routing/index.ts +143 -0
  247. package/src/content-routing/utils.ts +54 -0
  248. package/src/dht/dht-content-routing.ts +43 -0
  249. package/src/dht/dht-peer-routing.ts +35 -0
  250. package/src/dialer/auto-dialer.ts +40 -0
  251. package/src/dialer/{dial-request.js → dial-request.ts} +60 -51
  252. package/src/dialer/index.ts +374 -0
  253. package/src/errors.ts +71 -0
  254. package/src/fetch/README.md +1 -1
  255. package/src/fetch/constants.ts +3 -0
  256. package/src/fetch/{index.js → index.ts} +79 -70
  257. package/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
  258. package/src/fetch/pb/proto.js +331 -0
  259. package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
  260. package/src/get-peer.ts +57 -0
  261. package/src/identify/consts.ts +13 -0
  262. package/src/identify/index.ts +445 -0
  263. package/{dist/src/identify → src/identify/pb}/message.d.ts +20 -5
  264. package/src/identify/pb/message.js +369 -0
  265. package/src/identify/{message.proto → pb/message.proto} +0 -0
  266. package/src/index.ts +234 -0
  267. package/src/insecure/index.ts +97 -0
  268. package/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
  269. package/src/insecure/pb/proto.js +388 -0
  270. package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
  271. package/src/keychain/{cms.js → cms.ts} +62 -58
  272. package/src/keychain/index.ts +588 -0
  273. package/src/keychain/{util.js → util.ts} +7 -17
  274. package/src/libp2p.ts +501 -0
  275. package/src/metrics/index.ts +310 -0
  276. package/src/metrics/moving-average.ts +53 -0
  277. package/src/metrics/stats.ts +243 -0
  278. package/src/nat-manager.ts +194 -0
  279. package/src/peer-record-updater.ts +55 -0
  280. package/src/peer-routing.ts +185 -0
  281. package/src/ping/README.md +1 -1
  282. package/src/ping/constants.ts +5 -0
  283. package/src/ping/index.ts +83 -0
  284. package/src/pnet/README.md +2 -2
  285. package/src/pnet/crypto.ts +67 -0
  286. package/src/pnet/errors.ts +5 -0
  287. package/src/pnet/index.ts +95 -0
  288. package/src/pnet/key-generator.ts +28 -0
  289. package/src/registrar.ts +205 -0
  290. package/src/transport-manager.ts +279 -0
  291. package/src/upgrader.ts +499 -0
  292. package/src/version.ts +3 -0
  293. package/dist/index.min.js +0 -55
  294. package/dist/src/insecure/plaintext.d.ts +0 -12
  295. package/dist/src/insecure/plaintext.d.ts.map +0 -1
  296. package/dist/src/metrics/old-peers.d.ts +0 -3
  297. package/dist/src/metrics/old-peers.d.ts.map +0 -1
  298. package/dist/src/metrics/tracked-map.d.ts +0 -8
  299. package/dist/src/metrics/tracked-map.d.ts.map +0 -1
  300. package/dist/src/peer-store/address-book.d.ts +0 -67
  301. package/dist/src/peer-store/address-book.d.ts.map +0 -1
  302. package/dist/src/peer-store/index.d.ts +0 -58
  303. package/dist/src/peer-store/index.d.ts.map +0 -1
  304. package/dist/src/peer-store/key-book.d.ts +0 -40
  305. package/dist/src/peer-store/key-book.d.ts.map +0 -1
  306. package/dist/src/peer-store/metadata-book.d.ts +0 -58
  307. package/dist/src/peer-store/metadata-book.d.ts.map +0 -1
  308. package/dist/src/peer-store/pb/peer.d.ts +0 -222
  309. package/dist/src/peer-store/proto-book.d.ts +0 -43
  310. package/dist/src/peer-store/proto-book.d.ts.map +0 -1
  311. package/dist/src/peer-store/store.d.ts +0 -73
  312. package/dist/src/peer-store/store.d.ts.map +0 -1
  313. package/dist/src/peer-store/types.d.ts +0 -202
  314. package/dist/src/peer-store/types.d.ts.map +0 -1
  315. package/dist/src/ping/util.d.ts +0 -5
  316. package/dist/src/ping/util.d.ts.map +0 -1
  317. package/dist/src/pubsub-adapter.d.ts +0 -22
  318. package/dist/src/pubsub-adapter.d.ts.map +0 -1
  319. package/dist/src/record/envelope/envelope.d.ts +0 -77
  320. package/dist/src/record/envelope/index.d.ts +0 -80
  321. package/dist/src/record/envelope/index.d.ts.map +0 -1
  322. package/dist/src/record/peer-record/consts.d.ts +0 -4
  323. package/dist/src/record/peer-record/consts.d.ts.map +0 -1
  324. package/dist/src/record/peer-record/index.d.ts +0 -60
  325. package/dist/src/record/peer-record/index.d.ts.map +0 -1
  326. package/dist/src/record/peer-record/peer-record.d.ts +0 -133
  327. package/dist/src/record/utils.d.ts +0 -12
  328. package/dist/src/record/utils.d.ts.map +0 -1
  329. package/dist/src/types.d.ts +0 -90
  330. package/dist/src/types.d.ts.map +0 -1
  331. package/src/address-manager/index.js +0 -96
  332. package/src/circuit/auto-relay.js +0 -302
  333. package/src/circuit/circuit/hop.js +0 -205
  334. package/src/circuit/circuit/stop.js +0 -81
  335. package/src/circuit/circuit/stream-handler.js +0 -94
  336. package/src/circuit/constants.js +0 -12
  337. package/src/circuit/index.js +0 -102
  338. package/src/circuit/multicodec.js +0 -5
  339. package/src/circuit/transport.js +0 -229
  340. package/src/circuit/utils.js +0 -17
  341. package/src/config.js +0 -114
  342. package/src/connection-manager/auto-dialler.js +0 -132
  343. package/src/connection-manager/index.js +0 -363
  344. package/src/connection-manager/latency-monitor.js +0 -264
  345. package/src/constants.js +0 -18
  346. package/src/content-routing/index.js +0 -163
  347. package/src/content-routing/utils.js +0 -89
  348. package/src/dht/dht-content-routing.js +0 -44
  349. package/src/dht/dht-peer-routing.js +0 -51
  350. package/src/dialer/index.js +0 -376
  351. package/src/errors.js +0 -66
  352. package/src/fetch/constants.js +0 -6
  353. package/src/get-peer.js +0 -49
  354. package/src/identify/consts.js +0 -15
  355. package/src/identify/index.js +0 -384
  356. package/src/index.js +0 -811
  357. package/src/insecure/plaintext.js +0 -95
  358. package/src/keychain/index.js +0 -561
  359. package/src/metrics/index.js +0 -290
  360. package/src/metrics/old-peers.js +0 -16
  361. package/src/metrics/stats.js +0 -270
  362. package/src/metrics/tracked-map.js +0 -94
  363. package/src/nat-manager.js +0 -197
  364. package/src/peer-routing.js +0 -176
  365. package/src/peer-store/README.md +0 -145
  366. package/src/peer-store/address-book.js +0 -382
  367. package/src/peer-store/index.js +0 -121
  368. package/src/peer-store/key-book.js +0 -141
  369. package/src/peer-store/metadata-book.js +0 -250
  370. package/src/peer-store/pb/peer.d.ts +0 -222
  371. package/src/peer-store/pb/peer.js +0 -643
  372. package/src/peer-store/pb/peer.proto +0 -31
  373. package/src/peer-store/proto-book.js +0 -237
  374. package/src/peer-store/store.js +0 -263
  375. package/src/peer-store/types.ts +0 -245
  376. package/src/ping/constants.js +0 -8
  377. package/src/ping/index.js +0 -85
  378. package/src/ping/util.js +0 -18
  379. package/src/pnet/crypto.js +0 -84
  380. package/src/pnet/errors.js +0 -7
  381. package/src/pnet/index.js +0 -86
  382. package/src/pnet/key-generator.js +0 -33
  383. package/src/pubsub-adapter.js +0 -61
  384. package/src/record/README.md +0 -130
  385. package/src/record/envelope/envelope.d.ts +0 -77
  386. package/src/record/envelope/envelope.js +0 -243
  387. package/src/record/envelope/envelope.proto +0 -19
  388. package/src/record/envelope/index.js +0 -183
  389. package/src/record/peer-record/consts.js +0 -14
  390. package/src/record/peer-record/index.js +0 -113
  391. package/src/record/peer-record/peer-record.d.ts +0 -133
  392. package/src/record/peer-record/peer-record.js +0 -367
  393. package/src/record/peer-record/peer-record.proto +0 -18
  394. package/src/record/utils.js +0 -25
  395. package/src/registrar.js +0 -127
  396. package/src/transport-manager.js +0 -269
  397. package/src/types.ts +0 -98
  398. package/src/upgrader.js +0 -492
@@ -0,0 +1,369 @@
1
+ /*eslint-disable*/
2
+ import $protobuf from "protobufjs/minimal.js";
3
+
4
+ // Common aliases
5
+ const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
6
+
7
+ // Exported root namespace
8
+ const $root = $protobuf.roots["libp2p-identify"] || ($protobuf.roots["libp2p-identify"] = {});
9
+
10
+ export const Identify = $root.Identify = (() => {
11
+
12
+ /**
13
+ * Properties of an Identify.
14
+ * @exports IIdentify
15
+ * @interface IIdentify
16
+ * @property {string|null} [protocolVersion] Identify protocolVersion
17
+ * @property {string|null} [agentVersion] Identify agentVersion
18
+ * @property {Uint8Array|null} [publicKey] Identify publicKey
19
+ * @property {Array.<Uint8Array>|null} [listenAddrs] Identify listenAddrs
20
+ * @property {Uint8Array|null} [observedAddr] Identify observedAddr
21
+ * @property {Array.<string>|null} [protocols] Identify protocols
22
+ * @property {Uint8Array|null} [signedPeerRecord] Identify signedPeerRecord
23
+ */
24
+
25
+ /**
26
+ * Constructs a new Identify.
27
+ * @exports Identify
28
+ * @classdesc Represents an Identify.
29
+ * @implements IIdentify
30
+ * @constructor
31
+ * @param {IIdentify=} [p] Properties to set
32
+ */
33
+ function Identify(p) {
34
+ this.listenAddrs = [];
35
+ this.protocols = [];
36
+ if (p)
37
+ for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
38
+ if (p[ks[i]] != null)
39
+ this[ks[i]] = p[ks[i]];
40
+ }
41
+
42
+ /**
43
+ * Identify protocolVersion.
44
+ * @member {string|null|undefined} protocolVersion
45
+ * @memberof Identify
46
+ * @instance
47
+ */
48
+ Identify.prototype.protocolVersion = null;
49
+
50
+ /**
51
+ * Identify agentVersion.
52
+ * @member {string|null|undefined} agentVersion
53
+ * @memberof Identify
54
+ * @instance
55
+ */
56
+ Identify.prototype.agentVersion = null;
57
+
58
+ /**
59
+ * Identify publicKey.
60
+ * @member {Uint8Array|null|undefined} publicKey
61
+ * @memberof Identify
62
+ * @instance
63
+ */
64
+ Identify.prototype.publicKey = null;
65
+
66
+ /**
67
+ * Identify listenAddrs.
68
+ * @member {Array.<Uint8Array>} listenAddrs
69
+ * @memberof Identify
70
+ * @instance
71
+ */
72
+ Identify.prototype.listenAddrs = $util.emptyArray;
73
+
74
+ /**
75
+ * Identify observedAddr.
76
+ * @member {Uint8Array|null|undefined} observedAddr
77
+ * @memberof Identify
78
+ * @instance
79
+ */
80
+ Identify.prototype.observedAddr = null;
81
+
82
+ /**
83
+ * Identify protocols.
84
+ * @member {Array.<string>} protocols
85
+ * @memberof Identify
86
+ * @instance
87
+ */
88
+ Identify.prototype.protocols = $util.emptyArray;
89
+
90
+ /**
91
+ * Identify signedPeerRecord.
92
+ * @member {Uint8Array|null|undefined} signedPeerRecord
93
+ * @memberof Identify
94
+ * @instance
95
+ */
96
+ Identify.prototype.signedPeerRecord = null;
97
+
98
+ // OneOf field names bound to virtual getters and setters
99
+ let $oneOfFields;
100
+
101
+ /**
102
+ * Identify _protocolVersion.
103
+ * @member {"protocolVersion"|undefined} _protocolVersion
104
+ * @memberof Identify
105
+ * @instance
106
+ */
107
+ Object.defineProperty(Identify.prototype, "_protocolVersion", {
108
+ get: $util.oneOfGetter($oneOfFields = ["protocolVersion"]),
109
+ set: $util.oneOfSetter($oneOfFields)
110
+ });
111
+
112
+ /**
113
+ * Identify _agentVersion.
114
+ * @member {"agentVersion"|undefined} _agentVersion
115
+ * @memberof Identify
116
+ * @instance
117
+ */
118
+ Object.defineProperty(Identify.prototype, "_agentVersion", {
119
+ get: $util.oneOfGetter($oneOfFields = ["agentVersion"]),
120
+ set: $util.oneOfSetter($oneOfFields)
121
+ });
122
+
123
+ /**
124
+ * Identify _publicKey.
125
+ * @member {"publicKey"|undefined} _publicKey
126
+ * @memberof Identify
127
+ * @instance
128
+ */
129
+ Object.defineProperty(Identify.prototype, "_publicKey", {
130
+ get: $util.oneOfGetter($oneOfFields = ["publicKey"]),
131
+ set: $util.oneOfSetter($oneOfFields)
132
+ });
133
+
134
+ /**
135
+ * Identify _observedAddr.
136
+ * @member {"observedAddr"|undefined} _observedAddr
137
+ * @memberof Identify
138
+ * @instance
139
+ */
140
+ Object.defineProperty(Identify.prototype, "_observedAddr", {
141
+ get: $util.oneOfGetter($oneOfFields = ["observedAddr"]),
142
+ set: $util.oneOfSetter($oneOfFields)
143
+ });
144
+
145
+ /**
146
+ * Identify _signedPeerRecord.
147
+ * @member {"signedPeerRecord"|undefined} _signedPeerRecord
148
+ * @memberof Identify
149
+ * @instance
150
+ */
151
+ Object.defineProperty(Identify.prototype, "_signedPeerRecord", {
152
+ get: $util.oneOfGetter($oneOfFields = ["signedPeerRecord"]),
153
+ set: $util.oneOfSetter($oneOfFields)
154
+ });
155
+
156
+ /**
157
+ * Encodes the specified Identify message. Does not implicitly {@link Identify.verify|verify} messages.
158
+ * @function encode
159
+ * @memberof Identify
160
+ * @static
161
+ * @param {IIdentify} m Identify message or plain object to encode
162
+ * @param {$protobuf.Writer} [w] Writer to encode to
163
+ * @returns {$protobuf.Writer} Writer
164
+ */
165
+ Identify.encode = function encode(m, w) {
166
+ if (!w)
167
+ w = $Writer.create();
168
+ if (m.publicKey != null && Object.hasOwnProperty.call(m, "publicKey"))
169
+ w.uint32(10).bytes(m.publicKey);
170
+ if (m.listenAddrs != null && m.listenAddrs.length) {
171
+ for (var i = 0; i < m.listenAddrs.length; ++i)
172
+ w.uint32(18).bytes(m.listenAddrs[i]);
173
+ }
174
+ if (m.protocols != null && m.protocols.length) {
175
+ for (var i = 0; i < m.protocols.length; ++i)
176
+ w.uint32(26).string(m.protocols[i]);
177
+ }
178
+ if (m.observedAddr != null && Object.hasOwnProperty.call(m, "observedAddr"))
179
+ w.uint32(34).bytes(m.observedAddr);
180
+ if (m.protocolVersion != null && Object.hasOwnProperty.call(m, "protocolVersion"))
181
+ w.uint32(42).string(m.protocolVersion);
182
+ if (m.agentVersion != null && Object.hasOwnProperty.call(m, "agentVersion"))
183
+ w.uint32(50).string(m.agentVersion);
184
+ if (m.signedPeerRecord != null && Object.hasOwnProperty.call(m, "signedPeerRecord"))
185
+ w.uint32(66).bytes(m.signedPeerRecord);
186
+ return w;
187
+ };
188
+
189
+ /**
190
+ * Decodes an Identify message from the specified reader or buffer.
191
+ * @function decode
192
+ * @memberof Identify
193
+ * @static
194
+ * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
195
+ * @param {number} [l] Message length if known beforehand
196
+ * @returns {Identify} Identify
197
+ * @throws {Error} If the payload is not a reader or valid buffer
198
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
199
+ */
200
+ Identify.decode = function decode(r, l) {
201
+ if (!(r instanceof $Reader))
202
+ r = $Reader.create(r);
203
+ var c = l === undefined ? r.len : r.pos + l, m = new $root.Identify();
204
+ while (r.pos < c) {
205
+ var t = r.uint32();
206
+ switch (t >>> 3) {
207
+ case 5:
208
+ m.protocolVersion = r.string();
209
+ break;
210
+ case 6:
211
+ m.agentVersion = r.string();
212
+ break;
213
+ case 1:
214
+ m.publicKey = r.bytes();
215
+ break;
216
+ case 2:
217
+ if (!(m.listenAddrs && m.listenAddrs.length))
218
+ m.listenAddrs = [];
219
+ m.listenAddrs.push(r.bytes());
220
+ break;
221
+ case 4:
222
+ m.observedAddr = r.bytes();
223
+ break;
224
+ case 3:
225
+ if (!(m.protocols && m.protocols.length))
226
+ m.protocols = [];
227
+ m.protocols.push(r.string());
228
+ break;
229
+ case 8:
230
+ m.signedPeerRecord = r.bytes();
231
+ break;
232
+ default:
233
+ r.skipType(t & 7);
234
+ break;
235
+ }
236
+ }
237
+ return m;
238
+ };
239
+
240
+ /**
241
+ * Creates an Identify message from a plain object. Also converts values to their respective internal types.
242
+ * @function fromObject
243
+ * @memberof Identify
244
+ * @static
245
+ * @param {Object.<string,*>} d Plain object
246
+ * @returns {Identify} Identify
247
+ */
248
+ Identify.fromObject = function fromObject(d) {
249
+ if (d instanceof $root.Identify)
250
+ return d;
251
+ var m = new $root.Identify();
252
+ if (d.protocolVersion != null) {
253
+ m.protocolVersion = String(d.protocolVersion);
254
+ }
255
+ if (d.agentVersion != null) {
256
+ m.agentVersion = String(d.agentVersion);
257
+ }
258
+ if (d.publicKey != null) {
259
+ if (typeof d.publicKey === "string")
260
+ $util.base64.decode(d.publicKey, m.publicKey = $util.newBuffer($util.base64.length(d.publicKey)), 0);
261
+ else if (d.publicKey.length)
262
+ m.publicKey = d.publicKey;
263
+ }
264
+ if (d.listenAddrs) {
265
+ if (!Array.isArray(d.listenAddrs))
266
+ throw TypeError(".Identify.listenAddrs: array expected");
267
+ m.listenAddrs = [];
268
+ for (var i = 0; i < d.listenAddrs.length; ++i) {
269
+ if (typeof d.listenAddrs[i] === "string")
270
+ $util.base64.decode(d.listenAddrs[i], m.listenAddrs[i] = $util.newBuffer($util.base64.length(d.listenAddrs[i])), 0);
271
+ else if (d.listenAddrs[i].length)
272
+ m.listenAddrs[i] = d.listenAddrs[i];
273
+ }
274
+ }
275
+ if (d.observedAddr != null) {
276
+ if (typeof d.observedAddr === "string")
277
+ $util.base64.decode(d.observedAddr, m.observedAddr = $util.newBuffer($util.base64.length(d.observedAddr)), 0);
278
+ else if (d.observedAddr.length)
279
+ m.observedAddr = d.observedAddr;
280
+ }
281
+ if (d.protocols) {
282
+ if (!Array.isArray(d.protocols))
283
+ throw TypeError(".Identify.protocols: array expected");
284
+ m.protocols = [];
285
+ for (var i = 0; i < d.protocols.length; ++i) {
286
+ m.protocols[i] = String(d.protocols[i]);
287
+ }
288
+ }
289
+ if (d.signedPeerRecord != null) {
290
+ if (typeof d.signedPeerRecord === "string")
291
+ $util.base64.decode(d.signedPeerRecord, m.signedPeerRecord = $util.newBuffer($util.base64.length(d.signedPeerRecord)), 0);
292
+ else if (d.signedPeerRecord.length)
293
+ m.signedPeerRecord = d.signedPeerRecord;
294
+ }
295
+ return m;
296
+ };
297
+
298
+ /**
299
+ * Creates a plain object from an Identify message. Also converts values to other types if specified.
300
+ * @function toObject
301
+ * @memberof Identify
302
+ * @static
303
+ * @param {Identify} m Identify
304
+ * @param {$protobuf.IConversionOptions} [o] Conversion options
305
+ * @returns {Object.<string,*>} Plain object
306
+ */
307
+ Identify.toObject = function toObject(m, o) {
308
+ if (!o)
309
+ o = {};
310
+ var d = {};
311
+ if (o.arrays || o.defaults) {
312
+ d.listenAddrs = [];
313
+ d.protocols = [];
314
+ }
315
+ if (m.publicKey != null && m.hasOwnProperty("publicKey")) {
316
+ d.publicKey = o.bytes === String ? $util.base64.encode(m.publicKey, 0, m.publicKey.length) : o.bytes === Array ? Array.prototype.slice.call(m.publicKey) : m.publicKey;
317
+ if (o.oneofs)
318
+ d._publicKey = "publicKey";
319
+ }
320
+ if (m.listenAddrs && m.listenAddrs.length) {
321
+ d.listenAddrs = [];
322
+ for (var j = 0; j < m.listenAddrs.length; ++j) {
323
+ d.listenAddrs[j] = o.bytes === String ? $util.base64.encode(m.listenAddrs[j], 0, m.listenAddrs[j].length) : o.bytes === Array ? Array.prototype.slice.call(m.listenAddrs[j]) : m.listenAddrs[j];
324
+ }
325
+ }
326
+ if (m.protocols && m.protocols.length) {
327
+ d.protocols = [];
328
+ for (var j = 0; j < m.protocols.length; ++j) {
329
+ d.protocols[j] = m.protocols[j];
330
+ }
331
+ }
332
+ if (m.observedAddr != null && m.hasOwnProperty("observedAddr")) {
333
+ d.observedAddr = o.bytes === String ? $util.base64.encode(m.observedAddr, 0, m.observedAddr.length) : o.bytes === Array ? Array.prototype.slice.call(m.observedAddr) : m.observedAddr;
334
+ if (o.oneofs)
335
+ d._observedAddr = "observedAddr";
336
+ }
337
+ if (m.protocolVersion != null && m.hasOwnProperty("protocolVersion")) {
338
+ d.protocolVersion = m.protocolVersion;
339
+ if (o.oneofs)
340
+ d._protocolVersion = "protocolVersion";
341
+ }
342
+ if (m.agentVersion != null && m.hasOwnProperty("agentVersion")) {
343
+ d.agentVersion = m.agentVersion;
344
+ if (o.oneofs)
345
+ d._agentVersion = "agentVersion";
346
+ }
347
+ if (m.signedPeerRecord != null && m.hasOwnProperty("signedPeerRecord")) {
348
+ d.signedPeerRecord = o.bytes === String ? $util.base64.encode(m.signedPeerRecord, 0, m.signedPeerRecord.length) : o.bytes === Array ? Array.prototype.slice.call(m.signedPeerRecord) : m.signedPeerRecord;
349
+ if (o.oneofs)
350
+ d._signedPeerRecord = "signedPeerRecord";
351
+ }
352
+ return d;
353
+ };
354
+
355
+ /**
356
+ * Converts this Identify to JSON.
357
+ * @function toJSON
358
+ * @memberof Identify
359
+ * @instance
360
+ * @returns {Object.<string,*>} JSON object
361
+ */
362
+ Identify.prototype.toJSON = function toJSON() {
363
+ return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
364
+ };
365
+
366
+ return Identify;
367
+ })();
368
+
369
+ export { $root as default };
File without changes
package/src/index.ts ADDED
@@ -0,0 +1,234 @@
1
+ import { createLibp2pNode } from './libp2p.js'
2
+ import type { AbortOptions, EventEmitter, RecursivePartial, Startable } from '@libp2p/interfaces'
3
+ import type { Multiaddr } from '@multiformats/multiaddr'
4
+ import type { FAULT_TOLERANCE } from './transport-manager.js'
5
+ import type { HostProperties } from './identify/index.js'
6
+ import type { DualDHT } from '@libp2p/interfaces/dht'
7
+ import type { Datastore } from 'interface-datastore'
8
+ import type { PeerStore, PeerStoreInit } from '@libp2p/interfaces/peer-store'
9
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
10
+ import type { AutoRelayConfig, RelayAdvertiseConfig } from './circuit/index.js'
11
+ import type { PeerDiscovery } from '@libp2p/interfaces/peer-discovery'
12
+ import type { Connection, ConnectionGater, ConnectionProtector, ProtocolStream } from '@libp2p/interfaces/connection'
13
+ import type { Transport } from '@libp2p/interfaces/transport'
14
+ import type { StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer'
15
+ import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
16
+ import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
17
+ import type { ContentRouting } from '@libp2p/interfaces/content-routing'
18
+ import type { PubSub } from '@libp2p/interfaces/pubsub'
19
+ import type { ConnectionManager, StreamHandler } from '@libp2p/interfaces/registrar'
20
+ import type { MetricsInit } from '@libp2p/interfaces/metrics'
21
+ import type { PeerInfo } from '@libp2p/interfaces/peer-info'
22
+ import type { DialerInit } from '@libp2p/interfaces/dialer'
23
+ import type { KeyChain } from './keychain/index.js'
24
+
25
+ export interface PersistentPeerStoreOptions {
26
+ threshold?: number
27
+ }
28
+
29
+ export interface DEKConfig {
30
+ keyLength: number
31
+ iterationCount: number
32
+ salt: string
33
+ hash: string
34
+ }
35
+
36
+ export interface KeychainConfig {
37
+ pass?: string
38
+ dek?: DEKConfig
39
+ }
40
+
41
+ export interface MetricsConfig {
42
+ enabled?: boolean
43
+ }
44
+
45
+ export interface HopConfig {
46
+ enabled?: boolean
47
+ active?: boolean
48
+ }
49
+
50
+ export interface RelayConfig {
51
+ enabled: boolean
52
+ advertise: RelayAdvertiseConfig
53
+ hop: HopConfig
54
+ autoRelay: AutoRelayConfig
55
+ }
56
+
57
+ export interface NatManagerConfig {
58
+ enabled: boolean
59
+ externalAddress?: string
60
+ localAddress?: string
61
+ description?: string
62
+ ttl?: number
63
+ keepAlive: boolean
64
+ gateway?: string
65
+ }
66
+
67
+ export interface AddressesConfig {
68
+ listen: string[]
69
+ announce: string[]
70
+ noAnnounce: string[]
71
+ announceFilter: (multiaddrs: Multiaddr[]) => Multiaddr[]
72
+ }
73
+
74
+ export interface ConnectionManagerConfig {
75
+ /**
76
+ * If true, try to connect to all discovered peers up to the connection manager limit
77
+ */
78
+ autoDial?: boolean
79
+
80
+ /**
81
+ * The maximum number of connections to keep open
82
+ */
83
+ maxConnections: number
84
+
85
+ /**
86
+ * The minimum number of connections to keep open
87
+ */
88
+ minConnections: number
89
+
90
+ /**
91
+ * How long to wait between attempting to keep our number of concurrent connections
92
+ * above minConnections
93
+ */
94
+ autoDialInterval: number
95
+ }
96
+
97
+ export interface TransportManagerConfig {
98
+ faultTolerance?: FAULT_TOLERANCE
99
+ }
100
+
101
+ export interface PeerStoreConfig {
102
+ persistence?: boolean
103
+ threshold?: number
104
+ }
105
+
106
+ export interface PeerRoutingConfig {
107
+ refreshManager: RefreshManagerConfig
108
+ }
109
+
110
+ export interface RefreshManagerConfig {
111
+ enabled?: boolean
112
+ interval: number
113
+ bootDelay: number
114
+ }
115
+
116
+ export interface Libp2pInit {
117
+ peerId: PeerId
118
+ host: HostProperties
119
+ addresses: AddressesConfig
120
+ connectionManager: ConnectionManagerConfig
121
+ connectionGater: Partial<ConnectionGater>
122
+ transportManager: TransportManagerConfig
123
+ datastore: Datastore
124
+ dialer: DialerInit
125
+ metrics: MetricsInit
126
+ peerStore: PeerStoreInit
127
+ peerRouting: PeerRoutingConfig
128
+ keychain: KeychainConfig
129
+ protocolPrefix: string
130
+ nat: NatManagerConfig
131
+ relay: RelayConfig
132
+
133
+ transports: Transport[]
134
+ streamMuxers?: StreamMuxerFactory[]
135
+ connectionEncryption?: ConnectionEncrypter[]
136
+ peerDiscovery?: PeerDiscovery[]
137
+ peerRouters?: PeerRouting[]
138
+ contentRouters?: ContentRouting[]
139
+ dht?: DualDHT
140
+ pubsub?: PubSub
141
+ connectionProtector?: ConnectionProtector
142
+ }
143
+
144
+ export interface Libp2pEvents {
145
+ 'peer:discovery': CustomEvent<PeerInfo>
146
+ }
147
+
148
+ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
149
+ peerId: PeerId
150
+ peerStore: PeerStore
151
+ peerRouting: PeerRouting
152
+ contentRouting: ContentRouting
153
+ keychain: KeyChain
154
+ connectionManager: ConnectionManager
155
+
156
+ pubsub?: PubSub
157
+ dht?: DualDHT
158
+
159
+ /**
160
+ * Load keychain keys from the datastore.
161
+ * Imports the private key as 'self', if needed.
162
+ */
163
+ loadKeychain: () => Promise<void>
164
+
165
+ /**
166
+ * Get a deduplicated list of peer advertising multiaddrs by concatenating
167
+ * the listen addresses used by transports with any configured
168
+ * announce addresses as well as observed addresses reported by peers.
169
+ *
170
+ * If Announce addrs are specified, configured listen addresses will be
171
+ * ignored though observed addresses will still be included.
172
+ */
173
+ getMultiaddrs: () => Multiaddr[]
174
+
175
+ /**
176
+ * Return a list of all connections this node has open, optionally filtering
177
+ * by a PeerId
178
+ */
179
+ getConnections: (peerId?: PeerId) => Connection[]
180
+
181
+ /**
182
+ * Return a list of all peers we currently have a connection open to
183
+ */
184
+ getPeers: () => PeerId[]
185
+
186
+ /**
187
+ * Dials to the provided peer. If successful, the known metadata of the
188
+ * peer will be added to the nodes `peerStore`
189
+ */
190
+ dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
191
+
192
+ /**
193
+ * Dials to the provided peer and tries to handshake with the given protocols in order.
194
+ * If successful, the known metadata of the peer will be added to the nodes `peerStore`,
195
+ * and the `MuxedStream` will be returned together with the successful negotiated protocol.
196
+ */
197
+ dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<ProtocolStream>
198
+
199
+ /**
200
+ * Disconnects all connections to the given `peer`
201
+ */
202
+ hangUp: (peer: PeerId | Multiaddr | string) => Promise<void>
203
+
204
+ /**
205
+ * Registers the `handler` for each protocol
206
+ */
207
+ handle: (protocol: string | string[], handler: StreamHandler) => Promise<void>
208
+
209
+ /**
210
+ * Removes the handler for each protocol. The protocol
211
+ * will no longer be supported on streams.
212
+ */
213
+ unhandle: (protocols: string[] | string) => Promise<void>
214
+
215
+ /**
216
+ * Pings the given peer in order to obtain the operation latency
217
+ */
218
+ ping: (peer: Multiaddr |PeerId) => Promise<number>
219
+
220
+ /**
221
+ * Sends a request to fetch the value associated with the given key from the given peer.
222
+ */
223
+ fetch: (peer: PeerId | Multiaddr | string, key: string) => Promise<Uint8Array | null>
224
+ }
225
+
226
+ export type Libp2pOptions = RecursivePartial<Libp2pInit>
227
+
228
+ /**
229
+ * Returns a new instance of the Libp2p interface, generating a new PeerId
230
+ * if one is not passed as part of the options.
231
+ */
232
+ export async function createLibp2p (options: Libp2pOptions): Promise<Libp2p> {
233
+ return await createLibp2pNode(options)
234
+ }
@@ -0,0 +1,97 @@
1
+ import { logger } from '@libp2p/logger'
2
+ import { handshake } from 'it-handshake'
3
+ import * as lp from 'it-length-prefixed'
4
+ import { UnexpectedPeerError, InvalidCryptoExchangeError } from '@libp2p/interfaces/connection-encrypter/errors'
5
+ import { Exchange, IExchange, KeyType } from './pb/proto.js'
6
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
7
+ import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
8
+ import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interfaces/connection-encrypter'
9
+ import type { Duplex } from 'it-stream-types'
10
+
11
+ const log = logger('libp2p:plaintext')
12
+ const PROTOCOL = '/plaintext/2.0.0'
13
+
14
+ function lpEncodeExchange (exchange: IExchange) {
15
+ const pb = Exchange.encode(exchange).finish()
16
+
17
+ return lp.encode.single(pb)
18
+ }
19
+
20
+ /**
21
+ * Encrypt connection
22
+ */
23
+ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
24
+ const shake = handshake(conn)
25
+
26
+ let type = KeyType.RSA
27
+
28
+ if (localId.type === 'Ed25519') {
29
+ type = KeyType.Ed25519
30
+ } else if (localId.type === 'secp256k1') {
31
+ type = KeyType.Secp256k1
32
+ }
33
+
34
+ // Encode the public key and write it to the remote peer
35
+ shake.write(
36
+ lpEncodeExchange({
37
+ id: localId.toBytes(),
38
+ pubkey: {
39
+ Type: type,
40
+ Data: localId.publicKey
41
+ }
42
+ }).slice()
43
+ )
44
+
45
+ log('write pubkey exchange to peer %p', remoteId)
46
+
47
+ // Get the Exchange message
48
+ // @ts-expect-error needs to be generator
49
+ const response = (await lp.decode.fromReader(shake.reader).next()).value
50
+ const id = Exchange.decode(response.slice())
51
+ log('read pubkey exchange from peer %p', remoteId)
52
+
53
+ let peerId
54
+ try {
55
+ if (id.pubkey.Data.length === 0) {
56
+ throw new Error('Public key data too short')
57
+ }
58
+
59
+ if (id.id == null) {
60
+ throw new Error('Remote id missing')
61
+ }
62
+
63
+ peerId = await peerIdFromKeys(id.pubkey.Data)
64
+
65
+ if (!peerId.equals(peerIdFromBytes(id.id))) {
66
+ throw new Error('Public key did not match id')
67
+ }
68
+ } catch (err: any) {
69
+ log.error(err)
70
+ throw new InvalidCryptoExchangeError('Remote did not provide its public key')
71
+ }
72
+
73
+ if (remoteId != null && !peerId.equals(remoteId)) {
74
+ throw new UnexpectedPeerError()
75
+ }
76
+
77
+ log('plaintext key exchange completed successfully with peer %p', peerId)
78
+
79
+ shake.rest()
80
+ return {
81
+ conn: shake.stream,
82
+ remotePeer: peerId,
83
+ remoteEarlyData: new Uint8Array()
84
+ }
85
+ }
86
+
87
+ export class Plaintext implements ConnectionEncrypter {
88
+ public protocol: string = PROTOCOL
89
+
90
+ async secureInbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
91
+ return await encrypt(localId, conn, remoteId)
92
+ }
93
+
94
+ async secureOutbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId: PeerId): Promise<SecuredConnection> {
95
+ return await encrypt(localId, conn, remoteId)
96
+ }
97
+ }