libp2p 0.36.0 → 0.36.2-1473044
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 +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 +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/pb/index.d.ts +51 -0
- package/dist/src/circuit/pb/index.d.ts.map +1 -0
- package/dist/src/circuit/pb/index.js +72 -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 +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 +280 -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 +129 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/pb/proto.d.ts +26 -0
- package/dist/src/fetch/pb/proto.d.ts.map +1 -0
- package/dist/src/fetch/pb/proto.js +44 -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 +15 -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 +162 -581
- 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 +28 -0
- package/dist/src/insecure/pb/proto.d.ts.map +1 -0
- package/dist/src/insecure/pb/proto.js +46 -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 +61 -81
- 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 +78 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/dist/src/libp2p.js +375 -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 +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 +150 -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 +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.js → listener.ts} +21 -27
- package/src/circuit/multicodec.ts +2 -0
- package/src/circuit/{protocol → pb}/index.proto +1 -1
- package/src/circuit/pb/index.ts +88 -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 +371 -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.ts +167 -0
- package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
- package/src/fetch/pb/proto.ts +58 -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 +36 -0
- package/src/index.ts +242 -0
- package/src/insecure/index.ts +101 -0
- package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
- package/src/insecure/pb/proto.ts +61 -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 +513 -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 +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/circuit/protocol/index.d.ts +0 -173
- 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/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 -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/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 -811
- 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 -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/index.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { createLibp2pNode } from './libp2p.js'
|
|
2
|
+
import type { AbortOptions, EventEmitter, RecursivePartial, Startable } from '@libp2p/interfaces'
|
|
3
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
|
+
import type { FaultTolerance } from './transport-manager.js'
|
|
5
|
+
import type { HostProperties } from './identify/index.js'
|
|
6
|
+
import type { DualDHT } from '@libp2p/interfaces/dht'
|
|
7
|
+
import type { Datastore } from 'interface-datastore'
|
|
8
|
+
import type { PeerStore, PeerStoreInit } from '@libp2p/interfaces/peer-store'
|
|
9
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
10
|
+
import type { AutoRelayConfig, RelayAdvertiseConfig } from './circuit/index.js'
|
|
11
|
+
import type { PeerDiscovery } from '@libp2p/interfaces/peer-discovery'
|
|
12
|
+
import type { Connection, ConnectionGater, ConnectionProtector, ProtocolStream } from '@libp2p/interfaces/connection'
|
|
13
|
+
import type { Transport } from '@libp2p/interfaces/transport'
|
|
14
|
+
import type { StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer'
|
|
15
|
+
import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
|
|
16
|
+
import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
|
|
17
|
+
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
|
|
18
|
+
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
19
|
+
import type { ConnectionManager, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
20
|
+
import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
|
|
21
|
+
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
22
|
+
import type { DialerInit } from '@libp2p/interfaces/dialer'
|
|
23
|
+
import type { KeyChain } from './keychain/index.js'
|
|
24
|
+
|
|
25
|
+
export interface PersistentPeerStoreOptions {
|
|
26
|
+
threshold?: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface DEKConfig {
|
|
30
|
+
keyLength: number
|
|
31
|
+
iterationCount: number
|
|
32
|
+
salt: string
|
|
33
|
+
hash: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface KeychainConfig {
|
|
37
|
+
pass?: string
|
|
38
|
+
dek?: DEKConfig
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface MetricsConfig {
|
|
42
|
+
enabled?: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface HopConfig {
|
|
46
|
+
enabled?: boolean
|
|
47
|
+
active?: boolean
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface RelayConfig {
|
|
51
|
+
enabled: boolean
|
|
52
|
+
advertise: RelayAdvertiseConfig
|
|
53
|
+
hop: HopConfig
|
|
54
|
+
autoRelay: AutoRelayConfig
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface NatManagerConfig {
|
|
58
|
+
enabled: boolean
|
|
59
|
+
externalAddress?: string
|
|
60
|
+
localAddress?: string
|
|
61
|
+
description?: string
|
|
62
|
+
ttl?: number
|
|
63
|
+
keepAlive: boolean
|
|
64
|
+
gateway?: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface AddressesConfig {
|
|
68
|
+
listen: string[]
|
|
69
|
+
announce: string[]
|
|
70
|
+
noAnnounce: string[]
|
|
71
|
+
announceFilter: (multiaddrs: Multiaddr[]) => Multiaddr[]
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ConnectionManagerConfig {
|
|
75
|
+
/**
|
|
76
|
+
* If true, try to connect to all discovered peers up to the connection manager limit
|
|
77
|
+
*/
|
|
78
|
+
autoDial?: boolean
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The maximum number of connections to keep open
|
|
82
|
+
*/
|
|
83
|
+
maxConnections: number
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The minimum number of connections to keep open
|
|
87
|
+
*/
|
|
88
|
+
minConnections: number
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* How long to wait between attempting to keep our number of concurrent connections
|
|
92
|
+
* above minConnections
|
|
93
|
+
*/
|
|
94
|
+
autoDialInterval: number
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface TransportManagerConfig {
|
|
98
|
+
faultTolerance?: FaultTolerance
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface PeerStoreConfig {
|
|
102
|
+
persistence?: boolean
|
|
103
|
+
threshold?: number
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface PeerRoutingConfig {
|
|
107
|
+
refreshManager: RefreshManagerConfig
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface RefreshManagerConfig {
|
|
111
|
+
enabled?: boolean
|
|
112
|
+
interval: number
|
|
113
|
+
bootDelay: number
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface Libp2pInit {
|
|
117
|
+
peerId: PeerId
|
|
118
|
+
host: HostProperties
|
|
119
|
+
addresses: AddressesConfig
|
|
120
|
+
connectionManager: ConnectionManagerConfig
|
|
121
|
+
connectionGater: Partial<ConnectionGater>
|
|
122
|
+
transportManager: TransportManagerConfig
|
|
123
|
+
datastore: Datastore
|
|
124
|
+
dialer: DialerInit
|
|
125
|
+
metrics: MetricsInit
|
|
126
|
+
peerStore: PeerStoreInit
|
|
127
|
+
peerRouting: PeerRoutingConfig
|
|
128
|
+
keychain: KeychainConfig
|
|
129
|
+
protocolPrefix: string
|
|
130
|
+
nat: NatManagerConfig
|
|
131
|
+
relay: RelayConfig
|
|
132
|
+
|
|
133
|
+
transports: Transport[]
|
|
134
|
+
streamMuxers?: StreamMuxerFactory[]
|
|
135
|
+
connectionEncryption?: ConnectionEncrypter[]
|
|
136
|
+
peerDiscovery?: PeerDiscovery[]
|
|
137
|
+
peerRouters?: PeerRouting[]
|
|
138
|
+
contentRouters?: ContentRouting[]
|
|
139
|
+
dht?: DualDHT
|
|
140
|
+
pubsub?: PubSub
|
|
141
|
+
connectionProtector?: ConnectionProtector
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface Libp2pEvents {
|
|
145
|
+
'peer:discovery': CustomEvent<PeerInfo>
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
|
|
149
|
+
peerId: PeerId
|
|
150
|
+
peerStore: PeerStore
|
|
151
|
+
peerRouting: PeerRouting
|
|
152
|
+
contentRouting: ContentRouting
|
|
153
|
+
keychain: KeyChain
|
|
154
|
+
connectionManager: ConnectionManager
|
|
155
|
+
registrar: Registrar
|
|
156
|
+
metrics?: Metrics
|
|
157
|
+
|
|
158
|
+
pubsub?: PubSub
|
|
159
|
+
dht?: DualDHT
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Load keychain keys from the datastore.
|
|
163
|
+
* Imports the private key as 'self', if needed.
|
|
164
|
+
*/
|
|
165
|
+
loadKeychain: () => Promise<void>
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Get a deduplicated list of peer advertising multiaddrs by concatenating
|
|
169
|
+
* the listen addresses used by transports with any configured
|
|
170
|
+
* announce addresses as well as observed addresses reported by peers.
|
|
171
|
+
*
|
|
172
|
+
* If Announce addrs are specified, configured listen addresses will be
|
|
173
|
+
* ignored though observed addresses will still be included.
|
|
174
|
+
*/
|
|
175
|
+
getMultiaddrs: () => Multiaddr[]
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Return a list of all connections this node has open, optionally filtering
|
|
179
|
+
* by a PeerId
|
|
180
|
+
*/
|
|
181
|
+
getConnections: (peerId?: PeerId) => Connection[]
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Return a list of all peers we currently have a connection open to
|
|
185
|
+
*/
|
|
186
|
+
getPeers: () => PeerId[]
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Dials to the provided peer. If successful, the known metadata of the
|
|
190
|
+
* peer will be added to the nodes `peerStore`
|
|
191
|
+
*/
|
|
192
|
+
dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Dials to the provided peer and tries to handshake with the given protocols in order.
|
|
196
|
+
* If successful, the known metadata of the peer will be added to the nodes `peerStore`,
|
|
197
|
+
* and the `MuxedStream` will be returned together with the successful negotiated protocol.
|
|
198
|
+
*/
|
|
199
|
+
dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<ProtocolStream>
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Disconnects all connections to the given `peer`
|
|
203
|
+
*/
|
|
204
|
+
hangUp: (peer: PeerId | Multiaddr | string) => Promise<void>
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Registers the `handler` for each protocol
|
|
208
|
+
*/
|
|
209
|
+
handle: (protocol: string | string[], handler: StreamHandler) => Promise<void>
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Removes the handler for each protocol. The protocol
|
|
213
|
+
* will no longer be supported on streams.
|
|
214
|
+
*/
|
|
215
|
+
unhandle: (protocols: string[] | string) => Promise<void>
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Pings the given peer in order to obtain the operation latency
|
|
219
|
+
*/
|
|
220
|
+
ping: (peer: Multiaddr |PeerId) => Promise<number>
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Sends a request to fetch the value associated with the given key from the given peer.
|
|
224
|
+
*/
|
|
225
|
+
fetch: (peer: PeerId | Multiaddr | string, key: string) => Promise<Uint8Array | null>
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
|
|
229
|
+
* this may mean searching the DHT if the key is not present in the KeyStore.
|
|
230
|
+
*/
|
|
231
|
+
getPublicKey: (peer: PeerId, options?: AbortOptions) => Promise<Uint8Array>
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export type Libp2pOptions = RecursivePartial<Libp2pInit>
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Returns a new instance of the Libp2p interface, generating a new PeerId
|
|
238
|
+
* if one is not passed as part of the options.
|
|
239
|
+
*/
|
|
240
|
+
export async function createLibp2p (options: Libp2pOptions): Promise<Libp2p> {
|
|
241
|
+
return await createLibp2pNode(options)
|
|
242
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import { handshake } from 'it-handshake'
|
|
3
|
+
import * as lp from 'it-length-prefixed'
|
|
4
|
+
import { UnexpectedPeerError, InvalidCryptoExchangeError } from '@libp2p/interfaces/connection-encrypter/errors'
|
|
5
|
+
import { Exchange, KeyType } from './pb/proto.js'
|
|
6
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
7
|
+
import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
|
|
8
|
+
import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interfaces/connection-encrypter'
|
|
9
|
+
import type { Duplex } from 'it-stream-types'
|
|
10
|
+
|
|
11
|
+
const log = logger('libp2p:plaintext')
|
|
12
|
+
const PROTOCOL = '/plaintext/2.0.0'
|
|
13
|
+
|
|
14
|
+
function lpEncodeExchange (exchange: Exchange) {
|
|
15
|
+
const pb = Exchange.encode(exchange)
|
|
16
|
+
|
|
17
|
+
return lp.encode.single(pb)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Encrypt connection
|
|
22
|
+
*/
|
|
23
|
+
async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
|
|
24
|
+
const shake = handshake(conn)
|
|
25
|
+
|
|
26
|
+
let type = KeyType.RSA
|
|
27
|
+
|
|
28
|
+
if (localId.type === 'Ed25519') {
|
|
29
|
+
type = KeyType.Ed25519
|
|
30
|
+
} else if (localId.type === 'secp256k1') {
|
|
31
|
+
type = KeyType.Secp256k1
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Encode the public key and write it to the remote peer
|
|
35
|
+
shake.write(
|
|
36
|
+
lpEncodeExchange({
|
|
37
|
+
id: localId.toBytes(),
|
|
38
|
+
pubkey: {
|
|
39
|
+
Type: type,
|
|
40
|
+
Data: localId.publicKey ?? new Uint8Array(0)
|
|
41
|
+
}
|
|
42
|
+
}).slice()
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
log('write pubkey exchange to peer %p', remoteId)
|
|
46
|
+
|
|
47
|
+
// Get the Exchange message
|
|
48
|
+
// @ts-expect-error needs to be generator
|
|
49
|
+
const response = (await lp.decode.fromReader(shake.reader).next()).value
|
|
50
|
+
const id = Exchange.decode(response.slice())
|
|
51
|
+
log('read pubkey exchange from peer %p', remoteId)
|
|
52
|
+
|
|
53
|
+
let peerId
|
|
54
|
+
try {
|
|
55
|
+
if (id.pubkey == null) {
|
|
56
|
+
throw new Error('Public key missing')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (id.pubkey.Data.length === 0) {
|
|
60
|
+
throw new Error('Public key data too short')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (id.id == null) {
|
|
64
|
+
throw new Error('Remote id missing')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
peerId = await peerIdFromKeys(id.pubkey.Data)
|
|
68
|
+
|
|
69
|
+
if (!peerId.equals(peerIdFromBytes(id.id))) {
|
|
70
|
+
throw new Error('Public key did not match id')
|
|
71
|
+
}
|
|
72
|
+
} catch (err: any) {
|
|
73
|
+
log.error(err)
|
|
74
|
+
throw new InvalidCryptoExchangeError('Remote did not provide its public key')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (remoteId != null && !peerId.equals(remoteId)) {
|
|
78
|
+
throw new UnexpectedPeerError()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
log('plaintext key exchange completed successfully with peer %p', peerId)
|
|
82
|
+
|
|
83
|
+
shake.rest()
|
|
84
|
+
return {
|
|
85
|
+
conn: shake.stream,
|
|
86
|
+
remotePeer: peerId,
|
|
87
|
+
remoteEarlyData: new Uint8Array()
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class Plaintext implements ConnectionEncrypter {
|
|
92
|
+
public protocol: string = PROTOCOL
|
|
93
|
+
|
|
94
|
+
async secureInbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
|
|
95
|
+
return await encrypt(localId, conn, remoteId)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async secureOutbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId: PeerId): Promise<SecuredConnection> {
|
|
99
|
+
return await encrypt(localId, conn, remoteId)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
3
|
+
|
|
4
|
+
import { encodeMessage, decodeMessage, message, bytes, enumeration } from 'protons-runtime'
|
|
5
|
+
|
|
6
|
+
export interface Exchange {
|
|
7
|
+
id?: Uint8Array
|
|
8
|
+
pubkey?: PublicKey
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export namespace Exchange {
|
|
12
|
+
export const codec = () => {
|
|
13
|
+
return message<Exchange>({
|
|
14
|
+
1: { name: 'id', codec: bytes, optional: true },
|
|
15
|
+
2: { name: 'pubkey', codec: PublicKey.codec(), optional: true }
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const encode = (obj: Exchange): Uint8Array => {
|
|
20
|
+
return encodeMessage(obj, Exchange.codec())
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const decode = (buf: Uint8Array): Exchange => {
|
|
24
|
+
return decodeMessage(buf, Exchange.codec())
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export enum KeyType {
|
|
29
|
+
RSA = 'RSA',
|
|
30
|
+
Ed25519 = 'Ed25519',
|
|
31
|
+
Secp256k1 = 'Secp256k1',
|
|
32
|
+
ECDSA = 'ECDSA'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export namespace KeyType {
|
|
36
|
+
export const codec = () => {
|
|
37
|
+
return enumeration<typeof KeyType>(KeyType)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface PublicKey {
|
|
42
|
+
Type: KeyType
|
|
43
|
+
Data: Uint8Array
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export namespace PublicKey {
|
|
47
|
+
export const codec = () => {
|
|
48
|
+
return message<PublicKey>({
|
|
49
|
+
1: { name: 'Type', codec: KeyType.codec() },
|
|
50
|
+
2: { name: 'Data', codec: bytes }
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const encode = (obj: PublicKey): Uint8Array => {
|
|
55
|
+
return encodeMessage(obj, PublicKey.codec())
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const decode = (buf: Uint8Array): PublicKey => {
|
|
59
|
+
return decodeMessage(buf, PublicKey.codec())
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// @ts-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const privates = new WeakMap()
|
|
1
|
+
import 'node-forge/lib/pkcs7.js'
|
|
2
|
+
import 'node-forge/lib/pbe.js'
|
|
3
|
+
// @ts-expect-error types are missing
|
|
4
|
+
import forge from 'node-forge/lib/forge.js'
|
|
5
|
+
import { certificateForKey, findAsync } from './util.js'
|
|
6
|
+
import errCode from 'err-code'
|
|
7
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
9
|
+
import { codes } from '../errors.js'
|
|
10
|
+
import { logger } from '@libp2p/logger'
|
|
11
|
+
import type { KeyChain } from './index.js'
|
|
12
|
+
|
|
13
|
+
const log = logger('libp2p:keychain:cms')
|
|
14
|
+
|
|
15
|
+
const privates = new WeakMap<object, { dek: string }>()
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Cryptographic Message Syntax (aka PKCS #7)
|
|
@@ -23,16 +23,15 @@ const privates = new WeakMap()
|
|
|
23
23
|
*
|
|
24
24
|
* See RFC 5652 for all the details.
|
|
25
25
|
*/
|
|
26
|
-
class CMS {
|
|
26
|
+
export class CMS {
|
|
27
|
+
private readonly keychain: KeyChain
|
|
28
|
+
|
|
27
29
|
/**
|
|
28
30
|
* Creates a new instance with a keychain
|
|
29
|
-
*
|
|
30
|
-
* @param {import('./index')} keychain - the available keys
|
|
31
|
-
* @param {string} dek
|
|
32
31
|
*/
|
|
33
|
-
constructor (keychain, dek) {
|
|
34
|
-
if (
|
|
35
|
-
throw
|
|
32
|
+
constructor (keychain: KeyChain, dek: string) {
|
|
33
|
+
if (keychain == null) {
|
|
34
|
+
throw errCode(new Error('keychain is required'), codes.ERR_KEYCHAIN_REQUIRED)
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
this.keychain = keychain
|
|
@@ -43,20 +42,21 @@ class CMS {
|
|
|
43
42
|
* Creates some protected data.
|
|
44
43
|
*
|
|
45
44
|
* The output Uint8Array contains the PKCS #7 message in DER.
|
|
46
|
-
*
|
|
47
|
-
* @param {string} name - The local key name.
|
|
48
|
-
* @param {Uint8Array} plain - The data to encrypt.
|
|
49
|
-
* @returns {Promise<Uint8Array>}
|
|
50
45
|
*/
|
|
51
|
-
async encrypt (name, plain) {
|
|
46
|
+
async encrypt (name: string, plain: Uint8Array): Promise<Uint8Array> {
|
|
52
47
|
if (!(plain instanceof Uint8Array)) {
|
|
53
|
-
throw
|
|
48
|
+
throw errCode(new Error('Plain data must be a Uint8Array'), codes.ERR_INVALID_PARAMETERS)
|
|
54
49
|
}
|
|
55
50
|
|
|
56
51
|
const key = await this.keychain.findKeyByName(name)
|
|
57
|
-
const pem = await this.keychain.
|
|
58
|
-
|
|
59
|
-
|
|
52
|
+
const pem = await this.keychain.getPrivateKey(name)
|
|
53
|
+
const cached = privates.get(this)
|
|
54
|
+
|
|
55
|
+
if (cached == null) {
|
|
56
|
+
throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const dek = cached.dek
|
|
60
60
|
const privateKey = forge.pki.decryptRsaPrivateKey(pem, dek)
|
|
61
61
|
const certificate = await certificateForKey(key, privateKey)
|
|
62
62
|
|
|
@@ -76,71 +76,75 @@ class CMS {
|
|
|
76
76
|
*
|
|
77
77
|
* The keychain must contain one of the keys used to encrypt the data. If none of the keys
|
|
78
78
|
* exists, an Error is returned with the property 'missingKeys'. It is array of key ids.
|
|
79
|
-
*
|
|
80
|
-
* @param {Uint8Array} cmsData - The CMS encrypted data to decrypt.
|
|
81
|
-
* @returns {Promise<Uint8Array>}
|
|
82
79
|
*/
|
|
83
|
-
async decrypt (cmsData) {
|
|
80
|
+
async decrypt (cmsData: Uint8Array): Promise<Uint8Array> {
|
|
84
81
|
if (!(cmsData instanceof Uint8Array)) {
|
|
85
|
-
throw
|
|
82
|
+
throw errCode(new Error('CMS data is required'), codes.ERR_INVALID_PARAMETERS)
|
|
86
83
|
}
|
|
87
84
|
|
|
88
|
-
let cms
|
|
85
|
+
let cms: any
|
|
89
86
|
try {
|
|
90
87
|
const buf = forge.util.createBuffer(uint8ArrayToString(cmsData, 'ascii'))
|
|
91
88
|
const obj = forge.asn1.fromDer(buf)
|
|
92
|
-
|
|
89
|
+
|
|
93
90
|
cms = forge.pkcs7.messageFromAsn1(obj)
|
|
94
|
-
} catch (
|
|
95
|
-
|
|
91
|
+
} catch (err: any) {
|
|
92
|
+
log.error(err)
|
|
93
|
+
throw errCode(new Error('Invalid CMS'), codes.ERR_INVALID_CMS)
|
|
96
94
|
}
|
|
97
95
|
|
|
98
96
|
// Find a recipient whose key we hold. We only deal with recipient certs
|
|
99
97
|
// issued by ipfs (O=ipfs).
|
|
100
|
-
const recipients = cms.recipients
|
|
101
|
-
// @ts-
|
|
98
|
+
const recipients: any = cms.recipients
|
|
99
|
+
// @ts-expect-error cms types not defined
|
|
102
100
|
.filter(r => r.issuer.find(a => a.shortName === 'O' && a.value === 'ipfs'))
|
|
103
|
-
// @ts-
|
|
101
|
+
// @ts-expect-error cms types not defined
|
|
104
102
|
.filter(r => r.issuer.find(a => a.shortName === 'CN'))
|
|
105
|
-
// @ts-
|
|
103
|
+
// @ts-expect-error cms types not defined
|
|
106
104
|
.map(r => {
|
|
107
105
|
return {
|
|
108
106
|
recipient: r,
|
|
109
|
-
// @ts-
|
|
107
|
+
// @ts-expect-error cms types not defined
|
|
110
108
|
keyId: r.issuer.find(a => a.shortName === 'CN').value
|
|
111
109
|
}
|
|
112
110
|
})
|
|
113
111
|
|
|
114
|
-
const r = await findAsync(recipients, async (recipient) => {
|
|
112
|
+
const r = await findAsync(recipients, async (recipient: any) => {
|
|
115
113
|
try {
|
|
116
114
|
const key = await this.keychain.findKeyById(recipient.keyId)
|
|
117
|
-
if (key)
|
|
118
|
-
|
|
115
|
+
if (key != null) {
|
|
116
|
+
return true
|
|
117
|
+
}
|
|
118
|
+
} catch (err: any) {
|
|
119
119
|
return false
|
|
120
120
|
}
|
|
121
121
|
return false
|
|
122
122
|
})
|
|
123
123
|
|
|
124
|
-
if (
|
|
125
|
-
// @ts-
|
|
126
|
-
const missingKeys = recipients.map(r => r.keyId)
|
|
127
|
-
throw
|
|
124
|
+
if (r == null) {
|
|
125
|
+
// @ts-expect-error cms types not defined
|
|
126
|
+
const missingKeys: string[] = recipients.map(r => r.keyId)
|
|
127
|
+
throw errCode(new Error(`Decryption needs one of the key(s): ${missingKeys.join(', ')}`), codes.ERR_MISSING_KEYS, {
|
|
128
128
|
missingKeys
|
|
129
129
|
})
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
const key = await this.keychain.findKeyById(r.keyId)
|
|
133
133
|
|
|
134
|
-
if (
|
|
135
|
-
throw
|
|
134
|
+
if (key == null) {
|
|
135
|
+
throw errCode(new Error('No key available to decrypto'), codes.ERR_NO_KEY)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const pem = await this.keychain.getPrivateKey(key.name)
|
|
139
|
+
const cached = privates.get(this)
|
|
140
|
+
|
|
141
|
+
if (cached == null) {
|
|
142
|
+
throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
|
|
136
143
|
}
|
|
137
144
|
|
|
138
|
-
const
|
|
139
|
-
const dek = privates.get(this).dek
|
|
145
|
+
const dek = cached.dek
|
|
140
146
|
const privateKey = forge.pki.decryptRsaPrivateKey(pem, dek)
|
|
141
147
|
cms.decrypt(r.recipient, privateKey)
|
|
142
148
|
return uint8ArrayFromString(cms.content.getBytes(), 'ascii')
|
|
143
149
|
}
|
|
144
150
|
}
|
|
145
|
-
|
|
146
|
-
module.exports = CMS
|