libp2p 0.36.2 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -21
- package/README.md +13 -10
- package/dist/src/address-manager/index.d.ts +34 -52
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +81 -0
- package/dist/src/address-manager/index.js.map +1 -0
- package/dist/src/circuit/auto-relay.d.ts +24 -85
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +216 -0
- package/dist/src/circuit/auto-relay.js.map +1 -0
- package/dist/src/circuit/circuit/hop.d.ts +26 -53
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +138 -0
- package/dist/src/circuit/circuit/hop.js.map +1 -0
- package/dist/src/circuit/circuit/stop.d.ts +18 -10
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js +49 -0
- package/dist/src/circuit/circuit/stop.js.map +1 -0
- package/dist/src/circuit/circuit/stream-handler.d.ts +19 -38
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +59 -0
- package/dist/src/circuit/circuit/stream-handler.js.map +1 -0
- package/dist/src/circuit/circuit/utils.d.ts +3 -6
- package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/circuit/utils.js +43 -0
- package/dist/src/circuit/circuit/utils.js.map +1 -0
- package/dist/src/circuit/constants.d.ts +24 -6
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +26 -0
- package/dist/src/circuit/constants.js.map +1 -0
- package/dist/src/circuit/index.d.ts +41 -74
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +69 -0
- package/dist/src/circuit/index.js.map +1 -0
- package/dist/src/circuit/listener.d.ts +8 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +62 -0
- package/dist/src/circuit/listener.js.map +1 -0
- package/dist/src/circuit/multicodec.d.ts +1 -1
- package/dist/src/circuit/multicodec.d.ts.map +1 -1
- package/dist/src/circuit/multicodec.js +2 -0
- package/dist/src/circuit/multicodec.js.map +1 -0
- package/dist/src/circuit/pb/index.d.ts +52 -0
- package/dist/src/circuit/pb/index.d.ts.map +1 -0
- package/dist/src/circuit/pb/index.js +98 -0
- package/dist/src/circuit/pb/index.js.map +1 -0
- package/dist/src/circuit/transport.d.ts +18 -78
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +192 -0
- package/dist/src/circuit/transport.js.map +1 -0
- package/dist/src/circuit/utils.d.ts +5 -2
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +11 -0
- package/dist/src/circuit/utils.js.map +1 -0
- package/dist/src/config.d.ts +3 -85
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +91 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/connection-manager/auto-dialler.d.ts +24 -41
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +105 -0
- package/dist/src/connection-manager/auto-dialler.js.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts +16 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
- package/dist/src/connection-manager/dialer/dial-request.d.ts +27 -0
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/dial-request.js +83 -0
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/connection-manager/dialer/index.d.ts +104 -0
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/index.js +267 -0
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +102 -136
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +333 -0
- package/dist/src/connection-manager/index.js.map +1 -0
- package/dist/src/connection-manager/latency-monitor.d.ts +72 -97
- package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
- package/dist/src/connection-manager/latency-monitor.js +220 -0
- package/dist/src/connection-manager/latency-monitor.js.map +1 -0
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +15 -16
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +118 -0
- package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -0
- package/dist/src/constants.d.ts +22 -10
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +27 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/content-routing/index.d.ts +27 -81
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +99 -0
- package/dist/src/content-routing/index.js.map +1 -0
- package/dist/src/content-routing/utils.d.ts +6 -49
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +42 -0
- package/dist/src/content-routing/utils.js.map +1 -0
- package/dist/src/dht/dht-content-routing.d.ts +10 -24
- package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-content-routing.js +32 -0
- package/dist/src/dht/dht-content-routing.js.map +1 -0
- package/dist/src/dht/dht-peer-routing.d.ts +9 -24
- package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-peer-routing.js +26 -0
- package/dist/src/dht/dht-peer-routing.js.map +1 -0
- package/dist/src/dht/dummy-dht.d.ts +20 -0
- package/dist/src/dht/dummy-dht.d.ts.map +1 -0
- package/dist/src/dht/dummy-dht.js +46 -0
- package/dist/src/dht/dummy-dht.js.map +1 -0
- package/dist/src/errors.d.ts +70 -63
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +75 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/fetch/constants.d.ts +1 -1
- package/dist/src/fetch/constants.d.ts.map +1 -1
- package/dist/src/fetch/constants.js +3 -0
- package/dist/src/fetch/constants.js.map +1 -0
- package/dist/src/fetch/index.d.ts +30 -49
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +129 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/pb/proto.d.ts +27 -0
- package/dist/src/fetch/pb/proto.d.ts.map +1 -0
- package/dist/src/fetch/pb/proto.js +50 -0
- package/dist/src/fetch/pb/proto.js.map +1 -0
- package/dist/src/get-peer.d.ts +4 -11
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +43 -0
- package/dist/src/get-peer.js.map +1 -0
- package/dist/src/identify/consts.d.ts +9 -9
- package/dist/src/identify/consts.d.ts.map +1 -1
- package/dist/src/identify/consts.js +11 -0
- package/dist/src/identify/consts.js.map +1 -0
- package/dist/src/identify/index.d.ts +42 -96
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +322 -0
- package/dist/src/identify/index.js.map +1 -0
- package/dist/src/identify/pb/message.d.ts +16 -0
- package/dist/src/identify/pb/message.d.ts.map +1 -0
- package/dist/src/identify/pb/message.js +24 -0
- package/dist/src/identify/pb/message.js.map +1 -0
- package/dist/src/index.d.ts +150 -496
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/insecure/index.d.ts +9 -0
- package/dist/src/insecure/index.d.ts.map +1 -0
- package/dist/src/insecure/index.js +81 -0
- package/dist/src/insecure/index.js.map +1 -0
- package/dist/src/insecure/pb/proto.d.ts +29 -0
- package/dist/src/insecure/pb/proto.d.ts.map +1 -0
- package/dist/src/insecure/pb/proto.js +53 -0
- package/dist/src/insecure/pb/proto.js.map +1 -0
- package/dist/src/keychain/cms.d.ts +6 -14
- package/dist/src/keychain/cms.d.ts.map +1 -1
- package/dist/src/keychain/cms.js +129 -0
- package/dist/src/keychain/cms.js.map +1 -0
- package/dist/src/keychain/index.d.ts +60 -81
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +492 -0
- package/dist/src/keychain/index.js.map +1 -0
- package/dist/src/keychain/util.d.ts +3 -6
- package/dist/src/keychain/util.d.ts.map +1 -1
- package/dist/src/keychain/util.js +79 -0
- package/dist/src/keychain/util.js.map +1 -0
- package/dist/src/libp2p.d.ts +80 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/dist/src/libp2p.js +397 -0
- package/dist/src/libp2p.js.map +1 -0
- package/dist/src/metrics/index.d.ts +47 -104
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/index.js +231 -0
- package/dist/src/metrics/index.js.map +1 -0
- package/dist/src/metrics/moving-average.d.ts +14 -0
- package/dist/src/metrics/moving-average.d.ts.map +1 -0
- package/dist/src/metrics/moving-average.js +40 -0
- package/dist/src/metrics/moving-average.js.map +1 -0
- package/dist/src/metrics/stats.d.ts +41 -93
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js +183 -0
- package/dist/src/metrics/stats.js.map +1 -0
- package/dist/src/nat-manager.d.ts +42 -99
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +114 -0
- package/dist/src/nat-manager.js.map +1 -0
- package/dist/src/peer-record-updater.d.ts +15 -0
- package/dist/src/peer-record-updater.d.ts.map +1 -0
- package/dist/src/peer-record-updater.js +44 -0
- package/dist/src/peer-record-updater.js.map +1 -0
- package/dist/src/peer-routing.d.ts +40 -84
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +108 -0
- package/dist/src/peer-routing.js.map +1 -0
- package/dist/src/ping/constants.d.ts +4 -4
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js +5 -0
- package/dist/src/ping/constants.js.map +1 -0
- package/dist/src/ping/index.d.ts +17 -31
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +57 -0
- package/dist/src/ping/index.js.map +1 -0
- package/dist/src/pnet/crypto.d.ts +15 -5
- package/dist/src/pnet/crypto.d.ts.map +1 -1
- package/dist/src/pnet/crypto.js +60 -0
- package/dist/src/pnet/crypto.js.map +1 -0
- package/dist/src/pnet/errors.d.ts +5 -5
- package/dist/src/pnet/errors.d.ts.map +1 -1
- package/dist/src/pnet/errors.js +6 -0
- package/dist/src/pnet/errors.js.map +1 -0
- package/dist/src/pnet/index.d.ts +12 -20
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +67 -0
- package/dist/src/pnet/index.js.map +1 -0
- package/dist/src/pnet/key-generator.d.ts +3 -7
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pnet/key-generator.js +25 -0
- package/dist/src/pnet/key-generator.js.map +1 -0
- package/dist/src/pubsub/dummy-pubsub.d.ts +17 -0
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
- package/dist/src/pubsub/dummy-pubsub.js +37 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
- package/dist/src/registrar.d.ts +26 -65
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +144 -0
- package/dist/src/registrar.js.map +1 -0
- package/dist/src/transport-manager.d.ts +36 -82
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +223 -0
- package/dist/src/transport-manager.js.map +1 -0
- package/dist/src/upgrader.d.ts +49 -129
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +402 -0
- package/dist/src/upgrader.js.map +1 -0
- package/dist/src/version.d.ts +3 -0
- package/dist/src/version.d.ts.map +1 -0
- package/dist/src/version.js +3 -0
- package/dist/src/version.js.map +1 -0
- package/package.json +144 -201
- package/src/address-manager/index.ts +130 -0
- package/src/circuit/README.md +16 -11
- package/src/circuit/auto-relay.ts +286 -0
- package/src/circuit/circuit/hop.ts +211 -0
- package/src/circuit/circuit/stop.ts +78 -0
- package/src/circuit/circuit/stream-handler.ts +86 -0
- package/src/circuit/circuit/{utils.js → utils.ts} +9 -25
- package/src/circuit/constants.ts +31 -0
- package/src/circuit/index.ts +120 -0
- package/src/circuit/listener.ts +82 -0
- package/src/circuit/multicodec.ts +2 -0
- package/src/circuit/{protocol → pb}/index.proto +1 -1
- package/src/circuit/pb/index.ts +117 -0
- package/src/circuit/transport.ts +219 -0
- package/src/circuit/utils.ts +12 -0
- package/src/config.ts +99 -0
- package/src/connection-manager/auto-dialler.ts +154 -0
- package/src/connection-manager/dialer/auto-dialer.ts +59 -0
- package/src/{dialer/dial-request.js → connection-manager/dialer/dial-request.ts} +60 -51
- package/src/connection-manager/dialer/index.ts +382 -0
- package/src/connection-manager/index.ts +508 -0
- package/src/connection-manager/latency-monitor.ts +319 -0
- package/src/connection-manager/{visibility-change-emitter.js → visibility-change-emitter.ts} +48 -38
- package/src/constants.ts +31 -0
- package/src/content-routing/index.ts +144 -0
- package/src/content-routing/utils.ts +54 -0
- package/src/dht/dht-content-routing.ts +43 -0
- package/src/dht/dht-peer-routing.ts +35 -0
- package/src/dht/dummy-dht.ts +60 -0
- package/src/errors.ts +73 -0
- package/src/fetch/README.md +1 -1
- package/src/fetch/constants.ts +3 -0
- package/src/fetch/index.ts +167 -0
- package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
- package/src/fetch/pb/proto.ts +65 -0
- package/src/get-peer.ts +57 -0
- package/src/identify/consts.ts +13 -0
- package/src/identify/index.ts +445 -0
- package/src/identify/{message.proto → pb/message.proto} +0 -0
- package/src/identify/pb/message.ts +37 -0
- package/src/index.ts +220 -0
- package/src/insecure/index.ts +101 -0
- package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
- package/src/insecure/pb/proto.ts +68 -0
- package/src/keychain/{cms.js → cms.ts} +62 -58
- package/src/keychain/index.ts +587 -0
- package/src/keychain/{util.js → util.ts} +7 -17
- package/src/libp2p.ts +538 -0
- package/src/metrics/index.ts +310 -0
- package/src/metrics/moving-average.ts +53 -0
- package/src/metrics/stats.ts +238 -0
- package/src/nat-manager.ts +194 -0
- package/src/peer-record-updater.ts +55 -0
- package/src/peer-routing.ts +186 -0
- package/src/ping/README.md +1 -1
- package/src/ping/constants.ts +5 -0
- package/src/ping/index.ts +84 -0
- package/src/pnet/README.md +2 -2
- package/src/pnet/crypto.ts +67 -0
- package/src/pnet/errors.ts +5 -0
- package/src/pnet/index.ts +97 -0
- package/src/pnet/key-generator.ts +28 -0
- package/src/pubsub/dummy-pubsub.ts +51 -0
- package/src/registrar.ts +189 -0
- package/src/transport-manager.ts +281 -0
- package/src/upgrader.ts +499 -0
- package/src/version.ts +3 -0
- package/dist/index.min.js +0 -55
- package/dist/src/circuit/protocol/index.d.ts +0 -173
- package/dist/src/dialer/dial-request.d.ts +0 -55
- package/dist/src/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/dialer/index.d.ts +0 -182
- package/dist/src/dialer/index.d.ts.map +0 -1
- package/dist/src/fetch/proto.d.ts +0 -134
- package/dist/src/identify/message.d.ts +0 -95
- package/dist/src/insecure/plaintext.d.ts +0 -12
- package/dist/src/insecure/plaintext.d.ts.map +0 -1
- package/dist/src/insecure/proto.d.ts +0 -128
- package/dist/src/metrics/old-peers.d.ts +0 -3
- package/dist/src/metrics/old-peers.d.ts.map +0 -1
- package/dist/src/metrics/tracked-map.d.ts +0 -8
- package/dist/src/metrics/tracked-map.d.ts.map +0 -1
- package/dist/src/peer-store/address-book.d.ts +0 -67
- package/dist/src/peer-store/address-book.d.ts.map +0 -1
- package/dist/src/peer-store/index.d.ts +0 -58
- package/dist/src/peer-store/index.d.ts.map +0 -1
- package/dist/src/peer-store/key-book.d.ts +0 -40
- package/dist/src/peer-store/key-book.d.ts.map +0 -1
- package/dist/src/peer-store/metadata-book.d.ts +0 -58
- package/dist/src/peer-store/metadata-book.d.ts.map +0 -1
- package/dist/src/peer-store/pb/peer.d.ts +0 -222
- package/dist/src/peer-store/proto-book.d.ts +0 -43
- package/dist/src/peer-store/proto-book.d.ts.map +0 -1
- package/dist/src/peer-store/store.d.ts +0 -73
- package/dist/src/peer-store/store.d.ts.map +0 -1
- package/dist/src/peer-store/types.d.ts +0 -202
- package/dist/src/peer-store/types.d.ts.map +0 -1
- package/dist/src/ping/util.d.ts +0 -5
- package/dist/src/ping/util.d.ts.map +0 -1
- package/dist/src/pubsub-adapter.d.ts +0 -22
- package/dist/src/pubsub-adapter.d.ts.map +0 -1
- package/dist/src/record/envelope/envelope.d.ts +0 -77
- package/dist/src/record/envelope/index.d.ts +0 -80
- package/dist/src/record/envelope/index.d.ts.map +0 -1
- package/dist/src/record/peer-record/consts.d.ts +0 -4
- package/dist/src/record/peer-record/consts.d.ts.map +0 -1
- package/dist/src/record/peer-record/index.d.ts +0 -60
- package/dist/src/record/peer-record/index.d.ts.map +0 -1
- package/dist/src/record/peer-record/peer-record.d.ts +0 -133
- package/dist/src/record/utils.d.ts +0 -12
- package/dist/src/record/utils.d.ts.map +0 -1
- package/dist/src/types.d.ts +0 -90
- package/dist/src/types.d.ts.map +0 -1
- package/src/address-manager/index.js +0 -96
- package/src/circuit/auto-relay.js +0 -302
- package/src/circuit/circuit/hop.js +0 -205
- package/src/circuit/circuit/stop.js +0 -81
- package/src/circuit/circuit/stream-handler.js +0 -94
- package/src/circuit/constants.js +0 -12
- package/src/circuit/index.js +0 -102
- package/src/circuit/listener.js +0 -75
- package/src/circuit/multicodec.js +0 -5
- package/src/circuit/protocol/index.d.ts +0 -173
- package/src/circuit/protocol/index.js +0 -530
- package/src/circuit/transport.js +0 -229
- package/src/circuit/utils.js +0 -17
- package/src/config.js +0 -114
- package/src/connection-manager/auto-dialler.js +0 -132
- package/src/connection-manager/index.js +0 -374
- package/src/connection-manager/latency-monitor.js +0 -264
- package/src/constants.js +0 -18
- package/src/content-routing/index.js +0 -163
- package/src/content-routing/utils.js +0 -89
- package/src/dht/dht-content-routing.js +0 -44
- package/src/dht/dht-peer-routing.js +0 -51
- package/src/dialer/index.js +0 -376
- package/src/errors.js +0 -66
- package/src/fetch/constants.js +0 -6
- package/src/fetch/index.js +0 -159
- package/src/fetch/proto.d.ts +0 -134
- package/src/fetch/proto.js +0 -333
- package/src/get-peer.js +0 -49
- package/src/identify/consts.js +0 -15
- package/src/identify/index.js +0 -384
- package/src/identify/message.d.ts +0 -95
- package/src/identify/message.js +0 -328
- package/src/index.js +0 -813
- package/src/insecure/plaintext.js +0 -95
- package/src/insecure/proto.d.ts +0 -128
- package/src/insecure/proto.js +0 -371
- package/src/keychain/index.js +0 -561
- package/src/metrics/index.js +0 -290
- package/src/metrics/old-peers.js +0 -16
- package/src/metrics/stats.js +0 -270
- package/src/metrics/tracked-map.js +0 -94
- package/src/nat-manager.js +0 -197
- package/src/peer-routing.js +0 -176
- package/src/peer-store/README.md +0 -145
- package/src/peer-store/address-book.js +0 -382
- package/src/peer-store/index.js +0 -121
- package/src/peer-store/key-book.js +0 -141
- package/src/peer-store/metadata-book.js +0 -250
- package/src/peer-store/pb/peer.d.ts +0 -222
- package/src/peer-store/pb/peer.js +0 -643
- package/src/peer-store/pb/peer.proto +0 -31
- package/src/peer-store/proto-book.js +0 -237
- package/src/peer-store/store.js +0 -263
- package/src/peer-store/types.ts +0 -245
- package/src/ping/constants.js +0 -8
- package/src/ping/index.js +0 -84
- package/src/ping/util.js +0 -18
- package/src/pnet/crypto.js +0 -84
- package/src/pnet/errors.js +0 -7
- package/src/pnet/index.js +0 -86
- package/src/pnet/key-generator.js +0 -33
- package/src/pubsub-adapter.js +0 -61
- package/src/record/README.md +0 -130
- package/src/record/envelope/envelope.d.ts +0 -77
- package/src/record/envelope/envelope.js +0 -243
- package/src/record/envelope/envelope.proto +0 -19
- package/src/record/envelope/index.js +0 -183
- package/src/record/peer-record/consts.js +0 -14
- package/src/record/peer-record/index.js +0 -113
- package/src/record/peer-record/peer-record.d.ts +0 -133
- package/src/record/peer-record/peer-record.js +0 -367
- package/src/record/peer-record/peer-record.proto +0 -18
- package/src/record/utils.js +0 -25
- package/src/registrar.js +0 -127
- package/src/transport-manager.js +0 -269
- package/src/types.ts +0 -98
- package/src/upgrader.js +0 -492
package/src/libp2p.ts
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
3
|
+
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
|
|
4
|
+
import { Startable, isStartable } from '@libp2p/interfaces/startable'
|
|
5
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
6
|
+
import { MemoryDatastore } from 'datastore-core/memory'
|
|
7
|
+
import { DefaultPeerRouting } from './peer-routing.js'
|
|
8
|
+
import { CompoundContentRouting } from './content-routing/index.js'
|
|
9
|
+
import { getPeer } from './get-peer.js'
|
|
10
|
+
import { codes } from './errors.js'
|
|
11
|
+
import { DefaultAddressManager } from './address-manager/index.js'
|
|
12
|
+
import { DefaultConnectionManager } from './connection-manager/index.js'
|
|
13
|
+
import { AutoDialler } from './connection-manager/auto-dialler.js'
|
|
14
|
+
import { Circuit } from './circuit/transport.js'
|
|
15
|
+
import { Relay } from './circuit/index.js'
|
|
16
|
+
import { KeyChain } from './keychain/index.js'
|
|
17
|
+
import { DefaultMetrics } from './metrics/index.js'
|
|
18
|
+
import { DefaultTransportManager } from './transport-manager.js'
|
|
19
|
+
import { DefaultUpgrader } from './upgrader.js'
|
|
20
|
+
import { DefaultRegistrar } from './registrar.js'
|
|
21
|
+
import { IdentifyService } from './identify/index.js'
|
|
22
|
+
import { FetchService } from './fetch/index.js'
|
|
23
|
+
import { PingService } from './ping/index.js'
|
|
24
|
+
import { NatManager } from './nat-manager.js'
|
|
25
|
+
import { PeerRecordUpdater } from './peer-record-updater.js'
|
|
26
|
+
import { DHTPeerRouting } from './dht/dht-peer-routing.js'
|
|
27
|
+
import { PersistentPeerStore } from '@libp2p/peer-store'
|
|
28
|
+
import { DHTContentRouting } from './dht/dht-content-routing.js'
|
|
29
|
+
import { AutoDialer } from './connection-manager/dialer/auto-dialer.js'
|
|
30
|
+
import { Initializable, Components, isInitializable } from '@libp2p/interfaces/components'
|
|
31
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
32
|
+
import type { Connection } from '@libp2p/interfaces/connection'
|
|
33
|
+
import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
|
|
34
|
+
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
|
|
35
|
+
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
36
|
+
import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
37
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
38
|
+
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
39
|
+
import type { Libp2p, Libp2pEvents, Libp2pInit, Libp2pOptions } from './index.js'
|
|
40
|
+
import { validateConfig } from './config.js'
|
|
41
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
42
|
+
import type { PeerStore } from '@libp2p/interfaces/peer-store'
|
|
43
|
+
import type { DualDHT } from '@libp2p/interfaces/dht'
|
|
44
|
+
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
45
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
46
|
+
import errCode from 'err-code'
|
|
47
|
+
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
48
|
+
import type { Metrics } from '@libp2p/interfaces/metrics'
|
|
49
|
+
import { DummyDHT } from './dht/dummy-dht.js'
|
|
50
|
+
import { DummyPubSub } from './pubsub/dummy-pubsub.js'
|
|
51
|
+
import { PeerSet } from '@libp2p/peer-collections'
|
|
52
|
+
|
|
53
|
+
const log = logger('libp2p')
|
|
54
|
+
|
|
55
|
+
export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
56
|
+
public peerId: PeerId
|
|
57
|
+
public dht: DualDHT
|
|
58
|
+
public pubsub: PubSub
|
|
59
|
+
public identifyService?: IdentifyService
|
|
60
|
+
public fetchService: FetchService
|
|
61
|
+
public pingService: PingService
|
|
62
|
+
public components: Components
|
|
63
|
+
public peerStore: PeerStore
|
|
64
|
+
public contentRouting: ContentRouting
|
|
65
|
+
public peerRouting: PeerRouting
|
|
66
|
+
public keychain: KeyChain
|
|
67
|
+
public connectionManager: ConnectionManager
|
|
68
|
+
public registrar: Registrar
|
|
69
|
+
public metrics?: Metrics
|
|
70
|
+
|
|
71
|
+
private started: boolean
|
|
72
|
+
private readonly services: Startable[]
|
|
73
|
+
private readonly initializables: Initializable[]
|
|
74
|
+
|
|
75
|
+
constructor (init: Libp2pInit) {
|
|
76
|
+
super()
|
|
77
|
+
|
|
78
|
+
this.initializables = []
|
|
79
|
+
this.started = false
|
|
80
|
+
this.peerId = init.peerId
|
|
81
|
+
this.components = new Components({
|
|
82
|
+
peerId: init.peerId,
|
|
83
|
+
datastore: init.datastore ?? new MemoryDatastore(),
|
|
84
|
+
connectionGater: {
|
|
85
|
+
denyDialPeer: async () => await Promise.resolve(false),
|
|
86
|
+
denyDialMultiaddr: async () => await Promise.resolve(false),
|
|
87
|
+
denyInboundConnection: async () => await Promise.resolve(false),
|
|
88
|
+
denyOutboundConnection: async () => await Promise.resolve(false),
|
|
89
|
+
denyInboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
90
|
+
denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
91
|
+
denyInboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
92
|
+
denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
93
|
+
filterMultiaddrForPeer: async () => await Promise.resolve(true),
|
|
94
|
+
...init.connectionGater
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
this.components.setPeerStore(new PersistentPeerStore({
|
|
98
|
+
addressFilter: this.components.getConnectionGater().filterMultiaddrForPeer,
|
|
99
|
+
...init.peerStore
|
|
100
|
+
}))
|
|
101
|
+
|
|
102
|
+
this.services = [
|
|
103
|
+
this.components
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
// Create Metrics
|
|
107
|
+
if (init.metrics.enabled) {
|
|
108
|
+
this.metrics = this.components.setMetrics(new DefaultMetrics(init.metrics))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this.peerStore = this.components.getPeerStore()
|
|
112
|
+
|
|
113
|
+
this.peerStore.addEventListener('peer', evt => {
|
|
114
|
+
const { detail: peerData } = evt
|
|
115
|
+
|
|
116
|
+
this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', { detail: peerData }))
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
// Set up connection protector if configured
|
|
120
|
+
if (init.connectionProtector != null) {
|
|
121
|
+
this.components.setConnectionProtector(init.connectionProtector)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Set up the Upgrader
|
|
125
|
+
this.components.setUpgrader(new DefaultUpgrader(this.components, {
|
|
126
|
+
connectionEncryption: (init.connectionEncryption ?? []).map(component => this.configureComponent(component)),
|
|
127
|
+
muxers: (init.streamMuxers ?? []).map(component => this.configureComponent(component))
|
|
128
|
+
}))
|
|
129
|
+
|
|
130
|
+
// Create the Connection Manager
|
|
131
|
+
this.connectionManager = this.components.setConnectionManager(new DefaultConnectionManager(init.connectionManager))
|
|
132
|
+
|
|
133
|
+
// Create the Registrar
|
|
134
|
+
this.registrar = this.components.setRegistrar(new DefaultRegistrar(this.components))
|
|
135
|
+
|
|
136
|
+
// Setup the transport manager
|
|
137
|
+
this.components.setTransportManager(new DefaultTransportManager(this.components, init.transportManager))
|
|
138
|
+
|
|
139
|
+
// Addresses {listen, announce, noAnnounce}
|
|
140
|
+
this.components.setAddressManager(new DefaultAddressManager(this.components, init.addresses))
|
|
141
|
+
|
|
142
|
+
// update our peer record when addresses change
|
|
143
|
+
this.configureComponent(new PeerRecordUpdater(this.components))
|
|
144
|
+
|
|
145
|
+
this.configureComponent(new AutoDialler(this.components, {
|
|
146
|
+
enabled: init.connectionManager.autoDial,
|
|
147
|
+
minConnections: init.connectionManager.minConnections,
|
|
148
|
+
autoDialInterval: init.connectionManager.autoDialInterval
|
|
149
|
+
}))
|
|
150
|
+
|
|
151
|
+
// Create keychain
|
|
152
|
+
const keychainOpts = KeyChain.generateOptions()
|
|
153
|
+
this.keychain = this.configureComponent(new KeyChain(this.components, {
|
|
154
|
+
...keychainOpts,
|
|
155
|
+
...init.keychain
|
|
156
|
+
}))
|
|
157
|
+
|
|
158
|
+
// Create the Nat Manager
|
|
159
|
+
this.services.push(new NatManager(this.components, init.nat))
|
|
160
|
+
|
|
161
|
+
init.transports.forEach((transport) => {
|
|
162
|
+
this.components.getTransportManager().add(this.configureComponent(transport))
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
// Attach stream multiplexers
|
|
166
|
+
if (init.streamMuxers != null && init.streamMuxers.length > 0) {
|
|
167
|
+
// Add the identify service since we can multiplex
|
|
168
|
+
this.identifyService = new IdentifyService(this.components, {
|
|
169
|
+
protocolPrefix: init.protocolPrefix,
|
|
170
|
+
host: {
|
|
171
|
+
agentVersion: init.host.agentVersion
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
this.configureComponent(this.identifyService)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// dht provided components (peerRouting, contentRouting, dht)
|
|
178
|
+
if (init.dht != null) {
|
|
179
|
+
this.dht = this.components.setDHT(init.dht)
|
|
180
|
+
} else {
|
|
181
|
+
this.dht = new DummyDHT()
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Create pubsub if provided
|
|
185
|
+
if (init.pubsub != null) {
|
|
186
|
+
this.pubsub = this.components.setPubSub(init.pubsub)
|
|
187
|
+
} else {
|
|
188
|
+
this.pubsub = new DummyPubSub()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Attach remaining APIs
|
|
192
|
+
// peer and content routing will automatically get modules from _modules and _dht
|
|
193
|
+
|
|
194
|
+
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(component => this.configureComponent(component))
|
|
195
|
+
|
|
196
|
+
if (init.dht != null) {
|
|
197
|
+
// add dht to routers
|
|
198
|
+
peerRouters.push(this.configureComponent(new DHTPeerRouting(this.dht)))
|
|
199
|
+
|
|
200
|
+
// use dht for peer discovery
|
|
201
|
+
this.dht.addEventListener('peer', (evt) => {
|
|
202
|
+
this.onDiscoveryPeer(evt)
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
this.peerRouting = this.components.setPeerRouting(this.configureComponent(new DefaultPeerRouting(this.components, {
|
|
207
|
+
...init.peerRouting,
|
|
208
|
+
routers: peerRouters
|
|
209
|
+
})))
|
|
210
|
+
|
|
211
|
+
const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map(component => this.configureComponent(component))
|
|
212
|
+
|
|
213
|
+
if (init.dht != null) {
|
|
214
|
+
// add dht to routers
|
|
215
|
+
contentRouters.push(this.configureComponent(new DHTContentRouting(this.dht)))
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this.contentRouting = this.components.setContentRouting(this.configureComponent(new CompoundContentRouting(this.components, {
|
|
219
|
+
routers: contentRouters
|
|
220
|
+
})))
|
|
221
|
+
|
|
222
|
+
if (init.relay.enabled) {
|
|
223
|
+
this.components.getTransportManager().add(this.configureComponent(new Circuit()))
|
|
224
|
+
|
|
225
|
+
this.configureComponent(new Relay(this.components, {
|
|
226
|
+
addressSorter: init.connectionManager.addressSorter,
|
|
227
|
+
...init.relay
|
|
228
|
+
}))
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
this.fetchService = this.configureComponent(new FetchService(this.components, {
|
|
232
|
+
protocolPrefix: init.protocolPrefix
|
|
233
|
+
}))
|
|
234
|
+
|
|
235
|
+
this.pingService = this.configureComponent(new PingService(this.components, {
|
|
236
|
+
protocolPrefix: init.protocolPrefix
|
|
237
|
+
}))
|
|
238
|
+
|
|
239
|
+
const autoDialer = this.configureComponent(new AutoDialer(this.components, {
|
|
240
|
+
enabled: init.connectionManager.autoDial !== false,
|
|
241
|
+
minConnections: init.connectionManager.minConnections,
|
|
242
|
+
dialTimeout: init.connectionManager.dialTimeout ?? 30000
|
|
243
|
+
}))
|
|
244
|
+
|
|
245
|
+
this.addEventListener('peer:discovery', evt => {
|
|
246
|
+
if (!this.isStarted()) {
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
autoDialer.handle(evt)
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
// Discovery modules
|
|
254
|
+
for (const service of init.peerDiscovery ?? []) {
|
|
255
|
+
this.configureComponent(service)
|
|
256
|
+
|
|
257
|
+
service.addEventListener('peer', (evt) => {
|
|
258
|
+
this.onDiscoveryPeer(evt)
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private configureComponent <T> (component: T): T {
|
|
264
|
+
if (isStartable(component)) {
|
|
265
|
+
this.services.push(component)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (isInitializable(component)) {
|
|
269
|
+
this.initializables.push(component)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return component
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Starts the libp2p node and all its subsystems
|
|
277
|
+
*/
|
|
278
|
+
async start () {
|
|
279
|
+
if (this.started) {
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
this.started = true
|
|
284
|
+
|
|
285
|
+
log('libp2p is starting')
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
// Set available components on all modules interested in components
|
|
289
|
+
this.initializables.forEach(obj => {
|
|
290
|
+
obj.init(this.components)
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
await Promise.all(
|
|
294
|
+
this.services.map(async service => {
|
|
295
|
+
if (service.beforeStart != null) {
|
|
296
|
+
await service.beforeStart()
|
|
297
|
+
}
|
|
298
|
+
})
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
// start any startables
|
|
302
|
+
await Promise.all(
|
|
303
|
+
this.services.map(service => service.start())
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
await Promise.all(
|
|
307
|
+
this.services.map(async service => {
|
|
308
|
+
if (service.afterStart != null) {
|
|
309
|
+
await service.afterStart()
|
|
310
|
+
}
|
|
311
|
+
})
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
log('libp2p has started')
|
|
315
|
+
|
|
316
|
+
// Once we start, emit any peers we may have already discovered
|
|
317
|
+
// TODO: this should be removed, as we already discovered these peers in the past
|
|
318
|
+
await this.components.getPeerStore().forEach(peer => {
|
|
319
|
+
this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', {
|
|
320
|
+
detail: {
|
|
321
|
+
id: peer.id,
|
|
322
|
+
multiaddrs: peer.addresses.map(addr => addr.multiaddr),
|
|
323
|
+
protocols: peer.protocols
|
|
324
|
+
}
|
|
325
|
+
}))
|
|
326
|
+
})
|
|
327
|
+
} catch (err: any) {
|
|
328
|
+
log.error('An error occurred starting libp2p', err)
|
|
329
|
+
await this.stop()
|
|
330
|
+
throw err
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Stop the libp2p node by closing its listeners and open connections
|
|
336
|
+
*/
|
|
337
|
+
async stop () {
|
|
338
|
+
if (!this.started) {
|
|
339
|
+
return
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
log('libp2p is stopping')
|
|
343
|
+
|
|
344
|
+
this.started = false
|
|
345
|
+
|
|
346
|
+
await Promise.all(
|
|
347
|
+
this.services.map(async service => {
|
|
348
|
+
if (service.beforeStop != null) {
|
|
349
|
+
await service.beforeStop()
|
|
350
|
+
}
|
|
351
|
+
})
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
await Promise.all(
|
|
355
|
+
this.services.map(servce => servce.stop())
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
await Promise.all(
|
|
359
|
+
this.services.map(async service => {
|
|
360
|
+
if (service.afterStop != null) {
|
|
361
|
+
await service.afterStop()
|
|
362
|
+
}
|
|
363
|
+
})
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
log('libp2p has stopped')
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Load keychain keys from the datastore.
|
|
371
|
+
* Imports the private key as 'self', if needed.
|
|
372
|
+
*/
|
|
373
|
+
async loadKeychain () {
|
|
374
|
+
if (this.keychain == null) {
|
|
375
|
+
return
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
try {
|
|
379
|
+
await this.keychain.findKeyByName('self')
|
|
380
|
+
} catch (err: any) {
|
|
381
|
+
await this.keychain.importPeer('self', this.peerId)
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
isStarted () {
|
|
386
|
+
return this.started
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
getConnections (peerId?: PeerId): Connection[] {
|
|
390
|
+
return this.components.getConnectionManager().getConnections(peerId)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
getPeers (): PeerId[] {
|
|
394
|
+
const peerSet = new PeerSet()
|
|
395
|
+
|
|
396
|
+
for (const conn of this.components.getConnectionManager().getConnections()) {
|
|
397
|
+
peerSet.add(conn.remotePeer)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return Array.from(peerSet)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
async dial (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
|
404
|
+
const { id, multiaddrs } = getPeer(peer)
|
|
405
|
+
|
|
406
|
+
await this.components.getPeerStore().addressBook.add(id, multiaddrs)
|
|
407
|
+
|
|
408
|
+
return await this.components.getConnectionManager().openConnection(id, options)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
|
|
412
|
+
if (protocols == null) {
|
|
413
|
+
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
417
|
+
|
|
418
|
+
if (protocols.length === 0) {
|
|
419
|
+
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const connection = await this.dial(peer)
|
|
423
|
+
|
|
424
|
+
return await connection.newStream(protocols)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
getMultiaddrs (): Multiaddr[] {
|
|
428
|
+
return this.components.getAddressManager().getAddresses()
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async hangUp (peer: PeerId | Multiaddr | string): Promise<void> {
|
|
432
|
+
const { id } = getPeer(peer)
|
|
433
|
+
|
|
434
|
+
await this.components.getConnectionManager().closeConnections(id)
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Get the public key for the given peer id
|
|
439
|
+
*/
|
|
440
|
+
async getPublicKey (peer: PeerId, options: AbortOptions = {}): Promise<Uint8Array> {
|
|
441
|
+
log('getPublicKey %p', peer)
|
|
442
|
+
|
|
443
|
+
if (peer.publicKey != null) {
|
|
444
|
+
return peer.publicKey
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const peerInfo = await this.peerStore.get(peer)
|
|
448
|
+
|
|
449
|
+
if (peerInfo.pubKey != null) {
|
|
450
|
+
return peerInfo.pubKey
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (this.dht == null) {
|
|
454
|
+
throw errCode(new Error('Public key was not in the peer store and the DHT is not enabled'), codes.ERR_NO_ROUTERS_AVAILABLE)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const peerKey = uint8ArrayConcat([
|
|
458
|
+
uint8ArrayFromString('/pk/'),
|
|
459
|
+
peer.multihash.digest
|
|
460
|
+
])
|
|
461
|
+
|
|
462
|
+
// search the dht
|
|
463
|
+
for await (const event of this.dht.get(peerKey, options)) {
|
|
464
|
+
if (event.name === 'VALUE') {
|
|
465
|
+
const key = unmarshalPublicKey(event.value)
|
|
466
|
+
|
|
467
|
+
await this.peerStore.keyBook.set(peer, event.value)
|
|
468
|
+
|
|
469
|
+
return key.bytes
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
throw errCode(new Error(`Node not responding with its public key: ${peer.toString()}`), codes.ERR_INVALID_RECORD)
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async fetch (peer: PeerId | Multiaddr | string, key: string): Promise<Uint8Array | null> {
|
|
477
|
+
const { id, multiaddrs } = getPeer(peer)
|
|
478
|
+
|
|
479
|
+
if (multiaddrs != null) {
|
|
480
|
+
await this.components.getPeerStore().addressBook.add(id, multiaddrs)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return await this.fetchService.fetch(id, key)
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
async ping (peer: PeerId | Multiaddr | string): Promise<number> {
|
|
487
|
+
const { id, multiaddrs } = getPeer(peer)
|
|
488
|
+
|
|
489
|
+
if (multiaddrs.length > 0) {
|
|
490
|
+
await this.components.getPeerStore().addressBook.add(id, multiaddrs)
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return await this.pingService.ping(id)
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
async handle (protocols: string | string[], handler: StreamHandler): Promise<void> {
|
|
497
|
+
return await this.components.getRegistrar().handle(protocols, handler)
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
async unhandle (protocols: string[] | string): Promise<void> {
|
|
501
|
+
return await this.components.getRegistrar().unhandle(protocols)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Called whenever peer discovery services emit `peer` events.
|
|
506
|
+
* Known peers may be emitted.
|
|
507
|
+
*/
|
|
508
|
+
onDiscoveryPeer (evt: CustomEvent<PeerInfo>) {
|
|
509
|
+
const { detail: peer } = evt
|
|
510
|
+
|
|
511
|
+
if (peer.id.toString() === this.peerId.toString()) {
|
|
512
|
+
log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
|
513
|
+
return
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (peer.multiaddrs.length > 0) {
|
|
517
|
+
void this.components.getPeerStore().addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (peer.protocols.length > 0) {
|
|
521
|
+
void this.components.getPeerStore().protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', { detail: peer }))
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Returns a new Libp2pNode instance - this exposes more of the internals than the
|
|
530
|
+
* libp2p interface and is useful for testing and debugging.
|
|
531
|
+
*/
|
|
532
|
+
export async function createLibp2pNode (options: Libp2pOptions): Promise<Libp2pNode> {
|
|
533
|
+
if (options.peerId == null) {
|
|
534
|
+
options.peerId = await createEd25519PeerId()
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return new Libp2pNode(validateConfig(options))
|
|
538
|
+
}
|