libp2p 0.36.0 → 0.36.2-199395d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -21
- package/README.md +13 -10
- package/dist/src/address-manager/index.d.ts +33 -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 +215 -0
- package/dist/src/circuit/auto-relay.js.map +1 -0
- package/dist/src/circuit/circuit/hop.d.ts +25 -52
- 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 +16 -8
- 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 +16 -35
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +60 -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 +54 -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/{protocol → pb}/index.d.ts +0 -0
- package/{src/circuit/protocol → dist/src/circuit/pb}/index.js +7 -9
- 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 +190 -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 +93 -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/index.d.ts +79 -144
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +295 -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 +26 -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/dialer/auto-dialer.d.ts +14 -0
- package/dist/src/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/dialer/auto-dialer.js +24 -0
- package/dist/src/dialer/auto-dialer.js.map +1 -0
- package/dist/src/dialer/dial-request.d.ts +18 -46
- package/dist/src/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/dialer/dial-request.js +83 -0
- package/dist/src/dialer/dial-request.js.map +1 -0
- package/dist/src/dialer/index.d.ts +41 -148
- package/dist/src/dialer/index.d.ts.map +1 -1
- package/dist/src/dialer/index.js +281 -0
- package/dist/src/dialer/index.js.map +1 -0
- package/dist/src/errors.d.ts +68 -63
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +73 -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 +130 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
- package/{src/fetch → dist/src/fetch/pb}/proto.js +7 -9
- 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 +40 -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/{src/identify → dist/src/identify/pb}/message.d.ts +20 -5
- package/{src/identify → dist/src/identify/pb}/message.js +83 -42
- package/dist/src/index.d.ts +157 -583
- 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 +78 -0
- package/dist/src/insecure/index.js.map +1 -0
- package/dist/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
- package/{src/insecure → dist/src/insecure/pb}/proto.js +39 -22
- 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 +62 -82
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +493 -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 +75 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/dist/src/libp2p.js +368 -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 +45 -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 +39 -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 +26 -33
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +56 -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 +11 -20
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +66 -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/registrar.d.ts +27 -65
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +159 -0
- package/dist/src/registrar.js.map +1 -0
- package/dist/src/transport-manager.d.ts +34 -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 +145 -197
- package/src/address-manager/index.ts +129 -0
- package/src/circuit/README.md +16 -11
- package/src/circuit/auto-relay.ts +284 -0
- package/src/circuit/circuit/hop.ts +211 -0
- package/src/circuit/circuit/stop.ts +78 -0
- package/src/circuit/circuit/stream-handler.ts +87 -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.js → listener.ts} +21 -27
- package/src/circuit/multicodec.ts +2 -0
- package/src/circuit/{protocol → pb}/index.d.ts +0 -0
- package/src/circuit/pb/index.js +528 -0
- package/src/circuit/{protocol → pb}/index.proto +0 -0
- package/src/circuit/transport.ts +216 -0
- package/src/circuit/utils.ts +12 -0
- package/src/config.ts +101 -0
- package/src/connection-manager/auto-dialler.ts +154 -0
- package/src/connection-manager/index.ts +422 -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 +143 -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/dialer/auto-dialer.ts +40 -0
- package/src/dialer/{dial-request.js → dial-request.ts} +60 -51
- package/src/dialer/index.ts +374 -0
- package/src/errors.ts +71 -0
- package/src/fetch/README.md +1 -1
- package/src/fetch/constants.ts +3 -0
- package/src/fetch/{index.js → index.ts} +79 -70
- package/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
- package/src/fetch/pb/proto.js +331 -0
- package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
- package/src/get-peer.ts +57 -0
- package/src/identify/consts.ts +13 -0
- package/src/identify/index.ts +445 -0
- package/{dist/src/identify → src/identify/pb}/message.d.ts +20 -5
- package/src/identify/pb/message.js +369 -0
- package/src/identify/{message.proto → pb/message.proto} +0 -0
- package/src/index.ts +234 -0
- package/src/insecure/index.ts +97 -0
- package/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
- package/src/insecure/pb/proto.js +388 -0
- package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
- package/src/keychain/{cms.js → cms.ts} +62 -58
- package/src/keychain/index.ts +588 -0
- package/src/keychain/{util.js → util.ts} +7 -17
- package/src/libp2p.ts +501 -0
- package/src/metrics/index.ts +310 -0
- package/src/metrics/moving-average.ts +53 -0
- package/src/metrics/stats.ts +243 -0
- package/src/nat-manager.ts +194 -0
- package/src/peer-record-updater.ts +55 -0
- package/src/peer-routing.ts +185 -0
- package/src/ping/README.md +1 -1
- package/src/ping/constants.ts +5 -0
- package/src/ping/index.ts +83 -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 +95 -0
- package/src/pnet/key-generator.ts +28 -0
- package/src/registrar.ts +205 -0
- package/src/transport-manager.ts +279 -0
- package/src/upgrader.ts +499 -0
- package/src/version.ts +3 -0
- package/dist/index.min.js +0 -55
- package/dist/src/insecure/plaintext.d.ts +0 -12
- package/dist/src/insecure/plaintext.d.ts.map +0 -1
- 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/multicodec.js +0 -5
- 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 -363
- 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/get-peer.js +0 -49
- package/src/identify/consts.js +0 -15
- package/src/identify/index.js +0 -384
- package/src/index.js +0 -811
- package/src/insecure/plaintext.js +0 -95
- 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 -85
- 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/identify/index.js
DELETED
|
@@ -1,384 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')
|
|
4
|
-
const log = Object.assign(debug('libp2p:identify'), {
|
|
5
|
-
error: debug('libp2p:identify:err')
|
|
6
|
-
})
|
|
7
|
-
const errCode = require('err-code')
|
|
8
|
-
const lp = require('it-length-prefixed')
|
|
9
|
-
const { pipe } = require('it-pipe')
|
|
10
|
-
const { collect, take, consume } = require('streaming-iterables')
|
|
11
|
-
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
|
|
12
|
-
|
|
13
|
-
const PeerId = require('peer-id')
|
|
14
|
-
const { Multiaddr } = require('multiaddr')
|
|
15
|
-
// @ts-ignore it-buffer does not have types
|
|
16
|
-
const { toBuffer } = require('it-buffer')
|
|
17
|
-
|
|
18
|
-
const Message = require('./message')
|
|
19
|
-
|
|
20
|
-
const Envelope = require('../record/envelope')
|
|
21
|
-
const PeerRecord = require('../record/peer-record')
|
|
22
|
-
|
|
23
|
-
const {
|
|
24
|
-
MULTICODEC_IDENTIFY,
|
|
25
|
-
MULTICODEC_IDENTIFY_PUSH,
|
|
26
|
-
IDENTIFY_PROTOCOL_VERSION,
|
|
27
|
-
MULTICODEC_IDENTIFY_PROTOCOL_NAME,
|
|
28
|
-
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME,
|
|
29
|
-
MULTICODEC_IDENTIFY_PROTOCOL_VERSION,
|
|
30
|
-
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
|
|
31
|
-
} = require('./consts')
|
|
32
|
-
|
|
33
|
-
const { codes } = require('../errors')
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
37
|
-
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @typedef {Object} HostProperties
|
|
42
|
-
* @property {string} agentVersion
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
class IdentifyService {
|
|
46
|
-
/**
|
|
47
|
-
* @param {import('../')} libp2p
|
|
48
|
-
*/
|
|
49
|
-
static getProtocolStr (libp2p) {
|
|
50
|
-
return {
|
|
51
|
-
identifyProtocolStr: `/${libp2p._config.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`,
|
|
52
|
-
identifyPushProtocolStr: `/${libp2p._config.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @class
|
|
58
|
-
* @param {Object} options
|
|
59
|
-
* @param {import('../')} options.libp2p
|
|
60
|
-
*/
|
|
61
|
-
constructor ({ libp2p }) {
|
|
62
|
-
this._libp2p = libp2p
|
|
63
|
-
this.peerStore = libp2p.peerStore
|
|
64
|
-
this.addressManager = libp2p.addressManager
|
|
65
|
-
this.connectionManager = libp2p.connectionManager
|
|
66
|
-
this.peerId = libp2p.peerId
|
|
67
|
-
|
|
68
|
-
this.handleMessage = this.handleMessage.bind(this)
|
|
69
|
-
|
|
70
|
-
const protocolStr = IdentifyService.getProtocolStr(libp2p)
|
|
71
|
-
this.identifyProtocolStr = protocolStr.identifyProtocolStr
|
|
72
|
-
this.identifyPushProtocolStr = protocolStr.identifyPushProtocolStr
|
|
73
|
-
|
|
74
|
-
// Store self host metadata
|
|
75
|
-
this._host = {
|
|
76
|
-
protocolVersion: `${libp2p._config.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
|
|
77
|
-
...libp2p._options.host
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// When a new connection happens, trigger identify
|
|
81
|
-
this.connectionManager.on('peer:connect', (connection) => {
|
|
82
|
-
this.identify(connection).catch(log.error)
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
// When self multiaddrs change, trigger identify-push
|
|
86
|
-
this.peerStore.on('change:multiaddrs', ({ peerId }) => {
|
|
87
|
-
if (peerId.toString() === this.peerId.toString()) {
|
|
88
|
-
this.pushToPeerStore().catch(err => log.error(err))
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
// When self protocols change, trigger identify-push
|
|
93
|
-
this.peerStore.on('change:protocols', ({ peerId }) => {
|
|
94
|
-
if (peerId.toString() === this.peerId.toString()) {
|
|
95
|
-
this.pushToPeerStore().catch(err => log.error(err))
|
|
96
|
-
}
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async start () {
|
|
101
|
-
await this.peerStore.metadataBook.setValue(this.peerId, 'AgentVersion', uint8ArrayFromString(this._host.agentVersion))
|
|
102
|
-
await this.peerStore.metadataBook.setValue(this.peerId, 'ProtocolVersion', uint8ArrayFromString(this._host.protocolVersion))
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async stop () {
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Send an Identify Push update to the list of connections
|
|
111
|
-
*
|
|
112
|
-
* @param {Connection[]} connections
|
|
113
|
-
* @returns {Promise<void[]>}
|
|
114
|
-
*/
|
|
115
|
-
async push (connections) {
|
|
116
|
-
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
|
117
|
-
const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
|
|
118
|
-
const protocols = await this.peerStore.protoBook.get(this.peerId)
|
|
119
|
-
|
|
120
|
-
const pushes = connections.map(async connection => {
|
|
121
|
-
try {
|
|
122
|
-
const { stream } = await connection.newStream(this.identifyPushProtocolStr)
|
|
123
|
-
|
|
124
|
-
await pipe(
|
|
125
|
-
[Message.Identify.encode({
|
|
126
|
-
listenAddrs,
|
|
127
|
-
signedPeerRecord,
|
|
128
|
-
protocols
|
|
129
|
-
}).finish()],
|
|
130
|
-
lp.encode(),
|
|
131
|
-
stream,
|
|
132
|
-
consume
|
|
133
|
-
)
|
|
134
|
-
} catch (/** @type {any} */ err) {
|
|
135
|
-
// Just log errors
|
|
136
|
-
log.error('could not push identify update to peer', err)
|
|
137
|
-
}
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
return Promise.all(pushes)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Calls `push` for all peers in the `peerStore` that are connected
|
|
145
|
-
*/
|
|
146
|
-
async pushToPeerStore () {
|
|
147
|
-
// Do not try to push if libp2p node is not running
|
|
148
|
-
if (!this._libp2p.isStarted()) {
|
|
149
|
-
return
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const connections = []
|
|
153
|
-
let connection
|
|
154
|
-
for await (const peer of this.peerStore.getPeers()) {
|
|
155
|
-
if (peer.protocols.includes(this.identifyPushProtocolStr) && (connection = this.connectionManager.get(peer.id))) {
|
|
156
|
-
connections.push(connection)
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
await this.push(connections)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Requests the `Identify` message from peer associated with the given `connection`.
|
|
165
|
-
* If the identified peer does not match the `PeerId` associated with the connection,
|
|
166
|
-
* an error will be thrown.
|
|
167
|
-
*
|
|
168
|
-
* @async
|
|
169
|
-
* @param {Connection} connection
|
|
170
|
-
* @returns {Promise<void>}
|
|
171
|
-
*/
|
|
172
|
-
async identify (connection) {
|
|
173
|
-
const { stream } = await connection.newStream(this.identifyProtocolStr)
|
|
174
|
-
const [data] = await pipe(
|
|
175
|
-
[],
|
|
176
|
-
stream,
|
|
177
|
-
lp.decode(),
|
|
178
|
-
take(1),
|
|
179
|
-
toBuffer,
|
|
180
|
-
collect
|
|
181
|
-
)
|
|
182
|
-
|
|
183
|
-
if (!data) {
|
|
184
|
-
throw errCode(new Error('No data could be retrieved'), codes.ERR_CONNECTION_ENDED)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
let message
|
|
188
|
-
try {
|
|
189
|
-
message = Message.Identify.decode(data)
|
|
190
|
-
} catch (/** @type {any} */ err) {
|
|
191
|
-
throw errCode(err, codes.ERR_INVALID_MESSAGE)
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const {
|
|
195
|
-
publicKey,
|
|
196
|
-
listenAddrs,
|
|
197
|
-
protocols,
|
|
198
|
-
observedAddr,
|
|
199
|
-
signedPeerRecord
|
|
200
|
-
} = message
|
|
201
|
-
|
|
202
|
-
const id = await PeerId.createFromPubKey(publicKey)
|
|
203
|
-
|
|
204
|
-
if (connection.remotePeer.toB58String() !== id.toB58String()) {
|
|
205
|
-
throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Get the observedAddr if there is one
|
|
209
|
-
const cleanObservedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
|
|
210
|
-
|
|
211
|
-
try {
|
|
212
|
-
const envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
|
|
213
|
-
if (await this.peerStore.addressBook.consumePeerRecord(envelope)) {
|
|
214
|
-
await this.peerStore.protoBook.set(id, protocols)
|
|
215
|
-
await this.peerStore.metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
|
|
216
|
-
await this.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
|
|
217
|
-
return
|
|
218
|
-
}
|
|
219
|
-
} catch (/** @type {any} */ err) {
|
|
220
|
-
log('received invalid envelope, discard it and fallback to listenAddrs is available', err)
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// LEGACY: Update peers data in PeerStore
|
|
224
|
-
try {
|
|
225
|
-
await this.peerStore.addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
|
|
226
|
-
} catch (/** @type {any} */ err) {
|
|
227
|
-
log.error('received invalid addrs', err)
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
await this.peerStore.protoBook.set(id, protocols)
|
|
231
|
-
await this.peerStore.metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
|
|
232
|
-
await this.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
|
|
233
|
-
|
|
234
|
-
// TODO: Add and score our observed addr
|
|
235
|
-
log('received observed address of %s', cleanObservedAddr)
|
|
236
|
-
// this.addressManager.addObservedAddr(observedAddr)
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* A handler to register with Libp2p to process identify messages.
|
|
241
|
-
*
|
|
242
|
-
* @param {Object} options
|
|
243
|
-
* @param {Connection} options.connection
|
|
244
|
-
* @param {MuxedStream} options.stream
|
|
245
|
-
* @param {string} options.protocol
|
|
246
|
-
* @returns {Promise<void>|undefined}
|
|
247
|
-
*/
|
|
248
|
-
handleMessage ({ connection, stream, protocol }) {
|
|
249
|
-
switch (protocol) {
|
|
250
|
-
case this.identifyProtocolStr:
|
|
251
|
-
return this._handleIdentify({ connection, stream })
|
|
252
|
-
case this.identifyPushProtocolStr:
|
|
253
|
-
return this._handlePush({ connection, stream })
|
|
254
|
-
default:
|
|
255
|
-
log.error('cannot handle unknown protocol %s', protocol)
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Sends the `Identify` response with the Signed Peer Record
|
|
261
|
-
* to the requesting peer over the given `connection`
|
|
262
|
-
*
|
|
263
|
-
* @private
|
|
264
|
-
* @param {Object} options
|
|
265
|
-
* @param {MuxedStream} options.stream
|
|
266
|
-
* @param {Connection} options.connection
|
|
267
|
-
* @returns {Promise<void>}
|
|
268
|
-
*/
|
|
269
|
-
async _handleIdentify ({ connection, stream }) {
|
|
270
|
-
try {
|
|
271
|
-
let publicKey = new Uint8Array(0)
|
|
272
|
-
if (this.peerId.pubKey) {
|
|
273
|
-
publicKey = this.peerId.pubKey.bytes
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
|
277
|
-
const protocols = await this.peerStore.protoBook.get(this.peerId)
|
|
278
|
-
|
|
279
|
-
const message = Message.Identify.encode({
|
|
280
|
-
protocolVersion: this._host.protocolVersion,
|
|
281
|
-
agentVersion: this._host.agentVersion,
|
|
282
|
-
publicKey,
|
|
283
|
-
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
|
|
284
|
-
signedPeerRecord,
|
|
285
|
-
observedAddr: connection.remoteAddr.bytes,
|
|
286
|
-
protocols
|
|
287
|
-
}).finish()
|
|
288
|
-
|
|
289
|
-
await pipe(
|
|
290
|
-
[message],
|
|
291
|
-
lp.encode(),
|
|
292
|
-
stream,
|
|
293
|
-
consume
|
|
294
|
-
)
|
|
295
|
-
} catch (/** @type {any} */ err) {
|
|
296
|
-
log.error('could not respond to identify request', err)
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Reads the Identify Push message from the given `connection`
|
|
302
|
-
*
|
|
303
|
-
* @private
|
|
304
|
-
* @param {object} options
|
|
305
|
-
* @param {MuxedStream} options.stream
|
|
306
|
-
* @param {Connection} options.connection
|
|
307
|
-
* @returns {Promise<void>}
|
|
308
|
-
*/
|
|
309
|
-
async _handlePush ({ connection, stream }) {
|
|
310
|
-
let message
|
|
311
|
-
try {
|
|
312
|
-
const [data] = await pipe(
|
|
313
|
-
[],
|
|
314
|
-
stream,
|
|
315
|
-
lp.decode(),
|
|
316
|
-
take(1),
|
|
317
|
-
toBuffer,
|
|
318
|
-
collect
|
|
319
|
-
)
|
|
320
|
-
message = Message.Identify.decode(data)
|
|
321
|
-
} catch (/** @type {any} */ err) {
|
|
322
|
-
return log.error('received invalid message', err)
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
const id = connection.remotePeer
|
|
326
|
-
|
|
327
|
-
try {
|
|
328
|
-
const envelope = await Envelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
|
|
329
|
-
if (await this.peerStore.addressBook.consumePeerRecord(envelope)) {
|
|
330
|
-
await this.peerStore.protoBook.set(id, message.protocols)
|
|
331
|
-
return
|
|
332
|
-
}
|
|
333
|
-
} catch (/** @type {any} */ err) {
|
|
334
|
-
log('received invalid envelope, discard it and fallback to listenAddrs is available', err)
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// LEGACY: Update peers data in PeerStore
|
|
338
|
-
try {
|
|
339
|
-
await this.peerStore.addressBook.set(id,
|
|
340
|
-
message.listenAddrs.map((addr) => new Multiaddr(addr)))
|
|
341
|
-
} catch (/** @type {any} */ err) {
|
|
342
|
-
log.error('received invalid addrs', err)
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// Update the protocols
|
|
346
|
-
try {
|
|
347
|
-
await this.peerStore.protoBook.set(id, message.protocols)
|
|
348
|
-
} catch (/** @type {any} */ err) {
|
|
349
|
-
log.error('received invalid protocols', err)
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Takes the `addr` and converts it to a Multiaddr if possible
|
|
355
|
-
*
|
|
356
|
-
* @param {Uint8Array | string} addr
|
|
357
|
-
* @returns {Multiaddr|null}
|
|
358
|
-
*/
|
|
359
|
-
static getCleanMultiaddr (addr) {
|
|
360
|
-
if (addr && addr.length > 0) {
|
|
361
|
-
try {
|
|
362
|
-
return new Multiaddr(addr)
|
|
363
|
-
} catch (_) {
|
|
364
|
-
return null
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
return null
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* The protocols the IdentifyService supports
|
|
373
|
-
*
|
|
374
|
-
* @property multicodecs
|
|
375
|
-
*/
|
|
376
|
-
const multicodecs = {
|
|
377
|
-
IDENTIFY: MULTICODEC_IDENTIFY,
|
|
378
|
-
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
IdentifyService.multicodecs = multicodecs
|
|
382
|
-
IdentifyService.Messsage = Message
|
|
383
|
-
|
|
384
|
-
module.exports = IdentifyService
|