diff --git a/examples/browser-exchange-files/package.json b/examples/browser-exchange-files/package.json index 0f2458792d..58c1044f76 100644 --- a/examples/browser-exchange-files/package.json +++ b/examples/browser-exchange-files/package.json @@ -20,6 +20,7 @@ "dependencies": { "ipfs": "^0.52.2", "it-all": "^1.0.4", + "libp2p-websockets": "^0.15.0", "rimraf": "^3.0.2", "test-ipfs-example": "^2.0.3" }, diff --git a/examples/browser-exchange-files/public/app.js b/examples/browser-exchange-files/public/app.js index 11d6c8614b..5f42c7b309 100644 --- a/examples/browser-exchange-files/public/app.js +++ b/examples/browser-exchange-files/public/app.js @@ -2,6 +2,10 @@ 'use strict' const IPFS = require('ipfs') +const WS = require('libp2p-websockets') +const filters = require('libp2p-websockets/src/filters') +const transportKey = WS.prototype[Symbol.toStringTag] + const all = require('it-all') const uint8ArrayConcat = require('uint8arrays/concat') const uint8ArrayFromString = require('uint8arrays/from-string') @@ -59,6 +63,18 @@ async function start () { }, // If you want to connect to the public bootstrap nodes, remove the next line Bootstrap: [] + }, + libp2p: { + config: { + transport: { + // This is added for local demo! + // In a production environment the default filter should be used + // where only DNS + WSS addresses will be dialed by websockets in the browser. + [transportKey]: { + filter: filters.all + } + } + } } }) diff --git a/examples/circuit-relaying/package.json b/examples/circuit-relaying/package.json index 3a7a55bc05..7f5b60f476 100644 --- a/examples/circuit-relaying/package.json +++ b/examples/circuit-relaying/package.json @@ -17,6 +17,7 @@ "delay": "^4.4.0", "ipfs": "^0.52.2", "ipfs-pubsub-room": "^2.0.1", + "libp2p-websockets": "^0.15.0", "uint8arrays": "^1.1.0" }, "devDependencies": { diff --git a/examples/circuit-relaying/src/app.js b/examples/circuit-relaying/src/app.js index ffc97f877c..ea8963b358 100644 --- a/examples/circuit-relaying/src/app.js +++ b/examples/circuit-relaying/src/app.js @@ -2,6 +2,9 @@ 'use strict' const IPFS = require('ipfs') +const WS = require('libp2p-websockets') +const filters = require('libp2p-websockets/src/filters') +const transportKey = WS.prototype[Symbol.toStringTag] const Helpers = require('./helpers') document.addEventListener('DOMContentLoaded', async () => { @@ -38,6 +41,18 @@ document.addEventListener('DOMContentLoaded', async () => { }, config: { Bootstrap: [] + }, + libp2p: { + config: { + transport: { + // This is added for local demo! + // In a production environment the default filter should be used + // where only DNS + WSS addresses will be dialed by websockets in the browser. + [transportKey]: { + filter: filters.all + } + } + } } }) diff --git a/packages/interface-ipfs-core/package.json b/packages/interface-ipfs-core/package.json index a8782fab51..4ba8c38dc3 100644 --- a/packages/interface-ipfs-core/package.json +++ b/packages/interface-ipfs-core/package.json @@ -55,6 +55,7 @@ "it-map": "^1.0.4", "it-pushable": "^1.4.0", "libp2p-crypto": "^0.18.0", + "libp2p-websockets": "^0.15.0", "multiaddr": "^8.0.0", "multibase": "^3.0.0", "multihashing-async": "^2.0.1", diff --git a/packages/interface-ipfs-core/src/bitswap/transfer.js b/packages/interface-ipfs-core/src/bitswap/transfer.js index a6294c329b..c1cb73b3c9 100644 --- a/packages/interface-ipfs-core/src/bitswap/transfer.js +++ b/packages/interface-ipfs-core/src/bitswap/transfer.js @@ -11,6 +11,7 @@ const { nanoid } = require('nanoid') const uint8ArrayFromString = require('uint8arrays/from-string') const pmap = require('p-map') const multihashing = require('multihashing-async') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') const makeBlock = async () => { const d = uint8ArrayFromString(`IPFS is awesome ${nanoid()}`) @@ -25,6 +26,7 @@ const makeBlock = async () => { * @param {Object} options */ module.exports = (factory, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -37,7 +39,7 @@ module.exports = (factory, options) => { it('2 peers', async function () { // webworkers are not dialable because webrtc is not available const remote = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api - const local = (await factory.spawn()).api + const local = (await factory.spawn({ type: 'proc', ipfsOptions })).api await local.swarm.connect(remote.peerId.addresses[0]) const block = await makeBlock() @@ -51,7 +53,7 @@ module.exports = (factory, options) => { const blocks = await Promise.all([...Array(6).keys()].map(() => makeBlock())) const remote1 = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api const remote2 = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api - const local = (await factory.spawn()).api + const local = (await factory.spawn({ type: 'proc', ipfsOptions })).api await local.swarm.connect(remote1.peerId.addresses[0]) await local.swarm.connect(remote2.peerId.addresses[0]) await remote1.swarm.connect(remote2.peerId.addresses[0]) @@ -75,7 +77,7 @@ module.exports = (factory, options) => { it('2 peers', async () => { const content = randomBytes(1024 * 1024 * 10) const remote = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api - const local = (await factory.spawn()).api + const local = (await factory.spawn({ type: 'proc', ipfsOptions })).api local.swarm.connect(remote.peerId.addresses[0]) const file = await remote.add({ path: 'awesome.txt', content }) diff --git a/packages/interface-ipfs-core/src/bitswap/wantlist-for-peer.js b/packages/interface-ipfs-core/src/bitswap/wantlist-for-peer.js index e65321f16a..47e5ceb0ab 100644 --- a/packages/interface-ipfs-core/src/bitswap/wantlist-for-peer.js +++ b/packages/interface-ipfs-core/src/bitswap/wantlist-for-peer.js @@ -5,6 +5,7 @@ const { getDescribe, getIt } = require('../utils/mocha') const { waitForWantlistKey } = require('./utils') const { isWebWorker } = require('ipfs-utils/src/env') const testTimeout = require('../utils/test-timeout') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -12,6 +13,7 @@ const testTimeout = require('../utils/test-timeout') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -23,7 +25,7 @@ module.exports = (common, options) => { const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR' before(async () => { - ipfsA = (await common.spawn()).api + ipfsA = (await common.spawn({ type: 'proc', ipfsOptions })).api // webworkers are not dialable because webrtc is not available ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api // Add key to the wantlist for ipfsB diff --git a/packages/interface-ipfs-core/src/bitswap/wantlist.js b/packages/interface-ipfs-core/src/bitswap/wantlist.js index 1e743966bc..63d31bad59 100644 --- a/packages/interface-ipfs-core/src/bitswap/wantlist.js +++ b/packages/interface-ipfs-core/src/bitswap/wantlist.js @@ -8,6 +8,7 @@ const testTimeout = require('../utils/test-timeout') const AbortController = require('native-abort-controller') const CID = require('cids') const delay = require('delay') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -15,6 +16,7 @@ const delay = require('delay') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -26,7 +28,7 @@ module.exports = (common, options) => { const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR' before(async () => { - ipfsA = (await common.spawn()).api + ipfsA = (await common.spawn({ type: 'proc', ipfsOptions })).api // webworkers are not dialable because webrtc is not available ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api // Add key to the wantlist for ipfsB diff --git a/packages/interface-ipfs-core/src/miscellaneous/resolve.js b/packages/interface-ipfs-core/src/miscellaneous/resolve.js index e70978766a..91107486a2 100644 --- a/packages/interface-ipfs-core/src/miscellaneous/resolve.js +++ b/packages/interface-ipfs-core/src/miscellaneous/resolve.js @@ -10,6 +10,7 @@ const { getDescribe, getIt, expect } = require('../utils/mocha') const all = require('it-all') const { isWebWorker } = require('ipfs-utils/src/env') const testTimeout = require('../utils/test-timeout') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -17,6 +18,7 @@ const testTimeout = require('../utils/test-timeout') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -25,7 +27,7 @@ module.exports = (common, options) => { let ipfs before(async () => { - ipfs = (await common.spawn()).api + ipfs = (await common.spawn({ type: 'proc', ipfsOptions })).api }) after(() => common.clean()) diff --git a/packages/interface-ipfs-core/src/ping/ping.js b/packages/interface-ipfs-core/src/ping/ping.js index 59744264ca..8bdae916d4 100644 --- a/packages/interface-ipfs-core/src/ping/ping.js +++ b/packages/interface-ipfs-core/src/ping/ping.js @@ -7,6 +7,7 @@ const all = require('it-all') const drain = require('it-drain') const { isWebWorker } = require('ipfs-utils/src/env') const testTimeout = require('../utils/test-timeout') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -14,6 +15,7 @@ const testTimeout = require('../utils/test-timeout') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -24,7 +26,7 @@ module.exports = (common, options) => { let ipfsB before(async () => { - ipfsA = (await common.spawn()).api + ipfsA = (await common.spawn({ type: 'proc', ipfsOptions })).api // webworkers are not dialable because webrtc is not available ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api await ipfsA.swarm.connect(ipfsB.peerId.addresses[0]) diff --git a/packages/interface-ipfs-core/src/pubsub/peers.js b/packages/interface-ipfs-core/src/pubsub/peers.js index 3b209c43c1..5d1bc25f9d 100644 --- a/packages/interface-ipfs-core/src/pubsub/peers.js +++ b/packages/interface-ipfs-core/src/pubsub/peers.js @@ -6,6 +6,7 @@ const { getDescribe, getIt, expect } = require('../utils/mocha') const delay = require('delay') const { isWebWorker } = require('ipfs-utils/src/env') const testTimeout = require('../utils/test-timeout') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -13,6 +14,7 @@ const testTimeout = require('../utils/test-timeout') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -25,7 +27,7 @@ module.exports = (common, options) => { let subscribedTopics = [] before(async () => { - ipfs1 = (await common.spawn()).api + ipfs1 = (await common.spawn({ type: 'proc', ipfsOptions })).api // webworkers are not dialable because webrtc is not available ipfs2 = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api ipfs3 = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api diff --git a/packages/interface-ipfs-core/src/pubsub/subscribe.js b/packages/interface-ipfs-core/src/pubsub/subscribe.js index aca99579e5..0e3f098e1a 100644 --- a/packages/interface-ipfs-core/src/pubsub/subscribe.js +++ b/packages/interface-ipfs-core/src/pubsub/subscribe.js @@ -11,6 +11,7 @@ const { getDescribe, getIt, expect } = require('../utils/mocha') const delay = require('delay') const AbortController = require('native-abort-controller') const { isWebWorker, isNode } = require('ipfs-utils/src/env') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -18,6 +19,7 @@ const { isWebWorker, isNode } = require('ipfs-utils/src/env') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -30,7 +32,7 @@ module.exports = (common, options) => { let subscribedTopics = [] before(async () => { - ipfs1 = (await common.spawn()).api + ipfs1 = (await common.spawn({ type: 'proc', ipfsOptions })).api // TODO 'multiple connected nodes' tests fails with go in Firefox // and JS is flaky everywhere diff --git a/packages/interface-ipfs-core/src/swarm/addrs.js b/packages/interface-ipfs-core/src/swarm/addrs.js index 1c5487397a..d6d2889004 100644 --- a/packages/interface-ipfs-core/src/swarm/addrs.js +++ b/packages/interface-ipfs-core/src/swarm/addrs.js @@ -6,6 +6,7 @@ const Multiaddr = require('multiaddr') const { getDescribe, getIt, expect } = require('../utils/mocha') const { isWebWorker } = require('ipfs-utils/src/env') const testTimeout = require('../utils/test-timeout') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -13,6 +14,7 @@ const testTimeout = require('../utils/test-timeout') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -23,7 +25,7 @@ module.exports = (common, options) => { let ipfsB before(async () => { - ipfsA = (await common.spawn()).api + ipfsA = (await common.spawn({ type: 'proc', ipfsOptions })).api // webworkers are not dialable because webrtc is not available ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api await ipfsA.swarm.connect(ipfsB.peerId.addresses[0]) diff --git a/packages/interface-ipfs-core/src/swarm/connect.js b/packages/interface-ipfs-core/src/swarm/connect.js index bc0219b8af..76f92c3c5d 100644 --- a/packages/interface-ipfs-core/src/swarm/connect.js +++ b/packages/interface-ipfs-core/src/swarm/connect.js @@ -4,6 +4,7 @@ const { getDescribe, getIt, expect } = require('../utils/mocha') const { isWebWorker } = require('ipfs-utils/src/env') const testTimeout = require('../utils/test-timeout') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -11,6 +12,7 @@ const testTimeout = require('../utils/test-timeout') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -20,7 +22,7 @@ module.exports = (common, options) => { let ipfsB before(async () => { - ipfsA = (await common.spawn()).api + ipfsA = (await common.spawn({ type: 'proc', ipfsOptions })).api // webworkers are not dialable because webrtc is not available ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api }) diff --git a/packages/interface-ipfs-core/src/swarm/disconnect.js b/packages/interface-ipfs-core/src/swarm/disconnect.js index 606d4be1d7..09230535e9 100644 --- a/packages/interface-ipfs-core/src/swarm/disconnect.js +++ b/packages/interface-ipfs-core/src/swarm/disconnect.js @@ -4,6 +4,7 @@ const { getDescribe, getIt, expect } = require('../utils/mocha') const { isWebWorker } = require('ipfs-utils/src/env') const testTimeout = require('../utils/test-timeout') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -11,6 +12,7 @@ const testTimeout = require('../utils/test-timeout') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -21,7 +23,7 @@ module.exports = (common, options) => { let ipfsB before(async () => { - ipfsA = (await common.spawn()).api + ipfsA = (await common.spawn({ type: 'proc', ipfsOptions })).api // webworkers are not dialable because webrtc is not available ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api }) diff --git a/packages/interface-ipfs-core/src/swarm/peers.js b/packages/interface-ipfs-core/src/swarm/peers.js index 12701a8cdd..ecb5c54aa7 100644 --- a/packages/interface-ipfs-core/src/swarm/peers.js +++ b/packages/interface-ipfs-core/src/swarm/peers.js @@ -7,6 +7,7 @@ const delay = require('delay') const { isBrowser, isWebWorker } = require('ipfs-utils/src/env') const { getDescribe, getIt, expect } = require('../utils/mocha') const testTimeout = require('../utils/test-timeout') +const getIpfsOptions = require('../utils/ipfs-options-websockets-filter-all') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -14,6 +15,7 @@ const testTimeout = require('../utils/test-timeout') * @param {Object} options */ module.exports = (common, options) => { + const ipfsOptions = getIpfsOptions() const describe = getDescribe(options) const it = getIt(options) @@ -24,7 +26,7 @@ module.exports = (common, options) => { let ipfsB before(async () => { - ipfsA = (await common.spawn()).api + ipfsA = (await common.spawn({ type: 'proc', ipfsOptions })).api ipfsB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api await ipfsA.swarm.connect(ipfsB.peerId.addresses[0]) /* TODO: Seen if we still need this after this is fixed @@ -94,7 +96,7 @@ module.exports = (common, options) => { } it('should list peers only once', async () => { - const nodeA = (await common.spawn()).api + const nodeA = (await common.spawn({ type: 'proc', ipfsOptions })).api const nodeB = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api await nodeA.swarm.connect(nodeB.peerId.addresses[0]) await delay(1000) @@ -106,26 +108,7 @@ module.exports = (common, options) => { it('should list peers only once even if they have multiple addresses', async () => { // TODO: Change to port 0, needs: https://github.com/ipfs/interface-ipfs-core/issues/152 - let addresses - - if (isBrowser && common.opts.type !== 'go') { - addresses = [ - '/ip4/127.0.0.1/tcp/14578/ws/p2p-webrtc-star', - '/ip4/127.0.0.1/tcp/14579/ws/p2p-webrtc-star' - ] - } else if (isWebWorker) { - // webworkers are not dialable (no webrtc available) until stardust is async/await - // https://github.com/libp2p/js-libp2p-stardust/pull/14 - addresses = [] - } else { - addresses = [ - '/ip4/127.0.0.1/tcp/26543/ws', - '/ip4/127.0.0.1/tcp/26544/ws' - ] - } - - const configA = getConfig(addresses) - const configB = getConfig(isBrowser && common.opts.type !== 'go' ? [ + const config = getConfig(isBrowser && common.opts.type !== 'go' ? [ '/ip4/127.0.0.1/tcp/14578/ws/p2p-webrtc-star', '/ip4/127.0.0.1/tcp/14579/ws/p2p-webrtc-star' ] : [ @@ -133,11 +116,11 @@ module.exports = (common, options) => { '/ip4/127.0.0.1/tcp/26546/ws' ]) - const nodeA = (await common.spawn({ ipfsOptions: { config: configA } })).api + const nodeA = (await common.spawn({ type: 'proc', ipfsOptions })).api const nodeB = (await common.spawn({ type: isWebWorker ? 'go' : undefined, ipfsOptions: { - config: configB + config } })).api diff --git a/packages/interface-ipfs-core/src/utils/ipfs-options-websockets-filter-all.js b/packages/interface-ipfs-core/src/utils/ipfs-options-websockets-filter-all.js new file mode 100644 index 0000000000..63b891bac7 --- /dev/null +++ b/packages/interface-ipfs-core/src/utils/ipfs-options-websockets-filter-all.js @@ -0,0 +1,17 @@ +'use strict' + +const WS = require('libp2p-websockets') +const filters = require('libp2p-websockets/src/filters') +const transportKey = WS.prototype[Symbol.toStringTag] + +module.exports = () => ({ + libp2p: { + config: { + transport: { + [transportKey]: { + filter: filters.all + } + } + } + } +}) diff --git a/packages/ipfs-core/package.json b/packages/ipfs-core/package.json index 0f39fad8a3..4b712ba43a 100644 --- a/packages/ipfs-core/package.json +++ b/packages/ipfs-core/package.json @@ -88,11 +88,11 @@ "it-first": "^1.0.4", "it-last": "^1.0.4", "it-pipe": "^1.1.0", - "libp2p": "^0.29.3", + "libp2p": "^0.30.0", "libp2p-bootstrap": "^0.12.1", "libp2p-crypto": "^0.18.0", - "libp2p-floodsub": "^0.23.1", - "libp2p-gossipsub": "^0.6.1", + "libp2p-floodsub": "^0.24.1", + "libp2p-gossipsub": "^0.8.0", "libp2p-kad-dht": "^0.20.1", "libp2p-mdns": "^0.15.0", "libp2p-mplex": "^0.10.0", @@ -100,7 +100,7 @@ "libp2p-record": "^0.9.0", "libp2p-tcp": "^0.15.1", "libp2p-webrtc-star": "^0.20.1", - "libp2p-websockets": "^0.14.0", + "libp2p-websockets": "^0.15.0", "mafmt": "^8.0.0", "merge-options": "^2.0.0", "mortice": "^2.0.0", diff --git a/packages/ipfs-core/src/components/index.js b/packages/ipfs-core/src/components/index.js index f2d8b06759..4f02e91da8 100644 --- a/packages/ipfs-core/src/components/index.js +++ b/packages/ipfs-core/src/components/index.js @@ -70,6 +70,8 @@ class IPFS { }) const dns = createDNSAPI() const isOnline = createIsOnlineAPI({ network }) + // @ts-ignore This type check fails as options. + // libp2p can be a function, while IPNS router config expects libp2p config const ipns = new IPNSAPI(options) const dagReader = DagAPI.reader({ ipld, preload }) diff --git a/packages/ipfs-core/src/components/is-online.js b/packages/ipfs-core/src/components/is-online.js index 1e9593a0f8..9bb6467ef1 100644 --- a/packages/ipfs-core/src/components/is-online.js +++ b/packages/ipfs-core/src/components/is-online.js @@ -10,5 +10,5 @@ module.exports = ({ network }) => */ () => { const net = network.try() - return net != null && net.libp2p.isStarted() + return net != null && Boolean(net.libp2p.isStarted()) } diff --git a/packages/ipfs-core/src/components/key/export.js b/packages/ipfs-core/src/components/key/export.js index cbf432ed1c..b4ccc2f661 100644 --- a/packages/ipfs-core/src/components/key/export.js +++ b/packages/ipfs-core/src/components/key/export.js @@ -23,11 +23,10 @@ module.exports = ({ keychain }) => { * ``` * @param {string} name - The name of the key to export * @param {string} password - Password to set on the PEM output - * @param {import('.').AbortOptions} options * @returns {Promise} - The string representation of the key */ - const exportKey = (name, password, options) => - keychain.exportKey(name, password, options) + const exportKey = (name, password) => + keychain.exportKey(name, password) return withTimeoutOption(exportKey) } diff --git a/packages/ipfs-core/src/components/key/import.js b/packages/ipfs-core/src/components/key/import.js index 047ae4d5ce..35eebe224d 100644 --- a/packages/ipfs-core/src/components/key/import.js +++ b/packages/ipfs-core/src/components/key/import.js @@ -21,11 +21,10 @@ module.exports = ({ keychain }) => { * @param {string} name - The name of the key to import * @param {string} pem - The PEM encoded key * @param {string} password - The password that protects the PEM key - * @param {import('.').AbortOptions} options * @returns {Promise} - An object that describes the new key */ - const importKey = (name, pem, password, options) => { - return keychain.importKey(name, pem, password, options) + const importKey = (name, pem, password) => { + return keychain.importKey(name, pem, password) } return withTimeoutOption(importKey) diff --git a/packages/ipfs-core/src/components/key/info.js b/packages/ipfs-core/src/components/key/info.js index 491eed66b6..d585027a8c 100644 --- a/packages/ipfs-core/src/components/key/info.js +++ b/packages/ipfs-core/src/components/key/info.js @@ -9,10 +9,9 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') module.exports = ({ keychain }) => { /** * @param {string} name - * @param {AbortOptions} [options] * @returns {Promise} */ - const info = (name, options = {}) => keychain.findKeyByName(name, options) + const info = (name) => keychain.findKeyByName(name) return withTimeoutOption(info) } diff --git a/packages/ipfs-core/src/components/key/list.js b/packages/ipfs-core/src/components/key/list.js index 379b388547..66e05baca5 100644 --- a/packages/ipfs-core/src/components/key/list.js +++ b/packages/ipfs-core/src/components/key/list.js @@ -23,18 +23,9 @@ module.exports = ({ keychain }) => { * // ] * ``` * - * @param {AbortOptions} [options] - * @returns {Promise} + * @returns {Promise} */ - const list = (options = {}) => keychain.listKeys(options) + const list = () => keychain.listKeys() return withTimeoutOption(list) } - -/** - * @typedef {Object} KeyEntry - * @property {string} name - The name of the key - * @property {string} hash - The hash of the key - * - * @typedef {import('.').AbortOptions} AbortOptions - */ diff --git a/packages/ipfs-core/src/components/key/rename.js b/packages/ipfs-core/src/components/key/rename.js index 26b49c807d..2012a81f87 100644 --- a/packages/ipfs-core/src/components/key/rename.js +++ b/packages/ipfs-core/src/components/key/rename.js @@ -22,11 +22,11 @@ module.exports = ({ keychain }) => { * ``` * @param {string} oldName - The current key name * @param {string} newName - The desired key name - * @param {AbortOptions} [options] * @returns {Promise} */ - const rename = async (oldName, newName, options = {}) => { - const key = await keychain.renameKey(oldName, newName, options) + const rename = async (oldName, newName) => { + const key = await keychain.renameKey(oldName, newName) + return { was: oldName, now: key.name, diff --git a/packages/ipfs-core/src/components/key/rm.js b/packages/ipfs-core/src/components/key/rm.js index 2004a6553b..a27dd8e091 100644 --- a/packages/ipfs-core/src/components/key/rm.js +++ b/packages/ipfs-core/src/components/key/rm.js @@ -20,10 +20,9 @@ module.exports = ({ keychain }) => { * ``` * * @param {string} name - The name of the key to remove - * @param {import('.').AbortOptions} options * @returns {Promise} - An object that describes the removed key */ - const rm = (name, options) => keychain.removeKey(name, options) + const rm = (name) => keychain.removeKey(name) return withTimeoutOption(rm) } diff --git a/packages/ipfs-core/src/components/libp2p.js b/packages/ipfs-core/src/components/libp2p.js index c985b5632f..f0437c0945 100644 --- a/packages/ipfs-core/src/components/libp2p.js +++ b/packages/ipfs-core/src/components/libp2p.js @@ -164,6 +164,6 @@ function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, p * @typedef {import('.').PeerId} PeerId * @typedef {import('.').Options} IPFSOptions * @typedef {import('libp2p')} LibP2P - * @typedef {import('libp2p').Options} Options + * @typedef {import('libp2p').Libp2pOptions} Options * @typedef {import('.').IPFSConfig} IPFSConfig */ diff --git a/packages/ipfs-core/src/components/ping.js b/packages/ipfs-core/src/components/ping.js index 077986d66d..295263085d 100644 --- a/packages/ipfs-core/src/components/ping.js +++ b/packages/ipfs-core/src/components/ping.js @@ -35,21 +35,28 @@ module.exports = ({ network }) => { peerId = PeerId.createFromCID(peerId) } - let peer = libp2p.peerStore.get(peerId) + const storedPeer = libp2p.peerStore.get(peerId) + let id = storedPeer && storedPeer.id - if (!peer) { + if (!id) { yield { ...basePacket, text: `Looking up peer ${peerId}` } - peer = await libp2p.peerRouting.findPeer(peerId) + const remotePeer = await libp2p.peerRouting.findPeer(peerId) + + id = remotePeer && remotePeer.id + } + + if (!id) { + throw new Error('Peer was not found') } - yield { ...basePacket, text: `PING ${peer.id.toB58String()}` } + yield { ...basePacket, text: `PING ${id.toB58String()}` } let packetCount = 0 let totalTime = 0 for (let i = 0; i < options.count; i++) { try { - const time = await libp2p.ping(peer.id) + const time = await libp2p.ping(id) totalTime += time packetCount++ yield { ...basePacket, time } diff --git a/packages/ipfs-core/src/components/pubsub.js b/packages/ipfs-core/src/components/pubsub.js index db671706f0..56b659477a 100644 --- a/packages/ipfs-core/src/components/pubsub.js +++ b/packages/ipfs-core/src/components/pubsub.js @@ -41,6 +41,7 @@ module.exports = ({ network, config }) => { */ async function subscribe (topic, handler, options) { const { libp2p } = await network.use(options) + // @ts-ignore Libp2p Pubsub is deprecating the handler, using the EventEmitter return libp2p.pubsub.subscribe(topic, handler, options) } @@ -75,6 +76,7 @@ module.exports = ({ network, config }) => { */ async function unsubscribe (topic, handler, options) { const { libp2p } = await network.use(options) + // @ts-ignore Libp2p Pubsub is deprecating the handler, using the EventEmitter libp2p.pubsub.unsubscribe(topic, handler, options) } @@ -111,7 +113,7 @@ module.exports = ({ network, config }) => { */ async function ls (options) { const { libp2p } = await network.use(options) - return libp2p.pubsub.getTopics(options) + return libp2p.pubsub.getTopics() } /** @@ -131,7 +133,7 @@ module.exports = ({ network, config }) => { */ async function peers (topic, options) { const { libp2p } = await network.use(options) - return libp2p.pubsub.getSubscribers(topic, options) + return libp2p.pubsub.getSubscribers(topic) } } diff --git a/packages/ipfs-core/src/components/stats/bw.js b/packages/ipfs-core/src/components/stats/bw.js index 5634bf029c..a418e0d688 100644 --- a/packages/ipfs-core/src/components/stats/bw.js +++ b/packages/ipfs-core/src/components/stats/bw.js @@ -13,7 +13,9 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option') function getBandwidthStats (libp2p, opts) { let stats - if (opts.peer) { + if (!libp2p.metrics) { + stats = undefined + } else if (opts.peer) { stats = libp2p.metrics.forPeer(opts.peer) } else if (opts.proto) { stats = libp2p.metrics.forProtocol(opts.proto) @@ -85,7 +87,7 @@ module.exports = ({ network }) => { /** * @typedef {Object} BWOptions - * @property {PeerId|CID|string} [peer] - Specifies a peer to print bandwidth for + * @property {PeerId} [peer] - Specifies a peer to print bandwidth for * @property {string} [proto] - Specifies a protocol to print bandwidth for * @property {boolean} [poll] - Is used to yield bandwidth info at an interval * @property {number|string} [interval=1000] - The time interval to wait between updating output, if `poll` is `true`. diff --git a/packages/ipfs-core/src/components/storage.js b/packages/ipfs-core/src/components/storage.js index 4152d3bd24..9cf7d1b0dd 100644 --- a/packages/ipfs-core/src/components/storage.js +++ b/packages/ipfs-core/src/components/storage.js @@ -43,6 +43,8 @@ class Storage { const { peerId, keychain, isNew } = await loadRepo(repo, options) + // TODO: throw error? + // @ts-ignore On start, keychain will always be available return new Storage(peerId, keychain, repo, print, isNew) } } @@ -52,7 +54,7 @@ module.exports = Storage * * @param {Repo} repo * @param {RepoOptions & InitOptions} options - * @returns {Promise<{peerId: PeerId, keychain:Keychain, isNew:boolean }>} + * @returns {Promise<{peerId: PeerId, keychain?: Keychain, isNew:boolean }>} */ const loadRepo = async (repo, options) => { const openError = await openRepo(repo) @@ -96,7 +98,7 @@ const openRepo = async (repo) => { /** * @param {Repo} repo * @param {RepoOptions & InitOptions} options - * @returns {Promise<{peerId: PeerId, keychain:Keychain}>} + * @returns {Promise<{peerId: PeerId, keychain?: Keychain}>} */ const initRepo = async (repo, options) => { // 1. Verify that repo does not exist yet (if it does and we could not @@ -197,7 +199,7 @@ const peerIdToIdentity = (peerId) => ({ * * @param {Repo} repo * @param {ConfigureOptions} options - * @returns {Promise<{peerId: PeerId, keychain:Keychain}>} + * @returns {Promise<{peerId: PeerId, keychain?: Keychain}>} */ const configureRepo = async (repo, { config, profiles, pass }) => { const original = await repo.config.getAll() @@ -297,5 +299,5 @@ const applyProfiles = (config, profiles) => { * @typedef {import('.').IPFSConfig} IPFSConfig * @typedef {import('../interface/repo').Repo} Repo * @typedef {import('libp2p-crypto').KeyType} KeyType - * @typedef {import('libp2p').LibP2PKeychain} Keychain + * @typedef {import('libp2p/src/keychain')} Keychain */ diff --git a/packages/ipfs-core/src/components/swarm/addrs.js b/packages/ipfs-core/src/components/swarm/addrs.js index 03000ed58a..28ae132fd3 100644 --- a/packages/ipfs-core/src/components/swarm/addrs.js +++ b/packages/ipfs-core/src/components/swarm/addrs.js @@ -16,7 +16,7 @@ module.exports = ({ network }) => { async function addrs (options) { // eslint-disable-line require-await const peers = [] const { libp2p } = await network.use(options) - for (const [peerId, peer] of libp2p.peerStore.peers.entries(options)) { + for (const [peerId, peer] of libp2p.peerStore.peers.entries()) { peers.push({ id: peerId, addrs: peer.addresses.map((mi) => mi.multiaddr) diff --git a/packages/ipfs-core/src/components/swarm/connect.js b/packages/ipfs-core/src/components/swarm/connect.js index 306aba2487..49c078a3b8 100644 --- a/packages/ipfs-core/src/components/swarm/connect.js +++ b/packages/ipfs-core/src/components/swarm/connect.js @@ -16,7 +16,7 @@ module.exports = ({ network }) => { */ async function connect (addr, options) { const { libp2p } = await network.use(options) - return libp2p.dial(addr, options) + await libp2p.dial(addr, options) } return withTimeoutOption(connect) diff --git a/packages/ipfs-core/src/components/swarm/disconnect.js b/packages/ipfs-core/src/components/swarm/disconnect.js index 7bfdbbb839..6e3c8ad0f7 100644 --- a/packages/ipfs-core/src/components/swarm/disconnect.js +++ b/packages/ipfs-core/src/components/swarm/disconnect.js @@ -16,7 +16,7 @@ module.exports = ({ network }) => { */ async function disconnect (addr, options) { const { libp2p } = await network.use(options) - return libp2p.hangUp(addr, options) + await libp2p.hangUp(addr) } return withTimeoutOption(disconnect) diff --git a/packages/ipfs-core/test/libp2p.spec.js b/packages/ipfs-core/test/libp2p.spec.js index 7991ca1ac4..e347f29b9e 100644 --- a/packages/ipfs-core/test/libp2p.spec.js +++ b/packages/ipfs-core/test/libp2p.spec.js @@ -148,9 +148,7 @@ describe('libp2p customization', function () { }, pubsub: { enabled: true, - emitSelf: true, - signMessages: true, - strictSigning: true + emitSelf: true } }) const transports = Array.from(libp2p.transportManager.getTransports()) diff --git a/packages/ipfs-daemon/package.json b/packages/ipfs-daemon/package.json index d6e055957c..8e808baa64 100644 --- a/packages/ipfs-daemon/package.json +++ b/packages/ipfs-daemon/package.json @@ -37,7 +37,7 @@ "ipfs-http-server": "^0.1.4", "ipfs-utils": "^5.0.0", "just-safe-set": "^2.1.0", - "libp2p": "^0.29.3", + "libp2p": "^0.30.0", "libp2p-delegated-content-routing": "^0.8.0", "libp2p-delegated-peer-routing": "^0.8.0", "libp2p-webrtc-star": "^0.20.1", diff --git a/packages/ipfs/package.json b/packages/ipfs/package.json index 7c65fc8a14..41cedf256d 100644 --- a/packages/ipfs/package.json +++ b/packages/ipfs/package.json @@ -51,7 +51,7 @@ "go-ipfs": "^0.7.0", "interface-ipfs-core": "^0.142.3", "ipfs-http-client": "^48.1.3", - "ipfs-interop": "^3.0.0", + "ipfs-interop": "^4.0.1", "ipfs-utils": "^5.0.0", "ipfsd-ctl": "^7.1.1", "iso-url": "^1.0.0",