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
@@ -1,130 +0,0 @@
1
- # Libp2p Records
2
-
3
- Libp2p nodes need to store data in a public location (e.g. a DHT), or rely on potentially untrustworthy intermediaries to relay information over its lifetime. Accordingly, libp2p nodes need to be able to verify that the data came from a specific peer and that it hasn't been tampered with.
4
-
5
- ## Envelope
6
-
7
- Libp2p provides an all-purpose data container called **envelope**. It was created to enable the distribution of verifiable records, which we can prove originated from the addressed peer itself. The envelope includes a signature of the data, so that its authenticity is verified.
8
-
9
- This envelope stores a marshaled record implementing the [interface-record](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/record). These Records are designed to be serialized to bytes and placed inside of the envelopes before being shared with other peers.
10
-
11
- You can read further about the envelope in [libp2p/specs#217](https://github.com/libp2p/specs/pull/217).
12
-
13
- ### Usage
14
-
15
- - create an envelope with an instance of an [interface-record](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/record) implementation and prepare it for being exchanged:
16
-
17
- ```js
18
- // interface-record implementation example with the "libp2p-example" namespace
19
- const Record = require('libp2p-interfaces/src/record')
20
- const { fromString } = require('uint8arrays/from-string')
21
-
22
- class ExampleRecord extends Record {
23
- constructor () {
24
- super ('libp2p-example', fromString('0302', 'hex'))
25
- }
26
-
27
- marshal () {}
28
-
29
- equals (other) {}
30
- }
31
-
32
- ExampleRecord.createFromProtobuf = () => {}
33
- ```
34
-
35
- ```js
36
- const Envelope = require('libp2p/src/record/envelop')
37
- const ExampleRecord = require('./example-record')
38
-
39
- const rec = new ExampleRecord()
40
- const e = await Envelope.seal(rec, peerId)
41
- const wireData = e.marshal()
42
- ```
43
-
44
- - consume a received envelope (`wireData`) and transform it back to a record:
45
-
46
- ```js
47
- const Envelope = require('libp2p/src/record/envelop')
48
- const ExampleRecord = require('./example-record')
49
-
50
- const domain = 'libp2p-example'
51
- let e
52
-
53
- try {
54
- e = await Envelope.openAndCertify(wireData, domain)
55
- } catch (err) {}
56
-
57
- const rec = ExampleRecord.createFromProtobuf(e.payload)
58
- ```
59
-
60
- ## Peer Record
61
-
62
- All libp2p nodes keep a `PeerStore`, that among other information stores a set of known addresses for each peer, which can come from a variety of sources.
63
-
64
- Libp2p peer records were created to enable the distribution of verifiable address records, which we can prove originated from the addressed peer itself. With such guarantees, libp2p is able to prioritize addresses based on their authenticity, with the most strict strategy being to only dial certified addresses (no strategies have been implemented at the time of writing).
65
-
66
- A peer record contains the peers' publicly reachable listen addresses, and may be extended in the future to contain additional metadata relevant to routing. It also contains a `seqNumber` field, a timestamp per the spec, so that we can verify the most recent record.
67
-
68
- You can read further about the Peer Record in [libp2p/specs#217](https://github.com/libp2p/specs/pull/217).
69
-
70
- ### Usage
71
-
72
- - create a new Peer Record
73
-
74
- ```js
75
- const PeerRecord = require('libp2p/src/record/peer-record')
76
-
77
- const pr = new PeerRecord({
78
- peerId: node.peerId,
79
- multiaddrs: node.multiaddrs
80
- })
81
- ```
82
-
83
- - create a Peer Record from a protobuf
84
-
85
- ```js
86
- const PeerRecord = require('libp2p/src/record/peer-record')
87
-
88
- const pr = PeerRecord.createFromProtobuf(data)
89
- ```
90
-
91
- ### Libp2p Flows
92
-
93
- #### Self Record
94
-
95
- Once a libp2p node has started and is listening on a set of multiaddrs, its own peer record can be created.
96
-
97
- The identify service is responsible for creating the self record when the identify protocol kicks in for the first time. This record will be stored for future needs of the identify protocol when connecting with other peers.
98
-
99
- #### Self record Updates
100
-
101
- **_NOT_YET_IMPLEMENTED_**
102
-
103
- While creating peer records is fairly trivial, addresses are not static and might be modified at arbitrary times. This can happen via an Address Manager API, or even through AutoRelay/AutoNAT.
104
-
105
- When a libp2p node changes its listen addresses, the identify service will be informed. Once that happens, the identify service creates a new self record and stores it. With the new record, the identify push/delta protocol will be used to communicate this change to the connected peers.
106
-
107
- #### Subsystem receiving a record
108
-
109
- Considering that a node can discover other peers' addresses from a variety of sources, Libp2p Peerstore can differentiate the addresses that were obtained through a signed peer record.
110
-
111
- Once a record is received and its signature properly validated, its envelope is stored in the AddressBook in its byte representation. The `seqNumber` remains unmarshalled so that we can quickly compare it against incoming records to determine the most recent record.
112
-
113
- The AddressBook Addresses will be updated with the content of the envelope with a certified property. This allows other subsystems to identify the known certified addresses of a peer.
114
-
115
- #### Subsystem providing a record
116
-
117
- Libp2p subsystems that exchange other peers information will provide the envelope that they received by those peers. As a result, other peers can verify if the envelope was really created by the addressed peer.
118
-
119
- When a subsystem wants to provide a record, it will get it from the AddressBook, if it exists. Other subsystems are also able to provide the self record, since it is also stored in the AddressBook.
120
-
121
- ### Future Work
122
-
123
- - Persistence only considering certified addresses?
124
- - Peers may not know their own addresses. It's often impossible to automatically infer one's own public address, and peers may need to rely on third party peers to inform them of their observed public addresses.
125
- - A peer may inadvertently or maliciously sign an address that they do not control. In other words, a signature isn't a guarantee that a given address is valid.
126
- - Some addresses may be ambiguous. For example, addresses on a private subnet are valid within that subnet but are useless on the public internet.
127
- - Once all these pieces are in place, we will also need a way to prioritize addresses based on their authenticity, that is, the dialer can prioritize self-certified addresses over addresses from an unknown origin.
128
- - Modular dialer? (taken from go PR notes)
129
- - With the modular dialer, users should easily be able to configure precedence. With dialer v1, anything we do to prioritise dials is gonna be spaghetti and adhoc. With the modular dialer, you’d be able to specify the order of dials when instantiating the pipeline.
130
- - Multiple parallel dials. We already have the issue where new addresses aren't added to existing dials.
@@ -1,77 +0,0 @@
1
- import * as $protobuf from "protobufjs";
2
- /** Properties of an Envelope. */
3
- export interface IEnvelope {
4
-
5
- /** Envelope publicKey */
6
- publicKey?: (Uint8Array|null);
7
-
8
- /** Envelope payloadType */
9
- payloadType?: (Uint8Array|null);
10
-
11
- /** Envelope payload */
12
- payload?: (Uint8Array|null);
13
-
14
- /** Envelope signature */
15
- signature?: (Uint8Array|null);
16
- }
17
-
18
- /** Represents an Envelope. */
19
- export class Envelope implements IEnvelope {
20
-
21
- /**
22
- * Constructs a new Envelope.
23
- * @param [p] Properties to set
24
- */
25
- constructor(p?: IEnvelope);
26
-
27
- /** Envelope publicKey. */
28
- public publicKey: Uint8Array;
29
-
30
- /** Envelope payloadType. */
31
- public payloadType: Uint8Array;
32
-
33
- /** Envelope payload. */
34
- public payload: Uint8Array;
35
-
36
- /** Envelope signature. */
37
- public signature: Uint8Array;
38
-
39
- /**
40
- * Encodes the specified Envelope message. Does not implicitly {@link Envelope.verify|verify} messages.
41
- * @param m Envelope message or plain object to encode
42
- * @param [w] Writer to encode to
43
- * @returns Writer
44
- */
45
- public static encode(m: IEnvelope, w?: $protobuf.Writer): $protobuf.Writer;
46
-
47
- /**
48
- * Decodes an Envelope message from the specified reader or buffer.
49
- * @param r Reader or buffer to decode from
50
- * @param [l] Message length if known beforehand
51
- * @returns Envelope
52
- * @throws {Error} If the payload is not a reader or valid buffer
53
- * @throws {$protobuf.util.ProtocolError} If required fields are missing
54
- */
55
- public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Envelope;
56
-
57
- /**
58
- * Creates an Envelope message from a plain object. Also converts values to their respective internal types.
59
- * @param d Plain object
60
- * @returns Envelope
61
- */
62
- public static fromObject(d: { [k: string]: any }): Envelope;
63
-
64
- /**
65
- * Creates a plain object from an Envelope message. Also converts values to other types if specified.
66
- * @param m Envelope
67
- * @param [o] Conversion options
68
- * @returns Plain object
69
- */
70
- public static toObject(m: Envelope, o?: $protobuf.IConversionOptions): { [k: string]: any };
71
-
72
- /**
73
- * Converts this Envelope to JSON.
74
- * @returns JSON object
75
- */
76
- public toJSON(): { [k: string]: any };
77
- }
@@ -1,243 +0,0 @@
1
- /*eslint-disable*/
2
- "use strict";
3
-
4
- var $protobuf = require("protobufjs/minimal");
5
-
6
- // Common aliases
7
- var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
8
-
9
- // Exported root namespace
10
- var $root = $protobuf.roots["libp2p-envelope"] || ($protobuf.roots["libp2p-envelope"] = {});
11
-
12
- $root.Envelope = (function() {
13
-
14
- /**
15
- * Properties of an Envelope.
16
- * @exports IEnvelope
17
- * @interface IEnvelope
18
- * @property {Uint8Array|null} [publicKey] Envelope publicKey
19
- * @property {Uint8Array|null} [payloadType] Envelope payloadType
20
- * @property {Uint8Array|null} [payload] Envelope payload
21
- * @property {Uint8Array|null} [signature] Envelope signature
22
- */
23
-
24
- /**
25
- * Constructs a new Envelope.
26
- * @exports Envelope
27
- * @classdesc Represents an Envelope.
28
- * @implements IEnvelope
29
- * @constructor
30
- * @param {IEnvelope=} [p] Properties to set
31
- */
32
- function Envelope(p) {
33
- if (p)
34
- for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
35
- if (p[ks[i]] != null)
36
- this[ks[i]] = p[ks[i]];
37
- }
38
-
39
- /**
40
- * Envelope publicKey.
41
- * @member {Uint8Array} publicKey
42
- * @memberof Envelope
43
- * @instance
44
- */
45
- Envelope.prototype.publicKey = $util.newBuffer([]);
46
-
47
- /**
48
- * Envelope payloadType.
49
- * @member {Uint8Array} payloadType
50
- * @memberof Envelope
51
- * @instance
52
- */
53
- Envelope.prototype.payloadType = $util.newBuffer([]);
54
-
55
- /**
56
- * Envelope payload.
57
- * @member {Uint8Array} payload
58
- * @memberof Envelope
59
- * @instance
60
- */
61
- Envelope.prototype.payload = $util.newBuffer([]);
62
-
63
- /**
64
- * Envelope signature.
65
- * @member {Uint8Array} signature
66
- * @memberof Envelope
67
- * @instance
68
- */
69
- Envelope.prototype.signature = $util.newBuffer([]);
70
-
71
- /**
72
- * Encodes the specified Envelope message. Does not implicitly {@link Envelope.verify|verify} messages.
73
- * @function encode
74
- * @memberof Envelope
75
- * @static
76
- * @param {IEnvelope} m Envelope message or plain object to encode
77
- * @param {$protobuf.Writer} [w] Writer to encode to
78
- * @returns {$protobuf.Writer} Writer
79
- */
80
- Envelope.encode = function encode(m, w) {
81
- if (!w)
82
- w = $Writer.create();
83
- if (m.publicKey != null && Object.hasOwnProperty.call(m, "publicKey"))
84
- w.uint32(10).bytes(m.publicKey);
85
- if (m.payloadType != null && Object.hasOwnProperty.call(m, "payloadType"))
86
- w.uint32(18).bytes(m.payloadType);
87
- if (m.payload != null && Object.hasOwnProperty.call(m, "payload"))
88
- w.uint32(26).bytes(m.payload);
89
- if (m.signature != null && Object.hasOwnProperty.call(m, "signature"))
90
- w.uint32(42).bytes(m.signature);
91
- return w;
92
- };
93
-
94
- /**
95
- * Decodes an Envelope message from the specified reader or buffer.
96
- * @function decode
97
- * @memberof Envelope
98
- * @static
99
- * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
100
- * @param {number} [l] Message length if known beforehand
101
- * @returns {Envelope} Envelope
102
- * @throws {Error} If the payload is not a reader or valid buffer
103
- * @throws {$protobuf.util.ProtocolError} If required fields are missing
104
- */
105
- Envelope.decode = function decode(r, l) {
106
- if (!(r instanceof $Reader))
107
- r = $Reader.create(r);
108
- var c = l === undefined ? r.len : r.pos + l, m = new $root.Envelope();
109
- while (r.pos < c) {
110
- var t = r.uint32();
111
- switch (t >>> 3) {
112
- case 1:
113
- m.publicKey = r.bytes();
114
- break;
115
- case 2:
116
- m.payloadType = r.bytes();
117
- break;
118
- case 3:
119
- m.payload = r.bytes();
120
- break;
121
- case 5:
122
- m.signature = r.bytes();
123
- break;
124
- default:
125
- r.skipType(t & 7);
126
- break;
127
- }
128
- }
129
- return m;
130
- };
131
-
132
- /**
133
- * Creates an Envelope message from a plain object. Also converts values to their respective internal types.
134
- * @function fromObject
135
- * @memberof Envelope
136
- * @static
137
- * @param {Object.<string,*>} d Plain object
138
- * @returns {Envelope} Envelope
139
- */
140
- Envelope.fromObject = function fromObject(d) {
141
- if (d instanceof $root.Envelope)
142
- return d;
143
- var m = new $root.Envelope();
144
- if (d.publicKey != null) {
145
- if (typeof d.publicKey === "string")
146
- $util.base64.decode(d.publicKey, m.publicKey = $util.newBuffer($util.base64.length(d.publicKey)), 0);
147
- else if (d.publicKey.length)
148
- m.publicKey = d.publicKey;
149
- }
150
- if (d.payloadType != null) {
151
- if (typeof d.payloadType === "string")
152
- $util.base64.decode(d.payloadType, m.payloadType = $util.newBuffer($util.base64.length(d.payloadType)), 0);
153
- else if (d.payloadType.length)
154
- m.payloadType = d.payloadType;
155
- }
156
- if (d.payload != null) {
157
- if (typeof d.payload === "string")
158
- $util.base64.decode(d.payload, m.payload = $util.newBuffer($util.base64.length(d.payload)), 0);
159
- else if (d.payload.length)
160
- m.payload = d.payload;
161
- }
162
- if (d.signature != null) {
163
- if (typeof d.signature === "string")
164
- $util.base64.decode(d.signature, m.signature = $util.newBuffer($util.base64.length(d.signature)), 0);
165
- else if (d.signature.length)
166
- m.signature = d.signature;
167
- }
168
- return m;
169
- };
170
-
171
- /**
172
- * Creates a plain object from an Envelope message. Also converts values to other types if specified.
173
- * @function toObject
174
- * @memberof Envelope
175
- * @static
176
- * @param {Envelope} m Envelope
177
- * @param {$protobuf.IConversionOptions} [o] Conversion options
178
- * @returns {Object.<string,*>} Plain object
179
- */
180
- Envelope.toObject = function toObject(m, o) {
181
- if (!o)
182
- o = {};
183
- var d = {};
184
- if (o.defaults) {
185
- if (o.bytes === String)
186
- d.publicKey = "";
187
- else {
188
- d.publicKey = [];
189
- if (o.bytes !== Array)
190
- d.publicKey = $util.newBuffer(d.publicKey);
191
- }
192
- if (o.bytes === String)
193
- d.payloadType = "";
194
- else {
195
- d.payloadType = [];
196
- if (o.bytes !== Array)
197
- d.payloadType = $util.newBuffer(d.payloadType);
198
- }
199
- if (o.bytes === String)
200
- d.payload = "";
201
- else {
202
- d.payload = [];
203
- if (o.bytes !== Array)
204
- d.payload = $util.newBuffer(d.payload);
205
- }
206
- if (o.bytes === String)
207
- d.signature = "";
208
- else {
209
- d.signature = [];
210
- if (o.bytes !== Array)
211
- d.signature = $util.newBuffer(d.signature);
212
- }
213
- }
214
- if (m.publicKey != null && m.hasOwnProperty("publicKey")) {
215
- 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;
216
- }
217
- if (m.payloadType != null && m.hasOwnProperty("payloadType")) {
218
- d.payloadType = o.bytes === String ? $util.base64.encode(m.payloadType, 0, m.payloadType.length) : o.bytes === Array ? Array.prototype.slice.call(m.payloadType) : m.payloadType;
219
- }
220
- if (m.payload != null && m.hasOwnProperty("payload")) {
221
- d.payload = o.bytes === String ? $util.base64.encode(m.payload, 0, m.payload.length) : o.bytes === Array ? Array.prototype.slice.call(m.payload) : m.payload;
222
- }
223
- if (m.signature != null && m.hasOwnProperty("signature")) {
224
- d.signature = o.bytes === String ? $util.base64.encode(m.signature, 0, m.signature.length) : o.bytes === Array ? Array.prototype.slice.call(m.signature) : m.signature;
225
- }
226
- return d;
227
- };
228
-
229
- /**
230
- * Converts this Envelope to JSON.
231
- * @function toJSON
232
- * @memberof Envelope
233
- * @instance
234
- * @returns {Object.<string,*>} JSON object
235
- */
236
- Envelope.prototype.toJSON = function toJSON() {
237
- return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
238
- };
239
-
240
- return Envelope;
241
- })();
242
-
243
- module.exports = $root;
@@ -1,19 +0,0 @@
1
- syntax = "proto3";
2
-
3
- message Envelope {
4
- // public_key is the public key of the keypair the enclosed payload was
5
- // signed with.
6
- bytes public_key = 1;
7
-
8
- // payload_type encodes the type of payload, so that it can be deserialized
9
- // deterministically.
10
- bytes payload_type = 2;
11
-
12
- // payload is the actual payload carried inside this envelope.
13
- bytes payload = 3;
14
-
15
- // signature is the signature produced by the private key corresponding to
16
- // the enclosed public key, over the payload, prefixing a domain string for
17
- // additional security.
18
- bytes signature = 5;
19
- }
@@ -1,183 +0,0 @@
1
- 'use strict'
2
-
3
- const errCode = require('err-code')
4
- const { concat: uint8arraysConcat } = require('uint8arrays/concat')
5
- const { fromString: uint8arraysFromString } = require('uint8arrays/from-string')
6
- // @ts-ignore libp2p-crypto does not support types
7
- const cryptoKeys = require('libp2p-crypto/src/keys')
8
- const PeerId = require('peer-id')
9
- const varint = require('varint')
10
- const { equals: uint8arraysEquals } = require('uint8arrays/equals')
11
-
12
- const { codes } = require('../../errors')
13
- const { Envelope: Protobuf } = require('./envelope')
14
-
15
- /**
16
- * @typedef {import('libp2p-interfaces/src/record/types').Record} Record
17
- */
18
-
19
- class Envelope {
20
- /**
21
- * The Envelope is responsible for keeping an arbitrary signed record
22
- * by a libp2p peer.
23
- *
24
- * @class
25
- * @param {object} params
26
- * @param {PeerId} params.peerId
27
- * @param {Uint8Array} params.payloadType
28
- * @param {Uint8Array} params.payload - marshaled record
29
- * @param {Uint8Array} params.signature - signature of the domain string :: type hint :: payload.
30
- */
31
- constructor ({ peerId, payloadType, payload, signature }) {
32
- this.peerId = peerId
33
- this.payloadType = payloadType
34
- this.payload = payload
35
- this.signature = signature
36
-
37
- // Cache
38
- this._marshal = undefined
39
- }
40
-
41
- /**
42
- * Marshal the envelope content.
43
- *
44
- * @returns {Uint8Array}
45
- */
46
- marshal () {
47
- if (this._marshal) {
48
- return this._marshal
49
- }
50
-
51
- const publicKey = cryptoKeys.marshalPublicKey(this.peerId.pubKey)
52
-
53
- this._marshal = Protobuf.encode({
54
- publicKey: publicKey,
55
- payloadType: this.payloadType,
56
- payload: this.payload,
57
- signature: this.signature
58
- }).finish()
59
-
60
- return this._marshal
61
- }
62
-
63
- /**
64
- * Verifies if the other Envelope is identical to this one.
65
- *
66
- * @param {Envelope} other
67
- * @returns {boolean}
68
- */
69
- equals (other) {
70
- return uint8arraysEquals(this.peerId.pubKey.bytes, other.peerId.pubKey.bytes) &&
71
- uint8arraysEquals(this.payloadType, other.payloadType) &&
72
- uint8arraysEquals(this.payload, other.payload) &&
73
- uint8arraysEquals(this.signature, other.signature)
74
- }
75
-
76
- /**
77
- * Validate envelope data signature for the given domain.
78
- *
79
- * @param {string} domain
80
- * @returns {Promise<boolean>}
81
- */
82
- validate (domain) {
83
- const signData = formatSignaturePayload(domain, this.payloadType, this.payload)
84
-
85
- return this.peerId.pubKey.verify(signData, this.signature)
86
- }
87
- }
88
-
89
- /**
90
- * Helper function that prepares a Uint8Array to sign or verify a signature.
91
- *
92
- * @param {string} domain
93
- * @param {Uint8Array} payloadType
94
- * @param {Uint8Array} payload
95
- * @returns {Uint8Array}
96
- */
97
- const formatSignaturePayload = (domain, payloadType, payload) => {
98
- // When signing, a peer will prepare a Uint8Array by concatenating the following:
99
- // - The length of the domain separation string string in bytes
100
- // - The domain separation string, encoded as UTF-8
101
- // - The length of the payload_type field in bytes
102
- // - The value of the payload_type field
103
- // - The length of the payload field in bytes
104
- // - The value of the payload field
105
-
106
- const domainUint8Array = uint8arraysFromString(domain)
107
- const domainLength = varint.encode(domainUint8Array.byteLength)
108
- const payloadTypeLength = varint.encode(payloadType.length)
109
- const payloadLength = varint.encode(payload.length)
110
-
111
- return uint8arraysConcat([
112
- new Uint8Array(domainLength),
113
- domainUint8Array,
114
- new Uint8Array(payloadTypeLength),
115
- payloadType,
116
- new Uint8Array(payloadLength),
117
- payload
118
- ])
119
- }
120
-
121
- /**
122
- * Unmarshal a serialized Envelope protobuf message.
123
- *
124
- * @param {Uint8Array} data
125
- * @returns {Promise<Envelope>}
126
- */
127
- Envelope.createFromProtobuf = async (data) => {
128
- const envelopeData = Protobuf.decode(data)
129
- const peerId = await PeerId.createFromPubKey(envelopeData.publicKey)
130
-
131
- return new Envelope({
132
- peerId,
133
- payloadType: envelopeData.payloadType,
134
- payload: envelopeData.payload,
135
- signature: envelopeData.signature
136
- })
137
- }
138
-
139
- /**
140
- * Seal marshals the given Record, places the marshaled bytes inside an Envelope
141
- * and signs it with the given peerId's private key.
142
- *
143
- * @async
144
- * @param {Record} record
145
- * @param {PeerId} peerId
146
- * @returns {Promise<Envelope>}
147
- */
148
- Envelope.seal = async (record, peerId) => {
149
- const domain = record.domain
150
- const payloadType = record.codec
151
- const payload = record.marshal()
152
-
153
- const signData = formatSignaturePayload(domain, payloadType, payload)
154
- const signature = await peerId.privKey.sign(signData)
155
-
156
- return new Envelope({
157
- peerId,
158
- payloadType,
159
- payload,
160
- signature
161
- })
162
- }
163
-
164
- /**
165
- * Open and certify a given marshalled envelope.
166
- * Data is unmarshalled and the signature validated for the given domain.
167
- *
168
- * @param {Uint8Array} data
169
- * @param {string} domain
170
- * @returns {Promise<Envelope>}
171
- */
172
- Envelope.openAndCertify = async (data, domain) => {
173
- const envelope = await Envelope.createFromProtobuf(data)
174
- const valid = await envelope.validate(domain)
175
-
176
- if (!valid) {
177
- throw errCode(new Error('envelope signature is not valid for the given domain'), codes.ERR_SIGNATURE_NOT_VALID)
178
- }
179
-
180
- return envelope
181
- }
182
-
183
- module.exports = Envelope
@@ -1,14 +0,0 @@
1
- 'use strict'
2
-
3
- // The domain string used for peer records contained in a Envelope.
4
- const domain = 'libp2p-peer-record'
5
-
6
- // The type hint used to identify peer records in a Envelope.
7
- // Defined in https://github.com/multiformats/multicodec/blob/master/table.csv
8
- // with name "libp2p-peer-record"
9
- const payloadType = Uint8Array.from([3, 1])
10
-
11
- module.exports = {
12
- ENVELOPE_DOMAIN_PEER_RECORD: domain,
13
- ENVELOPE_PAYLOAD_TYPE_PEER_RECORD: payloadType
14
- }