mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-07 21:01:36 +00:00
Compare commits
951 Commits
Author | SHA1 | Date | |
---|---|---|---|
8e1743cac4 | |||
3ea95ce642 | |||
c4cae29ef3 | |||
a7128f07ec | |||
c3e147df6b | |||
b89445274d | |||
b9e3bcd91e | |||
f5c1cd1fb0 | |||
975e4b0fe0 | |||
9504f1951a | |||
8e1fc78353 | |||
8895a092b6 | |||
f2f361998d | |||
5f702f3481 | |||
03b34cac7d | |||
9c67364caa | |||
a1424826e7 | |||
3d5bba070b | |||
3f314d5e90 | |||
4ee3e1973b | |||
fc6558b897 | |||
3e302570e5 | |||
a34d2bbcc3 | |||
9941414a91 | |||
46cb46188a | |||
1af8472dc6 | |||
f6a4cad827 | |||
b1079474de | |||
a150ea60c5 | |||
aec8e3d3bb | |||
3abf4aeb35 | |||
a36b2112aa | |||
8d3b61710a | |||
5dbbeef311 | |||
3e7594f697 | |||
ce2a624a09 | |||
a64c02838c | |||
74d07e5e8c | |||
eeda056883 | |||
f06e06a006 | |||
28f52bbf75 | |||
ed5f8f853f | |||
0a6bc0d101 | |||
b5c9e48b68 | |||
9942cbd50c | |||
037c965a67 | |||
748b552876 | |||
961b48bb8d | |||
000826db21 | |||
45c33675a7 | |||
a28c878f4a | |||
67067c97d5 | |||
f45cd1c4b5 | |||
0a02207116 | |||
0b854a949f | |||
9014ea657a | |||
f40697975e | |||
6c41e30456 | |||
77e8273a64 | |||
d60922b799 | |||
42b51d8f01 | |||
d19401aa4c | |||
24bb8df521 | |||
58d4f9a915 | |||
239413e331 | |||
01d43a7b60 | |||
37d66fd88c | |||
21e8ced81a | |||
9ae1b758e9 | |||
408868655c | |||
c5f61ac05f | |||
5d0ac529e4 | |||
bc05083207 | |||
169bb806a7 | |||
7809e6444e | |||
f7e1426b9e | |||
7d76ba1367 | |||
b538ebdc0a | |||
baedf3fe5a | |||
4ebcdb085c | |||
4448de8432 | |||
585ad52b4c | |||
e50c6abcf2 | |||
49fffda23c | |||
689c35ed1c | |||
1a13e2c6ca | |||
5758db8ea9 | |||
ef9d3ca2c6 | |||
97e3633f47 | |||
e36b67a212 | |||
e977039c8a | |||
a5337c1797 | |||
ee23fb9508 | |||
11a46ea71e | |||
5c72424e57 | |||
0bf0b7cf89 | |||
55020056ee | |||
bb83cacb5a | |||
447d0ed0dd | |||
43eda43f06 | |||
7b93ece7f2 | |||
74bdfd1024 | |||
4d1fcdb3d2 | |||
caf66ea143 | |||
48656712ea | |||
1a5ae74741 | |||
8691465a52 | |||
6350a187c7 | |||
8e3bb09279 | |||
73204958ee | |||
e9e4b731a5 | |||
d0a9fada32 | |||
824a444f56 | |||
fef54b2b2c | |||
8f29a667a1 | |||
093c0ea13f | |||
61c36f9e09 | |||
f82da56901 | |||
06f26e586f | |||
8879634363 | |||
4a80afce8f | |||
f75ae341bb | |||
f2d010a3ab | |||
e04224a1e2 | |||
4c6be91588 | |||
5f50054d94 | |||
d7d8439e71 | |||
4c7a89b710 | |||
4eabe07bde | |||
2fd3b0a0e5 | |||
ec6f7d1cfd | |||
a1053bdc54 | |||
0d48fc4f5a | |||
60d437f595 | |||
96df4b7dc4 | |||
bb59b518f1 | |||
fb4b2734d3 | |||
0087218194 | |||
58b793d700 | |||
63ba2f8fa3 | |||
bd26bde876 | |||
3c2a45a9d2 | |||
bbf8ef7065 | |||
d6d1a746ea | |||
28b79a76a8 | |||
81e70df742 | |||
e9478cee2e | |||
7be17a3ce1 | |||
93dda74085 | |||
cfbd52d7f7 | |||
6cd23ea6c9 | |||
9b75a0f184 | |||
b606ce0e91 | |||
64c8c0f097 | |||
9be582e222 | |||
55c9bfac44 | |||
1e869717ff | |||
9107efe121 | |||
cd09327eb6 | |||
ca57e65ecc | |||
f574e82a5d | |||
15613ccf19 | |||
dab1c8b2a5 | |||
d437defede | |||
74d414c21f | |||
8f2e69048f | |||
b0a36ccbc8 | |||
3158366579 | |||
1d318e12d8 | |||
8a97dded26 | |||
e50f0eeb7b | |||
ee57a643cc | |||
89658dd655 | |||
4ab125e017 | |||
71daac24b1 | |||
02a5095b9c | |||
3e5d450eca | |||
098f3d1dd3 | |||
689f90a698 | |||
0e3cc5866b | |||
4851680c4d | |||
0e18735b8c | |||
f68ff35625 | |||
8c56ec0d23 | |||
fa5ee873e3 | |||
51d7ca44c1 | |||
726a746479 | |||
a331b84f13 | |||
78d152dd68 | |||
7e14aa19b5 | |||
2440c872df | |||
6c7e5e5eef | |||
388df6b6e6 | |||
7dbfe6ab1a | |||
cea59a1fe4 | |||
7da9ad44ab | |||
3896941128 | |||
856b38de67 | |||
798d7b73c1 | |||
f2d0d8b51d | |||
999c1b7740 | |||
99ff361a55 | |||
6115f8b680 | |||
9ccab40fc8 | |||
619e5dd73c | |||
0fd23f6a5f | |||
5a84dd56d0 | |||
7b05d6922b | |||
08a4fad80b | |||
51da8874d8 | |||
5cd8c19567 | |||
db766eaca9 | |||
a1308d640d | |||
19e7254c3d | |||
f4898eb883 | |||
9bec719fce | |||
b664c0aafd | |||
b524bbf627 | |||
07bd269fb0 | |||
a8219e61a0 | |||
a1590acc8b | |||
a0cf83c640 | |||
b56cdda0ef | |||
b621fbdfdc | |||
24dd1d22c5 | |||
a1a1213bdc | |||
90a9d93968 | |||
8f680e20e9 | |||
afafd08943 | |||
c038550fad | |||
d60a3215d0 | |||
1e51295150 | |||
5299995998 | |||
64a66f08ac | |||
e2a63bab62 | |||
294b032b0b | |||
72f37acd4a | |||
52a615f922 | |||
698c1df1b4 | |||
7f4662f8d8 | |||
84b935f682 | |||
0fbb59748e | |||
56316b8c1e | |||
aaa1155633 | |||
aa5e232479 | |||
eb7adcf1ab | |||
0be74e6a61 | |||
6065923356 | |||
2b45fee0ed | |||
8bf5a70bb8 | |||
6627278a87 | |||
65e87460b0 | |||
7b8d01697d | |||
ce38033c10 | |||
3f2b06dc26 | |||
1e3d6f4b56 | |||
48a9a3eecc | |||
9ea9287bea | |||
5123a8357b | |||
43630f1e0b | |||
aaf62a40ec | |||
7fbd1556e8 | |||
51474c334a | |||
d75cc97ced | |||
2a7967c1cc | |||
9e9ec0b575 | |||
d3a4bf0a3f | |||
d33919d0b3 | |||
c215339a27 | |||
cb597b57d7 | |||
12e48adafa | |||
ed6d5bb4b4 | |||
e9d225c9dc | |||
2aac3b0f69 | |||
9862653051 | |||
b781911ac2 | |||
3fa1fa3a0b | |||
dfed982404 | |||
60b6a55906 | |||
aadeb73c94 | |||
ea6b1ecce4 | |||
d3ab238738 | |||
97455957ac | |||
5255d87d0a | |||
03ce7c3103 | |||
ea0621b2f6 | |||
9197df309b | |||
8dd361338b | |||
9b13fe321a | |||
1f85e0213a | |||
09a604b5e5 | |||
ed16b69057 | |||
c940f2d384 | |||
2620d46f01 | |||
e192eb6508 | |||
b57de4ed0c | |||
760d8b4c3a | |||
7b56f559cb | |||
0fd5188176 | |||
bd7fd0f755 | |||
da83721d6d | |||
1414e45969 | |||
9e35fbc316 | |||
30728753cf | |||
afdbe3deac | |||
ce58805e90 | |||
b5303fd788 | |||
aa78d2ef15 | |||
5a4641980d | |||
f4ec35573e | |||
524e6f8433 | |||
fad9cb22e0 | |||
e1b158fbb9 | |||
1837d6e95e | |||
4bce6aa0b9 | |||
4d11edd62c | |||
a8984c6cd3 | |||
0882dcea3b | |||
b3d8132ea6 | |||
a317a8b011 | |||
8bed8f39ff | |||
58c0c7c03e | |||
f662fdcf36 | |||
5608178247 | |||
dcd58693f5 | |||
8bf05e6db6 | |||
0589d53616 | |||
eb2528c1d6 | |||
141cf90ca0 | |||
f4ec459a7b | |||
56a7842664 | |||
be8fc9daf1 | |||
f39e8f0da9 | |||
ed0c2cb6c1 | |||
c6592c5797 | |||
33cef10721 | |||
2d6408eb30 | |||
29a96690ad | |||
8b94cd1e2e | |||
b83880765f | |||
560d7084fe | |||
d21b7ce4ff | |||
c1038bec92 | |||
bb2e56e6c7 | |||
4222c49556 | |||
2f2ba420ef | |||
e9d16ba190 | |||
afb552c063 | |||
721151b9fc | |||
efc96c2f19 | |||
ba4681b312 | |||
6b00eeab0a | |||
c339be10ee | |||
7b326cc525 | |||
835a689965 | |||
b08d81cee7 | |||
6445fda050 | |||
4ca481b869 | |||
5d7ee50e76 | |||
48fd64182b | |||
58db09a9d3 | |||
15f7c2a974 | |||
4384d139d2 | |||
bd1068e5d9 | |||
854ac49a10 | |||
f9d0047bf6 | |||
ae5ce9a66f | |||
14a1955a78 | |||
f1eb373235 | |||
dabee00127 | |||
17b6a3fd73 | |||
4f7586886c | |||
71f46bf4a6 | |||
cba035a75b | |||
2c5183bd70 | |||
a586efb5f7 | |||
1db69c5f27 | |||
9e1a9f04ae | |||
287f1b98e5 | |||
90ecc57dbc | |||
ae52d483fc | |||
2570a1ba30 | |||
633b0c291f | |||
7d505494be | |||
1df52ae205 | |||
ba02764c5f | |||
404fa69513 | |||
85988cfaab | |||
def3574ba0 | |||
6b1da8326f | |||
a12006a4fa | |||
466b4dfd70 | |||
98e82df5e1 | |||
6ad840c3f3 | |||
7ac5e42a1a | |||
b86ef64904 | |||
b6cd48b798 | |||
a5b54a7017 | |||
e1e3be8106 | |||
b7432bd02b | |||
7c2fefda46 | |||
2788982c53 | |||
76ec1ccd21 | |||
e445a17278 | |||
2329ef3ea3 | |||
ce8e60bb0d | |||
12fc069873 | |||
9a6e07d70b | |||
8da9fc97d4 | |||
a020db183a | |||
f77ce39484 | |||
18a062ed12 | |||
1999606ecc | |||
472e14f2b4 | |||
953d185c39 | |||
c563e06a60 | |||
f28b09fc0d | |||
2104578924 | |||
bcad60995e | |||
432b0997ae | |||
2afdbb71b2 | |||
c7a54f34f7 | |||
9d52b80c45 | |||
582094a834 | |||
fe2a8eddbb | |||
5e1dbc21a2 | |||
b37ccc7279 | |||
440fbf0c04 | |||
0cef51d4c2 | |||
b277b26043 | |||
bc071ce7d7 | |||
01730214d6 | |||
0826531e31 | |||
35ac02dcb5 | |||
b73348078d | |||
21cd9c67bc | |||
0fc4537a5e | |||
6a05f3e6e7 | |||
93a1e42ef3 | |||
35aa45ce92 | |||
b11c6fc7e9 | |||
ebedd3510b | |||
ae6af20e8e | |||
2a80618740 | |||
5b1bd389f8 | |||
3e31c2d0df | |||
8079c2078b | |||
80cf0777b5 | |||
60b0cbc179 | |||
3eef695bc0 | |||
b3deb356f1 | |||
299cfefa01 | |||
aa95ab9928 | |||
b0f124b5ff | |||
b294301456 | |||
d92306f222 | |||
fd738f9d51 | |||
d788433b43 | |||
d5a977b227 | |||
0489972b4b | |||
3f31b1f422 | |||
a2b3446ed7 | |||
ff7a6c86a0 | |||
9a8d609a59 | |||
9fef58cb7d | |||
684f283aec | |||
3e95e6f9e4 | |||
f4f3f0f03a | |||
7c2c852fc0 | |||
e8d8aab278 | |||
dd48d268ec | |||
99a53592e2 | |||
2a2e7a1012 | |||
791f39a09b | |||
65d52857a5 | |||
48b1b442e9 | |||
9554b05c6f | |||
df6ef45a2d | |||
b4a70ea476 | |||
45716da465 | |||
905c911946 | |||
10811e9ced | |||
9c2789bc15 | |||
24be691bc1 | |||
9433c6c398 | |||
04faf1806c | |||
b06ca1b3c7 | |||
bde30cac45 | |||
28c054c21e | |||
c346e8066b | |||
40978a1940 | |||
71dcaafcac | |||
5319e065ec | |||
f3801f0e6c | |||
51cc993876 | |||
a800c1ad91 | |||
54c474de98 | |||
f28dffb268 | |||
c049074cb5 | |||
1bde70f1b5 | |||
cfa4df6e11 | |||
eb5aa03232 | |||
4cb541ddae | |||
aa1d9b273a | |||
7313f781fc | |||
313b1eae20 | |||
01aa44724e | |||
8b627797e2 | |||
e5f19e860b | |||
5204da73f7 | |||
66130ccba8 | |||
aa1bb3ab75 | |||
813a59b9ce | |||
eee60ed37d | |||
e52ce66ab7 | |||
da52af704e | |||
282ce62703 | |||
ef3238bdee | |||
4c06c54fc5 | |||
bb0c45d704 | |||
eb10b5c6d2 | |||
a282fbe139 | |||
6cfb0b2692 | |||
de84ee473c | |||
f28ecb2483 | |||
bba6f7eff8 | |||
9f5f07269e | |||
5f92acd5bb | |||
dfe8f632f7 | |||
9f47100713 | |||
d497961938 | |||
6e76aade7f | |||
ec7d0761de | |||
59fe9732d7 | |||
4ed5c039fc | |||
9e7a080a5c | |||
558e5987be | |||
dcf59a8468 | |||
ab028a2be3 | |||
91e60d4253 | |||
679d446daa | |||
8047fb76fa | |||
c4cab007af | |||
ebaab3e47f | |||
b31690c8e6 | |||
3bde9c8bed | |||
14e12ee1f1 | |||
2374929990 | |||
26de739bb1 | |||
0f8d6afd8f | |||
daa26859e0 | |||
fdfb7b4e86 | |||
15bdb795a4 | |||
7d78728f54 | |||
53ed3bdb99 | |||
ae513887f5 | |||
7c78faa171 | |||
7d12eb9e26 | |||
581a1de472 | |||
288ac17954 | |||
2e4459b315 | |||
2a5232b541 | |||
44915b3723 | |||
64bba57255 | |||
88ebd1fc09 | |||
92cd591da4 | |||
320d84f541 | |||
970deec2a4 | |||
714b6ec2b9 | |||
f71fdfdf35 | |||
e92053da9a | |||
17b5f73b3d | |||
c18d2a4147 | |||
f1baa7e0b1 | |||
4abc868ab3 | |||
40e840d5fd | |||
9518eb44b3 | |||
0b75f99d75 | |||
686379efb0 | |||
a95389a28e | |||
3226632d83 | |||
dd934b9690 | |||
cef3c8b5cc | |||
eedb20e9a3 | |||
e51260434c | |||
7e6c9eeb38 | |||
c537140fbc | |||
3b7c4b5eb0 | |||
4460e8246c | |||
a63432e24b | |||
69f7264123 | |||
e052021397 | |||
fdd714ee60 | |||
65e7223ce0 | |||
eddec7d2e4 | |||
7b6c921d36 | |||
05c16e4262 | |||
c8a86db310 | |||
ce29902691 | |||
0b729621db | |||
e6e5b872dc | |||
550af3cbde | |||
5aa9ebbbe8 | |||
7f68a13433 | |||
2b7cc55c88 | |||
40739e9639 | |||
6106915923 | |||
d9059dbad9 | |||
187d584086 | |||
7502ba86a5 | |||
cc51fa59f9 | |||
1c10842bd3 | |||
c07ffa1317 | |||
b7f67f2764 | |||
8665286764 | |||
a43d73eea7 | |||
4ad70efb00 | |||
1af5ba9093 | |||
be9eafe20f | |||
27c6587747 | |||
9521e79061 | |||
80f0b6077a | |||
ac5cacba33 | |||
e320854db7 | |||
501cc22fb4 | |||
a57f1b22d0 | |||
7baf9f47ac | |||
4e8ac0b7a8 | |||
b593adef56 | |||
8c803d5901 | |||
9fadd1d7b8 | |||
6905f1ba41 | |||
b80e89269c | |||
deba7ea28e | |||
d5972045da | |||
28ffa0c7dc | |||
1790ded144 | |||
abc6257bf7 | |||
dd84190d47 | |||
65073792aa | |||
0bf203b087 | |||
37471135fa | |||
2a21c20ea3 | |||
9a2d4e3d72 | |||
7c2a19e3cc | |||
26d5e69c62 | |||
5042e09bb4 | |||
409e7a8e1f | |||
52938e9f39 | |||
32941a807a | |||
32d34d3b83 | |||
074b8af09d | |||
4117bd7552 | |||
db3f6dbb06 | |||
3808c365b1 | |||
19528ef15e | |||
bb0c9905ed | |||
141920cd14 | |||
2dc94cd907 | |||
7fc1cd0f7c | |||
26f3f9a319 | |||
e9ce4ac795 | |||
bca86873cc | |||
bcca813171 | |||
cfc5958a4b | |||
4527d5fff1 | |||
2c04a71007 | |||
642b8ad751 | |||
ada73221b0 | |||
2e40e9dda1 | |||
e531b1bf3d | |||
fdfc237780 | |||
83a09dbc0e | |||
b28eba067e | |||
b871bb0a1a | |||
14d3578eaf | |||
10a8ec3f31 | |||
41d202c4ba | |||
a5fd05875c | |||
379febb610 | |||
99873e877b | |||
4e01c094bc | |||
8fcafe2d90 | |||
947eaf166b | |||
1ebf725ac4 | |||
0c543b7180 | |||
beeb36c10c | |||
0acc7e5d72 | |||
9fd94b98a1 | |||
5c3037037a | |||
362217c8da | |||
7733ba5cd7 | |||
52bf826ec6 | |||
10619afbe6 | |||
3debabdd26 | |||
56e095983a | |||
ebdb696742 | |||
721e6ee9ce | |||
98f2903088 | |||
3dda282dfd | |||
03faf69212 | |||
0062a4b5eb | |||
f7f85dce0a | |||
59df82a675 | |||
6651401f0b | |||
cd43863db6 | |||
23e8293b75 | |||
68c170a40d | |||
bd8a35aaf9 | |||
248d86d050 | |||
8225b11082 | |||
1355af2b51 | |||
a85f041843 | |||
2c0cda2a7c | |||
b4ba267589 | |||
4c81f39ebe | |||
76922383ab | |||
3fc57ff397 | |||
13b36dcaa6 | |||
61bebd10fc | |||
97cde1ccb4 | |||
07b0cdc30e | |||
73b6d60e32 | |||
808f7495a9 | |||
ae21a6facf | |||
3aba3fd176 | |||
dc2da39a19 | |||
f8b441fff5 | |||
190ad54f9e | |||
582f8bed2e | |||
59de0da19c | |||
0caf600c9a | |||
a4943b4509 | |||
c128873a07 | |||
5e2b6df04a | |||
8d2d8c98a0 | |||
30bf5bb161 | |||
c88eaf416c | |||
c7f20383d9 | |||
a86a95c19f | |||
1b7360fcfa | |||
67ca37c356 | |||
0870068ebc | |||
8563395d32 | |||
ef3cae5b5a | |||
f602cd9f79 | |||
ec956c1d42 | |||
c906f698e4 | |||
95f029eb7a | |||
38a68a01b7 | |||
c08aa517a0 | |||
8eac0f806e | |||
9fd092fddf | |||
87ddc25bee | |||
efb2b67f19 | |||
7c0b4daf49 | |||
aeb3ae1301 | |||
97df76f9c5 | |||
3ad696445d | |||
30bec8d53b | |||
db2b5300b4 | |||
df4d60df88 | |||
29cc0afc64 | |||
2504cbeb26 | |||
468cc421fa | |||
0b991e19d1 | |||
2a81fc75a8 | |||
e04c249d4d | |||
4a995613ae | |||
066c4976bb | |||
429cf9c361 | |||
01c20f8ab9 | |||
c8adce3906 | |||
6e3c5427de | |||
3e140d2c47 | |||
f0858c20bd | |||
ef9c2a6296 | |||
090d75d383 | |||
04a4ffcc36 | |||
7f7278fe82 | |||
2c03222136 | |||
65857838f9 | |||
cad173e3bd | |||
19ce266f1b | |||
a2a85eb8d6 | |||
b674bccfc2 | |||
4ae8e43b8b | |||
6b059a0ba8 | |||
363259f832 | |||
56dbd9e7c2 | |||
314b45897f | |||
0a6f1faf82 | |||
5a638794c5 | |||
d3b0f380bb | |||
c1ccc1d5fb | |||
49484f734c | |||
43345cee6d | |||
06eb7a19f3 | |||
76ef5fdf50 | |||
c8f4eaf982 | |||
abe5c8c171 | |||
a01221f89c | |||
4224c1fe61 | |||
019cd4715d | |||
f877609a58 | |||
4a7eb34056 | |||
fb0c64f9d4 | |||
7ef1c0a598 | |||
c23f147bda | |||
c1a471793d | |||
39145e9a91 | |||
74b6f5f793 | |||
a4e5247ca2 | |||
0349119536 | |||
5eb97a66df | |||
8da166e2a9 | |||
f583b1b2cd | |||
5f5ee98a50 | |||
7df0074667 | |||
b14a2ff717 | |||
7b78736e24 | |||
2b02be08f7 | |||
32cb59fa18 | |||
53cce8d8f6 | |||
b6e41922a7 | |||
195673c2d3 | |||
647d10fe10 | |||
73f2f6d050 | |||
2d40f3ad6a | |||
13452c959e | |||
547c4dbd33 | |||
606fa737b8 | |||
e3870f3f8a | |||
45b0f61824 | |||
0263b899e3 | |||
fb92e672ee | |||
2e326e1619 | |||
e4e9761c99 | |||
a5b7de1b6b | |||
9a7a381f85 | |||
1f00855e6e | |||
8aa932a491 | |||
babb90fe17 | |||
2f9070a725 | |||
a48ec65961 | |||
031ecb3fe7 | |||
c6bdd869be | |||
dee0340806 | |||
a4b41b0f9a | |||
291e79fc99 | |||
59ea9c388f | |||
300936f2f3 | |||
9f4ec2a915 | |||
c7f4368007 | |||
9ff04779f5 | |||
f73c045767 | |||
8840c9b250 | |||
df4c99df4a | |||
6e05a0b239 | |||
c781abc30c | |||
6a66931ba4 | |||
426fc27b26 | |||
d6325dee90 | |||
f74bc0596d | |||
8f9e82b0fb | |||
9724fa190e | |||
721da9ab41 | |||
d27bd2b912 | |||
2c23d9a718 | |||
a6623c1ba2 | |||
5d4d94e75f | |||
a4b97b9627 | |||
3584209947 | |||
b5209fc456 | |||
932d8772da | |||
baee2b7945 | |||
153f8884fa | |||
b71f77dcf7 | |||
8dc8e0530b | |||
4ed0ce16af | |||
cbb7290a97 | |||
63b10c01e3 | |||
11c21b6999 | |||
109ee51d7b | |||
09182b43bf | |||
a130ab2dc1 | |||
13355bf179 | |||
e24a1e43dc | |||
cb7d60ef65 | |||
a01b9479eb | |||
d01876a3d1 | |||
eb5eed8b21 | |||
3c58613b69 | |||
5dd5d37f6a | |||
6c3dd4b780 | |||
06913271c7 | |||
2cdd7033f0 | |||
c2cf1da2ce | |||
8d0124ee5c | |||
078956b310 | |||
4f44158889 | |||
3a8d51c142 | |||
f8c09db20f | |||
ee801c9be2 | |||
03349d7caf | |||
0c87dbc8f3 | |||
c4abbaa2f9 | |||
21759f1204 | |||
0830827c06 | |||
8f31d1cb17 | |||
fc43b99836 | |||
4027c3cca4 | |||
049c7ce347 | |||
54b65b3cbc | |||
3db3dca98b | |||
40fdc4147c | |||
e10ec13c33 | |||
5d0b25cb00 | |||
57c85fe346 | |||
d7eb5b7abc | |||
b5608f0ef8 | |||
b423a1c583 | |||
0f7bde7bc3 | |||
fecb41b275 | |||
14d14ff0b6 | |||
af493363aa | |||
fe2d097bfa | |||
3cf54db8d5 | |||
ed9d0e5a42 | |||
8486f04c55 | |||
012433b5d5 | |||
48fcf7ee90 | |||
517723a093 | |||
55223d528c | |||
f6f96d3b5f | |||
3c93b7cd4e | |||
c72ce8a7fb | |||
a06dc87670 | |||
93a0103fb8 | |||
a6652f295e | |||
f08dbbc8a9 | |||
815a85d51f | |||
c4b6de1db6 | |||
2c48724db7 | |||
d79abccb85 | |||
0293aee549 | |||
c007c4cdd0 | |||
dd4db33c03 | |||
640cea2252 | |||
51631eae5e | |||
03cc2c28fb |
62
.aegir.js
Normal file
62
.aegir.js
Normal file
@ -0,0 +1,62 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('./src')
|
||||
const { MULTIADDRS_WEBSOCKETS } = require('./test/fixtures/browser')
|
||||
const Peers = require('./test/fixtures/peers')
|
||||
const PeerId = require('peer-id')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const Muxer = require('libp2p-mplex')
|
||||
const { NOISE: Crypto } = require('libp2p-noise')
|
||||
const pipe = require('it-pipe')
|
||||
let libp2p
|
||||
|
||||
const before = async () => {
|
||||
// Use the last peer
|
||||
const peerId = await PeerId.createFromJSON(Peers[Peers.length - 1])
|
||||
|
||||
libp2p = new Libp2p({
|
||||
addresses: {
|
||||
listen: [MULTIADDRS_WEBSOCKETS[0]]
|
||||
},
|
||||
peerId,
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
streamMuxer: [Muxer],
|
||||
connEncryption: [Crypto]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
nat: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
})
|
||||
// Add the echo protocol
|
||||
libp2p.handle('/echo/1.0.0', ({ stream }) => pipe(stream, stream))
|
||||
|
||||
await libp2p.start()
|
||||
}
|
||||
|
||||
const after = async () => {
|
||||
await libp2p.stop()
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
bundlesize: { maxSize: '223kB' },
|
||||
hooks: {
|
||||
pre: before,
|
||||
post: after
|
||||
},
|
||||
webpack: {
|
||||
node: {
|
||||
// needed by bcrypto
|
||||
Buffer: true
|
||||
}
|
||||
}
|
||||
}
|
144
.github/workflows/main.yml
vendored
Normal file
144
.github/workflows/main.yml
vendored
Normal file
@ -0,0 +1,144 @@
|
||||
name: ci
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- '**'
|
||||
|
||||
jobs:
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: yarn lint
|
||||
- uses: gozala/typescript-error-reporter-action@v1.0.8
|
||||
- run: yarn build
|
||||
- run: yarn aegir dep-check
|
||||
- uses: ipfs/aegir/actions/bundle-size@master
|
||||
name: size
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
test-node:
|
||||
needs: check
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [windows-latest, ubuntu-latest, macos-latest]
|
||||
node: [14]
|
||||
fail-fast: true
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node }}
|
||||
- run: yarn
|
||||
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
|
||||
- uses: codecov/codecov-action@v1
|
||||
test-chrome:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: npx aegir test -t browser -t webworker --bail
|
||||
test-firefox:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless
|
||||
test-interop:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd node_modules/interop-libp2p && yarn && LIBP2P_JS=${GITHUB_WORKSPACE}/src/index.js npx aegir test -t node --bail
|
||||
test-auto-relay-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- auto-relay
|
||||
test-chat-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- chat
|
||||
test-connection-encryption-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- connection-encryption
|
||||
test-discovery-mechanisms-example:
|
||||
needs: check
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- discovery-mechanisms
|
||||
test-echo-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- echo
|
||||
test-libp2p-in-the-browser-example:
|
||||
needs: check
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- libp2p-in-the-browser
|
||||
test-peer-and-content-routing-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- peer-and-content-routing
|
||||
test-pnet-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- pnet
|
||||
test-protocol-and-stream-muxing-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- protocol-and-stream-muxing
|
||||
test-pubsub-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- pubsub
|
||||
test-transports-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- transports
|
||||
test-webrtc-direct-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- webrtc-direct
|
23
.gitignore
vendored
23
.gitignore
vendored
@ -1,7 +1,17 @@
|
||||
docs
|
||||
**/node_modules/
|
||||
**/*.log
|
||||
test/repo-tests*
|
||||
**/bundle.js
|
||||
.cache
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
|
||||
coverage
|
||||
.nyc_output
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
@ -19,9 +29,18 @@ coverage
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
build
|
||||
|
||||
# Dependency directory
|
||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||
node_modules
|
||||
|
||||
lib
|
||||
dist
|
||||
test/test-data/go-ipfs-repo/LOCK
|
||||
test/test-data/go-ipfs-repo/LOG
|
||||
test/test-data/go-ipfs-repo/LOG.old
|
||||
|
||||
# while testing npm5
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
|
13
.travis.yml
13
.travis.yml
@ -1,13 +0,0 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "iojs"
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
|
||||
before_install:
|
||||
- npm i -g npm
|
||||
# Workaround for a permissions issue with Travis virtual machine images
|
||||
script:
|
||||
- npm test
|
1403
CHANGELOG.md
Normal file
1403
CHANGELOG.md
Normal file
File diff suppressed because it is too large
Load Diff
3
CODE_OF_CONDUCT.md
Normal file
3
CODE_OF_CONDUCT.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Contributor Code of Conduct
|
||||
|
||||
The `js-libp2p` project follows the [`IPFS Community Code of Conduct`](https://github.com/ipfs/community/blob/master/code-of-conduct.md)
|
9
CONTRIBUTING.md
Normal file
9
CONTRIBUTING.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Contributing guidelines
|
||||
|
||||
libp2p as a project, including js-libp2p and all of its modules, follows the [standard IPFS Community contributing guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md).
|
||||
|
||||
We also adhere to the [IPFS JavaScript Community contributing guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md) which provide additional information of how to collaborate and contribute in the JavaScript implementation of libp2p.
|
||||
|
||||
We appreciate your time and attention for going over these. Please open an issue on [ipfs/community](https://github.com/ipfs/community) if you have any question.
|
||||
|
||||
Thank you.
|
43
ISSUE_TEMPLATE.md
Normal file
43
ISSUE_TEMPLATE.md
Normal file
@ -0,0 +1,43 @@
|
||||
<!--
|
||||
Thank you for reporting an issue.
|
||||
|
||||
This issue tracker is for bugs and issues found within the JavaScript implementation of libp2p.
|
||||
If you require more general support please file an issue on our discuss forum. https://discuss.ipfs.io/
|
||||
|
||||
Please fill in as much of the template below as you're able.
|
||||
|
||||
Version: package.json version or the commit you have installed.
|
||||
Platform: output of `uname -a` (UNIX), or version and 32 or 64-bit (Windows). If using in a Browser, please share the browser version as well.
|
||||
Subsystem: if known, please specify affected core module name (e.g Transports, SECIO, etc).
|
||||
|
||||
If possible, please provide code that demonstrates the problem, keeping it as
|
||||
simple and free of external dependencies as you are able.
|
||||
-->
|
||||
|
||||
- **Version**:
|
||||
- **Platform**:
|
||||
- **Subsystem**:
|
||||
|
||||
<!-- Bug, Feature, Question, Enhancement, Etc -->
|
||||
#### Type:
|
||||
|
||||
<!--
|
||||
One of following:
|
||||
Critical - System crash, application panic.
|
||||
High - The main functionality of the application does not work, API breakage, repo format breakage, etc.
|
||||
Medium - A non-essential functionality does not work, performance issues, etc.
|
||||
Low - An optional functionality does not work.
|
||||
Very Low - Translation or documentation mistake. Something that won't give anyone a bad day.
|
||||
-->
|
||||
#### Severity:
|
||||
|
||||
#### Description:
|
||||
|
||||
#### Steps to reproduce the error:
|
||||
|
||||
<!--
|
||||
This is for you! Please read, and then delete this text before posting it.
|
||||
The js-libp2p issues are only for bug reports and directly actionable features.
|
||||
|
||||
Read https://github.com/ipfs/community/blob/master/CONTRIBUTING.md#reporting-issues if your issue doesn't fit either of those categories.
|
||||
-->
|
45
MIGRATION_TEMPLATE.md
Normal file
45
MIGRATION_TEMPLATE.md
Normal file
@ -0,0 +1,45 @@
|
||||
<!--Specify versions for migration below-->
|
||||
# Migrating to libp2p@__
|
||||
|
||||
A migration guide for refactoring your application code from libp2p v__ to v__.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [API](#api)
|
||||
- [Module Updates](#module-updates)
|
||||
|
||||
## API
|
||||
|
||||
<!--Describe breaking APIs with examples for Before and After
|
||||
Example:
|
||||
|
||||
### Peer Discovery
|
||||
|
||||
__Describe__
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
-->
|
||||
|
||||
## Module Updates
|
||||
|
||||
With this release you should update the following libp2p modules if you are relying on them:
|
||||
|
||||
<!--Specify module versions in JSON for migration below.
|
||||
It's recommended to check package.json changes for this:
|
||||
`git diff <release> <prev> -- package.json`
|
||||
-->
|
||||
|
||||
```json
|
||||
|
||||
```
|
19
OKR.md
Normal file
19
OKR.md
Normal file
@ -0,0 +1,19 @@
|
||||
# Quarterly Objectives and Key Results
|
||||
|
||||
We try to frame our ongoing work using a process based on quarterly Objectives and Key Results (OKRs). Objectives reflect outcomes that are challenging, but realistic. Results are tangible and measurable.
|
||||
|
||||
## 2019 Q1
|
||||
|
||||
Find the js-libp2p OKRs for 2019 Q1 at the [2019 Q1 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/11GKG1DBRIIAiQnHvLD7_IqWxDGsVdaZFpxJM6NWtXe8/edit#gid=1271182838)
|
||||
|
||||
## 2018 Q4
|
||||
|
||||
Find the js-libp2p OKRs for 2018 Q4 at the [2018 Q4 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/1BYwmbVicgo6_tOHAbgiUXWge8Ej0qR1M_gAUulazmrg/edit#gid=1241853194)
|
||||
|
||||
## 2018 Q3
|
||||
|
||||
Find the js-libp2p OKRs for 2018 Q3 at the [2018 Q3 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/1HTXfgR5FyPTFhsTkFPRThkeMvHvCgJOaAs7BSl_vQ_0/edit#gid=1241853194)
|
||||
|
||||
## Previous Quarters
|
||||
|
||||
For the quarters before 2018 Q3, js-libp2p shared their KRs with the [IPFS OKRs](https://github.com/ipfs/js-ipfs/blob/master/OKR.md).
|
188
README.md
188
README.md
@ -1,42 +1,186 @@
|
||||
node-libp2p
|
||||
===========
|
||||
<h1 align="center">
|
||||
<a href="libp2p.io"><img width="250" src="https://github.com/libp2p/libp2p/blob/master/logo/black-bg-2.png?raw=true" alt="libp2p hex logo" /></a>
|
||||
</h1>
|
||||
|
||||
[](http://ipn.io) [[](http://webchat.freenode.net/?channels=%23ipfs) ](https://travis-ci.org/diasdavid/node-libp2p)  [](https://david-dm.org/diasdavid/node-libp2p) [](https://github.com/feross/standard)
|
||||
<h3 align="center">The JavaScript implementation of the libp2p Networking Stack.</h3>
|
||||
|
||||
> Node.js implementation of libp2p
|
||||
<p align="center">
|
||||
<a href="http://protocol.ai"><img src="https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" /></a>
|
||||
<a href="http://libp2p.io/"><img src="https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square" /></a>
|
||||
<a href="http://webchat.freenode.net/?channels=%23libp2p"><img src="https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square" /></a>
|
||||
<a href="https://riot.im/app/#/room/#libp2p:matrix.org"><img src="https://img.shields.io/badge/matrix-%23libp2p%3Apermaweb.io-blue.svg?style=flat-square" /> </a>
|
||||
<a href="https://discord.gg/66KBrm2"><img src="https://img.shields.io/discord/475789330380488707?color=blueviolet&label=discord&style=flat-square" /></a>
|
||||
<a href="https://discuss.libp2p.io"><img src="https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg" /></a>
|
||||
<a href="https://www.npmjs.com/package/libp2p"><img src="https://img.shields.io/npm/dm/libp2p.svg" /></a>
|
||||
<a href="https://www.jsdelivr.com/package/npm/libp2p"><img src="https://data.jsdelivr.com/v1/package/npm/libp2p/badge"/></a>
|
||||
</p>
|
||||
|
||||
## Interface
|
||||
<p align="center">
|
||||
<a href="https://github.com/libp2p/js-libp2p/actions?query=branch%3Amaster+workflow%3Aci+"><img src="https://img.shields.io/github/workflow/status/libp2p/js-libp2p/ci?label=ci&style=flat-square" /></a>
|
||||
<a href="https://codecov.io/gh/libp2p/js-libp2p"><img src="https://img.shields.io/codecov/c/github/libp2p/js-libp2p/master.svg?style=flat-square"></a>
|
||||
<a href="https://bundlephobia.com/result?p=ipfsd-ctl"><img src="https://flat.badgen.net/bundlephobia/minzip/ipfsd-ctl"></a>
|
||||
<br>
|
||||
<a href="https://david-dm.org/libp2p/js-libp2p"><img src="https://david-dm.org/libp2p/js-libp2p.svg?style=flat-square" /></a>
|
||||
<a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
|
||||
<a href="https://github.com/RichardLitt/standard-readme"><img src="https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D6.0.0-orange.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D12.0.0-orange.svg?style=flat-square" /></a>
|
||||
<br>
|
||||
</p>
|
||||
|
||||
> **This is a work in progress, interface might change at anytime**
|
||||
### Project status
|
||||
|
||||
libp2p expects a [Record Store interface](https://github.com/diasdavid/abstract-record-store), a swarm and one or more Peer Routers that implement the [Peer Routing](https://github.com/diasdavid/abstract-peer-routing), the goal is to keep simplicity and plugability while the remaining modules execute the heavy lifting.
|
||||
We've come a long way, but this project is still in Alpha, lots of development is happening, API might change, beware of the Dragons 🐉..
|
||||
|
||||
### Setting everything up
|
||||
The documentation in the master branch may contain changes from a pre-release.
|
||||
If you are looking for the documentation of the latest release, you can view the latest release on [**npm**](https://www.npmjs.com/package/libp2p), or select the tag in github that matches the version you are looking for.
|
||||
|
||||
```
|
||||
var libp2p = require('libp2p')
|
||||
**Want to get started?** Check our [GETTING_STARTED.md](./doc/GETTING_STARTED.md) guide and [examples folder](/examples).
|
||||
|
||||
**Want to update libp2p in your project?** Check our [migrations folder](./doc/migrations).
|
||||
|
||||
[**`Weekly Core Dev Calls`**](https://github.com/libp2p/team-mgmt/issues/16)
|
||||
|
||||
## Lead Maintainer
|
||||
|
||||
[Jacob Heun](https://github.com/jacobheun/)
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Background](#background)
|
||||
- [Install](#install)
|
||||
- [Usage](#usage)
|
||||
- [Configuration](#configuration)
|
||||
- [API](#api)
|
||||
- [Getting Started](#getting-started)
|
||||
- [Tutorials and Examples](#tutorials-and-examples)
|
||||
- [Development](#development)
|
||||
- [Tests](#tests)
|
||||
- [Packages](#packages)
|
||||
- [Contribute](#contribute)
|
||||
- [License](#license)
|
||||
|
||||
## Background
|
||||
|
||||
libp2p is the product of a long and arduous quest to understand the evolution of the Internet networking stack. In order to build P2P applications, devs have long had to make custom ad-hoc solutions to fit their needs, sometimes making some hard assumptions about their runtimes and the state of the network at the time of their development. Today, looking back more than 20 years, we see a clear pattern in the types of mechanisms built around the Internet Protocol, IP, which can be found throughout many layers of the OSI layer system, libp2p distils these mechanisms into flat categories and defines clear interfaces that once exposed, enable other protocols and applications to use and swap them, enabling upgradability and adaptability for the runtime, without breaking the API.
|
||||
|
||||
We are in the process of writing better documentation, blog posts, tutorials and a formal specification. Today you can find:
|
||||
|
||||
- [libp2p.io](https://libp2p.io)
|
||||
- [docs.libp2p.io](https://docs.libp2p.io)
|
||||
- [Specification (WIP)](https://github.com/libp2p/specs)
|
||||
- [Discussion Forums](https://discuss.libp2p.io)
|
||||
- Talks
|
||||
- [`libp2p <3 ethereum` at DEVCON2](https://ethereumfoundation.org/devcon/?session=libp2p) [📼 video](https://www.youtube.com/watch?v=HxueJbeMVG4) [slides](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p-HEART-devp2p-IPFS-PLUS-Ethereum-networking.pdf) [📼 demo-1](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo1-1.mp4) [📼 demo-2](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo2-1.mp4)
|
||||
- Articles
|
||||
- [The overview of libp2p](https://github.com/libp2p/libp2p#description)
|
||||
|
||||
To sum up, libp2p is a "network stack" -- a protocol suite -- that cleanly separates concerns, and enables sophisticated applications to only use the protocols they absolutely need, without giving up interoperability and upgradeability. libp2p grew out of IPFS, but it is built so that lots of people can use it, for lots of different projects.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install libp2p
|
||||
```
|
||||
|
||||
### Dialing and listening
|
||||
## Usage
|
||||
|
||||
libp2p.swarm.dialStream(peerInfo, protocol, options, function (err, stream) {})
|
||||
libp2p.swarm.handleProtocol(protocol, options, handlerFunction)
|
||||
### Configuration
|
||||
|
||||
### Using Peer Routing
|
||||
For all the information on how you can configure libp2p see [CONFIGURATION.md](./doc/CONFIGURATION.md).
|
||||
|
||||
libp2p.routing.findPeers(key, function (err, peerInfos) {})
|
||||
### API
|
||||
|
||||
### Using Records
|
||||
The specification is available on [API.md](./doc/API.md).
|
||||
|
||||
libp2p.record.get(key, function (err, records) {})
|
||||
libp2p.record.store(key, record)
|
||||
### Getting started
|
||||
|
||||
### Stats
|
||||
If you are starting your journey with `js-libp2p`, read the [GETTING_STARTED.md](./doc/GETTING_STARTED.md) guide.
|
||||
|
||||
### Tutorials and Examples
|
||||
|
||||
You can find multiple examples on the [examples folder](./examples) that will guide you through using libp2p for several scenarios.
|
||||
|
||||
## Notes
|
||||
## Development
|
||||
|
||||
Img for ref (till we get a better graph)
|
||||
**Clone and install dependencies:**
|
||||
|
||||

|
||||
```sh
|
||||
> git clone https://github.com/libp2p/js-libp2p.git
|
||||
> cd js-libp2p
|
||||
> npm install
|
||||
```
|
||||
|
||||
### Tests
|
||||
|
||||
#### Run unit tests
|
||||
|
||||
```sh
|
||||
# run all the unit tsts
|
||||
> npm test
|
||||
|
||||
# run just Node.js tests
|
||||
> npm run test:node
|
||||
|
||||
# run just Browser tests (Chrome)
|
||||
> npm run test:browser
|
||||
```
|
||||
|
||||
### Packages
|
||||
|
||||
List of packages currently in existence for libp2p
|
||||
|
||||
> This table is generated using the module `package-table` with `package-table --data=package-list.json`.
|
||||
|
||||
| Package | Version | Deps | CI | Coverage | Lead Maintainer |
|
||||
| ---------|---------|---------|---------|---------|--------- |
|
||||
| **libp2p** |
|
||||
| [`libp2p`](//github.com/libp2p/js-libp2p) | [](//github.com/libp2p/js-libp2p/releases) | [](https://david-dm.org/libp2p/js-libp2p) | [](https://travis-ci.com/libp2p/js-libp2p) | [](https://codecov.io/gh/libp2p/js-libp2p) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-daemon`](//github.com/libp2p/js-libp2p-daemon) | [](//github.com/libp2p/js-libp2p-daemon/releases) | [](https://david-dm.org/libp2p/js-libp2p-daemon) | [](https://travis-ci.com/libp2p/js-libp2p-daemon) | [](https://codecov.io/gh/libp2p/js-libp2p-daemon) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-daemon-client`](//github.com/libp2p/js-libp2p-daemon-client) | [](//github.com/libp2p/js-libp2p-daemon-client/releases) | [](https://david-dm.org/libp2p/js-libp2p-daemon-client) | [](https://travis-ci.com/libp2p/js-libp2p-daemon-client) | [](https://codecov.io/gh/libp2p/js-libp2p-daemon-client) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| [`libp2p-interfaces`](//github.com/libp2p/js-interfaces) | [](//github.com/libp2p/js-interfaces/releases) | [](https://david-dm.org/libp2p/js-interfaces) | [](https://travis-ci.com/libp2p/js-interfaces) | [](https://codecov.io/gh/libp2p/js-interfaces) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`interop-libp2p`](//github.com/libp2p/interop) | [](//github.com/libp2p/interop/releases) | [](https://david-dm.org/libp2p/interop) | [](https://travis-ci.com/libp2p/interop) | [](https://codecov.io/gh/libp2p/interop) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| **transports** |
|
||||
| [`libp2p-tcp`](//github.com/libp2p/js-libp2p-tcp) | [](//github.com/libp2p/js-libp2p-tcp/releases) | [](https://david-dm.org/libp2p/js-libp2p-tcp) | [](https://travis-ci.com/libp2p/js-libp2p-tcp) | [](https://codecov.io/gh/libp2p/js-libp2p-tcp) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-webrtc-direct`](//github.com/libp2p/js-libp2p-webrtc-direct) | [](//github.com/libp2p/js-libp2p-webrtc-direct/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-direct) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-direct) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-direct) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | [](https://travis-ci.com/libp2p/js-libp2p-websockets) | [](https://codecov.io/gh/libp2p/js-libp2p-websockets) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **secure channels** |
|
||||
| [`libp2p-noise`](//github.com/NodeFactoryIo/js-libp2p-noise) | [](//github.com/NodeFactoryIo/js-libp2p-noise/releases) | [](https://david-dm.org/NodeFactoryIo/js-libp2p-noise) | [](https://travis-ci.com/NodeFactoryIo/js-libp2p-noise) | [](https://codecov.io/gh/NodeFactoryIo/js-libp2p-noise) | N/A |
|
||||
| **stream multiplexers** |
|
||||
| [`libp2p-mplex`](//github.com/libp2p/js-libp2p-mplex) | [](//github.com/libp2p/js-libp2p-mplex/releases) | [](https://david-dm.org/libp2p/js-libp2p-mplex) | [](https://travis-ci.com/libp2p/js-libp2p-mplex) | [](https://codecov.io/gh/libp2p/js-libp2p-mplex) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **peer discovery** |
|
||||
| [`libp2p-bootstrap`](//github.com/libp2p/js-libp2p-bootstrap) | [](//github.com/libp2p/js-libp2p-bootstrap/releases) | [](https://david-dm.org/libp2p/js-libp2p-bootstrap) | [](https://travis-ci.com/libp2p/js-libp2p-bootstrap) | [](https://codecov.io/gh/libp2p/js-libp2p-bootstrap) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-mdns`](//github.com/libp2p/js-libp2p-mdns) | [](//github.com/libp2p/js-libp2p-mdns/releases) | [](https://david-dm.org/libp2p/js-libp2p-mdns) | [](https://travis-ci.com/libp2p/js-libp2p-mdns) | [](https://codecov.io/gh/libp2p/js-libp2p-mdns) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`@chainsafe/discv5`](//github.com/ChainSafe/discv5) | [](//github.com/ChainSafe/discv5/releases) | [](https://david-dm.org/ChainSafe/discv5) | [](https://travis-ci.com/ChainSafe/discv5) | [](https://codecov.io/gh/ChainSafe/discv5) | [Cayman Nava](mailto:caymannava@gmail.com) |
|
||||
| **content routing** |
|
||||
| [`libp2p-delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing) | [](//github.com/libp2p/js-libp2p-delegated-content-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing) | [](https://travis-ci.com/libp2p/js-libp2p-delegated-content-routing) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **peer routing** |
|
||||
| [`libp2p-delegated-peer-routing`](//github.com/libp2p/js-libp2p-delegated-peer-routing) | [](//github.com/libp2p/js-libp2p-delegated-peer-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-peer-routing) | [](https://travis-ci.com/libp2p/js-libp2p-delegated-peer-routing) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **utilities** |
|
||||
| [`libp2p-crypto`](//github.com/libp2p/js-libp2p-crypto) | [](//github.com/libp2p/js-libp2p-crypto/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto) | [](https://travis-ci.com/libp2p/js-libp2p-crypto) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-crypto-secp256k1`](//github.com/libp2p/js-libp2p-crypto-secp256k1) | [](//github.com/libp2p/js-libp2p-crypto-secp256k1/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto-secp256k1) | [](https://travis-ci.com/libp2p/js-libp2p-crypto-secp256k1) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
|
||||
| **data types** |
|
||||
| [`peer-id`](//github.com/libp2p/js-peer-id) | [](//github.com/libp2p/js-peer-id/releases) | [](https://david-dm.org/libp2p/js-peer-id) | [](https://travis-ci.com/libp2p/js-peer-id) | [](https://codecov.io/gh/libp2p/js-peer-id) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| **pubsub** |
|
||||
| [`libp2p-floodsub`](//github.com/libp2p/js-libp2p-floodsub) | [](//github.com/libp2p/js-libp2p-floodsub/releases) | [](https://david-dm.org/libp2p/js-libp2p-floodsub) | [](https://travis-ci.com/libp2p/js-libp2p-floodsub) | [](https://codecov.io/gh/libp2p/js-libp2p-floodsub) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-gossipsub`](//github.com/ChainSafe/js-libp2p-gossipsub) | [](//github.com/ChainSafe/js-libp2p-gossipsub/releases) | [](https://david-dm.org/ChainSafe/js-libp2p-gossipsub) | [](https://travis-ci.com/ChainSafe/js-libp2p-gossipsub) | [](https://codecov.io/gh/ChainSafe/js-libp2p-gossipsub) | [Cayman Nava](mailto:caymannava@gmail.com) |
|
||||
| **extensions** |
|
||||
| [`libp2p-nat-mgnr`](//github.com/libp2p/js-libp2p-nat-mgnr) | [](//github.com/libp2p/js-libp2p-nat-mgnr/releases) | [](https://david-dm.org/libp2p/js-libp2p-nat-mgnr) | [](https://travis-ci.com/libp2p/js-libp2p-nat-mgnr) | [](https://codecov.io/gh/libp2p/js-libp2p-nat-mgnr) | N/A |
|
||||
| [`libp2p-utils`](//github.com/libp2p/js-libp2p-utils) | [](//github.com/libp2p/js-libp2p-utils/releases) | [](https://david-dm.org/libp2p/js-libp2p-utils) | [](https://travis-ci.com/libp2p/js-libp2p-utils) | [](https://codecov.io/gh/libp2p/js-libp2p-utils) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
|
||||
## Contribute
|
||||
|
||||
The libp2p implementation in JavaScript is a work in progress. As such, there are a few things you can do right now to help out:
|
||||
|
||||
- Go through the modules and **check out existing issues**. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
|
||||
- **Perform code reviews**. Most of this has been developed by @diasdavid, which means that more eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
|
||||
- **Add tests**. There can never be enough tests.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE) © Protocol Labs
|
||||
|
60
RELEASE.md
Normal file
60
RELEASE.md
Normal file
@ -0,0 +1,60 @@
|
||||
# Release Template
|
||||
|
||||
> short tl;dr; of the release
|
||||
|
||||
# 🗺 What's left for release
|
||||
|
||||
# 🔦 Highlights
|
||||
|
||||
# 🏗 API Changes
|
||||
|
||||
# ✅ Release Checklist
|
||||
|
||||
- Robustness and quality
|
||||
- [ ] Ensure that all tests are passing, this includes:
|
||||
- [ ] unit
|
||||
- [ ] Publish a release candidate to npm
|
||||
```sh
|
||||
# Minor prerelease (e.g. 0.24.1 -> 0.25.0-rc.0)
|
||||
$ npx aegir release --type preminor -t node -t browser --preid rc --dist-tag next
|
||||
|
||||
# Increment prerelease (e.g. 0.25.0-rc.0 -> 0.25.0-rc.1)
|
||||
$ npx aegir release --type prerelease -t node -t browser --preid rc --dist-tag next
|
||||
```
|
||||
- [ ] Run tests of the following projects with the new release:
|
||||
- [ ] [js-ipfs](https://github.com/ipfs/js-ipfs)
|
||||
- Documentation
|
||||
- [ ] Ensure that README.md is up to date
|
||||
- [ ] Ensure that all the examples run
|
||||
- [ ] Ensure [libp2p/js-libp2p-examples](https://github.com/libp2p/js-libp2p-examples) is updated
|
||||
- [ ] Ensure that [libp2p/docs](https://github.com/libp2p/docs) is updated
|
||||
- Communication
|
||||
- [ ] Create the release issue
|
||||
- [ ] Take a snapshot between of everyone that has contributed to this release (including its subdeps in IPFS, libp2p, IPLD and multiformats) using [`name-your-contributors`](https://www.npmjs.com/package/name-your-contributors). Generate a nice markdown list with [this script](https://gist.github.com/jacobheun/d2ff479ca991733c13cdcf688a1317e5)
|
||||
- [ ] Announcements (both pre-release and post-release)
|
||||
- [ ] Twitter
|
||||
- [ ] IRC
|
||||
- [ ] Reddit
|
||||
- [ ] [discuss.libp2p.io](https://discuss.libp2p.io/c/news)
|
||||
- [ ] Blog post
|
||||
- [ ] Copy release notes to the [GitHub Release description](https://github.com/libp2p/js-libp2p/releases)
|
||||
|
||||
# ❤️ Huge thank you to everyone that made this release possible
|
||||
|
||||
In alphabetical order, here are all the humans that contributed to the release:
|
||||
|
||||
- ...
|
||||
|
||||
# 🙌🏽 Want to contribute?
|
||||
|
||||
Would you like to contribute to the libp2p project and don't know how? Well, there are a few places you can get started:
|
||||
|
||||
- Check the issues with the `help wanted` label in the [libp2p repo](https://github.com/libp2p/js-libp2p/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22)
|
||||
- Join an IPFS All Hands, introduce yourself and let us know where you would like to contribute - https://github.com/ipfs/team-mgmt#all-hands-call
|
||||
- Hack with IPFS and show us what you made! The All Hands call is also the perfect venue for demos, join in and show us what you built
|
||||
- Join the discussion at http://discuss.libp2p.io/ and help users finding their answers.
|
||||
- Join the [⚡️libp2p Weekly Sync 🙌🏽](https://github.com/libp2p/team-mgmt/issues/16) and be part of the Sprint action!
|
||||
|
||||
# ⁉️ Do you have questions?
|
||||
|
||||
The best place to ask your questions about libp2p, how it works and what you can do with it is at [discuss.libp2p.io](https://discuss.libp2p.io). We are also available at the #libp2p channel on Freenode.
|
2092
doc/API.md
Normal file
2092
doc/API.md
Normal file
File diff suppressed because it is too large
Load Diff
791
doc/CONFIGURATION.md
Normal file
791
doc/CONFIGURATION.md
Normal file
@ -0,0 +1,791 @@
|
||||
# Configuration
|
||||
|
||||
- [Configuration](#configuration)
|
||||
- [Overview](#overview)
|
||||
- [Modules](#modules)
|
||||
- [Transport](#transport)
|
||||
- [Stream Multiplexing](#stream-multiplexing)
|
||||
- [Connection Encryption](#connection-encryption)
|
||||
- [Peer Discovery](#peer-discovery)
|
||||
- [Content Routing](#content-routing)
|
||||
- [Peer Routing](#peer-routing)
|
||||
- [DHT](#dht)
|
||||
- [Pubsub](#pubsub)
|
||||
- [Customizing libp2p](#customizing-libp2p)
|
||||
- [Examples](#examples)
|
||||
- [Basic setup](#basic-setup)
|
||||
- [Customizing Peer Discovery](#customizing-peer-discovery)
|
||||
- [Setup webrtc transport and discovery](#setup-webrtc-transport-and-discovery)
|
||||
- [Customizing Pubsub](#customizing-pubsub)
|
||||
- [Customizing DHT](#customizing-dht)
|
||||
- [Setup with Content and Peer Routing](#setup-with-content-and-peer-routing)
|
||||
- [Setup with Relay](#setup-with-relay)
|
||||
- [Setup with Auto Relay](#setup-with-auto-relay)
|
||||
- [Setup with Keychain](#setup-with-keychain)
|
||||
- [Configuring Dialing](#configuring-dialing)
|
||||
- [Configuring Connection Manager](#configuring-connection-manager)
|
||||
- [Configuring Transport Manager](#configuring-transport-manager)
|
||||
- [Configuring Metrics](#configuring-metrics)
|
||||
- [Configuring PeerStore](#configuring-peerstore)
|
||||
- [Customizing Transports](#customizing-transports)
|
||||
- [Configuring the NAT Manager](#configuring-the-nat-manager)
|
||||
- [Browser support](#browser-support)
|
||||
- [UPnP and NAT-PMP](#upnp-and-nat-pmp)
|
||||
- [Configuration examples](#configuration-examples)
|
||||
|
||||
## Overview
|
||||
|
||||
libp2p is a modular networking stack. It's designed to be able to suit a variety of project needs. The configuration of libp2p is a key part of its structure. It enables you to bring exactly what you need, and only what you need. This document is a guide on how to configure libp2p for your particular project. Check out the [Configuration examples](#configuration-examples) section if you're just looking to leverage an existing configuration.
|
||||
|
||||
Regardless of how you configure libp2p, the top level [API](./API.md) will always remain the same. **Note**: if some modules are not configured, like Content Routing, using those methods will throw errors.
|
||||
|
||||
## Modules
|
||||
|
||||
`js-libp2p` acts as the composer for this modular p2p networking stack using libp2p compatible modules as its subsystems. For getting an instance of `js-libp2p` compliant with all types of networking requirements, it is possible to specify the following subsystems:
|
||||
|
||||
- Transports
|
||||
- Multiplexers
|
||||
- Connection encryption mechanisms
|
||||
- Peer discovery protocols
|
||||
- Content routing protocols
|
||||
- Peer routing protocols
|
||||
- DHT implementation
|
||||
- Pubsub router
|
||||
|
||||
The libp2p ecosystem contains at least one module for each of these subsystems. The user should install and import the modules that are relevant for their requirements. Moreover, thanks to the existing interfaces it is easy to create a libp2p compatible module and use it.
|
||||
|
||||
After selecting the modules to use, it is also possible to configure each one according to your needs.
|
||||
|
||||
Bear in mind that a **transport** and **connection encryption** module are **required**, while all the other subsystems are optional.
|
||||
|
||||
### Transport
|
||||
|
||||
> In a p2p system, we need to interact with other peers in the network. Transports are used to establish connections between peers. The libp2p transports to use should be decided according to the environment where your node will live, as well as other requirements that you might have.
|
||||
|
||||
Some available transports are:
|
||||
|
||||
- [libp2p/js-libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp)
|
||||
- [libp2p/js-libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
|
||||
- [libp2p/js-libp2p-webrtc-direct](https://github.com/libp2p/js-libp2p-webrtc-direct)
|
||||
- [libp2p/js-libp2p-websockets](https://github.com/libp2p/js-libp2p-websockets)
|
||||
- [libp2p/js-libp2p-utp](https://github.com/libp2p/js-libp2p-utp) (Work in Progress)
|
||||
|
||||
You should take into consideration that `js-libp2p-tcp` and `js-libp2p-utp` are not available in a **browser** environment.
|
||||
|
||||
If none of the available transports fulfills your needs, you can create a libp2p compatible transport. A libp2p transport just needs to be compliant with the [Transport Interface](https://github.com/libp2p/js-interfaces/tree/master/src/transport).
|
||||
|
||||
If you want to know more about libp2p transports, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/transport
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
|
||||
### Stream Multiplexing
|
||||
|
||||
> Libp2p peers will need to communicate with each other through several protocols during their life. Stream multiplexing allows multiple independent logical streams to share a common underlying transport medium, instead of creating a new connection with the same peer per needed protocol.
|
||||
|
||||
Some available stream multiplexers are:
|
||||
|
||||
- [libp2p/js-libp2p-mplex](https://github.com/libp2p/js-libp2p-mplex)
|
||||
|
||||
If none of the available stream multiplexers fulfills your needs, you can create a libp2p compatible stream multiplexer. A libp2p multiplexer just needs to be compliant with the [Stream Muxer Interface](https://github.com/libp2p/js-interfaces/tree/master/src/stream-muxer).
|
||||
|
||||
If you want to know more about libp2p stream multiplexing, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/stream-multiplexing
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
- https://github.com/libp2p/specs/tree/master/mplex
|
||||
|
||||
### Connection Encryption
|
||||
|
||||
> A connection encryption mechanism must be used, in order to ensure all exchanged data between two peers is encrypted.
|
||||
|
||||
Some available connection encryption protocols:
|
||||
|
||||
- [NodeFactoryIo/js-libp2p-noise](https://github.com/NodeFactoryIo/js-libp2p-noise)
|
||||
- [libp2p/js-libp2p-secio](https://github.com/libp2p/js-libp2p-secio) ⚠️ [DEPRECATED](https://blog.ipfs.io/2020-08-07-deprecating-secio)
|
||||
|
||||
If none of the available connection encryption mechanisms fulfills your needs, you can create a libp2p compatible one. A libp2p connection encryption protocol just needs to be compliant with the [Crypto Interface](https://github.com/libp2p/js-interfaces/tree/master/src/crypto).
|
||||
|
||||
If you want to know more about libp2p connection encryption, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/secure-comms
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
|
||||
### Peer Discovery
|
||||
|
||||
> In a p2p network, peer discovery is critical to a functioning system.
|
||||
|
||||
Some available peer discovery modules are:
|
||||
|
||||
- [js-libp2p-mdns](https://github.com/libp2p/js-libp2p-mdns)
|
||||
- [js-libp2p-bootstrap](https://github.com/libp2p/js-libp2p-bootstrap)
|
||||
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
|
||||
- [js-libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
|
||||
- [discv5](https://github.com/chainsafe/discv5)
|
||||
|
||||
**Note**: `peer-discovery` services within transports (such as `js-libp2p-webrtc-star`) are automatically gathered from the `transport`, via it's `discovery` property. As such, they do not need to be added in the discovery modules. However, these transports can also be configured and disabled as the other ones.
|
||||
|
||||
If none of the available peer discovery protocols fulfills your needs, you can create a libp2p compatible one. A libp2p peer discovery protocol just needs to be compliant with the [Peer Discovery Interface](https://github.com/libp2p/js-interfaces/tree/master/src/peer-discovery).
|
||||
|
||||
If you want to know more about libp2p peer discovery, you should read the following content:
|
||||
|
||||
- https://github.com/libp2p/specs/blob/master/discovery/mdns.md
|
||||
|
||||
### Content Routing
|
||||
|
||||
> Content routing provides a way to find where content lives in the network. It works in two steps: 1) Peers provide (announce) to the network that they are holders of specific content and 2) Peers issue queries to find where that content lives. A Content Routing mechanism could be as complex as a DHT or as simple as a registry somewhere in the network.
|
||||
|
||||
Some available content routing modules are:
|
||||
|
||||
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
|
||||
- [js-libp2p-delegated-content-routing](https://github.com/libp2p/js-libp2p-delegated-content-routing)
|
||||
|
||||
If none of the available content routing protocols fulfills your needs, you can create a libp2p compatible one. A libp2p content routing protocol just needs to be compliant with the [Content Routing Interface](https://github.com/libp2p/js-interfaces/tree/master/src/content-routing). **(WIP: This module is not yet implemented)**
|
||||
|
||||
If you want to know more about libp2p content routing, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/content-routing
|
||||
|
||||
### Peer Routing
|
||||
|
||||
> Peer Routing offers a way to find other peers in the network by issuing queries using a Peer Routing algorithm, which may be iterative or recursive. If the algorithm is unable to find the target peer, it will return the peers that are "closest" to the target peer, using a distance metric defined by the algorithm.
|
||||
|
||||
Some available peer routing modules are:
|
||||
|
||||
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
|
||||
- [js-libp2p-delegated-peer-routing](https://github.com/libp2p/js-libp2p-delegated-peer-routing)
|
||||
|
||||
If none of the available peer routing protocols fulfills your needs, you can create a libp2p compatible one. A libp2p peer routing protocol just needs to be compliant with the [Peer Routing Interface](https://github.com/libp2p/js-interfaces/tree/master/src/peer-routing). **(WIP: This module is not yet implemented)**
|
||||
|
||||
If you want to know more about libp2p peer routing, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/peer-routing
|
||||
|
||||
### DHT
|
||||
|
||||
> A DHT can provide content and peer routing capabilities in a p2p system, as well as peer discovery capabilities.
|
||||
|
||||
The DHT implementation currently available is [libp2p/js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht). This implementation is largely based on the Kademlia whitepaper, augmented with notions from S/Kademlia, Coral and mainlineDHT.
|
||||
|
||||
If this DHT implementation does not fulfill your needs and you want to create or use your own implementation, please get in touch with us through a github issue. We plan to work on improving the ability to bring your own DHT in a future release.
|
||||
|
||||
If you want to know more about libp2p DHT, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/protocols/#kad-dht
|
||||
- https://github.com/libp2p/specs/pull/108
|
||||
|
||||
### Pubsub
|
||||
|
||||
> Publish/Subscribe is a system where peers congregate around topics they are interested in. Peers interested in a topic are said to be subscribed to that topic and should receive the data published on it from other peers.
|
||||
|
||||
Some available pubsub routers are:
|
||||
|
||||
- [libp2p/js-libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub)
|
||||
- [ChainSafe/js-libp2p-gossipsub](https://github.com/ChainSafe/js-libp2p-gossipsub)
|
||||
|
||||
If none of the available pubsub routers fulfills your needs, you can create a libp2p compatible one. A libp2p pubsub router just needs to be created on top of [libp2p/js-libp2p-pubsub](https://github.com/libp2p/js-libp2p-pubsub), which ensures `js-libp2p` API expectations.
|
||||
|
||||
If you want to know more about libp2p pubsub, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/publish-subscribe
|
||||
- https://github.com/libp2p/specs/tree/master/pubsub
|
||||
|
||||
## Customizing libp2p
|
||||
|
||||
When [creating a libp2p node](./API.md#create), the modules needed should be specified as follows:
|
||||
|
||||
```js
|
||||
const modules = {
|
||||
transport: [],
|
||||
streamMuxer: [],
|
||||
connEncryption: [],
|
||||
contentRouting: [],
|
||||
peerRouting: [],
|
||||
peerDiscovery: [],
|
||||
dht: dhtImplementation,
|
||||
pubsub: pubsubImplementation
|
||||
}
|
||||
```
|
||||
|
||||
Moreover, the majority of the modules can be customized via option parameters. This way, it is also possible to provide this options through a `config` object. This config object should have the property name of each building block to configure, the same way as the modules specification.
|
||||
|
||||
Besides the `modules` and `config`, libp2p allows other internal options and configurations:
|
||||
- `datastore`: an instance of [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore/) modules.
|
||||
- This is used in modules such as the DHT. If it is not provided, `js-libp2p` will use an in memory datastore.
|
||||
- `peerId`: the identity of the node, an instance of [libp2p/js-peer-id](https://github.com/libp2p/js-peer-id).
|
||||
- This is particularly useful if you want to reuse the same `peer-id`, as well as for modules like `libp2p-delegated-content-routing`, which need a `peer-id` in their instantiation.
|
||||
- `addresses`: an object containing `listen`, `announce` and `announceFilter`:
|
||||
- `listen` addresses will be provided to the libp2p underlying transports for listening on them.
|
||||
- `announce` addresses will be used to compute the advertises that the node should advertise to the network.
|
||||
- `announceFilter`: filter function used to filter announced addresses programmatically: `(ma: Array<multiaddr>) => Array<multiaddr>`. Default: returns all addresses. [`libp2p-utils`](https://github.com/libp2p/js-libp2p-utils) provides useful [multiaddr utilities](https://github.com/libp2p/js-libp2p-utils/blob/master/API.md#multiaddr-isloopbackma) to create your filters.
|
||||
|
||||
### Examples
|
||||
|
||||
#### Basic setup
|
||||
|
||||
```js
|
||||
// Creating a libp2p node with:
|
||||
// transport: websockets + tcp
|
||||
// stream-muxing: mplex
|
||||
// crypto-channel: noise
|
||||
// discovery: multicast-dns
|
||||
// dht: kad-dht
|
||||
// pubsub: gossipsub
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
const GossipSub = require('libp2p-gossipsub')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
new WS() // It can take instances too!
|
||||
],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE],
|
||||
peerDiscovery: [MulticastDNS],
|
||||
dht: DHT,
|
||||
pubsub: GossipSub
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Customizing Peer Discovery
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE],
|
||||
peerDiscovery: [MulticastDNS, Bootstrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections)
|
||||
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
|
||||
// The associated object, will be passed to the service when it is instantiated.
|
||||
[MulticastDNS.tag]: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
},
|
||||
[Bootstrap.tag:] {
|
||||
list: [ // A list of bootstrap peers to connect to starting up the node
|
||||
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
||||
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
||||
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
|
||||
],
|
||||
interval: 2000,
|
||||
enabled: true
|
||||
}
|
||||
// .. other discovery module options.
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup webrtc transport and discovery
|
||||
|
||||
```js
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const WS = require('libp2p-websockets')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [
|
||||
WS,
|
||||
WebRTCStar
|
||||
],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE],
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
[WebRTCStar.tag]: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Customizing Pubsub
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const GossipSub = require('libp2p-gossipsub')
|
||||
|
||||
const { SignaturePolicy } = require('libp2p-interfaces/src/pubsub/signature-policy')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE],
|
||||
pubsub: GossipSub
|
||||
},
|
||||
config: {
|
||||
pubsub: { // The pubsub options (and defaults) can be found in the pubsub router documentation
|
||||
enabled: true,
|
||||
emitSelf: false, // whether the node should emit to self on publish
|
||||
globalSignaturePolicy: SignaturePolicy.StrictSign // message signing policy
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Customizing DHT
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
dht: { // The DHT options (and defaults) can be found in its documentation
|
||||
kBucketSize: 20,
|
||||
enabled: true,
|
||||
randomWalk: {
|
||||
enabled: true, // Allows to disable discovery (enabled by default)
|
||||
interval: 300e3,
|
||||
timeout: 10e3
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup with Content and Peer Routing
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const ipfsHttpClient = require('ipfs-http-client')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
// create a peerId
|
||||
const peerId = await PeerId.create()
|
||||
|
||||
const delegatedPeerRouting = new DelegatedPeerRouter(ipfsHttpClient({
|
||||
host: 'node0.delegate.ipfs.io', // In production you should setup your own delegates
|
||||
protocol: 'https',
|
||||
port: 443
|
||||
}))
|
||||
|
||||
const delegatedContentRouting = new DelegatedContentRouter(peerId, ipfsHttpClient({
|
||||
host: 'node0.delegate.ipfs.io', // In production you should setup your own delegates
|
||||
protocol: 'https',
|
||||
port: 443
|
||||
}))
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE],
|
||||
contentRouting: [delegatedContentRouting],
|
||||
peerRouting: [delegatedPeerRouting],
|
||||
},
|
||||
peerId,
|
||||
peerRouting: { // Peer routing configuration
|
||||
refreshManager: { // Refresh known and connected closest peers
|
||||
enabled: true, // Should find the closest peers.
|
||||
interval: 6e5, // Interval for getting the new for closest peers of 10min
|
||||
bootDelay: 10e3 // Delay for the initial query for closest peers
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup with Relay
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
config: {
|
||||
relay: { // Circuit Relay options (this config is part of libp2p core configurations)
|
||||
enabled: true, // Allows you to dial and accept relayed connections. Does not make you a relay.
|
||||
hop: {
|
||||
enabled: true, // Allows you to be a relay for other peers
|
||||
active: true // You will attempt to dial destination peers if you are not connected to them
|
||||
},
|
||||
advertise: {
|
||||
bootDelay: 15 * 60 * 1000, // Delay before HOP relay service is advertised on the network
|
||||
enabled: true, // Allows you to disable the advertise of the Hop service
|
||||
ttl: 30 * 60 * 1000 // Delay Between HOP relay service advertisements on the network
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup with Auto Relay
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
config: {
|
||||
relay: { // Circuit Relay options (this config is part of libp2p core configurations)
|
||||
enabled: true, // Allows you to dial and accept relayed connections. Does not make you a relay.
|
||||
autoRelay: {
|
||||
enabled: true, // Allows you to bind to relays with HOP enabled for improving node dialability
|
||||
maxListeners: 2 // Configure maximum number of HOP relays to use
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup with Keychain
|
||||
|
||||
Libp2p allows you to setup a secure keychain to manage your keys. The keychain configuration object should have the following properties:
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| pass | `string` | Passphrase to use in the keychain (minimum of 20 characters). |
|
||||
| datastore | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) |
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const LevelStore = require('datastore-level')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
keychain: {
|
||||
pass: 'notsafepassword123456789',
|
||||
datastore: new LevelStore('path/to/store')
|
||||
}
|
||||
})
|
||||
|
||||
await libp2p.loadKeychain()
|
||||
```
|
||||
|
||||
#### Configuring Dialing
|
||||
|
||||
Dialing in libp2p can be configured to limit the rate of dialing, and how long dials are allowed to take. The dialer configuration object should have the following properties:
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| maxParallelDials | `number` | How many multiaddrs we can dial in parallel. |
|
||||
| maxDialsPerPeer | `number` | How many multiaddrs we can dial per peer, in parallel. |
|
||||
| dialTimeout | `number` | Second dial timeout per peer in ms. |
|
||||
| resolvers | `object` | Dial [Resolvers](https://github.com/multiformats/js-multiaddr/blob/master/src/resolvers/index.js) for resolving multiaddrs |
|
||||
| addressSorter | `(Array<Address>) => Array<Address>` | Sort the known addresses of a peer before trying to dial. |
|
||||
|
||||
The below configuration example shows how the dialer should be configured, with the current defaults:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const { dnsaddrResolver } = require('multiaddr/src/resolvers')
|
||||
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
dialer: {
|
||||
maxParallelDials: 100,
|
||||
maxDialsPerPeer: 4,
|
||||
dialTimeout: 30e3,
|
||||
resolvers: {
|
||||
dnsaddr: dnsaddrResolver
|
||||
},
|
||||
addressSorter: publicAddressesFirst
|
||||
}
|
||||
```
|
||||
|
||||
#### Configuring Connection Manager
|
||||
|
||||
The Connection Manager prunes Connections in libp2p whenever certain limits are exceeded. If Metrics are enabled, you can also configure the Connection Manager to monitor the bandwidth of libp2p and prune connections as needed. You can read more about what Connection Manager does at [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md). The configuration values below show the defaults for Connection Manager. See [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md#options) for a full description of the parameters.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
connectionManager: {
|
||||
maxConnections: Infinity,
|
||||
minConnections: 0,
|
||||
pollInterval: 2000,
|
||||
defaultPeerValue: 1,
|
||||
// The below values will only be taken into account when Metrics are enabled
|
||||
maxData: Infinity,
|
||||
maxSentData: Infinity,
|
||||
maxReceivedData: Infinity,
|
||||
maxEventLoopDelay: Infinity,
|
||||
movingAverageInterval: 60000
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Configuring Transport Manager
|
||||
|
||||
The Transport Manager is responsible for managing the libp2p transports life cycle. This includes starting listeners for the provided listen addresses, closing these listeners and dialing using the provided transports. By default, if a libp2p node has a list of multiaddrs for listenning on and there are no valid transports for those multiaddrs, libp2p will throw an error on startup and shutdown. However, for some applications it is perfectly acceptable for libp2p nodes to start in dial only mode if all the listen multiaddrs failed. This error tolerance can be enabled as follows:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const { FaultTolerance } = require('libp2p/src/transport-manager')}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
transportManager: {
|
||||
faultTolerance: FaultTolerance.NO_FATAL
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Configuring Metrics
|
||||
|
||||
Metrics are disabled in libp2p by default. You can enable and configure them as follows:
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| enabled | `boolean` | Enabled metrics collection. |
|
||||
| computeThrottleMaxQueueSize | `number` | How many messages a stat will queue before processing. |
|
||||
| computeThrottleTimeout | `number` | Time in milliseconds a stat will wait, after the last item was added, before processing. |
|
||||
| movingAverageIntervals | `Array<number>` | The moving averages that will be computed. |
|
||||
| maxOldPeersRetention | `number` | How many disconnected peers we will retain stats for. |
|
||||
|
||||
The below configuration example shows how the metrics should be configured. Aside from enabled being `false` by default, the following default configuration options are listed below:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
metrics: {
|
||||
enabled: true,
|
||||
computeThrottleMaxQueueSize: 1000,
|
||||
computeThrottleTimeout: 2000,
|
||||
movingAverageIntervals: [
|
||||
60 * 1000, // 1 minute
|
||||
5 * 60 * 1000, // 5 minutes
|
||||
15 * 60 * 1000 // 15 minutes
|
||||
],
|
||||
maxOldPeersRetention: 50
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Configuring PeerStore
|
||||
|
||||
PeerStore persistence is disabled in libp2p by default. You can enable and configure it as follows. Aside from enabled being `false` by default, it will need an implementation of a [datastore](https://github.com/ipfs/interface-datastore). Take into consideration that using the memory datastore will be ineffective for persistence.
|
||||
|
||||
The threshold number represents the maximum number of "dirty peers" allowed in the PeerStore, i.e. peers that are not updated in the datastore. In this context, browser nodes should use a threshold of 1, since they might not "stop" properly in several scenarios and the PeerStore might end up with unflushed records when the window is closed.
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| persistence | `boolean` | Is persistence enabled. |
|
||||
| threshold | `number` | Number of dirty peers allowed. |
|
||||
|
||||
The below configuration example shows how the PeerStore should be configured. Aside from persistence being `false` by default, the following default configuration options are listed below:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const LevelStore = require('datastore-level')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
datastore: new LevelStore('path/to/store'),
|
||||
peerStore: {
|
||||
persistence: true,
|
||||
threshold: 5
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Customizing Transports
|
||||
|
||||
Some Transports can be passed additional options when they are created. For example, `libp2p-webrtc-star` accepts an optional, custom `wrtc` implementation. In addition to libp2p passing itself and an `Upgrader` to handle connection upgrading, libp2p will also pass the options, if they are provided, from `config.transport`.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const wrtc = require('wrtc')
|
||||
|
||||
const transportKey = WebRTCStar.prototype[Symbol.toStringTag]
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebRTCStar],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
config: {
|
||||
transport: {
|
||||
[transportKey]: {
|
||||
wrtc // You can use `wrtc` when running in Node.js
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
During Libp2p startup, transport listeners will be created for the configured listen multiaddrs. Some transports support custom listener options and you can set them using the `listenerOptions` in the transport configuration. For example, [libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star) transport listener supports the configuration of its underlying [simple-peer](https://github.com/feross/simple-peer) ice server(STUN/TURN) config as follows:
|
||||
|
||||
```js
|
||||
const transportKey = WebRTCStar.prototype[Symbol.toStringTag]
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebRTCStar],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
addresses: {
|
||||
listen: ['/dns4/your-wrtc-star.pub/tcp/443/wss/p2p-webrtc-star'] // your webrtc dns multiaddr
|
||||
},
|
||||
config: {
|
||||
transport: {
|
||||
[transportKey]: {
|
||||
listenerOptions: {
|
||||
config: {
|
||||
iceServers: [
|
||||
{"urls": ["turn:YOUR.TURN.SERVER:3478"], "username": "YOUR.USER", "credential": "YOUR.PASSWORD"},
|
||||
{"urls": ["stun:YOUR.STUN.SERVER:3478"], "username": "", "credential": ""}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Configuring the NAT Manager
|
||||
|
||||
Network Address Translation (NAT) is a function performed by your router to enable multiple devices on your local network to share a single IPv4 address. It's done transparently for outgoing connections, ensuring the correct response traffic is routed to your computer, but if you wish to accept incoming connections some configuration is necessary.
|
||||
|
||||
The NAT manager can be configured as follows:
|
||||
|
||||
```js
|
||||
const node = await Libp2p.create({
|
||||
config: {
|
||||
nat: {
|
||||
description: 'my-node', // set as the port mapping description on the router, defaults the current libp2p version and your peer id
|
||||
enabled: true, // defaults to true
|
||||
gateway: '192.168.1.1', // leave unset to auto-discover
|
||||
externalIp: '80.1.1.1', // leave unset to auto-discover
|
||||
ttl: 7200, // TTL for port mappings (min 20 minutes)
|
||||
keepAlive: true, // Refresh port mapping after TTL expires
|
||||
pmp: {
|
||||
enabled: false, // defaults to false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
##### Browser support
|
||||
|
||||
Browsers cannot open TCP ports or send the UDP datagrams necessary to configure external port mapping - to accept incoming connections in the browser please use a WebRTC transport.
|
||||
|
||||
##### UPnP and NAT-PMP
|
||||
|
||||
By default under nodejs libp2p will attempt to use [UPnP](https://en.wikipedia.org/wiki/Universal_Plug_and_Play) to configure your router to allow incoming connections to any TCP transports that have been configured.
|
||||
|
||||
[NAT-PMP](http://miniupnp.free.fr/nat-pmp.html) is a feature of some modern routers which performs a similar job to UPnP. NAT-PMP is disabled by default, if enabled libp2p will try to use NAT-PMP and will fall back to UPnP if it fails.
|
||||
|
||||
## Configuration examples
|
||||
|
||||
As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:
|
||||
|
||||
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/src/core/runtime/libp2p-nodejs.js) - libp2p configuration used by js-ipfs when running in Node.js
|
||||
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/src/core/runtime/libp2p-browser.js) - libp2p configuration used by js-ipfs when running in a Browser (that supports WebRTC)
|
||||
|
||||
If you have developed a project using `js-libp2p`, please consider submitting your configuration to this list so that it can be found easily by other users.
|
||||
|
||||
The [examples](../examples) are also a good source of help for finding a configuration that suits your needs.
|
20
doc/CONNECTION_MANAGER.md
Normal file
20
doc/CONNECTION_MANAGER.md
Normal file
@ -0,0 +1,20 @@
|
||||
# Connection Manager
|
||||
|
||||
The Connection Manager works with the Registrar to keep connections across libp2p within acceptable ranges, which can be configured. By default Connection Manager will monitor:
|
||||
- The total number of open connections
|
||||
- The latency/delay of the event loop
|
||||
|
||||
If Metrics are enabled for libp2p, see [./CONFIGURATION.md#configuring-metrics](./CONFIGURATION.md#configuring-metrics) on how to configure metrics, the Connection Manager can be used to prune connections when certain limits are exceeded.
|
||||
|
||||
The following is a list of available options for setting limits for the Connection Manager to enforce.
|
||||
|
||||
## Options
|
||||
- `maxConnections`: the maximum number of connections libp2p is willing to have before it starts disconnecting. Defaults to `Infinity`
|
||||
- `minConnections`: the minimum number of connections below which libp2p not activate preemptive disconnections. Defaults to `0`.
|
||||
- `maxData`: sets the maximum data — in bytes per second - (sent and received) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
- `maxSentData`: sets the maximum sent data — in bytes per second - this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
- `maxReceivedData`: sets the maximum received data — in bytes per second - this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
- `maxEventLoopDelay`: sets the maximum event loop delay (measured in milliseconds) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
- `pollInterval`: sets the poll interval (in milliseconds) for assessing the current state and determining if this peer needs to force a disconnect. Defaults to `2000` (2 seconds).
|
||||
- `movingAverageInterval`: the interval used to calculate moving averages (in milliseconds). Defaults to `60000` (1 minute). This must be an available interval configured in `Metrics`
|
||||
- `defaultPeerValue`: number between 0 and 1. Defaults to 1.
|
32
doc/DIALER.md
Normal file
32
doc/DIALER.md
Normal file
@ -0,0 +1,32 @@
|
||||
# js-libp2p Dialer
|
||||
|
||||
**Synopsis**
|
||||
* Parallel dials to the same peer will yield the same connection/error when the first dial settles.
|
||||
* All Dial Requests in js-libp2p must request a token(s) from the Dialer.
|
||||
* The number of tokens requested should be between 1 and the MAX_PER_PEER_DIALS max set in the Dialer.
|
||||
* If the number of available tokens is less than requested, the Dialer may return less than requested.
|
||||
* The number of tokens a DialRequest obtains reflects the maximum number of parallel Multiaddr Dials it can make.
|
||||
* If no tokens are available a DialRequest should immediately end and throw.
|
||||
* As tokens are limited, DialRequests should be given a prioritized list of Multiaddrs to minimize the potential request time.
|
||||
* Once a Multiaddr Dial has succeeded, all pending dials in that Dial Request should be aborted.
|
||||
* If DIAL_TIMEOUT time has elapsed before any one Multiaddr Dial succeeds, all remaining dials in the DialRequest should be aborted.
|
||||
* When a Multiaddr Dial is settled, if there are no more addresses to dial, its token should be released back to the dialer.
|
||||
* Once the DialRequest is settled, any remaining tokens should be released to the dialer.
|
||||
|
||||
## Multiaddr Confidence
|
||||
|
||||
An effective dialing system should involve the inclusion of a Confidence system for Multiaddrs. This enables ranking of Multiaddrs so that a prioritized list can be passed to DialRequests to maximize usage of Dialer Tokens, and minimize connection times.
|
||||
|
||||
**Not Yet Implemented**: This system will be designed and implemented in a future update.
|
||||
|
||||
## Notes
|
||||
|
||||
* A DialRequest gets a set of tokens from the Dialer, up to the MAX_PER_PEER_DIALS max.
|
||||
* A DialRequest SHOULD fail if no dial tokens are available. The DialRequest MAY proceed without tokens, but this should be reserved for High Priority actions and should be rare.
|
||||
* A DialRequest MUST NOT request more tokens than it has addresses to dial. Example: If the MAX_PER_PEER_DIALS is 4 and a DialRequest has 1 address, it should only request 1 token.
|
||||
* A DialRequest SHOULD execute parallel dials for each of its addresses up the total number of tokens it has.
|
||||
* On a successful dial, the DialRequest MUST abort any other in progress dials, return the successful connection and release all tokens.
|
||||
* A new DialRequest SHOULD be given a descending list of prioritized Multiaddrs, based on their confidence. Having higher confidence Multiaddrs first can help minimize the time a DialRequest is active.
|
||||
* A failed dial to a Multiaddr SHOULD add that Multiaddr to a temporary denyList.
|
||||
* A failed dial to a Multiaddr SHOULD lower the confidence of that Multiaddr.
|
||||
* A successful dial to a Multiaddr SHOULD increase the confidence of that Multiaddr.
|
256
doc/GETTING_STARTED.md
Normal file
256
doc/GETTING_STARTED.md
Normal file
@ -0,0 +1,256 @@
|
||||
# Getting Started
|
||||
|
||||
Welcome to libp2p! This guide will walk you through setting up a fully functional libp2p node 🚀
|
||||
|
||||
- [Getting Started](#getting-started)
|
||||
- [Install](#install)
|
||||
- [Configuring libp2p](#configuring-libp2p)
|
||||
- [Basic setup](#basic-setup)
|
||||
- [Transports](#transports)
|
||||
- [Connection Encryption](#connection-encryption)
|
||||
- [Multiplexing](#multiplexing)
|
||||
- [Running Libp2p](#running-libp2p)
|
||||
- [Custom setup](#custom-setup)
|
||||
- [Peer Discovery](#peer-discovery)
|
||||
- [Pubsub](#pubsub)
|
||||
- [What is next](#what-is-next)
|
||||
|
||||
## Install
|
||||
|
||||
The first step is to install libp2p in your project:
|
||||
|
||||
```sh
|
||||
npm install libp2p
|
||||
```
|
||||
|
||||
## Configuring libp2p
|
||||
|
||||
If you're new to libp2p, we recommend configuring your node in stages, as this can make troubleshooting configuration issues much easier. In this guide, we'll do just that. If you're more experienced with libp2p, you may wish to jump to the [Configuration readme](./CONFIGURATION.md).
|
||||
|
||||
### Basic setup
|
||||
|
||||
Now that we have libp2p installed, let's configure the minimum needed to get your node running. The only modules libp2p requires are a [**Transport**][transport] and [**Crypto**][crypto] module. However, we recommend that a basic setup should also have a [**Stream Multiplexer**](streamMuxer) configured, which we will explain shortly. Let's start by setting up a Transport.
|
||||
|
||||
#### Transports
|
||||
|
||||
Libp2p uses Transports to establish connections between peers over the network. Transports are the components responsible for performing the actual exchange of data between libp2p nodes. You can configure any number of Transports, but you only need 1 to start with. Supporting more Transports will improve the ability of your node to speak to a larger number of nodes on the network, as matching Transports are required for two nodes to communicate with one another.
|
||||
|
||||
You should select Transports according to the runtime of your application; Node.js or the browser. You can see a list of some of the available Transports in the [configuration readme](./CONFIGURATION.md#transport). For this guide let's install `libp2p-websockets`, as it can be used in both Node.js and the browser.
|
||||
|
||||
Start by installing `libp2p-websockets`:
|
||||
|
||||
```sh
|
||||
npm install libp2p-websockets
|
||||
```
|
||||
|
||||
Now that we have the module installed, let's configure libp2p to use the Transport. We'll use the [`Libp2p.create`](./API.md#create) method, which takes a single configuration object as its only parameter. We can add the Transport by passing it into the `modules.transport` array:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
There are multiple libp2p transports available, you should evaluate the needs of your application and select the Transport(s) that best suit your requirements. You can add as many transports as you like to `modules.transport` in order to establish connections with as many peers as possible.
|
||||
|
||||
<details><summary>Read More</summary>
|
||||
If you want to know more about libp2p transports, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/transport
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
</details>
|
||||
|
||||
#### Connection Encryption
|
||||
|
||||
Encryption is an important part of communicating on the libp2p network. Every connection must be encrypted to help ensure security for everyone. As such, Connection Encryption (Crypto) is a required component of libp2p.
|
||||
|
||||
There are a growing number of Crypto modules being developed for libp2p. As those are released they will be tracked in the [Connection Encryption section of the configuration readme](./CONFIGURATION.md#connection-encryption). For now, we are going to configure our node to use the `libp2p-noise` module.
|
||||
|
||||
```sh
|
||||
npm install libp2p-noise
|
||||
```
|
||||
|
||||
With `libp2p-noise` installed, we can add it to our existing configuration by importing it and adding it to the `modules.connEncryption` array:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
connEncryption: [NOISE]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
<details><summary>Read More</summary>
|
||||
If you want to know more about libp2p connection encryption, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/secure-comms
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
</details>
|
||||
|
||||
#### Multiplexing
|
||||
|
||||
While multiplexers are not strictly required, they are highly recommended as they improve the effectiveness and efficiency of connections for the various protocols libp2p runs. Adding a multiplexer to your configuration will allow libp2p to run several of its internal protocols, like Identify, as well as allow your application to easily run any number of protocols over a single connection.
|
||||
|
||||
Looking at the [available stream multiplexing](./CONFIGURATION.md#stream-multiplexing) modules, js-libp2p currently only supports `libp2p-mplex`, so we will use that here. Bear in mind that future libp2p Transports might have `multiplexing` capabilities already built-in (such as `QUIC`).
|
||||
|
||||
You can install `libp2p-mplex` and add it to your libp2p node as follows in the next example.
|
||||
|
||||
```sh
|
||||
npm install libp2p-mplex
|
||||
```
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
<details><summary>Read More</summary>
|
||||
If you want to know more about libp2p stream multiplexing, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/stream-multiplexing
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
- https://github.com/libp2p/specs/tree/master/mplex
|
||||
</details>
|
||||
|
||||
#### Running Libp2p
|
||||
|
||||
Now that you have configured a [**Transport**][transport], [**Crypto**][crypto] and [**Stream Multiplexer**](streamMuxer) module, you can start your libp2p node. We can start and stop libp2p using the [`libp2p.start()`](./API.md#start) and [`libp2p.stop()`](./API.md#stop) methods.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/8000/ws']
|
||||
},
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
|
||||
// start libp2p
|
||||
await node.start()
|
||||
console.log('libp2p has started')
|
||||
|
||||
const listenAddrs = node.transportManager.getAddrs()
|
||||
console.log('libp2p is listening on the following addresses: ', listenAddrs)
|
||||
|
||||
const advertiseAddrs = node.multiaddrs
|
||||
console.log('libp2p is advertising the following addresses: ', advertiseAddrs)
|
||||
|
||||
// stop libp2p
|
||||
await node.stop()
|
||||
console.log('libp2p has stopped')
|
||||
```
|
||||
|
||||
### Custom setup
|
||||
|
||||
Once your libp2p node is running, it is time to get it connected to the public network. We can do this via peer discovery.
|
||||
|
||||
#### Peer Discovery
|
||||
|
||||
Peer discovery is an important part of creating a well connected libp2p node. A static list of peers will often be used to join the network, but it's useful to couple other discovery mechanisms to ensure you're able to discover other peers that are important to your application.
|
||||
|
||||
For each discovered peer libp2p will emit a `peer:discovery` event which includes metadata about that peer. You can read the [Events](./API.md#events) in the API doc to learn more.
|
||||
|
||||
Looking at the [available peer discovery](./CONFIGURATION.md#peer-discovery) protocols, there are several options to be considered:
|
||||
- If you already know the addresses of some other network peers, you should consider using `libp2p-bootstrap` as this is the easiest way of getting your peer into the network.
|
||||
- If it is likely that you will have other peers on your local network, `libp2p-mdns` is a must if you're node is not running in the browser. It allows peers to discover each other when on the same local network.
|
||||
- If your application is browser based you can use the `libp2p-webrtc-star` Transport, which includes a rendezvous based peer sharing service.
|
||||
- A random walk approach can be used via `libp2p-kad-dht`, to crawl the network and find new peers along the way.
|
||||
|
||||
For this guide we will configure `libp2p-bootstrap` as this is useful for joining the public network.
|
||||
|
||||
Let's install `libp2p-bootstrap`.
|
||||
|
||||
```sh
|
||||
npm install libp2p-bootstrap
|
||||
```
|
||||
|
||||
We can provide specific configurations for each protocol within a `config.peerDiscovery` property in the options as shown below.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
// Known peers addresses
|
||||
const bootstrapMultiaddrs = [
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN'
|
||||
]
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX],
|
||||
peerDiscovery: [Bootstrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections)
|
||||
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
|
||||
// The associated object, will be passed to the service when it is instantiated.
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
list: bootstrapMultiaddrs // provide array of multiaddrs
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
node.on('peer:discovery', (peer) => {
|
||||
console.log('Discovered %s', peer.id.toB58String()) // Log discovered peer
|
||||
})
|
||||
|
||||
node.connectionManager.on('peer:connect', (connection) => {
|
||||
console.log('Connected to %s', connection.remotePeer.toB58String()) // Log connected peer
|
||||
})
|
||||
|
||||
// start libp2p
|
||||
await node.start()
|
||||
```
|
||||
|
||||
<details><summary>Read More</summary>
|
||||
If you want to know more about libp2p peer discovery, you should read the following content:
|
||||
|
||||
- https://github.com/libp2p/specs/blob/master/discovery/mdns.md
|
||||
</details>
|
||||
|
||||
## What is next
|
||||
|
||||
There are a lot of other concepts within `libp2p`, that are not covered in this guide. For additional configuration options we recommend checking out the [Configuration Readme](./CONFIGURATION.md) and the [examples folder](../examples). If you have any problems getting started, or if anything isn't clear, please let us know by submitting an issue!
|
||||
|
||||
|
||||
[transport]: https://github.com/libp2p/js-interfaces/tree/master/src/transport
|
||||
[crypto]: https://github.com/libp2p/js-interfaces/tree/master/src/crypto
|
||||
[streamMuxer]: https://github.com/libp2p/js-interfaces/tree/master/src/stream-muxer
|
28
doc/METRICS.md
Normal file
28
doc/METRICS.md
Normal file
@ -0,0 +1,28 @@
|
||||
# Bandwidth Metrics
|
||||
|
||||
- Metrics gathering is optional, as there is a performance hit to using it.
|
||||
- Metrics do NOT currently contain OS level stats, only libp2p application level Metrics. For example, TCP messages (ACK, FIN, etc) are not accounted for.
|
||||
- See the [API](./API.md) for Metrics usage. Metrics in libp2p do not emit events, as such applications wishing to read Metrics will need to do so actively. This ensures that the system is not unnecessarily firing update notifications.
|
||||
|
||||
## Tracking
|
||||
- When a transport hands off a connection for upgrading, Metrics are hooked up if enabled.
|
||||
- When a stream is created, Metrics will be tracked on that stream and associated to that streams protocol.
|
||||
- Tracked Metrics are associated to a specific peer, and count towards global bandwidth Metrics.
|
||||
|
||||
### Metrics Processing
|
||||
- The main Metrics object consists of individual `Stats` objects
|
||||
- The following categories are tracked:
|
||||
- Global stats; every byte in and out
|
||||
- Peer stats; every byte in and out, per peer
|
||||
- Protocol stats; every byte in and out, per protocol
|
||||
- When a message goes through Metrics:
|
||||
- It is added to the global stat
|
||||
- It is added to the stats for the remote peer
|
||||
- It is added to the protocol stats if there is one
|
||||
- When data is pushed onto a `Stat` it is added to a queue
|
||||
- The queue is processed at the earliest of either (configurable):
|
||||
- every 2 seconds after the last item was added to the queue
|
||||
- or once 1000 items have been queued
|
||||
- When the queue is processed:
|
||||
- The data length is added to either the `in` or `out` stat
|
||||
- The moving averages is calculated since the last queue processing (based on most recently processed item timestamp)
|
60
doc/PEER_DISCOVERY.md
Normal file
60
doc/PEER_DISCOVERY.md
Normal file
@ -0,0 +1,60 @@
|
||||
# Peer Discovery and Auto Dial
|
||||
|
||||
**Synopsis**:
|
||||
* All peers discovered are emitted via `peer:discovery` so applications can take any desired action.
|
||||
* Libp2p defaults to automatically connecting to new peers, when under the [ConnectionManager](https://github.com/libp2p/js-libp2p-connection-manager) low watermark (minimum peers).
|
||||
* Applications can disable this via the `peerDiscovery.autoDial` config property, and handle connections themselves.
|
||||
* Applications who have not disabled this should **never** connect on peer discovery. Applications should use the `peer:connect` event if they wish to take a specific action on new peers.
|
||||
|
||||
## Scenarios
|
||||
In any scenario, if a peer is discovered it should be added to the PeerBook. This ensures that even if we don't dial to a node when we discover it, we know about it in the event that it becomes known as a provider for something we need. The scenarios listed below detail what actions the auto dialer will take when peers are discovered.
|
||||
|
||||
### 1. Joining the network
|
||||
The node is new and needs to join the network. It currently has 0 peers.
|
||||
**Discovery Mechanisms**: [Ambient Discovery](#ambient-discovery)
|
||||
|
||||
### Action to take
|
||||
Connect to discovered peers. This should have some degree of concurrency limiting. While the case should be low, if we immediately discover more peers than our high watermark we should avoid dialing them all.
|
||||
|
||||
### 2. Connected to some
|
||||
The node is connected to other nodes. The current number of connections is less than the desired low watermark.
|
||||
**Discovery Mechanisms**: [Ambient Discovery](#ambient-discovery) and [Active Discovery](#active-discovery)
|
||||
|
||||
### Action to take
|
||||
Connect to discovered peers. This should have some degree of concurrency limiting. The concurrency may need to be modified to reflect the current number of peers connected. The more peers we have, the lower the concurrency may need to be.
|
||||
|
||||
### 3. Connected to enough
|
||||
**Discovery Mechanisms**: [Ambient Discovery](#ambient-discovery) and [Active Discovery](#active-discovery)
|
||||
|
||||
### Action to take
|
||||
None. If we are connected to enough peers, the low watermark, we should not connect to discovered peers. As other peers discover us, they may connect to us based on their current scenario.
|
||||
|
||||
For example, a long running node with adequate peers is on an MDNS network. A new peer joins the network and both become aware of each other. The new peer should be the peer that dials, as it has too few peers. The existing node has no reason to dial the new peer, but should keep a record of it in case it later becomes an important node due to its contents/capabilities.
|
||||
|
||||
Avoiding dials above the low watermark also allows for a pool of connections to be reserved for application specific actions, such as connecting to a specific content provider via a DHT query to find that content (ipfs-bitswap).
|
||||
|
||||
### 4. Connected to too many
|
||||
The node has more connections than it wants. The current number of connections is greater than the high watermark.
|
||||
|
||||
[WIP Connection Manager v2 spec](https://github.com/libp2p/specs/pull/161)
|
||||
**Discovery Mechanisms**: [Ambient Discovery](#ambient-discovery) and [Active Discovery](#active-discovery)
|
||||
|
||||
### Action to take
|
||||
None, the `ConnectionManager` will automatically prune connections.
|
||||
|
||||
## Discovery Mechanisms
|
||||
Means of which a libp2p node discovers other peers.
|
||||
|
||||
### Active Discovery
|
||||
Through active use of the libp2p network, a node may discovery peers.
|
||||
|
||||
* Content/Peer routing (DHT, delegated, etc) provider and peer queries
|
||||
* DHT random walk
|
||||
* Rendezvous servers
|
||||
|
||||
### Ambient Discovery
|
||||
Leveraging known addresses, or network discovery mechanisms, a node may discover peers outside of the bounds of the libp2p network.
|
||||
|
||||
* Bootstrap
|
||||
* MDNS
|
||||
* proximity based (bluetooth, sound, etc)
|
164
doc/STREAMING_ITERABLES.md
Normal file
164
doc/STREAMING_ITERABLES.md
Normal file
@ -0,0 +1,164 @@
|
||||
# Iterable Streams
|
||||
|
||||
> This document is a guide on how to use Iterable Streams in Libp2p. As a part of the [refactor away from callbacks](https://github.com/ipfs/js-ipfs/issues/1670), we have also moved to using Iterable Streams instead of [pull-streams](https://pull-stream.github.io/). If there are missing usage guides you feel should be added, please submit a PR!
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Iterable Streams](#iterable-streams)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [Usage Guide](#usage-guide)
|
||||
- [Transforming Bidirectional Data](#transforming-bidirectional-data)
|
||||
- [Iterable Stream Types](#iterable-stream-types)
|
||||
- [Source](#source)
|
||||
- [Sink](#sink)
|
||||
- [Transform](#transform)
|
||||
- [Duplex](#duplex)
|
||||
- [Iterable Modules](#iterable-modules)
|
||||
|
||||
## Usage Guide
|
||||
|
||||
### Transforming Bidirectional Data
|
||||
|
||||
Sometimes you may need to wrap an existing duplex stream in order to perform incoming and outgoing [transforms](#transform) on data. This type of wrapping is commonly used in stream encryption/decryption. Using [it-pair][it-pair] and [it-pipe][it-pipe], we can do this rather easily, given an existing [duplex iterable](#duplex).
|
||||
|
||||
```js
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
// Wrapper is what we will write and read from
|
||||
// This gives us two duplex iterables that are internally connected
|
||||
const [internal, external] = duplexPair()
|
||||
|
||||
// Now we can pipe our wrapper to the existing duplex iterable
|
||||
pipe(
|
||||
external, // The external half of the pair interacts with the existing duplex
|
||||
outgoingTransform, // A transform iterable to send data through (ie: encrypting)
|
||||
existingDuplex, // The original duplex iterable we are wrapping
|
||||
incomingTransform, // A transform iterable to read data through (ie: decrypting)
|
||||
external
|
||||
)
|
||||
|
||||
// We can now read and write from the other half of our pair
|
||||
pipe(
|
||||
['some data'],
|
||||
internal, // The internal half of the pair is what we will interact with to read/write data
|
||||
async (source) => {
|
||||
for await (const chunk of source) {
|
||||
console.log('Data: %s', chunk.toString())
|
||||
// > Data: some data
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## Iterable Stream Types
|
||||
|
||||
These types are pulled from [@alanshaw's gist](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9) on streaming iterables.
|
||||
|
||||
### Source
|
||||
|
||||
A "source" is something that can be consumed. It is an iterable object.
|
||||
|
||||
```js
|
||||
const ints = {
|
||||
[Symbol.asyncIterator] () {
|
||||
let i = 0
|
||||
return {
|
||||
async next () {
|
||||
return { done: false, value: i++ }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// or, more succinctly using a generator and for/await:
|
||||
|
||||
const ints = (async function * () {
|
||||
let i = 0
|
||||
while (true) yield i++
|
||||
})()
|
||||
```
|
||||
|
||||
### Sink
|
||||
|
||||
A "sink" is something that consumes (or drains) a source. It is a function that takes a source and iterates over it. It optionally returns a value.
|
||||
|
||||
```js
|
||||
const logger = async source => {
|
||||
const it = source[Symbol.asyncIterator]()
|
||||
while (true) {
|
||||
const { done, value } = await it.next()
|
||||
if (done) break
|
||||
console.log(value) // prints 0, 1, 2, 3...
|
||||
}
|
||||
}
|
||||
|
||||
// or, more succinctly using a generator and for/await:
|
||||
|
||||
const logger = async source => {
|
||||
for await (const chunk of source) {
|
||||
console.log(chunk) // prints 0, 1, 2, 3...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Transform
|
||||
|
||||
A "transform" is both a sink _and_ a source where the values it consumes and the values that can be consumed from it are connected in some way. It is a function that takes a source and returns a source.
|
||||
|
||||
```js
|
||||
const doubler = source => {
|
||||
return {
|
||||
[Symbol.asyncIterator] () {
|
||||
const it = source[Symbol.asyncIterator]()
|
||||
return {
|
||||
async next () {
|
||||
const { done, value } = await it.next()
|
||||
if (done) return { done }
|
||||
return { done, value: value * 2 }
|
||||
}
|
||||
return () {
|
||||
return it.return && it.return()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// or, more succinctly using a generator and for/await:
|
||||
|
||||
const doubler = source => (async function * () {
|
||||
for await (const chunk of source) {
|
||||
yield chunk * 2
|
||||
}
|
||||
})()
|
||||
```
|
||||
|
||||
### Duplex
|
||||
|
||||
A "duplex" is similar to a transform but the values it consumes are not necessarily connected to the values that can be consumed from it. It is an object with two properties, `sink` and `source`.
|
||||
|
||||
```js
|
||||
const duplex = {
|
||||
sink: async source => {/* ... */},
|
||||
source: { [Symbol.asyncIterator] () {/* ... */} }
|
||||
}
|
||||
```
|
||||
|
||||
## Iterable Modules
|
||||
|
||||
- [it-handshake][it-handshake] Handshakes for binary protocols with iterable streams.
|
||||
- [it-length-prefixed][it-length-prefixed] Streaming length prefixed buffers with async iterables.
|
||||
- [it-pair][it-pair] Paired streams that are internally connected.
|
||||
- [it-pipe][it-pipe] Create a pipeline of iterables. Works with duplex streams.
|
||||
- [it-pushable][it-pushable] An iterable that you can push values into.
|
||||
- [it-reader][it-reader] Read an exact number of bytes from a binary, async iterable.
|
||||
- [streaming-iterables][streaming-iterables] A Swiss army knife for async iterables.
|
||||
|
||||
[it-handshake]: https://github.com/jacobheun/it-handshake
|
||||
[it-length-prefixed]: https://github.com/alanshaw/it-length-prefixed
|
||||
[it-pair]: https://github.com/alanshaw/it-pair
|
||||
[it-pipe]: https://github.com/alanshaw/it-pipe
|
||||
[it-pushable]: https://github.com/alanshaw/it-pushable
|
||||
[it-reader]: https://github.com/alanshaw/it-reader
|
||||
[streaming-iterables]: https://github.com/reconbot/streaming-iterables
|
178
doc/migrations/v0.26-v0.27.md
Normal file
178
doc/migrations/v0.26-v0.27.md
Normal file
@ -0,0 +1,178 @@
|
||||
# Migrating to the libp2p@0.27 API
|
||||
|
||||
A migration guide for refactoring your application code from libp2p v0.26.x to v0.27.0.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Migrating from callbacks](#migrating-from-callbacks)
|
||||
- [Pull Streams to Streaming Iterables](#pull-streams-to-streaming-iterables)
|
||||
- [Sample API Migrations](#sample-api-migrations)
|
||||
- [Registering Protocol Handlers](#registering-protocol-handlers)
|
||||
- [Dialing and Sending Data](#dialing-and-sending-data)
|
||||
- [Checking if a peer is connected](#checking-if-a-peer-is-connected)
|
||||
- [Pinging another peer](#pinging-another-peer)
|
||||
- [Pubsub](#pubsub)
|
||||
- [Getting subscribers](#getting-subscribers)
|
||||
- [Getting subscribed topics](#getting-subscribed-topics)
|
||||
|
||||
## Migrating from callbacks
|
||||
|
||||
Callbacks are no longer supported in the libp2p API, as the API has now fully moved to async / await. You can see a full list of the available methods in the [API readme][api]
|
||||
|
||||
**Before**
|
||||
```js
|
||||
libp2p.start((err) => {
|
||||
if (err) throw err
|
||||
console.log('libp2p started')
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
await libp2p.start()
|
||||
console.log('libp2p started')
|
||||
```
|
||||
|
||||
## Pull Streams to Streaming Iterables
|
||||
|
||||
The libp2p API no longer supports Pull Streams and has migrated to [Streaming Iterables][streaming_iterable]. If you would like to continue using Pull Streams in your application code, or need additional time to migrate your code base, you can leverage the conversion modules [async-iterator-to-pull-stream](https://github.com/alanshaw/async-iterator-to-pull-stream) and [pull-stream-to-async-iterator](https://github.com/alanshaw/pull-stream-to-async-iterator).
|
||||
|
||||
For a growing list of async iterator modules, you should follow the [it-awesome repo][it_awesome].
|
||||
|
||||
## Sample API Migrations
|
||||
|
||||
### Registering Protocol Handlers
|
||||
|
||||
Protocol registration is very similar to how it previously was, however, the handler now takes a single parameter containing the incoming stream and its protocol. Additionally, you can now pass an array of protocols to `.handle`, but a single string is still supported.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const pull = require('pull-stream')
|
||||
libp2p.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const pipe = require('it-pipe')
|
||||
libp2p.handle(['/echo/1.0.0'], ({ protocol, stream }) => pipe(stream, stream))
|
||||
```
|
||||
|
||||
### Dialing and Sending Data
|
||||
|
||||
`dialProtocol` no longer takes a callback, and will now return a [Streaming Iterable][streaming_iterable] and the protocol that was successfully negotiated. The new stream can be used with async iterator modules, see [it-awesome][it_awesome], instead of pull streams.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const pull = require('pull-stream')
|
||||
libp2p.dialProtocol(peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(
|
||||
pull.values(['hey']),
|
||||
conn,
|
||||
pull.drain((data) => {
|
||||
console.log('received echo:', data.toString())
|
||||
}, (err) => {
|
||||
if (err) { throw err }
|
||||
})
|
||||
)
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const pipe = require('it-pipe')
|
||||
const { protocol, stream } = await libp2p.dialProtocol(peerInfo, '/echo/1.0.0')
|
||||
await pipe(
|
||||
['hey'],
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const data of source) {
|
||||
console.log('received echo:', data.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
### Checking if a peer is connected
|
||||
|
||||
`peerInfo.isConnected` has been deprecated. libp2p now tracks all connections centrally and will no longer update the state of `peerInfo.isConnected`. Consumers should switch to using `libp2p.registrar.getConnection(peerInfo)`, which will return an open connection to that peer if one exists.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
if (peerInfo.isConnected()) {
|
||||
// ...do something if connected
|
||||
}
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const connection = libp2p.registrar.getConnection(peerInfo)
|
||||
if (connection) {
|
||||
// ...do something if connected
|
||||
}
|
||||
```
|
||||
|
||||
### Pinging another peer
|
||||
|
||||
`libp2p.ping` will no longer callback with a `Ping` event emitter. The internal logic has been simplified to give more flexibility to the API. `libp2p.ping` will now execute a single ping and return the latency.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
libp2p.ping(peerInfo, (err, ping) => {
|
||||
if (err) throw err
|
||||
ping.once('ping', (latency) => {
|
||||
console.log('Latency is %s ms', latency)
|
||||
ping.stop()
|
||||
})
|
||||
|
||||
ping.start()
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const latency = await libp2p.ping(peerInfo)
|
||||
console.log('Latency is %s ms', latency)
|
||||
```
|
||||
|
||||
### Pubsub
|
||||
|
||||
#### Getting subscribers
|
||||
|
||||
`libp2p.pubsub.peers()` is now `libp2p.pubsub.getSubscribers()` and is no longer an asynchronous action.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
libp2p.pubsub.peers(topic, (err, subscribers) => {
|
||||
if (err) throw err
|
||||
console.log('Subscribers:', subscribers)
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const subscribers = libp2p.pubsub.getSubscribers(topic)
|
||||
console.log('Subscribers:', subscribers)
|
||||
```
|
||||
|
||||
#### Getting subscribed topics
|
||||
|
||||
`libp2p.pubsub.ls()` is now `libp2p.pubsub.getTopics()` and is no longer an asynchronous action.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
libp2p.pubsub.ls((err, topics) => {
|
||||
if (err) throw err
|
||||
console.log('Topics:', topics)
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const topics = libp2p.pubsub.getTopics()
|
||||
console.log('Topics:', topics)
|
||||
```
|
||||
|
||||
[api]: ../API.md
|
||||
[it_awesome]: https://github.com/alanshaw/it-awesome
|
||||
[streaming_iterable]: ../STREAMING_ITERABLES.md
|
343
doc/migrations/v0.27-v0.28.md
Normal file
343
doc/migrations/v0.27-v0.28.md
Normal file
@ -0,0 +1,343 @@
|
||||
# Migrating to the libp2p@0.28 API
|
||||
|
||||
A migration guide for refactoring your application code from libp2p v0.27.x to v0.28.0.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [PeerStore API](#peerstore-api)
|
||||
- [Migrating from Peer Info](#migrating-from-peer-info)
|
||||
- [Create](#create)
|
||||
- [API Implications](#api-implications)
|
||||
- [Connection Manager and Registrar](#connection-manager-and-registrar)
|
||||
- [Events](#events)
|
||||
- [Module Updates](#module-updates)
|
||||
|
||||
## PeerStore API
|
||||
|
||||
In `libp2p@0.27` we integrated the PeerStore (former [peer-book](https://github.com/libp2p/js-peer-book)) into the codebase. By that time, it was not documented in the [API DOC](../API.md) since it kept the same API as the `peer-book` and it was expected to be completelly rewritten in `libp2p@0.28`.
|
||||
|
||||
Moving towards a separation of concerns regarding known peers' data, as well as enabling PeerStore persistence, the PeerStore is now divided into four main components: `AddressBook`, `ProtoBook`, `KeyBook` and `MetadataBook`. This resulted in API changes in the PeerStore, since each type of peer data should now be added in an atomic fashion.
|
||||
|
||||
### Adding a Peer
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peerId = ...
|
||||
const peerInfo = new PeerInfo(peerId)
|
||||
|
||||
peerInfo.protocols.add('/ping/1.0.0')
|
||||
peerInfo.protocols.add('/ping/2.0.0')
|
||||
peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/0')
|
||||
|
||||
libp2p.peerStore.put(peerInfo)
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peerId = ...
|
||||
const protocols = ['/ping/1.0.0', 'ping/2.0.0']
|
||||
const multiaddrs = ['/ip4/127.0.0.1/tcp/0']
|
||||
|
||||
libp2p.peerStore.protoBook.add(peerId, protocols)
|
||||
libp2p.peerStore.addressBook.add(peerId, multiaddrs)
|
||||
```
|
||||
|
||||
### Getting a Peer
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peerId = ...
|
||||
const peerInfo = libp2p.peerStore.get(peerId)
|
||||
// { id: PeerId, multiaddrs: MultiaddrSet, protocols: Set<string>}
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peerId = ...
|
||||
const peer = libp2p.peerStore.get(peerId)
|
||||
// { id: PeerId, addresses: Array<{ multiaddr: Multiaddr }>, protocols: Array<string> }
|
||||
```
|
||||
|
||||
### Checking for a Peer
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peerId = ...
|
||||
const hasData = libp2p.peerStore.has(peerId)
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peerId = ...
|
||||
const hasData = Boolean(libp2p.peerStore.get(peerId))
|
||||
```
|
||||
|
||||
### Removing a Peer
|
||||
|
||||
**Before**
|
||||
```js
|
||||
libp2p.peerStore.remove(peerId)
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
// Atomic
|
||||
libp2p.peerStore.protoBook.delete(peerId)
|
||||
libp2p.peerStore.addressBook.delete(peerId)
|
||||
// Remove the peer and ALL of its associated data
|
||||
libp2p.peerStore.delete(peerId)
|
||||
```
|
||||
|
||||
### Get all known Peers
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peers = libp2p.peerStore.peers
|
||||
// Map<string, PeerInfo>
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peers = libp2p.peerStore.peers
|
||||
// Similar to libp2p.peerStore.get()
|
||||
// Map<string, { id: PeerId, addresses: Array<{ multiaddr: Multiaddr }>, protocols: Array<string> }
|
||||
```
|
||||
|
||||
## Migrating from Peer Info
|
||||
|
||||
[`PeerInfo`][peer-info] is a libp2p peer abstraction layer that combines a [`PeerId`][peer-id] with known data of the peer, namely its multiaddrs and protocols. It has been used for a long time by `js-libp2p` and its modules to carry this data around the libp2p stack, as well as by the libp2p API, both for providing this data to the users or to receive it from them.
|
||||
|
||||
Since this PeerInfo instances were navigating through the entire codebases, some data inconsistencies could be observed in libp2p. Different libp2p subsystems were running with different visions of the known peers data. For instance, a libp2p subsystem receives a copy of this instance with the peer multiaddrs and protocols, but if new data of the peer is obtained from other subsystem, it would not be updated on the former. Moreover, considering that several subsystems were modifying the peer data, libp2p had no way to determine the accurate data.
|
||||
|
||||
Considering the complete revamp of the libp2p PeerStore towards its second version, the PeerStore now acts as the single source of truth, we do not need to carry [`PeerInfo`][peer-info] instances around. This also solves all the problems stated above, since subsystems will report new observations to the PeerStore.
|
||||
|
||||
### Create
|
||||
|
||||
While it was possible to create a libp2p node without providing a [`PeerInfo`][peer-info], there were 2 use cases where a [`PeerInfo`][peer-info] was provided when creating a libp2p node.
|
||||
|
||||
#### Using an existing PeerId
|
||||
|
||||
`libp2p.create` receives a `peerId` property instead of a `peerInfo` property.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peerId = ...
|
||||
const peerInfo = new PeerInfo(peerId)
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
peerInfo
|
||||
// ...
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peerId = ...
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
peerId
|
||||
// ...
|
||||
})
|
||||
```
|
||||
|
||||
#### Providing listen addresses
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peerId = ...
|
||||
const peerInfo = new PeerInfo(peerId)
|
||||
|
||||
peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/0')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
peerInfo
|
||||
// ...
|
||||
})
|
||||
|
||||
await libp2p.start()
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peerId = ...
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
peerId,
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0']
|
||||
}
|
||||
// ...
|
||||
})
|
||||
await libp2p.start()
|
||||
```
|
||||
|
||||
There is also a bonus regarding the peer addresses. `libp2p@0.28` comes with an AddressManager that also allows the configuration of `announce` and `noAnnounce` addresses.
|
||||
This was possible to achieve before, but in a hacky way by removing or adding addresses to the `peerInfo`, after the node starts.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peerId = ...
|
||||
const peerInfo = new PeerInfo(peerId)
|
||||
|
||||
peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/8000')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
peerInfo
|
||||
// ...
|
||||
})
|
||||
|
||||
await libp2p.start()
|
||||
peerInfo.multiaddrs.add('/dns4/peer.io') // Announce
|
||||
peerInfo.multiaddrs.delete('/ip4/127.0.0.1/tcp/8000') // Not announce
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peerId = ...
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
peerId,
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/8000'],
|
||||
announce: ['/dns4/peer.io'],
|
||||
noAnnounce: ['/ip4/127.0.0.1/tcp/8000']
|
||||
}
|
||||
// ...
|
||||
})
|
||||
await libp2p.start()
|
||||
```
|
||||
|
||||
### API Implications
|
||||
|
||||
#### Peer Dialing, Hangup and Ping
|
||||
|
||||
`libp2p.dial`, `libp2p.dialProtocol`, `libp2p.hangup` and `libp2p.ping` supported as the target parameter a [`PeerInfo`](peer-info), a [`PeerId`](peer-id), a [`Multiaddr`][multiaddr] and a string representation of the multiaddr. Considering that [`PeerInfo`](peer-info) is being removed from libp2p, all these methods will now support the other 3 possibilities.
|
||||
|
||||
There is one relevant aspect to consider with this change. When using a [`PeerId`](peer-id), the PeerStore **MUST** have known addresses for that peer in its AddressBook, so that it can perform the request. This was also true in the past, but it is important pointing it out because it might not be enough to switch from using [`PeerInfo`](peer-info) to [`PeerId`](peer-id). When using a [`PeerInfo`](peer-info), the PeerStore was not required to have the multiaddrs when they existed on the PeerInfo instance.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peerInfo = ... // PeerInfo containing its multiaddrs
|
||||
|
||||
const connection = await libp2p.dial(peerInfo)
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peerId = ...
|
||||
|
||||
// Known multiaddrs should be added to the PeerStore
|
||||
libp2p.peerStore.addressBook.add(peerId, multiaddrs)
|
||||
|
||||
const connection = await libp2p.dial(peerId)
|
||||
```
|
||||
|
||||
#### Content Routing and Peer Routing
|
||||
|
||||
Both [content-routing](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/content-routing) and [peer-routing](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/peer-routing) interfaces were modified to not return a ['PeerInfo'][peer-info] instance.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
for await (const peerInfo of libp2p.contentRouting.findProviders(cid)) {
|
||||
// peerInfo is a PeerInfo instance
|
||||
}
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
for await (const peer of libp2p.contentRouting.findProviders(cid)) {
|
||||
// { id: PeerId, multiaddrs: Multiaddr[] }
|
||||
}
|
||||
```
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const peerInfo = await libp2p.peerRouting.findPeer(peerId)
|
||||
// peerInfo is a PeerInfo instance
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const peer = await libp2p.peerRouting.findPeer(peerId)
|
||||
// { id: PeerId, multiaddrs: Multiaddr[] }
|
||||
```
|
||||
|
||||
## Connection Manager and Registrar
|
||||
|
||||
Registrar was introduced in `libp2p@0.27` along with [libp2p topologies](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/topology). `Registrar` and `ConnectionManager` were both listening on new connections and keeping their record of the open connections with other peers.
|
||||
|
||||
The registrar API was not documented in the [API DOC](../API.md). However, it exposed a useful method for some libp2p users, `libp2p.registrar.getConnection()`. On the other hand, the connection Manager did not provide any methods to access its stored connections. On `libp2p@0.28` we removed this data duplication and the connections are handled solely by the `ConnectionManager`.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
const connection = libp2p.registrar.getConnection(peerId)
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
const connection = libp2p.connectionManager.get(peerId)
|
||||
```
|
||||
|
||||
## Events
|
||||
|
||||
### Connection Events
|
||||
|
||||
Libp2p emits events whenever new connections are established. These emitted events previously providing the [`PeerInfo`](peer-info) of the peer that connected. In `libp2p@0.28` these events are now emitted from the Connection Manager and will now emit the [`connection`](connection) itself.
|
||||
|
||||
**Before**
|
||||
```js
|
||||
libp2p.on('peer:connect', (peerInfo) => {
|
||||
// PeerInfo instance
|
||||
})
|
||||
|
||||
libp2p.on('peer:disconnect', (peerInfo) => {
|
||||
// PeerInfo instance
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
libp2p.connectionManager.on('peer:connect', (connection) => {
|
||||
// Connection instance
|
||||
})
|
||||
|
||||
libp2p.connectionManager.on('peer:disconnect', (connection) => {
|
||||
// Connection instance
|
||||
})
|
||||
```
|
||||
|
||||
### Peer Discovery
|
||||
|
||||
**Before**
|
||||
```js
|
||||
libp2p.on('peer:discovery', (peerInfo) => {
|
||||
// PeerInfo instance
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
```js
|
||||
libp2p.on('peer:discovery', (peerId) => {
|
||||
// peerId instance
|
||||
})
|
||||
```
|
||||
|
||||
## Module Updates
|
||||
|
||||
With `libp2p@0.28` you should update the following libp2p modules if you are relying on them:
|
||||
|
||||
```json
|
||||
"libp2p-bootstrap": "^0.11.0",
|
||||
"libp2p-delegated-content-routing": "^0.5.0",
|
||||
"libp2p-delegated-peer-routing": "^0.5.0",
|
||||
"libp2p-floodsub": "^0.21.0",
|
||||
"libp2p-gossipsub": "^0.4.0",
|
||||
"libp2p-kad-dht": "^0.19.1",
|
||||
"libp2p-mdns": "^0.14.1",
|
||||
"libp2p-webrtc-star": "^0.18.0"
|
||||
```
|
||||
|
||||
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
|
||||
[multiaddr]: https://github.com/multiformats/js-multiaddr
|
||||
[peer-id]: https://github.com/libp2p/js-peer-id
|
||||
[peer-info]: https://github.com/libp2p/js-peer-info
|
312
doc/migrations/v0.28-v0.29.md
Normal file
312
doc/migrations/v0.28-v0.29.md
Normal file
@ -0,0 +1,312 @@
|
||||
<!--Specify versions for migration below-->
|
||||
# Migrating to libp2p@0.29
|
||||
|
||||
A migration guide for refactoring your application code from libp2p v0.28.x to v0.29.0.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [API](#api)
|
||||
- [Pubsub](#pubsub)
|
||||
- [Uint8Arrays replace node Buffers](#uint8arrays-replace-node-buffers)
|
||||
- [Module Updates](#module-updates)
|
||||
|
||||
## API
|
||||
|
||||
### Pubsub
|
||||
|
||||
The [`libp2p-gossipsub`](https://github.com/ChainSafe/js-libp2p-gossipsub) javascript implementation is now upgraded according to the Gossipsub v1.1 [spec](https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md) and it packs several security hardening extensions. You can read more about it in its [blogpost](https://blog.ipfs.io/2020-05-20-gossipsub-v1.1/).
|
||||
|
||||
We leveraged this update to rethink the pubsub interface, in order to make it easier, as well as to be consistent with the API of the routers. Moreover, the interface was also reconstructed to ease new pubsub router implementations.
|
||||
|
||||
#### Access router instance
|
||||
|
||||
Libp2p prior to 0.29 unnecessarily added a layer of abstraction over the pubsub routers. We now expose the pubsub router API directly and have a test suite in the [interface-pubsub](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/pubsub) to guarantee routers compliance. This enables more advanced usage of the underlying router.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
libp2p.pubsub._pubsub.*
|
||||
libp2p.pubsub._pubsub.topicValidators.set(topic, validator)
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
libp2p.pubsub.*
|
||||
libp2p.pubsub.topicValidators.set(topic, validator)
|
||||
```
|
||||
|
||||
#### Publish
|
||||
|
||||
Publish uses `Uint8Array` data instead of `Buffer`.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const topic = 'topic'
|
||||
const data = Buffer.from('data')
|
||||
|
||||
await libp2p.pubsub.publish(topic, data)
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const topic = 'topic'
|
||||
const data = uint8ArrayFromString('data')
|
||||
|
||||
await libp2p.pubsub.publish(topic, data)
|
||||
```
|
||||
|
||||
#### Subscribe
|
||||
|
||||
Handlers should no longer be passed when subscribing, instead, applications should bind event handlers for each topic they wish to subscribe too. This enables more flexibility at the application level without changing the underlying subscriptions.
|
||||
Message data is now a `Uint8Array` instead of `Buffer`.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const topic = 'topic'
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
const data = msg.data.toString()
|
||||
}
|
||||
libp2p.pubsub.subscribe(topic, handler)
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const topic = 'topic'
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
const data = uint8ArrayToString(msg.data)
|
||||
}
|
||||
libp2p.pubsub.on(topic, handler)
|
||||
libp2p.pubsub.subscribe(topic)
|
||||
```
|
||||
|
||||
In the latest release, despite not being documented in `libp2p` the underlying pubsub routers supported subscribing to multiple topics at the same time. We removed that code complexity, since this is easily achieved in the application layer if needed.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const topics = ['a', 'b']
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
const data = msg.data.toString()
|
||||
}
|
||||
libp2p.pubsub.subscribe(topics, handler)
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const topics = ['a', 'b']
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
const data = uint8ArrayToString(msg.data)
|
||||
}
|
||||
|
||||
topics.forEach((topic) => {
|
||||
libp2p.pubsub.on(topic, handler)
|
||||
libp2p.pubsub.subscribe(topic)
|
||||
})
|
||||
```
|
||||
|
||||
#### Unsubscribe
|
||||
|
||||
Handlers should not be directly bound to the subscription anymore.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const topic = 'topic'
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
}
|
||||
libp2p.pubsub.unsubscribe(topic, handler)
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const topic = 'topic'
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
}
|
||||
libp2p.pubsub.removeListener(topic, handler)
|
||||
libp2p.pubsub.unsubscribe(topic)
|
||||
```
|
||||
|
||||
#### Topic Validators
|
||||
|
||||
The validator function does not include the peer parameter anymore. It was redundant since it is included in the message and it could lead to issues as the peer that sent the message might not be the one who created the message in first place. The validator function should also throw an error instead of returning `false` when the message is not valid.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const validator = (msgTopic, peer, msg) => {
|
||||
// process message
|
||||
return false
|
||||
}
|
||||
libp2p.pubsub._pubsub.topicValidators.set(topic, validator)
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const validator = (msgTopic, msg) => {
|
||||
const from = msg.from
|
||||
// process message
|
||||
throw new Error('not a valid message')
|
||||
}
|
||||
libp2p.pubsub.topicValidators.set(topic, validator)
|
||||
```
|
||||
|
||||
### Uint8Arrays replace node Buffers
|
||||
|
||||
Aiming to improve libp2p browser support, we are moving away from node core modules unless we can guarantee that the code we are writing will not run in a browser. It is worth mentioning that modern JavaScript runtimes have TypedArrays such as Uint8Array backed by ArrayBuffers. All libp2p dependencies were also updated to use Uint8Array.
|
||||
|
||||
We use the [uint8arrays](https://www.npmjs.com/package/uint8arrays) utilities module to deal with `Uint8Arrays` easily and we recommend its usage in the application layer. Thanks for the module [@achingbrain](https://github.com/achingbrain)! It includes utilities like `compare`, `concat`, `equals`, `fromString` and `toString`. In this migration examples, we will be using the following:
|
||||
|
||||
```js
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
```
|
||||
|
||||
#### contentRouting.put
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const key = '/key'
|
||||
const value = Buffer.from('oh hello there')
|
||||
|
||||
await libp2p.contentRouting.put(key, value)
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const key = '/key'
|
||||
const value = uint8ArrayFromString('oh hello there')
|
||||
|
||||
await libp2p.contentRouting.put(key, value)
|
||||
```
|
||||
|
||||
#### contentRouting.get
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const key = '/key'
|
||||
const value = await libp2p.contentRouting.put(key)
|
||||
|
||||
console.log('store value is: ', value.toString())
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const key = '/key'
|
||||
const value = await libp2p.contentRouting.put(key)
|
||||
|
||||
console.log('store value is: ', uint8ArrayToString(value))
|
||||
```
|
||||
|
||||
#### metadataBook.set
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Saturn'))
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Saturn'))
|
||||
```
|
||||
|
||||
#### metadataBook.get
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const data = peerStore.metadataBook.get(peerId)
|
||||
|
||||
console.log('stored location: ', data.get('location').toString())
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const data = peerStore.metadataBook.get(peerId)
|
||||
|
||||
console.log('stored location: ', uint8ArrayToString(data.get('location')))
|
||||
```
|
||||
|
||||
#### metadataBook.getValue
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const location = peerStore.metadataBook.getValue(peerId, 'location')
|
||||
|
||||
console.log('stored location: ', location.toString())
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const location = peerStore.metadataBook.getValue(peerId, 'location')
|
||||
|
||||
console.log('stored location: ', uint8ArrayToString(location))
|
||||
```
|
||||
|
||||
#### keychain.cms.encrypt
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const keyInfo = await libp2p.keychain.createKey('keyTest', 'rsa', 4096)
|
||||
const enc = await libp2p.keychain.cms.encrypt('keyTest', Buffer.from('data'))
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const keyInfo = await libp2p.keychain.createKey('keyTest', 'rsa', 4096)
|
||||
const enc = await libp2p.keychain.cms.encrypt('keyTest', uint8ArrayFromString('data'))
|
||||
```
|
||||
|
||||
#### pubsub
|
||||
|
||||
Already specified in its own chapter above.
|
||||
|
||||
## Module Updates
|
||||
|
||||
With this release you should update the following libp2p modules if you are relying on them:
|
||||
|
||||
```json
|
||||
"libp2p-bootstrap": "^0.12.0",
|
||||
"libp2p-delegated-content-routing": "^0.6.0",
|
||||
"libp2p-delegated-peer-routing": "^0.6.0",
|
||||
"libp2p-floodsub": "^0.23.0",
|
||||
"libp2p-gossipsub": "^0.6.0",
|
||||
"libp2p-kad-dht": "^0.20.0",
|
||||
"libp2p-mdns": "^0.15.0",
|
||||
"libp2p-mplex": "^0.10.0",
|
||||
"libp2p-noise": "^2.0.0",
|
||||
"libp2p-secio": "^0.13.1",
|
||||
"libp2p-tcp": "^0.15.1",
|
||||
"libp2p-webrtc-star": "^0.20.0",
|
||||
"libp2p-websockets": "^0.14.0",
|
||||
```
|
185
doc/migrations/v0.29-v0.30.md
Normal file
185
doc/migrations/v0.29-v0.30.md
Normal file
@ -0,0 +1,185 @@
|
||||
<!--Specify versions for migration below-->
|
||||
# Migrating to libp2p@30
|
||||
|
||||
A migration guide for refactoring your application code from libp2p v0.29.x to v0.30.0.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [API](#api)
|
||||
- [Development and Testing](#development-and-testing)
|
||||
- [Module Updates](#module-updates)
|
||||
|
||||
## API
|
||||
|
||||
### Pubsub
|
||||
|
||||
`js-libp2p` nodes prior to this version were emitting to self on publish by default.
|
||||
This default value was changed on the pubsub router layer in the past, but we kept it overwritten in libp2p to avoid an upstream breaking change.
|
||||
Now `js-libp2p` does not overwrite the pubsub router options anymore. Upstream projects that want this feature should enable it on their libp2p configuration.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
// ... Add required modules according to the Configuration docs
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
// ... Add required modules according to the Configuration docs
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
pubsub: {
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
The [Pubsub interface](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/pubsub) was updated on its message signing properties, taking into account the Gossipsub spec updates on [libp2p/specs#294](https://github.com/libp2p/specs/pull/294) and [libp2p/specs#299](https://github.com/libp2p/specs/pull/299)
|
||||
|
||||
The signing property is now based on a `globalSignaturePolicy` option instead of the previous `signMessages` and `strictSigning` options. The default to strict signing pubsub messages was kept, but if you would like to disable it, the properties should be changed as follows:
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
// ... Add required modules according to the Configuration docs
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
pubsub: {
|
||||
signMessages: false,
|
||||
strictSigning: false
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const { SignaturePolicy } = require('libp2p-interfaces/src/pubsub/signature-policy')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
// ... Add required modules according to the Configuration docs
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
pubsub: {
|
||||
globalSignaturePolicy: SignaturePolicy.StrictNoSign
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Addresses
|
||||
|
||||
Libp2p has supported `noAnnounce` addresses configuration for some time now. However, it did not provide the best developer experience. In this release, we dropped the `noAnnounce` configuration property in favor of an `announceFilter` property function.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/8000/ws'],
|
||||
noAnnounce: ['/ip4/127.0.0.1/tcp/8000/ws'],
|
||||
},
|
||||
// ... additional configuration per the Configuration docs
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
// Libp2p utils has several multiaddr utils you can leverage
|
||||
const isPrivate = require('libp2p-utils/src/multiaddr/is-private')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/8000/ws'],
|
||||
// Filter function: (ma: Array<multiaddr>) => Array<multiaddr>
|
||||
announceFilter: (multiaddrs) => multiaddrs.filter(m => !isPrivate(m))
|
||||
},
|
||||
// ... additional configuration per the Configuration docs
|
||||
})
|
||||
```
|
||||
|
||||
It is important pointing out another change regarding address advertising. This is not an API breaking change, but it might have influence on your libp2p setup.
|
||||
Previously, when using the addresses `announce` property, its multiaddrs were concatenated with the `listen` multiaddrs and then they were filtered out by the `noAnnounce` multiaddrs, in order to create the list of multiaddrs to advertise.
|
||||
In `libp2p@0.30` the logic now operates as follows:
|
||||
|
||||
- If `announce` addresses are provided, only they will be announced (no filters are applied)
|
||||
- If `announce` is not provided, the transport addresses will be filtered (if a filter is provided)
|
||||
- if the `announceFilter` is provide it will be passed the transport addresses
|
||||
|
||||
## Development and Testing
|
||||
|
||||
While this is not an API breaking change, there was a behavioral breaking change on the Websockets transport when in a browser environment. This change might create issues on local test setups.
|
||||
`libp2p-websockets` has allowed `TCP` and `DNS` addresses, both with `ws` or `wss` to be used for dial purposes. Taking into account security (and browser policies), we are now restricting addresses to `DNS` + `wss` in the browser
|
||||
With this new behavior, if you need to use non DNS addresses, you can configure your libp2p node as follows:
|
||||
|
||||
```js
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const filters = require('libp2p-websockets/src/filters')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const transportKey = Websockets.prototype[Symbol.toStringTag]
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets]
|
||||
// ... Add required modules according to the Configuration docs
|
||||
},
|
||||
config: {
|
||||
transport: {
|
||||
[transportKey]: {
|
||||
filter: filters.all
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Module Updates
|
||||
|
||||
With this release you should update the following libp2p modules if you are relying on them:
|
||||
|
||||
<!--Specify module versions in JSON for migration below.
|
||||
It's recommended to check package.json changes for this:
|
||||
`git diff <release> <prev> -- package.json`
|
||||
-->
|
||||
|
||||
```json
|
||||
"libp2p-delegated-content-routing": "^0.8.0",
|
||||
"libp2p-delegated-peer-routing": "^0.8.0",
|
||||
"libp2p-floodsub": "^0.24.0",
|
||||
"libp2p-gossipsub": "^0.7.0",
|
||||
"libp2p-websockets": "^0.15.0",
|
||||
```
|
||||
|
||||
Note that some of them do not need to be updated for this libp2p version to work as expected, but we suggest you to keep them updated as part of this release.
|
3
doc/production/DELEGATE_NODES.md
Normal file
3
doc/production/DELEGATE_NODES.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Delegate Nodes
|
||||
|
||||
[TODO](https://github.com/libp2p/js-libp2p/pull/718)
|
65
doc/production/README.md
Normal file
65
doc/production/README.md
Normal file
@ -0,0 +1,65 @@
|
||||
# Production
|
||||
|
||||
Nowadays, you can run JavaScript code in several different environments, some of them with their own particularities. Moreover, you can use `js-libp2p` for a wide range of use cases. Different environments and different use cases mean different configurations and challenges in the network.
|
||||
|
||||
Libp2p nodes can vary from nodes behind an application, to infrastructure nodes that enable the network to operate and to be efficient. In this context, the Libp2p project provides public infrastructure to boost the network, enable nodes connectivity and improve constrained nodes performance. This public infrastructure should be leveraged for learning the concepts and experimenting. When an application on top of libp2p aims to move into production, its own infrastructure should be setup as the public nodes will be intensively used by others and its availability is not guaranteed.
|
||||
|
||||
This guide aims to guide you from using the public infrastructure into setting up your own.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
* [Joining the Network](#joining-the-network)
|
||||
* [Connecting to Nodes with connectivity limitations](#connecting-to-nodes-with-connectivity-limitations)
|
||||
* [`webrtc-star` servers](#webrtc-star-servers)
|
||||
* [Circuit Relay](#circuit-relay)
|
||||
* [Querying the network from the browser](#querying-the-network-from-the-browser)
|
||||
* [Others](#others)
|
||||
* [SSL](#ssl)
|
||||
|
||||
## Joining the Network
|
||||
|
||||
Once a libp2p node stars, it will need to connect to a set of peers in order to establish its overlay network.
|
||||
|
||||
Currently `js-libp2p` is not the best choice for being a bootstrap node. Its DHT needs to be improved, in order to become an effective server to enable other nodes to properly bootstrap their network.
|
||||
|
||||
Setting up a fleet of [`go-libp2p`](https://github.com/libp2p/go-libp2p) nodes is the recommended way to proceed here.
|
||||
|
||||
## Connecting to Nodes with connectivity limitations
|
||||
|
||||
While the libp2p core codebase aims to work in multiple environments, there are some limitations that are not possible to overcome at the time of writing. These limitations include browser nodes, nodes behind NAT, reverse proxies, firewalls, or lack of compatible transports.
|
||||
|
||||
In the browser, libp2p supports two transports: `websockets` and `webrtc-star`. Nowadays, browsers do not support listening for connections, but only to dial known addresses. `webrtc-star` servers can be used to enable libp2p nodes to discover other nodes running on the browser and to help them establish a connection.
|
||||
|
||||
For nodes that cannot be dialed (including browser), circuit relay nodes should be used.
|
||||
|
||||
### `webrtc-star` servers
|
||||
|
||||
Regarding `webRTC` connections, a set of star servers are needed to act as a rendezvous point, where peers can learn about other peers (`peer-discovery`), as well as exchange their SDP offers (signaling data).
|
||||
|
||||
You can read on how to setup your own star servers in [libp2p/js-libp2p-webrtc-star/DEPLOYMENT.md](https://github.com/libp2p/js-libp2p-webrtc-star/blob/master/DEPLOYMENT.md).
|
||||
|
||||
It is worth pointing out that with new discovery protocols on the way, as well as support for distributed signaling, the star servers should be deprecated on the long run.
|
||||
|
||||
### Circuit Relay
|
||||
|
||||
Libp2p nodes acting as circuit relay aim to establish connectivity between libp2p nodes (e.g. IPFS nodes) that wouldn't otherwise be able to establish a direct connection to each other.
|
||||
|
||||
A relay is needed in situations where nodes are behind NAT, reverse proxies, firewalls and/or simply don't support the same transports (e.g. go-libp2p vs. browser-libp2p). The circuit relay protocol exists to overcome those scenarios. Nodes with the `auto-relay` feature enabled can automatically bind themselves on a relay to listen for connections on their behalf.
|
||||
|
||||
You can use [libp2p/js-libp2p-relay-server](https://github.com/libp2p/js-libp2p-relay-server) to setup your own relay server. This also includes an easy to customize Docker setup for a HOP Relay.
|
||||
|
||||
## Querying the network from the browser
|
||||
|
||||
Libp2p nodes in scenarios such as browser environment and constrained devices will not be an efficient node in the libp2p DHT overlay, as a consequence of their known limitations regarding connectivity and performance.
|
||||
|
||||
Aiming to support these type of nodes to find other peers and content in the network, delegate nodes can be setup. With a set of well known IPFS delegate nodes, nodes with limitations in the network can leverage them to perform peer and content routing queries.
|
||||
|
||||
Currently, delegate nodes must be IPFS nodes as the IPFS HTTP API is leveraged by them to make routing queries.
|
||||
|
||||
You can read on how to setup your own set of delegated nodes in [DELEGATE_NODES.md](./DELEGATE_NODES.md).
|
||||
|
||||
## Others
|
||||
|
||||
### SSL
|
||||
|
||||
TODO
|
26
examples/README.md
Normal file
26
examples/README.md
Normal file
@ -0,0 +1,26 @@
|
||||
# `js-libp2p` Examples and Tutorials
|
||||
|
||||
In this folder, you can find a variety of examples to help you get started in using js-libp2p, in Node.js and in the Browser. Every example has a specific purpose and some incorporate a full tutorial that you can follow through, helping you expand your knowledge about libp2p and p2p networks in general.
|
||||
|
||||
Let us know if you find any issues, or if you want to contribute and add a new tutorial, feel free to submit a PR, thank you!
|
||||
|
||||
## Understanding how libp2p works
|
||||
|
||||
- [Transports](./transports)
|
||||
- [Protocol and Stream Muxing](./protocol-and-stream-muxing)
|
||||
- [Connection Encryption](./connection-encryption)
|
||||
- [Discovery Mechanisms](./discovery-mechanisms)
|
||||
- [Peer and Content Routing](./peer-and-content-routing)
|
||||
- [PubSub](./pubsub)
|
||||
- [NAT Traversal](./nat-traversal)
|
||||
- Circuit Relay (future)
|
||||
- Naming (future)
|
||||
|
||||
## Other examples
|
||||
|
||||
- [Running libp2p in the Browser](./libp2p-in-the-browser)
|
||||
- Running libp2p in the Electron (future)
|
||||
- [The standard echo net example with libp2p](./echo)
|
||||
- [A simple chat app with libp2p](./chat)
|
||||
|
||||
For go-libp2p examples, check out https://github.com/libp2p/go-libp2p-examples#examples-and-tutorials
|
192
examples/auto-relay/README.md
Normal file
192
examples/auto-relay/README.md
Normal file
@ -0,0 +1,192 @@
|
||||
# Auto relay
|
||||
|
||||
Auto Relay enables libp2p nodes to dynamically find and bind to relays on the network. Once binding (listening) is done, the node can and should advertise its addresses on the network, allowing any other node to dial it over its bound relay(s).
|
||||
While direct connections to nodes are preferable, it's not always possible to do so due to NATs or browser limitations.
|
||||
|
||||
## 0. Setup the example
|
||||
|
||||
Before moving into the examples, you should run `npm install` on the top level `js-libp2p` folder, in order to install all the dependencies needed for this example. Once the install finishes, you should move into the example folder with `cd examples/auto-relay`.
|
||||
|
||||
This example comes with 3 main files. A `relay.js` file to be used in the first step, a `listener.js` file to be used in the second step and a `dialer.js` file to be used on the third step. All of these scripts will run their own libp2p node, which will interact with the previous ones. All nodes must be running in order for you to proceed.
|
||||
|
||||
## 1. Set up a relay node
|
||||
|
||||
In the first step of this example, we need to configure and run a relay node in order for our target node to bind to for accepting inbound connections.
|
||||
|
||||
The relay node will need to have its relay subsystem enabled, as well as its HOP capability. It can be configured as follows:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
},
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0/ws']
|
||||
// TODO check "What is next?" section
|
||||
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true
|
||||
},
|
||||
advertise: {
|
||||
enabled: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
console.log('Listening on:')
|
||||
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
|
||||
```
|
||||
|
||||
The Relay HOP advertise functionality is **NOT** required to be enabled. However, if you are interested in advertising on the network that this node is available to be used as a HOP Relay you can enable it. A content router module or Rendezvous needs to be configured to leverage this option.
|
||||
|
||||
You should now run the following to start the relay node:
|
||||
|
||||
```sh
|
||||
node relay.js
|
||||
```
|
||||
|
||||
This should print out something similar to the following:
|
||||
|
||||
```sh
|
||||
Node started with id QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
|
||||
Listening on:
|
||||
/ip4/127.0.0.1/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
|
||||
/ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
|
||||
```
|
||||
|
||||
## 2. Set up a listener node with Auto Relay Enabled
|
||||
|
||||
One of the typical use cases for Auto Relay is nodes behind a NAT or browser nodes due to their inability to expose a public address. For running a libp2p node that automatically binds itself to connected HOP relays, you can see the following:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const relayAddr = process.argv[2]
|
||||
if (!relayAddr) {
|
||||
throw new Error('the relay address needs to be specified as a parameter')
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
autoRelay: {
|
||||
enabled: true,
|
||||
maxListeners: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
|
||||
const conn = await node.dial(relayAddr)
|
||||
|
||||
// Wait for connection and relay to be bind for the example purpose
|
||||
await new Promise((resolve) => {
|
||||
node.peerStore.on('change:multiaddrs', ({ peerId }) => {
|
||||
// Updated self multiaddrs?
|
||||
if (peerId.equals(node.peerId)) {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
console.log(`Connected to the HOP relay ${conn.remotePeer.toString()}`)
|
||||
console.log(`Advertising with a relay address of ${node.multiaddrs[0].toString()}/p2p/${node.peerId.toB58String()}`)
|
||||
```
|
||||
|
||||
As you can see in the code, we need to provide the relay address, `relayAddr`, as a process argument. This node will dial the provided relay address and automatically bind to it.
|
||||
|
||||
You should now run the following to start the node running Auto Relay:
|
||||
|
||||
```sh
|
||||
node listener.js /ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
|
||||
```
|
||||
|
||||
This should print out something similar to the following:
|
||||
|
||||
```sh
|
||||
Node started with id QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
|
||||
Connected to the HOP relay QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
|
||||
Advertising with a relay address of /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
|
||||
```
|
||||
|
||||
Per the address, it is possible to verify that the auto relay node is listening on the circuit relay node address.
|
||||
|
||||
Instead of dialing this relay manually, you could set up this node with the Bootstrap module and provide it in the bootstrap list. Moreover, you can use other `peer-discovery` modules to discover peers in the network and the node will automatically bind to the relays that support HOP until reaching the maximum number of listeners.
|
||||
|
||||
## 3. Set up a dialer node for testing connectivity
|
||||
|
||||
Now that you have a relay node and a node bound to that relay, you can test connecting to the auto relay node via the relay.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const autoRelayNodeAddr = process.argv[2]
|
||||
if (!autoRelayNodeAddr) {
|
||||
throw new Error('the auto relay node address needs to be specified')
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
|
||||
const conn = await node.dial(autoRelayNodeAddr)
|
||||
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)
|
||||
```
|
||||
|
||||
You should now run the following to start the relay node using the listen address from step 2:
|
||||
|
||||
```sh
|
||||
node dialer.js /ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
|
||||
```
|
||||
|
||||
Once you start your test node, it should print out something similar to the following:
|
||||
|
||||
```sh
|
||||
Node started: Qme7iEzDxFoFhhkrsrkHkMnM11aPYjysaehP4NZeUfVMKG
|
||||
Connected to the auto relay node via /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
|
||||
```
|
||||
|
||||
As you can see from the output, the remote address of the established connection uses the relayed connection.
|
||||
|
||||
## 4. What is next?
|
||||
|
||||
Before moving into production, there are a few things that you should take into account.
|
||||
|
||||
A relay node should not advertise its private address in a real world scenario, as the node would not be reachable by others. You should provide an array of public addresses in the libp2p `addresses.announce` option. If you are using websockets, bear in mind that due to browser’s security policies you cannot establish unencrypted connection from secure context. The simplest solution is to setup SSL with nginx and proxy to the node and setup a domain name for the certificate.
|
29
examples/auto-relay/dialer.js
Normal file
29
examples/auto-relay/dialer.js
Normal file
@ -0,0 +1,29 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
async function main () {
|
||||
const autoRelayNodeAddr = process.argv[2]
|
||||
if (!autoRelayNodeAddr) {
|
||||
throw new Error('the auto relay node address needs to be specified')
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
|
||||
const conn = await node.dial(autoRelayNodeAddr)
|
||||
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)
|
||||
}
|
||||
|
||||
main()
|
47
examples/auto-relay/listener.js
Normal file
47
examples/auto-relay/listener.js
Normal file
@ -0,0 +1,47 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
async function main () {
|
||||
const relayAddr = process.argv[2]
|
||||
if (!relayAddr) {
|
||||
throw new Error('the relay address needs to be specified as a parameter')
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
autoRelay: {
|
||||
enabled: true,
|
||||
maxListeners: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
|
||||
const conn = await node.dial(relayAddr)
|
||||
|
||||
console.log(`Connected to the HOP relay ${conn.remotePeer.toString()}`)
|
||||
|
||||
// Wait for connection and relay to be bind for the example purpose
|
||||
node.peerStore.on('change:multiaddrs', ({ peerId }) => {
|
||||
// Updated self multiaddrs?
|
||||
if (peerId.equals(node.peerId)) {
|
||||
console.log(`Advertising with a relay address of ${node.multiaddrs[0].toString()}/p2p/${node.peerId.toB58String()}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
main()
|
40
examples/auto-relay/relay.js
Normal file
40
examples/auto-relay/relay.js
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
async function main () {
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
},
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0/ws']
|
||||
// TODO check "What is next?" section
|
||||
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true
|
||||
},
|
||||
advertise: {
|
||||
enabled: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
console.log('Listening on:')
|
||||
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
|
||||
}
|
||||
|
||||
main()
|
94
examples/auto-relay/test.js
Normal file
94
examples/auto-relay/test.js
Normal file
@ -0,0 +1,94 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
function startProcess (name, args = []) {
|
||||
return execa('node', [path.join(__dirname, name), ...args], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
}
|
||||
|
||||
async function test () {
|
||||
let output1 = ''
|
||||
let output2 = ''
|
||||
let output3 = ''
|
||||
let relayAddr
|
||||
let autoRelayAddr
|
||||
|
||||
const proc1Ready = pDefer()
|
||||
const proc2Ready = pDefer()
|
||||
|
||||
// Step 1 process
|
||||
process.stdout.write('relay.js\n')
|
||||
|
||||
const proc1 = startProcess('relay.js')
|
||||
proc1.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
output1 += uint8ArrayToString(data)
|
||||
|
||||
if (output1.includes('Listening on:') && output1.includes('/p2p/')) {
|
||||
relayAddr = output1.trim().split('Listening on:\n')[1].split('\n')[0]
|
||||
proc1Ready.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await proc1Ready.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 2 process
|
||||
process.stdout.write('listener.js\n')
|
||||
|
||||
const proc2 = startProcess('listener.js', [relayAddr])
|
||||
proc2.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
output2 += uint8ArrayToString(data)
|
||||
|
||||
if (output2.includes('Advertising with a relay address of') && output2.includes('/p2p/')) {
|
||||
autoRelayAddr = output2.trim().split('Advertising with a relay address of ')[1]
|
||||
proc2Ready.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await proc2Ready.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 3 process
|
||||
process.stdout.write('dialer.js\n')
|
||||
|
||||
const proc3 = startProcess('dialer.js', [autoRelayAddr])
|
||||
proc3.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
output3 += uint8ArrayToString(data)
|
||||
|
||||
if (output3.includes('Connected to the auto relay node via')) {
|
||||
const remoteAddr = output3.trim().split('Connected to the auto relay node via ')[1]
|
||||
|
||||
if (remoteAddr === autoRelayAddr) {
|
||||
proc3.kill()
|
||||
proc2.kill()
|
||||
proc1.kill()
|
||||
} else {
|
||||
throw new Error('dialer did not dial through the relay')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
proc1,
|
||||
proc2,
|
||||
proc3
|
||||
]).catch((err) => {
|
||||
if (err.signal !== 'SIGTERM') {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = test
|
13
examples/chat/README.md
Normal file
13
examples/chat/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# Chat example with libp2p
|
||||
|
||||
This example creates a simple chat app in your terminal.
|
||||
|
||||
## Setup
|
||||
1. Install the modules in the libp2p root directory, `npm install`.
|
||||
2. Open 2 terminal windows in the `./examples/chat/src` directory.
|
||||
|
||||
## Running
|
||||
1. Run the listener in window 1, `node listener.js`
|
||||
2. Run the dialer in window 2, `node dialer.js`
|
||||
3. Type a message in either window and hit _enter_
|
||||
4. Tell yourself secrets to your hearts content!
|
45
examples/chat/src/dialer.js
Normal file
45
examples/chat/src/dialer.js
Normal file
@ -0,0 +1,45 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
const createLibp2p = require('./libp2p')
|
||||
const { stdinToStream, streamToConsole } = require('./stream')
|
||||
|
||||
async function run() {
|
||||
const [idDialer, idListener] = await Promise.all([
|
||||
PeerId.createFromJSON(require('./peer-id-dialer')),
|
||||
PeerId.createFromJSON(require('./peer-id-listener'))
|
||||
])
|
||||
|
||||
// Create a new libp2p node on localhost with a randomly chosen port
|
||||
const nodeDialer = await createLibp2p({
|
||||
peerId: idDialer,
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
}
|
||||
})
|
||||
|
||||
// Start the libp2p host
|
||||
await nodeDialer.start()
|
||||
|
||||
// Output this node's address
|
||||
console.log('Dialer ready, listening on:')
|
||||
nodeDialer.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + idDialer.toB58String())
|
||||
})
|
||||
|
||||
// Dial to the remote peer (the "listener")
|
||||
const listenerMa = multiaddr(`/ip4/127.0.0.1/tcp/10333/p2p/${idListener.toB58String()}`)
|
||||
const { stream } = await nodeDialer.dialProtocol(listenerMa, '/chat/1.0.0')
|
||||
|
||||
console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
|
||||
console.log('Type a message and see what happens')
|
||||
|
||||
// Send stdin to the stream
|
||||
stdinToStream(stream)
|
||||
// Read the stream and output to console
|
||||
streamToConsole(stream)
|
||||
}
|
||||
|
||||
run()
|
22
examples/chat/src/libp2p.js
Normal file
22
examples/chat/src/libp2p.js
Normal file
@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
async function createLibp2p(_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [TCP, WS],
|
||||
streamMuxer: [mplex],
|
||||
connEncryption: [NOISE],
|
||||
},
|
||||
}
|
||||
|
||||
return libp2p.create(defaultsDeep(_options, defaults))
|
||||
}
|
||||
|
||||
module.exports = createLibp2p
|
41
examples/chat/src/listener.js
Normal file
41
examples/chat/src/listener.js
Normal file
@ -0,0 +1,41 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const createLibp2p = require('./libp2p.js')
|
||||
const { stdinToStream, streamToConsole } = require('./stream')
|
||||
|
||||
async function run() {
|
||||
// Create a new libp2p node with the given multi-address
|
||||
const idListener = await PeerId.createFromJSON(require('./peer-id-listener'))
|
||||
const nodeListener = await createLibp2p({
|
||||
peerId: idListener,
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/10333']
|
||||
}
|
||||
})
|
||||
|
||||
// Log a message when a remote peer connects to us
|
||||
nodeListener.connectionManager.on('peer:connect', (connection) => {
|
||||
console.log('connected to: ', connection.remotePeer.toB58String())
|
||||
})
|
||||
|
||||
// Handle messages for the protocol
|
||||
await nodeListener.handle('/chat/1.0.0', async ({ stream }) => {
|
||||
// Send stdin to the stream
|
||||
stdinToStream(stream)
|
||||
// Read the stream and output to console
|
||||
streamToConsole(stream)
|
||||
})
|
||||
|
||||
// Start listening
|
||||
await nodeListener.start()
|
||||
|
||||
// Output listen addresses to the console
|
||||
console.log('Listener ready, listening on:')
|
||||
nodeListener.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
5
examples/chat/src/peer-id-dialer.json
Normal file
5
examples/chat/src/peer-id-dialer.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"id": "Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
|
||||
"privKey": "CAASpwkwggSjAgEAAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAECggEAZnrCJ6IYiLyyRdr9SbKXCNDb4YByGYPEi/HT1aHgIJfFE1PSMjxcdytxfyjP4JJpVtPjiT9JFVU2ddoYu5qJN6tGwjVwgJEWg1UXmPaAw1T/drjS94kVsAs82qICtFmwp52Apg3dBZ0Qwq/8qE1XbG7lLyohIbfCBiL0tiPYMfkcsN9gnFT/kFCX0LVs2pa9fHCRMY9rqCc4/rWJa1w8sMuQ23y4lDaxKF9OZVvOHFQkbBDrkquWHE4r55fchCz/rJklkPJUNENuncBRu0/2X+p4IKFD1DnttXNwb8j4LPiSlLro1T0hiUr5gO2QmdYwXFF63Q3mjQy0+5I4eNbjjQKBgQDZvZy3gUKS/nQNkYfq9za80uLbIj/cWbO+ZZjXCsj0fNIcQFJcKMBoA7DjJvu2S/lf86/41YHkPdmrLAEQAkJ+5BBNOycjYK9minTEjIMMmZDTXXugZ62wnU6F46uLkgEChTqEP57Y6xwwV+JaEDFEsW5N1eE9lEVX9nGIr4phMwKBgQC1TazLuEt1WBx/iUT83ita7obXqoKNzwsS/MWfY2innzYZKDOqeSYZzLtt9uTtp4X4uLyPbYs0qFYhXLsUYMoGHNN8+NdjoyxCjQRJRBkMtaNR0lc5lVDWl3bTuJovjFCgAr9uqJrmI5OHcCIk/cDpdWb3nWaMihVlePmiTcTy9wKBgQCU0u7c1jKkudqks4XM6a+2HAYGdUBk4cLjLhnrUWnNAcuyl5wzdX8dGPi8KZb+IKuQE8WBNJ2VXVj7kBYh1QmSJVunDflQSvNYCOaKuOeRoxzD+y9Wkca74qkbBmPn/6FFEb7PSZTO+tPHjyodGNgz9XpJJRjQuBk1aDJtlF3m1QKBgE5SAr5ym65SZOU3UGUIOKRsfDW4Q/OsqDUImvpywCgBICaX9lHDShFFHwau7FA52ScL7vDquoMB4UtCOtLfyQYA9995w9oYCCurrVlVIJkb8jSLcADBHw3EmqF1kq3NqJqm9TmBfoDCh52vdCCUufxgKh33kfBOSlXuf7B8dgMbAoGAZ3r0/mBQX6S+s5+xCETMTSNv7TQzxgtURIpVs+ZVr2cMhWhiv+n0Omab9X9Z50se8cWl5lkvx8vn3D/XHHIPrMF6qk7RAXtvReb+PeitNvm0odqjFv0J2qki6fDs0HKwq4kojAXI1Md8Th0eobNjsy21fEEJT7uKMJdovI/SErI=",
|
||||
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAE="
|
||||
}
|
5
examples/chat/src/peer-id-listener.json
Normal file
5
examples/chat/src/peer-id-listener.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"id": "QmcrQZ6RJdpYuGvZqD5QEHAv6qX4BrQLJLQPQUrTrzdcgm",
|
||||
"privKey": "CAASqAkwggSkAgEAAoIBAQDLZZcGcbe4urMBVlcHgN0fpBymY+xcr14ewvamG70QZODJ1h9sljlExZ7byLiqRB3SjGbfpZ1FweznwNxWtWpjHkQjTVXeoM4EEgDSNO/Cg7KNlU0EJvgPJXeEPycAZX9qASbVJ6EECQ40VR/7+SuSqsdL1hrmG1phpIju+D64gLyWpw9WEALfzMpH5I/KvdYDW3N4g6zOD2mZNp5y1gHeXINHWzMF596O72/6cxwyiXV1eJ000k1NVnUyrPjXtqWdVLRk5IU1LFpoQoXZU5X1hKj1a2qt/lZfH5eOrF/ramHcwhrYYw1txf8JHXWO/bbNnyemTHAvutZpTNrsWATfAgMBAAECggEAQj0obPnVyjxLFZFnsFLgMHDCv9Fk5V5bOYtmxfvcm50us6ye+T8HEYWGUa9RrGmYiLweuJD34gLgwyzE1RwptHPj3tdNsr4NubefOtXwixlWqdNIjKSgPlaGULQ8YF2tm/kaC2rnfifwz0w1qVqhPReO5fypL+0ShyANVD3WN0Fo2ugzrniCXHUpR2sHXSg6K+2+qWdveyjNWog34b7CgpV73Ln96BWae6ElU8PR5AWdMnRaA9ucA+/HWWJIWB3Fb4+6uwlxhu2L50Ckq1gwYZCtGw63q5L4CglmXMfIKnQAuEzazq9T4YxEkp+XDnVZAOgnQGUBYpetlgMmkkh9qQKBgQDvsEs0ThzFLgnhtC2Jy//ZOrOvIAKAZZf/mS08AqWH3L0/Rjm8ZYbLsRcoWU78sl8UFFwAQhMRDBP9G+RPojWVahBL/B7emdKKnFR1NfwKjFdDVaoX5uNvZEKSl9UubbC4WZJ65u/cd5jEnj+w3ir9G8n+P1gp/0yBz02nZXFgSwKBgQDZPQr4HBxZL7Kx7D49ormIlB7CCn2i7mT11Cppn5ifUTrp7DbFJ2t9e8UNk6tgvbENgCKXvXWsmflSo9gmMxeEOD40AgAkO8Pn2R4OYhrwd89dECiKM34HrVNBzGoB5+YsAno6zGvOzLKbNwMG++2iuNXqXTk4uV9GcI8OnU5ZPQKBgCZUGrKSiyc85XeiSGXwqUkjifhHNh8yH8xPwlwGUFIZimnD4RevZI7OEtXw8iCWpX2gg9XGuyXOuKORAkF5vvfVriV4e7c9Ad4Igbj8mQFWz92EpV6NHXGCpuKqRPzXrZrNOA9PPqwSs+s9IxI1dMpk1zhBCOguWx2m+NP79NVhAoGBAI6WSoTfrpu7ewbdkVzTWgQTdLzYNe6jmxDf2ZbKclrf7lNr/+cYIK2Ud5qZunsdBwFdgVcnu/02czeS42TvVBgs8mcgiQc/Uy7yi4/VROlhOnJTEMjlU2umkGc3zLzDgYiRd7jwRDLQmMrYKNyEr02HFKFn3w8kXSzW5I8rISnhAoGBANhchHVtJd3VMYvxNcQb909FiwTnT9kl9pkjhwivx+f8/K8pDfYCjYSBYCfPTM5Pskv5dXzOdnNuCj6Y2H/9m2SsObukBwF0z5Qijgu1DsxvADVIKZ4rzrGb4uSEmM6200qjJ/9U98fVM7rvOraakrhcf9gRwuspguJQnSO9cLj6",
|
||||
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLZZcGcbe4urMBVlcHgN0fpBymY+xcr14ewvamG70QZODJ1h9sljlExZ7byLiqRB3SjGbfpZ1FweznwNxWtWpjHkQjTVXeoM4EEgDSNO/Cg7KNlU0EJvgPJXeEPycAZX9qASbVJ6EECQ40VR/7+SuSqsdL1hrmG1phpIju+D64gLyWpw9WEALfzMpH5I/KvdYDW3N4g6zOD2mZNp5y1gHeXINHWzMF596O72/6cxwyiXV1eJ000k1NVnUyrPjXtqWdVLRk5IU1LFpoQoXZU5X1hKj1a2qt/lZfH5eOrF/ramHcwhrYYw1txf8JHXWO/bbNnyemTHAvutZpTNrsWATfAgMBAAE="
|
||||
}
|
40
examples/chat/src/stream.js
Normal file
40
examples/chat/src/stream.js
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
const lp = require('it-length-prefixed')
|
||||
|
||||
function stdinToStream(stream) {
|
||||
// Read utf-8 from stdin
|
||||
process.stdin.setEncoding('utf8')
|
||||
pipe(
|
||||
// Read from stdin (the source)
|
||||
process.stdin,
|
||||
// Encode with length prefix (so receiving side knows how much data is coming)
|
||||
lp.encode(),
|
||||
// Write to the stream (the sink)
|
||||
stream.sink
|
||||
)
|
||||
}
|
||||
|
||||
function streamToConsole(stream) {
|
||||
pipe(
|
||||
// Read from the stream (the source)
|
||||
stream.source,
|
||||
// Decode length-prefixed data
|
||||
lp.decode(),
|
||||
// Sink function
|
||||
async function (source) {
|
||||
// For each chunk of data
|
||||
for await (const msg of source) {
|
||||
// Output the data as a utf8 string
|
||||
console.log('> ' + msg.toString().replace('\n', ''))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
stdinToStream,
|
||||
streamToConsole
|
||||
}
|
77
examples/chat/test.js
Normal file
77
examples/chat/test.js
Normal file
@ -0,0 +1,77 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
function startProcess(name) {
|
||||
return execa('node', [path.join(__dirname, name)], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
}
|
||||
|
||||
async function test () {
|
||||
const message = 'test message'
|
||||
let listenerOutput = ''
|
||||
let dialerOutput = ''
|
||||
|
||||
let isListening = false
|
||||
let messageSent = false
|
||||
const listenerReady = pDefer()
|
||||
const dialerReady = pDefer()
|
||||
const messageReceived = pDefer()
|
||||
|
||||
// Step 1 process
|
||||
process.stdout.write('node listener.js\n')
|
||||
const listenerProc = startProcess('src/listener.js')
|
||||
listenerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
listenerOutput += uint8ArrayToString(data)
|
||||
|
||||
if (!isListening && listenerOutput.includes('Listener ready, listening on')) {
|
||||
listenerReady.resolve()
|
||||
isListening = true
|
||||
} else if (isListening && listenerOutput.includes(message)) {
|
||||
messageReceived.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await listenerReady.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 2 process
|
||||
process.stdout.write('node dialer.js\n')
|
||||
const dialerProc = startProcess('src/dialer.js')
|
||||
dialerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
dialerOutput += uint8ArrayToString(data)
|
||||
|
||||
if (!messageSent && dialerOutput.includes('Type a message and see what happens')) {
|
||||
dialerReady.resolve()
|
||||
dialerProc.stdin.write(message)
|
||||
dialerProc.stdin.write('\n')
|
||||
messageSent = true
|
||||
}
|
||||
})
|
||||
|
||||
await dialerReady.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
await messageReceived.promise
|
||||
process.stdout.write('chat message received\n')
|
||||
|
||||
listenerProc.kill()
|
||||
dialerProc.kill()
|
||||
await Promise.all([
|
||||
listenerProc,
|
||||
dialerProc
|
||||
]).catch((err) => {
|
||||
if (err.signal !== 'SIGTERM') {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = test
|
52
examples/connection-encryption/1.js
Normal file
52
examples/connection-encryption/1.js
Normal file
@ -0,0 +1,52 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../..')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
|
||||
node2.handle('/a-protocol', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const { stream } = await node1.dialProtocol(node2.peerId, '/a-protocol')
|
||||
|
||||
await pipe(
|
||||
['This information is sent out encrypted to the other peer'],
|
||||
stream
|
||||
)
|
||||
})();
|
31
examples/connection-encryption/README.md
Normal file
31
examples/connection-encryption/README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# Connection Encryption
|
||||
|
||||
libp2p can leverage the encrypted communications from the transports it uses (i.e WebRTC). To ensure that every connection is encrypted, independently of how it was set up, libp2p also supports a set of modules that encrypt every communication established.
|
||||
|
||||
We call this usage a _connection upgrade_ where given a connection between peer A to peer B, a protocol handshake can be performed that gives that connection new properties.
|
||||
|
||||
A byproduct of having these encrypted communications modules is that we can authenticate the peers we are dialing to. You might have noticed that every time we dial to a peer in libp2p space, we always use its PeerId at the end (e.g /ip4/127.0.0.1/tcp/89765/p2p/QmWCbVw1XZ8hiYBwwshPce2yaTDYTqTaP7GCHGpry3ykWb), this PeerId is generated by hashing the Public Key of the peer. With this, we can create a crypto challenge when dialing to another peer and prove that peer is the owner of a PrivateKey that matches the Public Key we know.
|
||||
|
||||
# 1. Set up encrypted communications
|
||||
|
||||
We will build this example on top of example for [Protocol and Stream Multiplexing](../protocol-and-stream-multiplexing). You will need the `libp2p-noise` module to complete it, go ahead and `npm install libp2p-noise`.
|
||||
|
||||
To add them to your libp2p configuration, all you have to do is:
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const createNode = () => {
|
||||
return Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
// Attach noise as the crypto channel to use
|
||||
connEncryption: [ NOISE ]
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
And that's it, from now on, all your libp2p communications are encrypted. Try running the example [1.js](./1.js) to see it working.
|
30
examples/connection-encryption/test.js
Normal file
30
examples/connection-encryption/test.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test () {
|
||||
const messageReceived = pDefer()
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const s = uint8ArrayToString(data)
|
||||
if (s.includes('This information is sent out encrypted to the other peer')) {
|
||||
messageReceived.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await messageReceived.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
53
examples/delegated-routing/README.md
Normal file
53
examples/delegated-routing/README.md
Normal file
@ -0,0 +1,53 @@
|
||||
❗❗Outdated: This example is still not refactored with the `0.27.*` release.
|
||||
WIP on [libp2p/js-libp2p#507](https://github.com/libp2p/js-libp2p/pull/507)
|
||||
======
|
||||
|
||||
# Delegated Routing with Libp2p and IPFS
|
||||
|
||||
This example shows how to use delegated peer and content routing. The [Peer and Content Routing Example](../peer-and-content-routing) focuses
|
||||
on the DHT implementation. This example takes that a step further and introduces delegated routing. Delegated routing is
|
||||
especially useful when your libp2p node will have limited resources, making running a DHT impractical. It's
|
||||
also highly useful if your node is generating content, but can't reliably be on the network. You can use delegate nodes
|
||||
to provide content on your behalf.
|
||||
|
||||
The starting [Libp2p Bundle](./src/libp2p-bundle.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
|
||||
Once you've completed the example, you should try enabled the DHT and see what kind of results you get! You can also enable the
|
||||
various Peer Discovery modules and see the impact it has on your Peer count.
|
||||
|
||||
## Prerequisite
|
||||
**NOTE**: This example is currently dependent on a clone of the [delegated routing support branch of go-ipfs](https://github.com/ipfs/go-ipfs/pull/4595).
|
||||
|
||||
## Running this example
|
||||
|
||||
1. Install IPFS locally if you dont already have it. [Install Guide](https://docs.ipfs.io/introduction/install/)
|
||||
2. Run the IPFS daemon: `ipfs daemon`
|
||||
3. The daemon will output a line about its API address, like `API server listening on /ip4/127.0.0.1/tcp/8080`
|
||||
4. In another window output the addresses of the node: `ipfs id`. Make note of the websocket address, it will contain `/ws/` in the address.
|
||||
5. In `./src/libp2p-bundle.js` check if the host and port of your node are correct, according to the previous step. If they are different, replace them.
|
||||
6. In `./src/App.js` replace `BootstrapNode` with your nodes Websocket address from step 4.
|
||||
7. Start this example:
|
||||
|
||||
```sh
|
||||
npm install
|
||||
npm start
|
||||
```
|
||||
|
||||
This should open your browser to http://localhost:3000. If it does not, go ahead and do that now.
|
||||
|
||||
8. Your browser should show you connected to at least 1 peer.
|
||||
|
||||
### Finding Content via the Delegate
|
||||
1. Add a file to your IPFS node. From this example root you can do `ipfs add ./README.md` to add the example readme.
|
||||
2. Copy the hash from line 5, it will look something like *Qmf33vz4HJFkqgH7XPP1uA6atYKTX1BWQEQthzpKcAdeyZ*.
|
||||
3. In the browser, paste the hash into the *Hash* field and hit `Find`. The readme contents should display.
|
||||
|
||||
This will do a few things:
|
||||
* The delegate nodes api will be queried to find providers of the content
|
||||
* The content will be fetched from the providers
|
||||
* Since we now have the content, we tell the delegate node to fetch the content from us and become a provider
|
||||
|
||||
### Finding Peers via the Delegate
|
||||
1. Get a list of your delegate nodes peer by querying the IPFS daemon: `ipfs swarm peers`
|
||||
2. Copy one of the CIDs from the list of peer addresses, this will be the last portion of the address and will look something like `QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8`.
|
||||
3. In your browser, paste the CID into the *Peer* field and hit `Find`.
|
||||
4. You should see information about the peer including its addresses.
|
29
examples/delegated-routing/package.json
Normal file
29
examples/delegated-routing/package.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "delegated-routing-example",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"ipfs": "~0.34.4",
|
||||
"libp2p": "github:libp2p/js-libp2p#master",
|
||||
"libp2p-delegated-content-routing": "~0.2.2",
|
||||
"libp2p-delegated-peer-routing": "~0.2.2",
|
||||
"libp2p-kad-dht": "~0.14.12",
|
||||
"libp2p-mplex": "~0.8.5",
|
||||
"libp2p-secio": "~0.11.1",
|
||||
"libp2p-webrtc-star": "~0.15.8",
|
||||
"libp2p-websocket-star": "~0.10.2",
|
||||
"libp2p-websockets": "~0.12.2",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
"react-scripts": "2.1.8"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start"
|
||||
},
|
||||
"browserslist": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not ie <= 11",
|
||||
"not op_mini all"
|
||||
]
|
||||
}
|
BIN
examples/delegated-routing/public/favicon.ico
Normal file
BIN
examples/delegated-routing/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.8 KiB |
16
examples/delegated-routing/public/index.html
Normal file
16
examples/delegated-routing/public/index.html
Normal file
@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<meta name="theme-color" content="#000000">
|
||||
<title>Delegated Routing</title>
|
||||
<link rel="stylesheet" type="text/css" href="main.css">
|
||||
</head>
|
||||
<body>
|
||||
<noscript>
|
||||
You need to enable JavaScript to run this app.
|
||||
</noscript>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
67
examples/delegated-routing/public/main.css
Normal file
67
examples/delegated-routing/public/main.css
Normal file
@ -0,0 +1,67 @@
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
|
||||
section * {
|
||||
margin: 10px;
|
||||
}
|
||||
|
||||
header {
|
||||
background-color: #222;
|
||||
height: 150px;
|
||||
padding: 20px;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.center {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: bisque;
|
||||
min-height: 100px;
|
||||
margin: 0px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.loader {
|
||||
text-align: center;
|
||||
height: 64px;
|
||||
margin-bottom: -64px;
|
||||
}
|
||||
|
||||
.loading .lds-ripple {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
width: 64px;
|
||||
height: 64px;
|
||||
}
|
||||
.loading .lds-ripple div {
|
||||
position: absolute;
|
||||
border: 4px solid #000;
|
||||
opacity: 1;
|
||||
border-radius: 50%;
|
||||
animation: lds-ripple 1s cubic-bezier(0, 0.2, 0.8, 1) infinite;
|
||||
margin: auto;
|
||||
}
|
||||
.loading .lds-ripple div:nth-child(2) {
|
||||
animation-delay: -0.5s;
|
||||
}
|
||||
@keyframes lds-ripple {
|
||||
0% {
|
||||
top: 28px;
|
||||
left: 28px;
|
||||
width: 0;
|
||||
height: 0;
|
||||
opacity: 1;
|
||||
}
|
||||
100% {
|
||||
top: -1px;
|
||||
left: -1px;
|
||||
width: 58px;
|
||||
height: 58px;
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
152
examples/delegated-routing/src/App.js
Normal file
152
examples/delegated-routing/src/App.js
Normal file
@ -0,0 +1,152 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
import React from 'react'
|
||||
import Ipfs from 'ipfs'
|
||||
import libp2pBundle from './libp2p-bundle'
|
||||
const Component = React.Component
|
||||
|
||||
const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/p2p/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8'
|
||||
|
||||
class App extends Component {
|
||||
constructor (props) {
|
||||
super(props)
|
||||
this.state = {
|
||||
peers: 0,
|
||||
// This hash is the IPFS readme
|
||||
hash: 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB',
|
||||
// This peer is one of the Bootstrap nodes for IPFS
|
||||
peer: 'QmV6kA2fB8kTr6jc3pL5zbNsjKbmPUHAPKKHRBYe1kDEyc',
|
||||
isLoading: 0
|
||||
}
|
||||
this.peerInterval = null
|
||||
|
||||
this.handleHashChange = this.handleHashChange.bind(this)
|
||||
this.handleHashSubmit = this.handleHashSubmit.bind(this)
|
||||
this.handlePeerChange = this.handlePeerChange.bind(this)
|
||||
this.handlePeerSubmit = this.handlePeerSubmit.bind(this)
|
||||
}
|
||||
|
||||
handleHashChange (event) {
|
||||
this.setState({
|
||||
hash: event.target.value
|
||||
})
|
||||
}
|
||||
handlePeerChange (event) {
|
||||
this.setState({
|
||||
peer: event.target.value
|
||||
})
|
||||
}
|
||||
|
||||
handleHashSubmit (event) {
|
||||
event.preventDefault()
|
||||
this.setState({
|
||||
isLoading: this.state.isLoading + 1
|
||||
})
|
||||
|
||||
this.ipfs.cat(this.state.hash, (err, data) => {
|
||||
if (err) console.log('Error', err)
|
||||
|
||||
this.setState({
|
||||
response: data.toString(),
|
||||
isLoading: this.state.isLoading - 1
|
||||
})
|
||||
})
|
||||
}
|
||||
handlePeerSubmit (event) {
|
||||
event.preventDefault()
|
||||
this.setState({
|
||||
isLoading: this.state.isLoading + 1
|
||||
})
|
||||
|
||||
this.ipfs.dht.findpeer(this.state.peer, (err, results) => {
|
||||
if (err) console.log('Error', err)
|
||||
|
||||
this.setState({
|
||||
response: JSON.stringify(results, null, 2),
|
||||
isLoading: this.state.isLoading - 1
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
componentDidMount () {
|
||||
window.ipfs = this.ipfs = new Ipfs({
|
||||
config: {
|
||||
Addresses: {
|
||||
Swarm: []
|
||||
},
|
||||
Discovery: {
|
||||
MDNS: {
|
||||
Enabled: false
|
||||
},
|
||||
webRTCStar: {
|
||||
Enabled: false
|
||||
}
|
||||
},
|
||||
Bootstrap: [
|
||||
BootstrapNode
|
||||
]
|
||||
},
|
||||
preload: {
|
||||
enabled: false
|
||||
},
|
||||
libp2p: libp2pBundle
|
||||
})
|
||||
this.ipfs.on('ready', () => {
|
||||
if (this.peerInterval) {
|
||||
clearInterval(this.peerInterval)
|
||||
}
|
||||
|
||||
this.ipfs.swarm.connect(BootstrapNode, (err) => {
|
||||
if (err) {
|
||||
console.log('Error connecting to the node', err)
|
||||
}
|
||||
console.log('Connected!')
|
||||
})
|
||||
|
||||
this.peerInterval = setInterval(() => {
|
||||
this.ipfs.swarm.peers((err, peers) => {
|
||||
if (err) console.log(err)
|
||||
if (peers) this.setState({peers: peers.length})
|
||||
})
|
||||
}, 2500)
|
||||
})
|
||||
}
|
||||
|
||||
render () {
|
||||
return (
|
||||
<div>
|
||||
<header className="center">
|
||||
<h1>Delegated Routing</h1>
|
||||
<h2>There are currently {this.state.peers} peers.</h2>
|
||||
</header>
|
||||
<section className="center">
|
||||
<form onSubmit={this.handleHashSubmit}>
|
||||
<label>
|
||||
Hash:
|
||||
<input type="text" value={this.state.hash} onChange={this.handleHashChange} />
|
||||
<input type="submit" value="Find" />
|
||||
</label>
|
||||
</form>
|
||||
<form onSubmit={this.handlePeerSubmit}>
|
||||
<label>
|
||||
Peer:
|
||||
<input type="text" value={this.state.peer} onChange={this.handlePeerChange} />
|
||||
<input type="submit" value="Find" />
|
||||
</label>
|
||||
</form>
|
||||
</section>
|
||||
<section className={[this.state.isLoading > 0 ? 'loading' : '', 'loader'].join(' ')}>
|
||||
<div className="lds-ripple"><div></div><div></div></div>
|
||||
</section>
|
||||
<section>
|
||||
<pre>
|
||||
{this.state.response}
|
||||
</pre>
|
||||
</section>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default App
|
8
examples/delegated-routing/src/index.js
Normal file
8
examples/delegated-routing/src/index.js
Normal file
@ -0,0 +1,8 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
import React from 'react' // eslint-disable-line no-unused-vars
|
||||
import ReactDOM from 'react-dom'
|
||||
import App from './App' // eslint-disable-line no-unused-vars
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'))
|
76
examples/delegated-routing/src/libp2p-bundle.js
Normal file
76
examples/delegated-routing/src/libp2p-bundle.js
Normal file
@ -0,0 +1,76 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
|
||||
export default function Libp2pBundle ({peerInfo, peerBook}) {
|
||||
const wrtcstar = new WebRTCStar({id: peerInfo.id})
|
||||
const wsstar = new WebSocketStar({id: peerInfo.id})
|
||||
const delegatedApiOptions = {
|
||||
host: '0.0.0.0',
|
||||
protocol: 'http',
|
||||
port: '8080'
|
||||
}
|
||||
|
||||
return new Libp2p({
|
||||
peerInfo,
|
||||
peerBook,
|
||||
// Lets limit the connection managers peers and have it check peer health less frequently
|
||||
connectionManager: {
|
||||
maxPeers: 10,
|
||||
pollInterval: 5000
|
||||
},
|
||||
modules: {
|
||||
contentRouting: [
|
||||
new DelegatedContentRouter(peerInfo.id, delegatedApiOptions)
|
||||
],
|
||||
peerRouting: [
|
||||
new DelegatedPeerRouter(delegatedApiOptions)
|
||||
],
|
||||
peerDiscovery: [
|
||||
wrtcstar.discovery,
|
||||
wsstar.discovery
|
||||
],
|
||||
transport: [
|
||||
wrtcstar,
|
||||
wsstar,
|
||||
Websockets
|
||||
],
|
||||
streamMuxer: [
|
||||
MPLEX
|
||||
],
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: false,
|
||||
webrtcStar: {
|
||||
enabled: false
|
||||
},
|
||||
websocketStar: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
enabled: false
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
44
examples/discovery-mechanisms/1.js
Normal file
44
examples/discovery-mechanisms/1.js
Normal file
@ -0,0 +1,44 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
const bootstrapers = require('./bootstrapers')
|
||||
|
||||
;(async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
peerDiscovery: [Bootstrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 60e3,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
node.connectionManager.on('peer:connect', (connection) => {
|
||||
console.log('Connection established to:', connection.remotePeer.toB58String()) // Emitted when a peer has been found
|
||||
})
|
||||
|
||||
node.on('peer:discovery', (peerId) => {
|
||||
// No need to dial, autoDial is on
|
||||
console.log('Discovered:', peerId.toB58String())
|
||||
})
|
||||
|
||||
await node.start()
|
||||
})();
|
47
examples/discovery-mechanisms/2.js
Normal file
47
examples/discovery-mechanisms/2.js
Normal file
@ -0,0 +1,47 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
peerDiscovery: [MulticastDNS]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
[MulticastDNS.tag]: {
|
||||
interval: 20e3,
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node1.on('peer:discovery', (peerId) => console.log('Discovered:', peerId.toB58String()))
|
||||
node2.on('peer:discovery', (peerId) => console.log('Discovered:', peerId.toB58String()))
|
||||
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
})();
|
68
examples/discovery-mechanisms/3.js
Normal file
68
examples/discovery-mechanisms/3.js
Normal file
@ -0,0 +1,68 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const PubsubPeerDiscovery = require('libp2p-pubsub-peer-discovery')
|
||||
|
||||
const createRelayServer = require('libp2p-relay-server')
|
||||
|
||||
const createNode = async (bootstrapers) => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
pubsub: Gossipsub,
|
||||
peerDiscovery: [Bootstrap, PubsubPeerDiscovery]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
[PubsubPeerDiscovery.tag]: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
},
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const relay = await createRelayServer({
|
||||
listenAddresses: ['/ip4/0.0.0.0/tcp/0']
|
||||
})
|
||||
console.log(`libp2p relay starting with id: ${relay.peerId.toB58String()}`)
|
||||
await relay.start()
|
||||
const relayMultiaddrs = relay.multiaddrs.map((m) => `${m.toString()}/p2p/${relay.peerId.toB58String()}`)
|
||||
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(relayMultiaddrs),
|
||||
createNode(relayMultiaddrs)
|
||||
])
|
||||
|
||||
node1.on('peer:discovery', (peerId) => {
|
||||
console.log(`Peer ${node1.peerId.toB58String()} discovered: ${peerId.toB58String()}`)
|
||||
})
|
||||
node2.on('peer:discovery', (peerId) => {
|
||||
console.log(`Peer ${node2.peerId.toB58String()} discovered: ${peerId.toB58String()}`)
|
||||
})
|
||||
|
||||
;[node1, node2].forEach((node, index) => console.log(`Node ${index} starting with id: ${node.peerId.toB58String()}`))
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
})();
|
258
examples/discovery-mechanisms/README.md
Normal file
258
examples/discovery-mechanisms/README.md
Normal file
@ -0,0 +1,258 @@
|
||||
# Peer Discovery Mechanisms
|
||||
|
||||
A Peer Discovery module enables libp2p to find peers to connect to. Think of these mechanisms as ways to join the rest of the network, as railing points.
|
||||
|
||||
With this system, a libp2p node can both have a set of nodes to always connect on boot (bootstraper nodes), discover nodes through locality (e.g connected in the same LAN) or through serendipity (random walks on a DHT).
|
||||
|
||||
These mechanisms save configuration and enable a node to operate without any explicit dials, it will just work. Once new peers are discovered, their known data is stored in the peer's PeerStore.
|
||||
|
||||
## 1. Bootstrap list of Peers when booting a node
|
||||
|
||||
For this demo, we will connect to IPFS default bootstrapper nodes and so, we will need to support the same set of features those nodes have, that are: TCP, mplex, and NOISE. You can see the complete example at [1.js](./1.js).
|
||||
|
||||
First, we create our libp2p node.
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ NOISE ],
|
||||
peerDiscovery: [ Bootstrap ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 60e3,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
In this configuration, we use a `bootstrappers` array listing peers to connect _on boot_. Here is the list used by js-ipfs and go-ipfs.
|
||||
|
||||
```JavaScript
|
||||
const bootstrapers = [
|
||||
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
|
||||
]
|
||||
```
|
||||
|
||||
Now, once we create and start the node, we can listen for events such as `peer:discovery` and `peer:connect`, these events tell us when we found a peer, independently of the discovery mechanism used and when we actually dialed to that peer.
|
||||
|
||||
```JavaScript
|
||||
const node = await Libp2p.create({
|
||||
peerId,
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
}
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ NOISE ],
|
||||
peerDiscovery: [ Bootstrap ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 60e3,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
node.connectionManager.on('peer:connect', (connection) => {
|
||||
console.log('Connection established to:', connection.remotePeer.toB58String()) // Emitted when a new connection has been created
|
||||
})
|
||||
|
||||
node.on('peer:discovery', (peerId) => {
|
||||
// No need to dial, autoDial is on
|
||||
console.log('Discovered:', peerId.toB58String())
|
||||
})
|
||||
|
||||
await node.start()
|
||||
```
|
||||
|
||||
From running [1.js](./1.js), you should see the following:
|
||||
|
||||
```bash
|
||||
> node 1.js
|
||||
Discovered: QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
|
||||
Discovered: QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN
|
||||
Discovered: QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb
|
||||
Discovered: QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp
|
||||
Discovered: QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa
|
||||
Discovered: QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
|
||||
Connection established to: QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
|
||||
Connection established to: QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN
|
||||
Connection established to: QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp
|
||||
Connection established to: QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa
|
||||
Connection established to: QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
|
||||
Connection established to: QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb
|
||||
```
|
||||
|
||||
## 2. MulticastDNS to find other peers in the network
|
||||
|
||||
For this example, we need `libp2p-mdns`, go ahead and `npm install` it. You can find the complete solution at [2.js](./2.js).
|
||||
|
||||
Update your libp2p configuration to include MulticastDNS.
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
|
||||
const createNode = () => {
|
||||
return Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
}
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ NOISE ],
|
||||
peerDiscovery: [ MulticastDNS ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 20e3,
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
To observe it working, spawn two nodes.
|
||||
|
||||
```JavaScript
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node1.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
||||
node2.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
||||
```
|
||||
|
||||
If you run this example, you will see the other peers being discovered.
|
||||
|
||||
```bash
|
||||
> node 2.js
|
||||
Discovered: QmSSbQpuKrxkoXHm1v4Pi35hPN5hUHMZoBoawEs2Nhvi8m
|
||||
Discovered: QmRcXXhtG8vTqwVBRonKWtV4ovDoC1Fe56WYtcrw694eiJ
|
||||
```
|
||||
|
||||
## 3. Pubsub based Peer Discovery
|
||||
|
||||
For this example, we need [`libp2p-pubsub-peer-discovery`](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/), go ahead and `npm install` it. You also need to spin up a set of [`libp2p-relay-servers`](https://github.com/libp2p/js-libp2p-relay-server). These servers act as relay servers and a peer discovery source.
|
||||
|
||||
In the context of this example, we will create and run the `libp2p-relay-server` in the same code snippet. You can find the complete solution at [3.js](./3.js).
|
||||
|
||||
You can create your libp2p nodes as follows:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const PubsubPeerDiscovery = require('libp2p-pubsub-peer-discovery')
|
||||
|
||||
const createNode = async (bootstrapers) => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
pubsub: Gossipsub,
|
||||
peerDiscovery: [Bootstrap, PubsubPeerDiscovery]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
[PubsubPeerDiscovery.tag]: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
},
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
We will use the `libp2p-relay-server` as bootstrap nodes for the libp2p nodes, so that they establish a connection with the relay after starting. As a result, after they establish a connection with the relay, the pubsub discovery will kick in and the relay will advertise them.
|
||||
|
||||
```js
|
||||
const relay = await createRelayServer({
|
||||
listenAddresses: ['/ip4/0.0.0.0/tcp/0']
|
||||
})
|
||||
console.log(`libp2p relay starting with id: ${relay.peerId.toB58String()}`)
|
||||
await relay.start()
|
||||
const relayMultiaddrs = relay.multiaddrs.map((m) => `${m.toString()}/p2p/${relay.peerId.toB58String()}`)
|
||||
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(relayMultiaddrs),
|
||||
createNode(relayMultiaddrs)
|
||||
])
|
||||
|
||||
node1.on('peer:discovery', (peerId) => {
|
||||
console.log(`Peer ${node1.peerId.toB58String()} discovered: ${peerId.toB58String()}`)
|
||||
})
|
||||
node2.on('peer:discovery', (peerId) => {
|
||||
console.log(`Peer ${node2.peerId.toB58String()} discovered: ${peerId.toB58String()}`)
|
||||
})
|
||||
|
||||
;[node1, node2].forEach((node, index) => console.log(`Node ${index} starting with id: ${node.peerId.toB58String()}`))
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
```
|
||||
|
||||
If you run this example, you will see the other peers being discovered.
|
||||
|
||||
```bash
|
||||
> node 3.js
|
||||
libp2p relay starting with id: QmW6FqVV6RsyoGC5zaeFGW9gSWA3LcBRVZrjkKMruh38Bo
|
||||
Node 0 starting with id: QmezqDTmEjZ5BfMgVqjSpLY19mVVLTQ9bE9mRpZwtGxL8N
|
||||
Node 1 starting with id: QmYWeom2odTkm79DzB68NHULqVHDaNDqHhoyqLdcV1fqdv
|
||||
Peer QmezqDTmEjZ5BfMgVqjSpLY19mVVLTQ9bE9mRpZwtGxL8N discovered: QmW6FqVV6RsyoGC5zaeFGW9gSWA3LcBRVZrjkKMruh38Bo
|
||||
Peer QmYWeom2odTkm79DzB68NHULqVHDaNDqHhoyqLdcV1fqdv discovered: QmW6FqVV6RsyoGC5zaeFGW9gSWA3LcBRVZrjkKMruh38Bo
|
||||
Peer QmYWeom2odTkm79DzB68NHULqVHDaNDqHhoyqLdcV1fqdv discovered: QmezqDTmEjZ5BfMgVqjSpLY19mVVLTQ9bE9mRpZwtGxL8N
|
||||
Peer QmezqDTmEjZ5BfMgVqjSpLY19mVVLTQ9bE9mRpZwtGxL8N discovered: QmYWeom2odTkm79DzB68NHULqVHDaNDqHhoyqLdcV1fqdv
|
||||
```
|
||||
|
||||
Taking into account the output, after the relay and both libp2p nodes start, both libp2p nodes will discover the bootstrap node (relay) and connect with it. After establishing a connection with the relay, they will discover each other.
|
||||
|
||||
This is really useful when running libp2p in constrained environments like a browser. You can run a set of `libp2p-relay-server` nodes that will be responsible for both relaying websocket connections between browser nodes and for discovering other browser peers.
|
||||
|
||||
## 4. Where to find other Peer Discovery Mechanisms
|
||||
|
||||
There are plenty more Peer Discovery Mechanisms out there, you can:
|
||||
|
||||
- Find one in [libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star). Yes, a transport with discovery capabilities! This happens because WebRTC requires a rendezvous point for peers to exchange [SDP](https://tools.ietf.org/html/rfc4317) offer, which means we have one or more points that can introduce peers to each other. Think of it as MulticastDNS for the Web, as in MulticastDNS only works in LAN.
|
||||
- Any DHT will offer you a discovery capability. You can simple _random-walk_ the routing tables to find other peers to connect to. For example [libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht) can be used for peer discovery. An example of how to configure it to enable random walks can be found [here](https://github.com/libp2p/js-libp2p/blob/v0.28.4/doc/CONFIGURATION.md#customizing-dht).
|
||||
- You can create your own Discovery service, a registry, a list, a radio beacon, you name it!
|
13
examples/discovery-mechanisms/bootstrapers.js
vendored
Normal file
13
examples/discovery-mechanisms/bootstrapers.js
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs-core/src/runtime/config-nodejs.js
|
||||
const bootstrapers = [
|
||||
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
|
||||
]
|
||||
|
||||
module.exports = bootstrapers
|
42
examples/discovery-mechanisms/test-1.js
Normal file
42
examples/discovery-mechanisms/test-1.js
Normal file
@ -0,0 +1,42 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
const bootstrapers = require('./bootstrapers')
|
||||
|
||||
const discoveredCopy = 'Discovered:'
|
||||
const connectedCopy = 'Connection established to:'
|
||||
|
||||
async function test () {
|
||||
const discoveredNodes = []
|
||||
const connectedNodes = []
|
||||
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
// Discovered or Connected
|
||||
if (line.includes(discoveredCopy)) {
|
||||
const id = line.trim().split(discoveredCopy)[1]
|
||||
discoveredNodes.push(id)
|
||||
} else if (line.includes(connectedCopy)) {
|
||||
const id = line.trim().split(connectedCopy)[1]
|
||||
connectedNodes.push(id)
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => discoveredNodes.length === bootstrapers.length && connectedNodes.length === bootstrapers.length)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
35
examples/discovery-mechanisms/test-2.js
Normal file
35
examples/discovery-mechanisms/test-2.js
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const discoveredCopy = 'Discovered:'
|
||||
|
||||
async function test() {
|
||||
const discoveredNodes = []
|
||||
|
||||
process.stdout.write('2.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '2.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes(discoveredCopy)) {
|
||||
const id = line.trim().split(discoveredCopy)[1]
|
||||
discoveredNodes.push(id)
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => discoveredNodes.length === 2)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
35
examples/discovery-mechanisms/test-3.js
Normal file
35
examples/discovery-mechanisms/test-3.js
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const discoveredCopy = 'discovered:'
|
||||
|
||||
async function test() {
|
||||
let discoverCount = 0
|
||||
|
||||
process.stdout.write('3.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '3.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
// Discovered or Connected
|
||||
if (line.includes(discoveredCopy)) {
|
||||
discoverCount++
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => discoverCount === 4)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
13
examples/discovery-mechanisms/test.js
Normal file
13
examples/discovery-mechanisms/test.js
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const test1 = require('./test-1')
|
||||
const test2 = require('./test-2')
|
||||
const test3 = require('./test-3')
|
||||
|
||||
async function test () {
|
||||
await test1()
|
||||
await test2()
|
||||
await test3()
|
||||
}
|
||||
|
||||
module.exports = test
|
13
examples/echo/README.md
Normal file
13
examples/echo/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# Echo example with libp2p
|
||||
|
||||
This example performs a simple echo from the listener to the dialer.
|
||||
|
||||
## Setup
|
||||
1. Install the modules from libp2p root, `npm install`.
|
||||
2. Open 2 terminal windows in the `./src` directory.
|
||||
|
||||
## Running
|
||||
1. Run the listener in window 1, `node listener.js`
|
||||
2. Run the dialer in window 2, `node dialer.js`
|
||||
3. You should see console logs showing the dial, and the received echo of _hey_
|
||||
4. If you look at the listener window, you will see it receiving the dial
|
58
examples/echo/src/dialer.js
Normal file
58
examples/echo/src/dialer.js
Normal file
@ -0,0 +1,58 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
/*
|
||||
* Dialer Node
|
||||
*/
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const createLibp2p = require('./libp2p')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
async function run() {
|
||||
const [dialerId, listenerId] = await Promise.all([
|
||||
PeerId.createFromJSON(require('./id-d')),
|
||||
PeerId.createFromJSON(require('./id-l'))
|
||||
])
|
||||
|
||||
// Dialer
|
||||
const dialerNode = await createLibp2p({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
peerId: dialerId
|
||||
})
|
||||
|
||||
// Add peer to Dial (the listener) into the PeerStore
|
||||
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/p2p/' + listenerId.toB58String()
|
||||
|
||||
// Start the dialer libp2p node
|
||||
await dialerNode.start()
|
||||
|
||||
console.log('Dialer ready, listening on:')
|
||||
dialerNode.multiaddrs.forEach((ma) => console.log(ma.toString() +
|
||||
'/p2p/' + dialerId.toB58String()))
|
||||
|
||||
// Dial the listener node
|
||||
console.log('Dialing to peer:', listenerMultiaddr)
|
||||
const { stream } = await dialerNode.dialProtocol(listenerMultiaddr, '/echo/1.0.0')
|
||||
|
||||
console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')
|
||||
|
||||
pipe(
|
||||
// Source data
|
||||
['hey'],
|
||||
// Write to the stream, and pass its output to the next function
|
||||
stream,
|
||||
// Sink function
|
||||
async function (source) {
|
||||
// For each chunk of data
|
||||
for await (const data of source) {
|
||||
// Output the data
|
||||
console.log('received echo:', data.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
run()
|
5
examples/echo/src/id-d.json
Normal file
5
examples/echo/src/id-d.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"id": "Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
|
||||
"privKey": "CAASpwkwggSjAgEAAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAECggEAZnrCJ6IYiLyyRdr9SbKXCNDb4YByGYPEi/HT1aHgIJfFE1PSMjxcdytxfyjP4JJpVtPjiT9JFVU2ddoYu5qJN6tGwjVwgJEWg1UXmPaAw1T/drjS94kVsAs82qICtFmwp52Apg3dBZ0Qwq/8qE1XbG7lLyohIbfCBiL0tiPYMfkcsN9gnFT/kFCX0LVs2pa9fHCRMY9rqCc4/rWJa1w8sMuQ23y4lDaxKF9OZVvOHFQkbBDrkquWHE4r55fchCz/rJklkPJUNENuncBRu0/2X+p4IKFD1DnttXNwb8j4LPiSlLro1T0hiUr5gO2QmdYwXFF63Q3mjQy0+5I4eNbjjQKBgQDZvZy3gUKS/nQNkYfq9za80uLbIj/cWbO+ZZjXCsj0fNIcQFJcKMBoA7DjJvu2S/lf86/41YHkPdmrLAEQAkJ+5BBNOycjYK9minTEjIMMmZDTXXugZ62wnU6F46uLkgEChTqEP57Y6xwwV+JaEDFEsW5N1eE9lEVX9nGIr4phMwKBgQC1TazLuEt1WBx/iUT83ita7obXqoKNzwsS/MWfY2innzYZKDOqeSYZzLtt9uTtp4X4uLyPbYs0qFYhXLsUYMoGHNN8+NdjoyxCjQRJRBkMtaNR0lc5lVDWl3bTuJovjFCgAr9uqJrmI5OHcCIk/cDpdWb3nWaMihVlePmiTcTy9wKBgQCU0u7c1jKkudqks4XM6a+2HAYGdUBk4cLjLhnrUWnNAcuyl5wzdX8dGPi8KZb+IKuQE8WBNJ2VXVj7kBYh1QmSJVunDflQSvNYCOaKuOeRoxzD+y9Wkca74qkbBmPn/6FFEb7PSZTO+tPHjyodGNgz9XpJJRjQuBk1aDJtlF3m1QKBgE5SAr5ym65SZOU3UGUIOKRsfDW4Q/OsqDUImvpywCgBICaX9lHDShFFHwau7FA52ScL7vDquoMB4UtCOtLfyQYA9995w9oYCCurrVlVIJkb8jSLcADBHw3EmqF1kq3NqJqm9TmBfoDCh52vdCCUufxgKh33kfBOSlXuf7B8dgMbAoGAZ3r0/mBQX6S+s5+xCETMTSNv7TQzxgtURIpVs+ZVr2cMhWhiv+n0Omab9X9Z50se8cWl5lkvx8vn3D/XHHIPrMF6qk7RAXtvReb+PeitNvm0odqjFv0J2qki6fDs0HKwq4kojAXI1Md8Th0eobNjsy21fEEJT7uKMJdovI/SErI=",
|
||||
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAE="
|
||||
}
|
5
examples/echo/src/id-l.json
Normal file
5
examples/echo/src/id-l.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"id": "QmcrQZ6RJdpYuGvZqD5QEHAv6qX4BrQLJLQPQUrTrzdcgm",
|
||||
"privKey": "CAASqAkwggSkAgEAAoIBAQDLZZcGcbe4urMBVlcHgN0fpBymY+xcr14ewvamG70QZODJ1h9sljlExZ7byLiqRB3SjGbfpZ1FweznwNxWtWpjHkQjTVXeoM4EEgDSNO/Cg7KNlU0EJvgPJXeEPycAZX9qASbVJ6EECQ40VR/7+SuSqsdL1hrmG1phpIju+D64gLyWpw9WEALfzMpH5I/KvdYDW3N4g6zOD2mZNp5y1gHeXINHWzMF596O72/6cxwyiXV1eJ000k1NVnUyrPjXtqWdVLRk5IU1LFpoQoXZU5X1hKj1a2qt/lZfH5eOrF/ramHcwhrYYw1txf8JHXWO/bbNnyemTHAvutZpTNrsWATfAgMBAAECggEAQj0obPnVyjxLFZFnsFLgMHDCv9Fk5V5bOYtmxfvcm50us6ye+T8HEYWGUa9RrGmYiLweuJD34gLgwyzE1RwptHPj3tdNsr4NubefOtXwixlWqdNIjKSgPlaGULQ8YF2tm/kaC2rnfifwz0w1qVqhPReO5fypL+0ShyANVD3WN0Fo2ugzrniCXHUpR2sHXSg6K+2+qWdveyjNWog34b7CgpV73Ln96BWae6ElU8PR5AWdMnRaA9ucA+/HWWJIWB3Fb4+6uwlxhu2L50Ckq1gwYZCtGw63q5L4CglmXMfIKnQAuEzazq9T4YxEkp+XDnVZAOgnQGUBYpetlgMmkkh9qQKBgQDvsEs0ThzFLgnhtC2Jy//ZOrOvIAKAZZf/mS08AqWH3L0/Rjm8ZYbLsRcoWU78sl8UFFwAQhMRDBP9G+RPojWVahBL/B7emdKKnFR1NfwKjFdDVaoX5uNvZEKSl9UubbC4WZJ65u/cd5jEnj+w3ir9G8n+P1gp/0yBz02nZXFgSwKBgQDZPQr4HBxZL7Kx7D49ormIlB7CCn2i7mT11Cppn5ifUTrp7DbFJ2t9e8UNk6tgvbENgCKXvXWsmflSo9gmMxeEOD40AgAkO8Pn2R4OYhrwd89dECiKM34HrVNBzGoB5+YsAno6zGvOzLKbNwMG++2iuNXqXTk4uV9GcI8OnU5ZPQKBgCZUGrKSiyc85XeiSGXwqUkjifhHNh8yH8xPwlwGUFIZimnD4RevZI7OEtXw8iCWpX2gg9XGuyXOuKORAkF5vvfVriV4e7c9Ad4Igbj8mQFWz92EpV6NHXGCpuKqRPzXrZrNOA9PPqwSs+s9IxI1dMpk1zhBCOguWx2m+NP79NVhAoGBAI6WSoTfrpu7ewbdkVzTWgQTdLzYNe6jmxDf2ZbKclrf7lNr/+cYIK2Ud5qZunsdBwFdgVcnu/02czeS42TvVBgs8mcgiQc/Uy7yi4/VROlhOnJTEMjlU2umkGc3zLzDgYiRd7jwRDLQmMrYKNyEr02HFKFn3w8kXSzW5I8rISnhAoGBANhchHVtJd3VMYvxNcQb909FiwTnT9kl9pkjhwivx+f8/K8pDfYCjYSBYCfPTM5Pskv5dXzOdnNuCj6Y2H/9m2SsObukBwF0z5Qijgu1DsxvADVIKZ4rzrGb4uSEmM6200qjJ/9U98fVM7rvOraakrhcf9gRwuspguJQnSO9cLj6",
|
||||
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLZZcGcbe4urMBVlcHgN0fpBymY+xcr14ewvamG70QZODJ1h9sljlExZ7byLiqRB3SjGbfpZ1FweznwNxWtWpjHkQjTVXeoM4EEgDSNO/Cg7KNlU0EJvgPJXeEPycAZX9qASbVJ6EECQ40VR/7+SuSqsdL1hrmG1phpIju+D64gLyWpw9WEALfzMpH5I/KvdYDW3N4g6zOD2mZNp5y1gHeXINHWzMF596O72/6cxwyiXV1eJ000k1NVnUyrPjXtqWdVLRk5IU1LFpoQoXZU5X1hKj1a2qt/lZfH5eOrF/ramHcwhrYYw1txf8JHXWO/bbNnyemTHAvutZpTNrsWATfAgMBAAE="
|
||||
}
|
23
examples/echo/src/libp2p.js
Normal file
23
examples/echo/src/libp2p.js
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
async function createLibp2p(_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [TCP, WS],
|
||||
streamMuxer: [mplex],
|
||||
connEncryption: [NOISE],
|
||||
},
|
||||
}
|
||||
|
||||
return libp2p.create(defaultsDeep(_options, defaults))
|
||||
}
|
||||
|
||||
module.exports = createLibp2p
|
41
examples/echo/src/listener.js
Normal file
41
examples/echo/src/listener.js
Normal file
@ -0,0 +1,41 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
/*
|
||||
* Listener Node
|
||||
*/
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const createLibp2p = require('./libp2p')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
async function run() {
|
||||
const listenerId = await PeerId.createFromJSON(require('./id-l'))
|
||||
|
||||
// Listener libp2p node
|
||||
const listenerNode = await createLibp2p({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/10333']
|
||||
},
|
||||
peerId: listenerId
|
||||
})
|
||||
|
||||
// Log a message when we receive a connection
|
||||
listenerNode.connectionManager.on('peer:connect', (connection) => {
|
||||
console.log('received dial to me from:', connection.remotePeer.toB58String())
|
||||
})
|
||||
|
||||
// Handle incoming connections for the protocol by piping from the stream
|
||||
// back to itself (an echo)
|
||||
await listenerNode.handle('/echo/1.0.0', ({ stream }) => pipe(stream.source, stream.sink))
|
||||
|
||||
// Start listening
|
||||
await listenerNode.start()
|
||||
|
||||
console.log('Listener ready, listening on:')
|
||||
listenerNode.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + listenerId.toB58String())
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
61
examples/echo/test.js
Normal file
61
examples/echo/test.js
Normal file
@ -0,0 +1,61 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
function startProcess(name) {
|
||||
return execa('node', [path.join(__dirname, name)], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
}
|
||||
|
||||
async function test () {
|
||||
const listenerReady = pDefer()
|
||||
const messageReceived = pDefer()
|
||||
|
||||
// Step 1 process
|
||||
process.stdout.write('node listener.js\n')
|
||||
const listenerProc = startProcess('src/listener.js')
|
||||
listenerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const s = uint8ArrayToString(data)
|
||||
|
||||
if (s.includes('Listener ready, listening on:')) {
|
||||
listenerReady.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await listenerReady.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 2 process
|
||||
process.stdout.write('node dialer.js\n')
|
||||
const dialerProc = startProcess('src/dialer.js')
|
||||
dialerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const s = uint8ArrayToString(data)
|
||||
|
||||
if (s.includes('received echo:')) {
|
||||
messageReceived.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await messageReceived.promise
|
||||
process.stdout.write('echo message received\n')
|
||||
|
||||
listenerProc.kill()
|
||||
dialerProc.kill()
|
||||
await Promise.all([
|
||||
listenerProc,
|
||||
dialerProc
|
||||
]).catch((err) => {
|
||||
if (err.signal !== 'SIGTERM') {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = test
|
4
examples/libp2p-in-the-browser/.babelrc
Normal file
4
examples/libp2p-in-the-browser/.babelrc
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"presets": ["@babel/preset-env"],
|
||||
"plugins": ["syntax-async-functions","transform-regenerator"]
|
||||
}
|
46
examples/libp2p-in-the-browser/README.md
Normal file
46
examples/libp2p-in-the-browser/README.md
Normal file
@ -0,0 +1,46 @@
|
||||
# libp2p in the browser
|
||||
|
||||
This example leverages the [Parcel.js bundler](https://parceljs.org/) to compile and serve the libp2p code in the browser. Parcel uses [Babel](https://babeljs.io/) to handle transpilation of the code. You can use other bundlers such as Webpack or Browserify, but we will not be covering them here.
|
||||
|
||||
## Setup
|
||||
|
||||
In order to run the example, first install the dependencies from same directory as this README:
|
||||
|
||||
```
|
||||
cd ./examples/libp2p-in-the-browser
|
||||
npm install
|
||||
```
|
||||
|
||||
## Running the examples
|
||||
|
||||
Start by running the Parcel server:
|
||||
|
||||
```
|
||||
npm start
|
||||
```
|
||||
|
||||
The output should look something like this:
|
||||
|
||||
```log
|
||||
$ npm start
|
||||
|
||||
> libp2p-in-browser@1.0.0 start
|
||||
> parcel index.html
|
||||
|
||||
Server running at http://localhost:1234
|
||||
✨ Built in 1000ms.
|
||||
```
|
||||
|
||||
This will compile the code and start a server listening on port [http://localhost:1234](http://localhost:1234). Now open your browser to `http://localhost:1234`. You should see a log of your node's Peer ID, the discovered peers from the Bootstrap module, and connections to those peers as they are created.
|
||||
|
||||
Now, if you open a second browser tab to `http://localhost:1234`, you should discover your node from the previous tab. This is due to the fact that the `libp2p-webrtc-star` transport also acts as a Peer Discovery interface. Your node will be notified of any peer that connects to the same signaling server you are connected to. Once libp2p discovers this new peer, it will attempt to establish a direct WebRTC connection.
|
||||
|
||||
**Note**: In the example we assign libp2p to `window.libp2p`, in case you would like to play around with the API directly in the browser. You can of course make changes to `index.js` and Parcel will automatically rebuild and reload the browser tabs.
|
||||
|
||||
## Going to production?
|
||||
|
||||
This example uses public `libp2p-webrtc-star` servers. These servers should be used for experimenting and demos, they **MUST** not be used in production as there is no guarantee on availability.
|
||||
|
||||
You can see how to deploy your own signaling server in [libp2p/js-libp2p-webrtc-star/DEPLOYMENT.md](https://github.com/libp2p/js-libp2p-webrtc-star/blob/master/DEPLOYMENT.md).
|
||||
|
||||
Once you have your own server running, you should add its listen address in your libp2p node configuration.
|
23
examples/libp2p-in-the-browser/index.html
Normal file
23
examples/libp2p-in-the-browser/index.html
Normal file
@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>js-libp2p parcel.js browser example</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<h1 id="status">Starting libp2p...</h1>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<pre id="output"></pre>
|
||||
</main>
|
||||
|
||||
<script src="./index.js"></script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
77
examples/libp2p-in-the-browser/index.js
Normal file
77
examples/libp2p-in-the-browser/index.js
Normal file
@ -0,0 +1,77 @@
|
||||
import 'babel-polyfill'
|
||||
import Libp2p from 'libp2p'
|
||||
import Websockets from 'libp2p-websockets'
|
||||
import WebRTCStar from 'libp2p-webrtc-star'
|
||||
import { NOISE } from 'libp2p-noise'
|
||||
import Mplex from 'libp2p-mplex'
|
||||
import Bootstrap from 'libp2p-bootstrap'
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
// Create our libp2p node
|
||||
const libp2p = await Libp2p.create({
|
||||
addresses: {
|
||||
// Add the signaling server address, along with our PeerId to our multiaddrs list
|
||||
// libp2p will automatically attempt to dial to the signaling server so that it can
|
||||
// receive inbound connections from other peers
|
||||
listen: [
|
||||
'/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
|
||||
'/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
|
||||
]
|
||||
},
|
||||
modules: {
|
||||
transport: [Websockets, WebRTCStar],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [Mplex],
|
||||
peerDiscovery: [Bootstrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
|
||||
// The associated object, will be passed to the service when it is instantiated.
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
list: [
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// UI elements
|
||||
const status = document.getElementById('status')
|
||||
const output = document.getElementById('output')
|
||||
|
||||
output.textContent = ''
|
||||
|
||||
function log (txt) {
|
||||
console.info(txt)
|
||||
output.textContent += `${txt.trim()}\n`
|
||||
}
|
||||
|
||||
// Listen for new peers
|
||||
libp2p.on('peer:discovery', (peerId) => {
|
||||
log(`Found peer ${peerId.toB58String()}`)
|
||||
})
|
||||
|
||||
// Listen for new connections to peers
|
||||
libp2p.connectionManager.on('peer:connect', (connection) => {
|
||||
log(`Connected to ${connection.remotePeer.toB58String()}`)
|
||||
})
|
||||
|
||||
// Listen for peers disconnecting
|
||||
libp2p.connectionManager.on('peer:disconnect', (connection) => {
|
||||
log(`Disconnected from ${connection.remotePeer.toB58String()}`)
|
||||
})
|
||||
|
||||
await libp2p.start()
|
||||
status.innerText = 'libp2p started!'
|
||||
log(`libp2p id is ${libp2p.peerId.toB58String()}`)
|
||||
|
||||
// Export libp2p to the window so you can play with the API
|
||||
window.libp2p = libp2p
|
||||
})
|
34
examples/libp2p-in-the-browser/package.json
Normal file
34
examples/libp2p-in-the-browser/package.json
Normal file
@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "libp2p-in-browser",
|
||||
"version": "1.0.0",
|
||||
"description": "A libp2p node running in the browser",
|
||||
"main": "index.js",
|
||||
"browserslist": [
|
||||
"last 2 Chrome versions"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"build": "parcel build index.html",
|
||||
"start": "parcel index.html"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@babel/preset-env": "^7.13.0",
|
||||
"libp2p": "../../",
|
||||
"libp2p-bootstrap": "^0.12.1",
|
||||
"libp2p-mplex": "^0.10.0",
|
||||
"libp2p-noise": "^2.0.0",
|
||||
"libp2p-webrtc-star": "^0.20.0",
|
||||
"libp2p-websockets": "^0.14.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.13.10",
|
||||
"@babel/core": "^7.13.0",
|
||||
"babel-plugin-syntax-async-functions": "^6.13.0",
|
||||
"babel-plugin-transform-regenerator": "^6.26.0",
|
||||
"babel-polyfill": "^6.26.0",
|
||||
"parcel-bundler": "1.12.3"
|
||||
}
|
||||
}
|
52
examples/libp2p-in-the-browser/test.js
Normal file
52
examples/libp2p-in-the-browser/test.js
Normal file
@ -0,0 +1,52 @@
|
||||
'use strict'
|
||||
|
||||
const execa = require('execa')
|
||||
const { chromium } = require('playwright');
|
||||
|
||||
async function run() {
|
||||
let url = ''
|
||||
const proc = execa('parcel', ['./index.html'], {
|
||||
preferLocal: true,
|
||||
localDir: __dirname,
|
||||
cwd: __dirname,
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (chunk) => {
|
||||
/**@type {string} */
|
||||
const out = chunk.toString()
|
||||
|
||||
if (out.includes('Server running at')) {
|
||||
url = out.replace('Server running at ', '')
|
||||
}
|
||||
|
||||
if (out.includes('✨ Built in ')) {
|
||||
try {
|
||||
const browser = await chromium.launch();
|
||||
const page = await browser.newPage();
|
||||
await page.goto(url);
|
||||
await page.waitForFunction(selector => document.querySelector(selector).innerText === 'libp2p started!', '#status')
|
||||
await page.waitForFunction(
|
||||
selector => {
|
||||
const text = document.querySelector(selector).innerText
|
||||
return text.includes('libp2p id is') &&
|
||||
text.includes('Found peer') &&
|
||||
text.includes('Connected to')
|
||||
},
|
||||
'#output',
|
||||
{ timeout: 5000 }
|
||||
)
|
||||
await browser.close();
|
||||
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
} finally {
|
||||
proc.cancel()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
module.exports = run
|
2
examples/nat-traversal/README.md
Normal file
2
examples/nat-traversal/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# WIP - This example is still in the works
|
||||

|
21
examples/package.json
Normal file
21
examples/package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "libp2p-examples",
|
||||
"version": "1.0.0",
|
||||
"description": "Examples of how to use libp2p",
|
||||
"scripts": {
|
||||
"test": "node ./test.js",
|
||||
"test:all": "node ./test-all.js"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"execa": "^2.1.0",
|
||||
"fs-extra": "^8.1.0",
|
||||
"libp2p-pubsub-peer-discovery": "^3.0.0",
|
||||
"libp2p-relay-server": "^0.1.2",
|
||||
"p-defer": "^3.0.0",
|
||||
"which": "^2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"playwright": "^1.7.1"
|
||||
}
|
||||
}
|
56
examples/peer-and-content-routing/1.js
Normal file
56
examples/peer-and-content-routing/1.js
Normal file
@ -0,0 +1,56 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
|
||||
const delay = require('delay')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2, node3] = await Promise.all([
|
||||
createNode(),
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
node2.peerStore.addressBook.set(node3.peerId, node3.multiaddrs)
|
||||
|
||||
await Promise.all([
|
||||
node1.dial(node2.peerId),
|
||||
node2.dial(node3.peerId)
|
||||
])
|
||||
|
||||
// The DHT routing tables need a moment to populate
|
||||
await delay(100)
|
||||
|
||||
const peer = await node1.peerRouting.findPeer(node3.peerId)
|
||||
|
||||
console.log('Found it, multiaddrs are:')
|
||||
peer.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${peer.id.toB58String()}`))
|
||||
})();
|
65
examples/peer-and-content-routing/2.js
Normal file
65
examples/peer-and-content-routing/2.js
Normal file
@ -0,0 +1,65 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const CID = require('cids')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
|
||||
const all = require('it-all')
|
||||
const delay = require('delay')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2, node3] = await Promise.all([
|
||||
createNode(),
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
node2.peerStore.addressBook.set(node3.peerId, node3.multiaddrs)
|
||||
|
||||
await Promise.all([
|
||||
node1.dial(node2.peerId),
|
||||
node2.dial(node3.peerId)
|
||||
])
|
||||
|
||||
// Wait for onConnect handlers in the DHT
|
||||
await delay(100)
|
||||
|
||||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
||||
await node1.contentRouting.provide(cid)
|
||||
|
||||
console.log('Node %s is providing %s', node1.peerId.toB58String(), cid.toBaseEncodedString())
|
||||
|
||||
// wait for propagation
|
||||
await delay(300)
|
||||
|
||||
const providers = await all(node3.contentRouting.findProviders(cid, { timeout: 3000 }))
|
||||
|
||||
console.log('Found provider:', providers[0].id.toB58String())
|
||||
})();
|
107
examples/peer-and-content-routing/README.md
Normal file
107
examples/peer-and-content-routing/README.md
Normal file
@ -0,0 +1,107 @@
|
||||
# Peer and Content Routing
|
||||
|
||||
DHTs (Distributed Hash Tables) are one of the most common building blocks used when creating P2P networks. However, the name doesn't make justice to all the benefits it brings and putting the whole set of features in one box has proven to be limiting when we want to integrate multiple pieces together. With this in mind, we've come up with a new definition for what a DHT offers: Peer Routing and Content Routing.
|
||||
|
||||
Peer Routing is the category of modules that offer a way to find other peers in the network by intentionally issuing queries, iterative or recursive, until a Peer is found or the closest Peers, given the Peer Routing algorithm strategy are found.
|
||||
|
||||
Content Routing is the category of modules that offer a way to find where content lives in the network, it works in two steps: 1) Peers provide (announce) to the network that they are holders of specific content (multihashes) and 2) Peers issue queries to find where that content lives. A Content Routing mechanism could be as complex as a Kademlia DHT or a simple registry somewhere in the network.
|
||||
|
||||
# 1. Using Peer Routing to find other peers
|
||||
|
||||
This example builds on top of the [Protocol and Stream Muxing](../protocol-and-stream-muxing). We need to install `libp2p-kad-dht`, go ahead and `npm install libp2p-kad-dht`. If you want to see the final version, open [1.js](./1.js).
|
||||
|
||||
First, let's update our config to support Peer Routing and Content Routing.
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ NOISE ],
|
||||
// we add the DHT module that will enable Peer and Content Routing
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
// dht must be enabled
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Once that is done, we can use the createNode function we developed in the previous example to create 3 nodes. Connect node 1 to node 2 and node 2 to node 3. We will use node 2 as a way to find the whereabouts of node 3
|
||||
|
||||
```JavaScript
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
const node3 = nodes[2]
|
||||
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
node2.peerStore.addressBook.set(node3.peerId, node3.multiaddrs)
|
||||
|
||||
await Promise.all([
|
||||
node1.dial(node2.peerId),
|
||||
node2.dial(node3.peerId)
|
||||
])
|
||||
|
||||
// Set up of the cons might take time
|
||||
await delay(100)
|
||||
|
||||
const peer = await node1.peerRouting.findPeer(node3.peerId)
|
||||
|
||||
console.log('Found it, multiaddrs are:')
|
||||
peer.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${peer.id.toB58String()}`))
|
||||
```
|
||||
|
||||
You should see the output being something like:
|
||||
|
||||
```Bash
|
||||
> node 1.js
|
||||
Found it, multiaddrs are:
|
||||
/ip4/127.0.0.1/tcp/63617
|
||||
/ip4/192.168.86.41/tcp/63617
|
||||
```
|
||||
|
||||
You have successfully used Peer Routing to find a peer that you were not directly connected. Now all you have to do is to dial to the multiaddrs you discovered.
|
||||
|
||||
# 2. Using Content Routing to find providers of content
|
||||
|
||||
With Content Routing, you can create records that are stored in multiple points in the network, these records can be resolved by you or other peers and they act as memos or rendezvous points. A great usage of this feature is to support discovery of content, where one node holds a file and instead of using a centralized tracker to inform other nodes that it holds that file, it simply puts a record in the network that can be resolved by other peers. Peer Routing and Content Routing are commonly known as Distributed Hash Tables, DHT.
|
||||
|
||||
You can find this example completed in [2.js](./2.js), however as you will see it is very simple to update the previous example.
|
||||
|
||||
Instead of calling `peerRouting.findPeer`, we will use `contentRouting.provide` and `contentRouting.findProviders`.
|
||||
|
||||
```JavaScript
|
||||
await node1.contentRouting.provide(cid)
|
||||
console.log('Node %s is providing %s', node1.peerId.toB58String(), cid.toBaseEncodedString())
|
||||
|
||||
const provs = await all(node3.contentRouting.findProviders(cid, { timeout: 5000 }))
|
||||
|
||||
console.log('Found provider:', providers[0].id.toB58String())
|
||||
```
|
||||
|
||||
The output of your program should look like:
|
||||
|
||||
```bash
|
||||
> node 2.js
|
||||
Node QmSsmVPoTy3WpzwiNPnsKmonBaZjK2HitFs2nWUvwK31Pz is providing QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL
|
||||
Found provider: QmSsmVPoTy3WpzwiNPnsKmonBaZjK2HitFs2nWUvwK31Pz
|
||||
```
|
||||
|
||||
That's it, now you know how to find peers that have pieces of information that interest you!
|
||||
|
||||
# 3. Future Work
|
||||
|
||||
Currently, the only mechanisms for Peer and Content Routing come from the DHT, however we do have the intention to support:
|
||||
|
||||
- Multiple Peer Routing Mechanisms, including ones that do recursive searches (i.e [webrtc-explorer](http://daviddias.me/blog/webrtc-explorer-2-0-0-alpha-release/) like packet switching or [CJDNS](https://github.com/cjdelisle/cjdns) path finder)
|
||||
- Content Routing via PubSub
|
||||
- Content Routing via centralized index (i.e a tracker)
|
36
examples/peer-and-content-routing/test-1.js
Normal file
36
examples/peer-and-content-routing/test-1.js
Normal file
@ -0,0 +1,36 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test() {
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const addrs = []
|
||||
let foundIt = false
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
// Discovered peer
|
||||
if (!foundIt && line.includes('Found it, multiaddrs are:')) {
|
||||
foundIt = true
|
||||
}
|
||||
|
||||
addrs.push(line)
|
||||
})
|
||||
|
||||
await pWaitFor(() => addrs.length === 2)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
40
examples/peer-and-content-routing/test-2.js
Normal file
40
examples/peer-and-content-routing/test-2.js
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const providedCopy = 'is providing'
|
||||
const foundCopy = 'Found provider:'
|
||||
|
||||
async function test() {
|
||||
process.stdout.write('2.js\n')
|
||||
const providedDefer = pDefer()
|
||||
const foundDefer = pDefer()
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '2.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes(providedCopy)) {
|
||||
providedDefer.resolve()
|
||||
} else if (line.includes(foundCopy)) {
|
||||
foundDefer.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
providedDefer.promise,
|
||||
foundDefer.promise
|
||||
])
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
11
examples/peer-and-content-routing/test.js
Normal file
11
examples/peer-and-content-routing/test.js
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const test1 = require('./test-1')
|
||||
const test2 = require('./test-2')
|
||||
|
||||
async function test() {
|
||||
await test1()
|
||||
await test2()
|
||||
}
|
||||
|
||||
module.exports = test
|
1
examples/pnet/.gitignore
vendored
Normal file
1
examples/pnet/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
tmp/
|
25
examples/pnet/README.md
Normal file
25
examples/pnet/README.md
Normal file
@ -0,0 +1,25 @@
|
||||
# Private Networking
|
||||
This example shows how to set up a private network of libp2p nodes.
|
||||
|
||||
## Setup
|
||||
1. Install the modules in the libp2p root directory, `npm install`.
|
||||
|
||||
## Run
|
||||
Running the example will cause two nodes with the same swarm key to be started and exchange basic information.
|
||||
|
||||
```
|
||||
node index.js
|
||||
```
|
||||
|
||||
### Using different keys
|
||||
This example includes `TASK` comments that can be used to try the example with different swarm keys. This will
|
||||
allow you to see how nodes will fail to connect if they are on different private networks and try to connect to
|
||||
one another.
|
||||
|
||||
To change the swarm key of one of the nodes, look through `index.js` for comments starting with `TASK` to indicate
|
||||
where lines are that pertain to changing the swarm key of node 2.
|
||||
|
||||
### Exploring the repos
|
||||
Once you've run the example you can take a look at the repos in the `./tmp` directory to see how they differ, including
|
||||
the swarm keys. You should see a `swarm.key` file in each of the repos and when the nodes are on the same private network
|
||||
this contents of the `swarm.key` files should be the same.
|
52
examples/pnet/index.js
Normal file
52
examples/pnet/index.js
Normal file
@ -0,0 +1,52 @@
|
||||
/* eslint no-console: ["off"] */
|
||||
'use strict'
|
||||
|
||||
const { generate } = require('libp2p/src/pnet')
|
||||
const privateLibp2pNode = require('./libp2p-node')
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
// Create a Uint8Array and write the swarm key to it
|
||||
const swarmKey = new Uint8Array(95)
|
||||
generate(swarmKey)
|
||||
|
||||
// This key is for testing a different key not working
|
||||
const otherSwarmKey = new Uint8Array(95)
|
||||
generate(otherSwarmKey)
|
||||
|
||||
;(async () => {
|
||||
const node1 = await privateLibp2pNode(swarmKey)
|
||||
|
||||
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
|
||||
const node2 = await privateLibp2pNode(swarmKey)
|
||||
// const node2 = await privateLibp2pNode(otherSwarmKey)
|
||||
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
|
||||
console.log('nodes started...')
|
||||
|
||||
// Add node 2 data to node1's PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
await node1.dial(node2.peerId)
|
||||
|
||||
node2.handle('/private', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const { stream } = await node1.dialProtocol(node2.peerId, '/private')
|
||||
|
||||
await pipe(
|
||||
['This message is sent on a private network'],
|
||||
stream
|
||||
)
|
||||
})()
|
38
examples/pnet/libp2p-node.js
Normal file
38
examples/pnet/libp2p-node.js
Normal file
@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Protector = require('libp2p/src/pnet')
|
||||
|
||||
/**
|
||||
* privateLibp2pNode returns a libp2p node function that will use the swarm
|
||||
* key with the given `swarmKey` to create the Protector
|
||||
*
|
||||
* @param {Uint8Array} swarmKey
|
||||
* @returns {Promise<libp2p>} Returns a libp2pNode function for use in IPFS creation
|
||||
*/
|
||||
const privateLibp2pNode = async (swarmKey) => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP], // We're only using the TCP transport for this example
|
||||
streamMuxer: [MPLEX], // We're only using mplex muxing
|
||||
// Let's make sure to use identifying crypto in our pnet since the protector doesn't
|
||||
// care about node identity, and only the presence of private keys
|
||||
connEncryption: [NOISE],
|
||||
// Leave peer discovery empty, we don't want to find peers. We could omit the property, but it's
|
||||
// being left in for explicit readability.
|
||||
// We should explicitly dial pnet peers, or use a custom discovery service for finding nodes in our pnet
|
||||
peerDiscovery: [],
|
||||
connProtector: new Protector(swarmKey)
|
||||
}
|
||||
})
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
module.exports = privateLibp2pNode
|
30
examples/pnet/test.js
Normal file
30
examples/pnet/test.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test () {
|
||||
const messageReceived = pDefer()
|
||||
process.stdout.write('index.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, 'index.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const s = uint8ArrayToString(data)
|
||||
if (s.includes('This message is sent on a private network')) {
|
||||
messageReceived.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await messageReceived.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
28
examples/pnet/utils.js
Normal file
28
examples/pnet/utils.js
Normal file
@ -0,0 +1,28 @@
|
||||
'use strict'
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
/**
|
||||
* mkdirp recursively creates needed folders for the given dir path
|
||||
* @param {string} dir
|
||||
* @returns {string} The path that was created
|
||||
*/
|
||||
module.exports.mkdirp = (dir) => {
|
||||
return path
|
||||
.resolve(dir)
|
||||
.split(path.sep)
|
||||
.reduce((acc, cur) => {
|
||||
const currentPath = path.normalize(acc + path.sep + cur)
|
||||
|
||||
try {
|
||||
fs.statSync(currentPath)
|
||||
} catch (e) {
|
||||
if (e.code === 'ENOENT') {
|
||||
fs.mkdirSync(currentPath)
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
return currentPath
|
||||
}, '')
|
||||
}
|
80
examples/protocol-and-stream-muxing/1.js
Normal file
80
examples/protocol-and-stream-muxing/1.js
Normal file
@ -0,0 +1,80 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
// Add node's 2 data to the PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
|
||||
// exact matching
|
||||
node2.handle('/your-protocol', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
// multiple protocols
|
||||
/*
|
||||
node2.handle(['/another-protocol/1.0.0', '/another-protocol/2.0.0'], ({ protocol, stream }) => {
|
||||
if (protocol === '/another-protocol/2.0.0') {
|
||||
// handle backwards compatibility
|
||||
}
|
||||
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
*/
|
||||
|
||||
const { stream } = await node1.dialProtocol(node2.peerId, ['/your-protocol'])
|
||||
await pipe(
|
||||
['my own protocol, wow!'],
|
||||
stream
|
||||
)
|
||||
|
||||
/*
|
||||
const { stream } = node1.dialProtocol(node2.peerId, ['/another-protocol/1.0.0'])
|
||||
|
||||
await pipe(
|
||||
['my own protocol, wow!'],
|
||||
stream
|
||||
)
|
||||
*/
|
||||
})();
|
64
examples/protocol-and-stream-muxing/2.js
Normal file
64
examples/protocol-and-stream-muxing/2.js
Normal file
@ -0,0 +1,64 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
// Add node's 2 data to the PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
|
||||
node2.handle(['/a', '/b'], ({ protocol, stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(`from: ${protocol}, msg: ${msg.toString()}`)
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const { stream: stream1 } = await node1.dialProtocol(node2.peerId, ['/a'])
|
||||
await pipe(
|
||||
['protocol (a)'],
|
||||
stream1
|
||||
)
|
||||
|
||||
const { stream: stream2 } = await node1.dialProtocol(node2.peerId, ['/b'])
|
||||
await pipe(
|
||||
['protocol (b)'],
|
||||
stream2
|
||||
)
|
||||
|
||||
const { stream: stream3 } = await node1.dialProtocol(node2.peerId, ['/b'])
|
||||
await pipe(
|
||||
['another stream on protocol (b)'],
|
||||
stream3
|
||||
)
|
||||
})();
|
70
examples/protocol-and-stream-muxing/3.js
Normal file
70
examples/protocol-and-stream-muxing/3.js
Normal file
@ -0,0 +1,70 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
// Add node's 2 data to the PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
|
||||
node1.handle('/node-1', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
node2.handle('/node-2', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const { stream: stream1 } = await node1.dialProtocol(node2.peerId, ['/node-2'])
|
||||
await pipe(
|
||||
['from 1 to 2'],
|
||||
stream1
|
||||
)
|
||||
|
||||
const { stream: stream2 } = await node2.dialProtocol(node1.peerId, ['/node-1'])
|
||||
await pipe(
|
||||
['from 2 to 1'],
|
||||
stream2
|
||||
)
|
||||
})();
|
174
examples/protocol-and-stream-muxing/README.md
Normal file
174
examples/protocol-and-stream-muxing/README.md
Normal file
@ -0,0 +1,174 @@
|
||||
# Protocol and Stream Multiplexing (aka muxing)
|
||||
|
||||
One of the specialties of libp2p is solving the bane of protocol discovery and handshake between machines. Before libp2p, you would have to assign a listener to a port and then through some process of formal specification you would assign ports to special protocols so that other hosts would know before hand which port to dial (e.g ssh (22), http (80), https (443), ftp (21), etc). With libp2p you don't need to do that anymore, not only you don't have to assign ports before hand, you don't even need to think about ports at all since all the protocol handshaking happens in the wire!
|
||||
|
||||
The feature of agreeing on a protocol over an established connection is what we call _protocol multiplexing_ and it is possible through [multistream-select](https://github.com/multiformats/multistream), another protocol that lets you agree per connection (or stream) which protocol is going to be talked over that connection (select), it also enables you to request the other end to tell you which protocols it supports (ls). You can learn more about multistream-select at its [specification repo](https://github.com/multiformats/multistream).
|
||||
|
||||
# 1. Handle multiple protocols
|
||||
|
||||
Let's see _protocol multiplexing_ in action! You will need the following modules for this example: `libp2p`, `libp2p-tcp`, `peer-id`, `it-pipe`, `it-buffer` and `streaming-iterables`. This example reuses the base left by the [Transports](../transports) example. You can see the complete solution at [1.js](./1.js).
|
||||
|
||||
After creating the nodes, we need to tell libp2p which protocols to handle.
|
||||
|
||||
```JavaScript
|
||||
const pipe = require('it-pipe')
|
||||
const { map } = require('streaming-iterables')
|
||||
const { toBuffer } = require('it-buffer')
|
||||
|
||||
// ...
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
// Add node's 2 data to the PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
|
||||
// Here we are telling libp2p that if someone dials this node to talk with the `/your-protocol`
|
||||
// multicodec, the protocol identifier, please call this handler and give it the stream
|
||||
// so that incomming data can be handled
|
||||
node2.handle('/your-protocol', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
source => (async function () {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
})()
|
||||
)
|
||||
})
|
||||
```
|
||||
|
||||
After the protocol is _handled_, now we can dial to it.
|
||||
|
||||
```JavaScript
|
||||
const { stream } = await node1.dialProtocol(node2.peerId, ['/your-protocol'])
|
||||
|
||||
await pipe(
|
||||
['my own protocol, wow!'],
|
||||
stream
|
||||
)
|
||||
```
|
||||
|
||||
You might have seen this in the [Transports](../transports) examples. However, what it was not explained is that you can do more than exact string matching, for example, you can use semver.
|
||||
|
||||
```JavaScript
|
||||
node2.handle('/another-protocol/1.0.1', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
// ...
|
||||
const { stream } = await node1.dialProtocol(node2.peerId, ['/another-protocol/1.0.0'])
|
||||
|
||||
await pipe(
|
||||
['my own protocol, wow!'],
|
||||
stream
|
||||
)
|
||||
```
|
||||
|
||||
This feature is super power for network protocols. It works in the same way as versioning your RPC/REST API, but for anything that goes in the wire. We had to use this feature to upgrade protocols within the IPFS Stack (i.e Bitswap) and we successfully managed to do so without any network splits.
|
||||
|
||||
There is still one last feature, you can provide multiple protocols for the same handler. If you have a backwards incompatible change, but it only requires minor changes to the code, you may prefer to do protocol checking instead of having multiple handlers
|
||||
|
||||
```JavaScript
|
||||
node2.handle(['/another-protocol/1.0.0', '/another-protocol/2.0.0'], ({ protocol, stream }) => {
|
||||
if (protocol === '/another-protocol/2.0.0') {
|
||||
// handle backwards compatibility
|
||||
}
|
||||
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
```
|
||||
|
||||
Try all of this out by executing [1.js](./1.js).
|
||||
|
||||
# 2. Reuse existing connection
|
||||
|
||||
The examples above would require a node to create a whole new connection for every time it dials in one of the protocols, this is a waste of resources and also it might be simply not possible (e.g lack of file descriptors, not enough ports being open, etc). What we really want is to dial a connection once and then multiplex several virtual connections (stream) over a single connection, this is where _stream multiplexing_ comes into play.
|
||||
|
||||
Stream multiplexing is an old concept, in fact it happens in many of the layers of the [OSI System](https://en.wikipedia.org/wiki/OSI_model). In libp2p, we make this feature to our avail by letting the user pick which module for stream multiplexing to use.
|
||||
|
||||
Currently, we have [libp2p-mplex](https://github.com/libp2p/js-libp2p-mplex) and pluging it in is as easy as adding a transport. Let's revisit our libp2p configuration.
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
//...
|
||||
|
||||
const createNode = () => {
|
||||
return Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ]
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
With this, we can dial as many times as we want to a peer and always reuse the same established underlying connection.
|
||||
|
||||
```JavaScript
|
||||
node2.handle(['/a', '/b'], ({ protocol, stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(`from: ${protocol}, msg: ${msg.toString()}`)
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const { stream } = await node1.dialProtocol(node2.peerId, ['/a'])
|
||||
await pipe(
|
||||
['protocol (a)'],
|
||||
stream
|
||||
)
|
||||
|
||||
const { stream: stream2 } = await node1.dialProtocol(node2.peerId, ['/b'])
|
||||
await pipe(
|
||||
['protocol (b)'],
|
||||
stream2
|
||||
)
|
||||
|
||||
const { stream: stream3 } = await node1.dialProtocol(node2.peerId, ['/b'])
|
||||
await pipe(
|
||||
['another stream on protocol (b)'],
|
||||
stream3
|
||||
)
|
||||
```
|
||||
|
||||
By running [2.js](./2.js) you should see the following result:
|
||||
|
||||
```
|
||||
> node 2.js
|
||||
from: /a, msg: protocol (a)
|
||||
from: /b, msg: protocol (b)
|
||||
from: /b, msg: another stream on protocol (b)
|
||||
```
|
||||
|
||||
# 3. Bidirectional connections
|
||||
|
||||
There is one last trick on _protocol and stream multiplexing_ that libp2p uses to make everyone's life easier and that is _bidirectional connection_.
|
||||
|
||||
With the aid of both mechanisms, we can reuse an incomming connection to dial streams out too, this is specially useful when you are behind tricky NAT, firewalls or if you are running in a browser, where you can't have listening addrs, but you can dial out. By dialing out, you enable other peers to talk with you in Protocols that they want, simply by opening a new multiplexed stream.
|
||||
|
||||
You can see this working on example [3.js](./3.js). The result should look like the following:
|
||||
|
||||
```Bash
|
||||
> node 3.js
|
||||
from 1 to 2
|
||||
from 2 to 1
|
||||
```
|
31
examples/protocol-and-stream-muxing/test-1.js
Normal file
31
examples/protocol-and-stream-muxing/test-1.js
Normal file
@ -0,0 +1,31 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test() {
|
||||
const messageDefer = pDefer()
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes('my own protocol, wow!')) {
|
||||
messageDefer.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await messageDefer.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
38
examples/protocol-and-stream-muxing/test-2.js
Normal file
38
examples/protocol-and-stream-muxing/test-2.js
Normal file
@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const messages = [
|
||||
'protocol (a)',
|
||||
'protocol (b)',
|
||||
'another stream on protocol (b)'
|
||||
]
|
||||
|
||||
async function test() {
|
||||
process.stdout.write('2.js\n')
|
||||
|
||||
let count = 0
|
||||
const proc = execa('node', [path.join(__dirname, '2.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (messages.find((m) => line.includes(m))) {
|
||||
count += 1
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => count === messages.length)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
37
examples/protocol-and-stream-muxing/test-3.js
Normal file
37
examples/protocol-and-stream-muxing/test-3.js
Normal file
@ -0,0 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const messages = [
|
||||
'from 1 to 2',
|
||||
'from 2 to 1'
|
||||
]
|
||||
|
||||
async function test() {
|
||||
process.stdout.write('3.js\n')
|
||||
|
||||
let count = 0
|
||||
const proc = execa('node', [path.join(__dirname, '3.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (messages.find((m) => line.includes(m))) {
|
||||
count += 1
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => count === messages.length)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
13
examples/protocol-and-stream-muxing/test.js
Normal file
13
examples/protocol-and-stream-muxing/test.js
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const test1 = require('./test-1')
|
||||
const test2 = require('./test-2')
|
||||
const test3 = require('./test-3')
|
||||
|
||||
async function test() {
|
||||
await test1()
|
||||
await test2()
|
||||
await test3()
|
||||
}
|
||||
|
||||
module.exports = test
|
56
examples/pubsub/1.js
Normal file
56
examples/pubsub/1.js
Normal file
@ -0,0 +1,56 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const topic = 'news'
|
||||
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
// Add node's 2 data to the PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
await node1.dial(node2.peerId)
|
||||
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
// Will not receive own published messages by default
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node2.pubsub.subscribe(topic)
|
||||
|
||||
// node2 publishes "news" every second
|
||||
setInterval(() => {
|
||||
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
|
||||
}, 1000)
|
||||
})()
|
106
examples/pubsub/README.md
Normal file
106
examples/pubsub/README.md
Normal file
@ -0,0 +1,106 @@
|
||||
# Publish Subscribe
|
||||
|
||||
Publish Subscribe is also included on the stack. Currently, we have two PubSub implementation available [libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub) and [libp2p-gossipsub](https://github.com/ChainSafe/js-libp2p-gossipsub), with many more being researched at [research-pubsub](https://github.com/libp2p/research-pubsub).
|
||||
|
||||
We've seen many interesting use cases appear with this, here are some highlights:
|
||||
|
||||
- [Collaborative Text Editing](https://www.youtube.com/watch?v=-kdx8rJd8rQ)
|
||||
- [IPFS PubSub (using libp2p-floodsub) for IoT](https://www.youtube.com/watch?v=qLpM5pBDGiE).
|
||||
- [Real Time distributed Applications](https://www.youtube.com/watch?v=vQrbxyDPSXg)
|
||||
|
||||
## 1. Setting up a simple PubSub network on top of libp2p
|
||||
|
||||
For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in [Discovery Mechanisms](../discovery-mechanisms). You can find the complete version at [1.js](./1.js).
|
||||
|
||||
Using PubSub is super simple, you only need to provide the implementation of your choice and you are ready to go. No need for extra configuration.
|
||||
|
||||
First, let's update our libp2p configuration with a pubsub implementation.
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ NOISE ],
|
||||
// we add the Pubsub module we want
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Once that is done, we only need to create a few libp2p nodes, connect them and everything is ready to start using pubsub.
|
||||
|
||||
```JavaScript
|
||||
const topic = 'news'
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
// Add node's 2 data to the PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
await node1.dial(node2.peerId)
|
||||
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
// Will not receive own published messages by default
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node2.pubsub.subscribe(topic)
|
||||
|
||||
// node2 publishes "news" every second
|
||||
setInterval(() => {
|
||||
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
|
||||
}, 1000)
|
||||
```
|
||||
|
||||
The output of the program should look like:
|
||||
|
||||
```
|
||||
> node 1.js
|
||||
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
```
|
||||
|
||||
You can change the pubsub `emitSelf` option if you want the publishing node to receive its own messages.
|
||||
|
||||
```JavaScript
|
||||
const defaults = {
|
||||
config: {
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The output of the program should look like:
|
||||
|
||||
```
|
||||
> node 1.js
|
||||
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
```
|
||||
|
||||
## 2. Future work
|
||||
|
||||
libp2p/IPFS PubSub is enabling a whole set of Distributed Real Time applications using CRDT (Conflict-Free Replicated Data Types). It is still going through heavy research (and hacking) and we invite you to join the conversation at [research-CRDT](https://github.com/ipfs/research-CRDT). Here is a list of some of the exciting examples:
|
||||
|
||||
- [PubSub Room](https://github.com/ipfs-labs/ipfs-pubsub-room)
|
||||
- [Live DB - A always in Sync DB using CRDT](https://github.com/ipfs-labs/ipfs-live-db)
|
||||
- [IIIF Annotations over IPFS, CRDT and libp2p](https://www.youtube.com/watch?v=hmAniA6g9D0&feature=youtu.be&t=10m40s)
|
||||
- [orbit.chat - p2p chat application, fully running in the browser with js-ipfs, js-libp2p and orbit-db](http://orbit.chat/)
|
88
examples/pubsub/message-filtering/1.js
Normal file
88
examples/pubsub/message-filtering/1.js
Normal file
@ -0,0 +1,88 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const topic = 'fruit'
|
||||
|
||||
const [node1, node2, node3] = await Promise.all([
|
||||
createNode(),
|
||||
createNode(),
|
||||
createNode(),
|
||||
])
|
||||
|
||||
// node1 conect to node2 and node2 conect to node3
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
await node1.dial(node2.peerId)
|
||||
|
||||
node2.peerStore.addressBook.set(node3.peerId, node3.multiaddrs)
|
||||
await node2.dial(node3.peerId)
|
||||
|
||||
//subscribe
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
// Will not receive own published messages by default
|
||||
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node2.pubsub.subscribe(topic)
|
||||
|
||||
node3.pubsub.on(topic, (msg) => {
|
||||
console.log(`node3 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node3.pubsub.subscribe(topic)
|
||||
|
||||
const validateFruit = (msgTopic, msg) => {
|
||||
const fruit = uint8ArrayToString(msg.data)
|
||||
const validFruit = ['banana', 'apple', 'orange']
|
||||
|
||||
if (!validFruit.includes(fruit)) {
|
||||
throw new Error('no valid fruit received')
|
||||
}
|
||||
}
|
||||
|
||||
//validate fruit
|
||||
node1.pubsub.topicValidators.set(topic, validateFruit)
|
||||
node2.pubsub.topicValidators.set(topic, validateFruit)
|
||||
node3.pubsub.topicValidators.set(topic, validateFruit)
|
||||
|
||||
// node1 publishes "fruits" every five seconds
|
||||
var count = 0;
|
||||
const myFruits = ['banana', 'apple', 'car', 'orange'];
|
||||
// car is not a fruit !
|
||||
setInterval(() => {
|
||||
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
||||
node1.pubsub.publish(topic, uint8ArrayFromString(myFruits[count]))
|
||||
count++
|
||||
if (count == myFruits.length) {
|
||||
count = 0
|
||||
}
|
||||
}, 5000)
|
||||
})()
|
110
examples/pubsub/message-filtering/README.md
Normal file
110
examples/pubsub/message-filtering/README.md
Normal file
@ -0,0 +1,110 @@
|
||||
# Filter Messages
|
||||
|
||||
To prevent undesired data from being propagated on the network, we can apply a filter to Gossipsub. Messages that fail validation in the filter will not be re-shared.
|
||||
|
||||
## 1. Setting up a PubSub network with three nodes
|
||||
|
||||
First, let's update our libp2p configuration with a pubsub implementation.
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ NOISE ],
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Then, create three nodes and connect them together. In this example, we will connect the nodes in series. Node 1 connected with node 2 and node 2 connected with node 3.
|
||||
|
||||
```JavaScript
|
||||
const [node1, node2, node3] = await Promise.all([
|
||||
createNode(),
|
||||
createNode(),
|
||||
createNode(),
|
||||
])
|
||||
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
await node1.dial(node2.peerId)
|
||||
|
||||
node2.peerStore.addressBook.set(node3.peerId, node3.multiaddrs)
|
||||
await node2.dial(node3.peerId)
|
||||
```
|
||||
|
||||
Now we' can subscribe to the fruit topic and log incoming messages.
|
||||
|
||||
```JavaScript
|
||||
const topic = 'fruit'
|
||||
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node2.pubsub.subscribe(topic)
|
||||
|
||||
node3.pubsub.on(topic, (msg) => {
|
||||
console.log(`node3 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node3.pubsub.subscribe(topic)
|
||||
```
|
||||
Finally, let's define the additional filter in the fruit topic.
|
||||
|
||||
```JavaScript
|
||||
const validateFruit = (msgTopic, msg) => {
|
||||
const fruit = uint8ArrayToString(msg.data)
|
||||
const validFruit = ['banana', 'apple', 'orange']
|
||||
|
||||
if (!validFruit.includes(fruit)) {
|
||||
throw new Error('no valid fruit received')
|
||||
}
|
||||
}
|
||||
|
||||
node1.pubsub.topicValidators.set(topic, validateFruit)
|
||||
node2.pubsub.topicValidators.set(topic, validateFruit)
|
||||
node3.pubsub.topicValidators.set(topic, validateFruit)
|
||||
```
|
||||
|
||||
In this example, node one has an outdated version of the system, or is a malicious node. When it tries to publish fruit, the messages are re-shared and all the nodes share the message. However, when it tries to publish a vehicle the message is not re-shared.
|
||||
|
||||
```JavaScript
|
||||
var count = 0;
|
||||
const myFruits = ['banana', 'apple', 'car', 'orange'];
|
||||
|
||||
setInterval(() => {
|
||||
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
||||
node1.pubsub.publish(topic, new TextEncoder().encode(myFruits[count]))
|
||||
count++
|
||||
if (count == myFruits.length) {
|
||||
count = 0
|
||||
}
|
||||
}, 5000)
|
||||
```
|
||||
|
||||
Result
|
||||
|
||||
```
|
||||
> node 1.js
|
||||
############## fruit banana ##############
|
||||
node2 received: banana
|
||||
node3 received: banana
|
||||
############## fruit apple ##############
|
||||
node2 received: apple
|
||||
node3 received: apple
|
||||
############## fruit car ##############
|
||||
############## fruit orange ##############
|
||||
node1 received: orange
|
||||
node2 received: orange
|
||||
node3 received: orange
|
||||
```
|
67
examples/pubsub/message-filtering/test.js
Normal file
67
examples/pubsub/message-filtering/test.js
Normal file
@ -0,0 +1,67 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const stdout = [
|
||||
{
|
||||
topic: 'banana',
|
||||
messageCount: 2
|
||||
},
|
||||
{
|
||||
topic: 'apple',
|
||||
messageCount: 2
|
||||
},
|
||||
{
|
||||
topic: 'car',
|
||||
messageCount: 0
|
||||
},
|
||||
{
|
||||
topic: 'orange',
|
||||
messageCount: 2
|
||||
},
|
||||
]
|
||||
|
||||
async function test () {
|
||||
const defer = pDefer()
|
||||
let topicCount = 0
|
||||
let topicMessageCount = 0
|
||||
|
||||
process.stdout.write('message-filtering/1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
// End
|
||||
if (topicCount === stdout.length) {
|
||||
defer.resolve()
|
||||
proc.all.removeAllListeners('data')
|
||||
}
|
||||
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (stdout[topicCount] && line.includes(stdout[topicCount].topic)) {
|
||||
// Validate previous number of messages
|
||||
if (topicCount > 0 && topicMessageCount > stdout[topicCount - 1].messageCount) {
|
||||
defer.reject()
|
||||
throw new Error(`topic ${stdout[topicCount - 1].topic} had ${topicMessageCount} messages instead of ${stdout[topicCount - 1].messageCount}`)
|
||||
}
|
||||
|
||||
topicCount++
|
||||
topicMessageCount = 0
|
||||
} else {
|
||||
topicMessageCount++
|
||||
}
|
||||
})
|
||||
|
||||
await defer.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user