From 3517fa111478df9f219debd3ba7ff9ef34421cf7 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 2 Jun 2021 14:24:57 +0100 Subject: [PATCH 1/6] chore: update to new multiformats Depends on: - [ ] https://github.com/multiformats/js-multiformats/pull/91 BREAKING CHANGE: uses the CID class from the new multiformats module --- README.md | 2 +- package.json | 10 +++++----- src/index.js | 4 ++-- test/index.spec.js | 14 +++++++------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ec408c1..fdbefb4 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ const DelegatedPeerRouting = require('libp2p-delegated-peer-routing') const ipfsHttpClient = require('ipfs-http-client') // default is to use ipfs.io -const routing = new DelegatedPeerRouting(ipfsHttpClient({ +const routing = new DelegatedPeerRouting(ipfsHttpClient.create({ // use default api settings protocol: 'https', port: 443, diff --git a/package.json b/package.json index 3c8e033..19e7252 100644 --- a/package.json +++ b/package.json @@ -24,17 +24,17 @@ "devDependencies": { "aegir": "^33.0.0", "go-ipfs": "^0.8.0", - "ipfs-http-client": "^49.0.4", - "ipfs-utils": "^6.0.6", - "ipfsd-ctl": "^7.0.2", + "ipfs-http-client": "^50.1.0", + "ipfs-utils": "^8.1.2", + "ipfsd-ctl": "^8.0.2", "it-all": "^1.0.2" }, "dependencies": { - "cids": "^1.0.0", "debug": "^4.3.1", + "multiformats": "^9.0.2", "p-defer": "^3.0.0", "p-queue": "^6.3.0", - "peer-id": "^0.14.0" + "peer-id": "libp2p/js-peer-id#chore/update-to-new-multiformats" }, "browser": { "go-ipfs": false diff --git a/src/index.js b/src/index.js index fb019e8..c990980 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ 'use strict' const PeerId = require('peer-id') -const CID = require('cids') +const { CID } = require('multiformats') const { default: PQueue } = require('p-queue') const defer = require('p-defer') const debug = require('debug') @@ -89,7 +89,7 @@ class DelegatedPeerRouting { * @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>} */ async * getClosestPeers (key, options = {}) { - key = new CID(key) + key = CID.decode(key) const keyStr = key.toString() log('getClosestPeers starts:', keyStr) diff --git a/test/index.spec.js b/test/index.spec.js index ddc8efd..51e5099 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -74,7 +74,7 @@ describe('DelegatedPeerRouting', function () { }) it('should accept an http api client instance at construction time', () => { - const client = ipfsHttpClient({ + const client = ipfsHttpClient.create({ protocol: 'http', port: 8000, host: 'localhost' @@ -97,7 +97,7 @@ describe('DelegatedPeerRouting', function () { it('should be able to find peers via the delegate with a peer id string', async () => { const opts = delegatedNode.apiAddr.toOptions() - const router = new DelegatedPeerRouting(ipfsHttpClient({ + const router = new DelegatedPeerRouting(ipfsHttpClient.create({ protocol: 'http', port: opts.port, host: opts.host @@ -114,7 +114,7 @@ describe('DelegatedPeerRouting', function () { it('should be able to find peers via the delegate with a peerid', async () => { const opts = delegatedNode.apiAddr.toOptions() - const router = new DelegatedPeerRouting(ipfsHttpClient({ + const router = new DelegatedPeerRouting(ipfsHttpClient.create({ protocol: 'http', port: opts.port, host: opts.host @@ -132,7 +132,7 @@ describe('DelegatedPeerRouting', function () { it('should be able to specify a timeout', async () => { const opts = delegatedNode.apiAddr.toOptions() - const router = new DelegatedPeerRouting(ipfsHttpClient({ + const router = new DelegatedPeerRouting(ipfsHttpClient.create({ protocol: 'http', port: opts.port, host: opts.host @@ -150,7 +150,7 @@ describe('DelegatedPeerRouting', function () { it('should not be able to find peers not on the network', async () => { const opts = delegatedNode.apiAddr.toOptions() - const router = new DelegatedPeerRouting(ipfsHttpClient({ + const router = new DelegatedPeerRouting(ipfsHttpClient.create({ protocol: 'http', port: opts.port, host: opts.host @@ -167,7 +167,7 @@ describe('DelegatedPeerRouting', function () { it('should be able to query for the closest peers', async () => { const opts = delegatedNode.apiAddr.toOptions() - const router = new DelegatedPeerRouting(ipfsHttpClient({ + const router = new DelegatedPeerRouting(ipfsHttpClient.create({ protocol: 'http', port: opts.port, host: opts.host @@ -191,7 +191,7 @@ describe('DelegatedPeerRouting', function () { it('should find closest peers even if the peer doesnt exist', async () => { const opts = delegatedNode.apiAddr.toOptions() - const router = new DelegatedPeerRouting(ipfsHttpClient({ + const router = new DelegatedPeerRouting(ipfsHttpClient.create({ protocol: 'http', port: opts.port, host: opts.host From 1e5df53ce4a211b779929adf50e1e577628ab376 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 2 Jun 2021 17:46:04 +0100 Subject: [PATCH 2/6] chore: create from strings not cids --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index c990980..ff82b3f 100644 --- a/src/index.js +++ b/src/index.js @@ -116,14 +116,14 @@ class DelegatedPeerRouting { // Track the addresses, so we can yield them when done result.responses.forEach(response => { peers.set(response.id, { - id: PeerId.createFromCID(response.id), + id: PeerId.createFromB58String(response.id), multiaddrs: response.addrs }) }) break case 2: // Final Peer yield peers.get(result.id.string) || { - id: PeerId.createFromCID(result.id), + id: PeerId.createFromB58String(result.id), multiaddrs: [] } break From d7957476da0d0b3d4ea59c623e07fe2ab4b94d91 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 3 Jun 2021 18:20:30 +0100 Subject: [PATCH 3/6] chore: do not treat peer id as if it were a cid --- src/index.js | 5 ++--- test/index.spec.js | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index ff82b3f..131239f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ 'use strict' const PeerId = require('peer-id') -const { CID } = require('multiformats') +const { base58btc } = require('multiformats/bases/base58') const { default: PQueue } = require('p-queue') const defer = require('p-defer') const debug = require('debug') @@ -89,8 +89,7 @@ class DelegatedPeerRouting { * @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>} */ async * getClosestPeers (key, options = {}) { - key = CID.decode(key) - const keyStr = key.toString() + const keyStr = base58btc.encode(key).substring(1) log('getClosestPeers starts:', keyStr) options.timeout = options.timeout || DEFAULT_TIMEOUT diff --git a/test/index.spec.js b/test/index.spec.js index 51e5099..4ca4b91 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -174,7 +174,7 @@ describe('DelegatedPeerRouting', function () { })) const nodeId = await delegatedNode.api.id() - const delegatePeerId = PeerID.createFromCID(nodeId.id) + const delegatePeerId = PeerID.createFromB58String(nodeId.id) const key = PeerID.createFromB58String(peerIdToFind.id).id const results = await concat(router.getClosestPeers(key)) @@ -188,7 +188,7 @@ describe('DelegatedPeerRouting', function () { }) }) - it('should find closest peers even if the peer doesnt exist', async () => { + it('should find closest peers even if the peer does not exist', async () => { const opts = delegatedNode.apiAddr.toOptions() const router = new DelegatedPeerRouting(ipfsHttpClient.create({ @@ -198,7 +198,7 @@ describe('DelegatedPeerRouting', function () { })) const nodeId = await delegatedNode.api.id() - const delegatePeerId = PeerID.createFromCID(nodeId.id) + const delegatePeerId = PeerID.createFromB58String(nodeId.id) const peerId = await PeerID.create({ keyType: 'ed25519' }) const results = await concat(router.getClosestPeers(peerId.id)) From 1d6c7ff4d07a9957a628840e8abb8236e10ebf95 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 3 Jun 2021 18:21:55 +0100 Subject: [PATCH 4/6] chore: add prepare script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 19e7252..23ccd90 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ ], "scripts": { "lint": "aegir lint", + "prepare": "npm run build", "build": "aegir build", "test": "aegir test -f test/**/*.spec.js", "test:node": "aegir test -t node -f test/**/*.spec.js", From a4473cea9f58a1e0d59c0c4ffd18c9211115f5e1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 6 Jul 2021 14:19:56 +0100 Subject: [PATCH 5/6] chore: remove gh dep urls --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 23ccd90..dacf79c 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "multiformats": "^9.0.2", "p-defer": "^3.0.0", "p-queue": "^6.3.0", - "peer-id": "libp2p/js-peer-id#chore/update-to-new-multiformats" + "peer-id": "^0.15.0" }, "browser": { "go-ipfs": false From 7f9cf34e42e04567c3ba444860cf9d4007e835e5 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 6 Jul 2021 14:29:40 +0100 Subject: [PATCH 6/6] chore: use parse --- .aegir.js | 2 +- src/index.js | 4 ++-- test/index.spec.js | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.aegir.js b/.aegir.js index 1b23949..feae220 100644 --- a/.aegir.js +++ b/.aegir.js @@ -8,7 +8,7 @@ module.exports = { before: async () => { server = createServer({ host: '127.0.0.1', - port: 57483 + port: 57583 }, { type: 'go', ipfsHttpModule: require('ipfs-http-client'), diff --git a/src/index.js b/src/index.js index 131239f..0fa642e 100644 --- a/src/index.js +++ b/src/index.js @@ -115,14 +115,14 @@ class DelegatedPeerRouting { // Track the addresses, so we can yield them when done result.responses.forEach(response => { peers.set(response.id, { - id: PeerId.createFromB58String(response.id), + id: PeerId.parse(response.id), multiaddrs: response.addrs }) }) break case 2: // Final Peer yield peers.get(result.id.string) || { - id: PeerId.createFromB58String(result.id), + id: PeerId.createFromCID(result.id), multiaddrs: [] } break diff --git a/test/index.spec.js b/test/index.spec.js index 4ca4b91..4e6f209 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -15,7 +15,7 @@ const factory = createFactory({ ipfsBin: isNode ? require('go-ipfs').path() : undefined, test: true, disposable: true, - endpoint: 'http://localhost:57483' + endpoint: 'http://localhost:57583' }) async function spawnNode (bootstrap = []) { @@ -120,7 +120,7 @@ describe('DelegatedPeerRouting', function () { host: opts.host })) - const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id)) + const peer = await router.findPeer(PeerID.parse(peerIdToFind.id)) expect(peer).to.be.ok() const { id, multiaddrs } = peer @@ -138,7 +138,7 @@ describe('DelegatedPeerRouting', function () { host: opts.host })) - const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { timeout: 2000 }) + const peer = await router.findPeer(PeerID.parse(peerIdToFind.id), { timeout: 2000 }) expect(peer).to.be.ok() const { id, multiaddrs } = peer @@ -174,9 +174,9 @@ describe('DelegatedPeerRouting', function () { })) const nodeId = await delegatedNode.api.id() - const delegatePeerId = PeerID.createFromB58String(nodeId.id) + const delegatePeerId = PeerID.parse(nodeId.id) - const key = PeerID.createFromB58String(peerIdToFind.id).id + const key = PeerID.parse(peerIdToFind.id).id const results = await concat(router.getClosestPeers(key)) // we should be closest to the 2 other peers @@ -198,7 +198,7 @@ describe('DelegatedPeerRouting', function () { })) const nodeId = await delegatedNode.api.id() - const delegatePeerId = PeerID.createFromB58String(nodeId.id) + const delegatePeerId = PeerID.parse(nodeId.id) const peerId = await PeerID.create({ keyType: 'ed25519' }) const results = await concat(router.getClosestPeers(peerId.id))