-
Notifications
You must be signed in to change notification settings - Fork 65
feat: resolve multiaddrs #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
569923c
feat: resolve multiaddrs
vasco-santos f064934
chore: use node lts and stable for ci
vasco-santos 5957d95
chore: revert recursive option for now
vasco-santos e309d53
chore: use released package
vasco-santos eeb5822
chore: address review
vasco-santos b0f26ad
chore: address review
vasco-santos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ stages: | |
| - cov | ||
|
|
||
| node_js: | ||
| - '10' | ||
| - 'lts/*' | ||
| - 'stable' | ||
|
|
||
| os: | ||
| - linux | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,8 @@ | |
| "dependencies": { | ||
| "cids": "^1.0.0", | ||
| "class-is": "^1.1.0", | ||
| "dns-over-http-resolver": "^1.0.0", | ||
| "err-code": "^2.0.3", | ||
| "is-ip": "^3.1.0", | ||
| "multibase": "^3.0.0", | ||
| "uint8arrays": "^1.1.0", | ||
|
|
@@ -45,6 +47,7 @@ | |
| "@types/mocha": "^8.0.1", | ||
| "@types/node": "^14.0.11", | ||
| "aegir": "^26.0.0", | ||
| "sinon": "^9.2.0", | ||
| "typescript": "^3.9.5" | ||
| }, | ||
| "contributors": [ | ||
|
|
@@ -79,4 +82,4 @@ | |
| "Linus Unnebäck <[email protected]>", | ||
| "Alex Potsides <[email protected]>" | ||
| ] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| 'use strict' | ||
|
|
||
| let dns | ||
|
|
||
| try { | ||
| dns = require('dns').promises | ||
| } catch (err) { | ||
| dns = require('dns-over-http-resolver') | ||
| } | ||
|
|
||
| module.exports = dns |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 'use strict' | ||
|
|
||
| const Multiaddr = require('..') // eslint-disable-line no-unused-vars | ||
| const protocols = require('../protocols-table') | ||
|
|
||
| const { code: dnsaddrCode } = protocols('dnsaddr') | ||
|
|
||
| /** | ||
| * Resolver for dnsaddr addresses. | ||
| * | ||
| * @param {Multiaddr} addr | ||
| * @returns {Promise<Array<string>>} | ||
| */ | ||
| async function dnsaddrResolver (addr) { | ||
| const { Resolver } = require('./dns') | ||
| const resolver = new Resolver() | ||
|
|
||
| const peerId = addr.getPeerId() | ||
| const [, hostname] = addr.stringTuples().find(([proto]) => proto === dnsaddrCode) || [] | ||
|
|
||
| const records = await resolver.resolveTxt(`_dnsaddr.${hostname}`) | ||
| // @ts-ignore | ||
| let addresses = records.flat().map((a) => a.split('=')[1]) | ||
|
|
||
| if (peerId) { | ||
| addresses = addresses.filter((entry) => entry.includes(peerId)) | ||
| } | ||
|
|
||
| return addresses | ||
| } | ||
|
|
||
| module.exports = { | ||
| dnsaddrResolver | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* eslint-env mocha */ | ||
| 'use strict' | ||
|
|
||
| const { expect } = require('aegir/utils/chai') | ||
| const sinon = require('sinon') | ||
|
|
||
| const multiaddr = require('../src') | ||
| const resolvers = require('../src/resolvers') | ||
| const { Resolver } = require('../src/resolvers/dns') | ||
|
|
||
| const dnsaddrStub1 = [ | ||
| ['dnsaddr=/dnsaddr/ams-1.bootstrap.libp2p.io/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd'], | ||
| ['dnsaddr=/dnsaddr/ams-2.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'], | ||
| ['dnsaddr=/dnsaddr/lon-1.bootstrap.libp2p.io/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3'], | ||
| ['dnsaddr=/dnsaddr/nrt-1.bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'], | ||
| ['dnsaddr=/dnsaddr/nyc-1.bootstrap.libp2p.io/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm'], | ||
| ['dnsaddr=/dnsaddr/sfo-2.bootstrap.libp2p.io/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'] | ||
| ] | ||
jacobheun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const dnsaddrStub2 = [ | ||
| ['dnsaddr=/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'], | ||
| ['dnsaddr=/ip4/147.75.83.83/tcp/443/wss/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'], | ||
| ['dnsaddr=/ip4/147.75.83.83/udp/4001/quic/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'], | ||
| ['dnsaddr=/ip6/2604:1380:2000:7a00::1/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'], | ||
| ['dnsaddr=/ip6/2604:1380:2000:7a00::1/tcp/443/wss/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'], | ||
| ['dnsaddr=/ip6/2604:1380:2000:7a00::1/udp/4001/quic/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'] | ||
| ] | ||
|
|
||
| describe('multiaddr resolve', () => { | ||
| it('should throw if no resolver is available', async () => { | ||
| const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io') | ||
|
|
||
| // Resolve | ||
| await expect(ma.resolve()).to.eventually.be.rejected() | ||
| .and.to.have.property('code', 'ERR_NO_AVAILABLE_RESOLVER') | ||
| }) | ||
|
|
||
| describe('dnsaddr', () => { | ||
| before(() => { | ||
| // Set resolvers | ||
| multiaddr.resolvers.set('dnsaddr', resolvers.dnsaddrResolver) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| sinon.restore() | ||
| }) | ||
|
|
||
| it('can resolve dnsaddr without no peerId', async () => { | ||
| const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io') | ||
|
|
||
| const stub = sinon.stub(Resolver.prototype, 'resolveTxt') | ||
| stub.onCall(0).returns(Promise.resolve(dnsaddrStub1)) | ||
|
|
||
| // Resolve | ||
| const resolvedMas = await ma.resolve() | ||
|
|
||
| expect(resolvedMas).to.have.length(dnsaddrStub1.length) | ||
| resolvedMas.forEach((ma, index) => { | ||
| const stubAddr = dnsaddrStub1[index][0].split('=')[1] | ||
|
|
||
| expect(ma.equals(multiaddr(stubAddr))).to.equal(true) | ||
| }) | ||
| }) | ||
|
|
||
| it('can resolve dnsaddr with peerId', async () => { | ||
| const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb') | ||
|
|
||
| const stub = sinon.stub(Resolver.prototype, 'resolveTxt') | ||
| stub.onCall(0).returns(Promise.resolve(dnsaddrStub1)) | ||
| stub.onCall(1).returns(Promise.resolve(dnsaddrStub2)) | ||
|
|
||
| // Resolve | ||
| const resolvedMas = await ma.resolve() | ||
|
|
||
| expect(resolvedMas).to.have.length(1) | ||
| expect(resolvedMas[0].equals(multiaddr('/dnsaddr/ams-2.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'))).to.eql(true) | ||
| }) | ||
|
|
||
| it('can resolve dnsaddr with peerId two levels', async () => { | ||
| const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb') | ||
|
|
||
| const stub = sinon.stub(Resolver.prototype, 'resolveTxt') | ||
| stub.onCall(0).returns(Promise.resolve(dnsaddrStub1)) | ||
| stub.onCall(1).returns(Promise.resolve(dnsaddrStub2)) | ||
|
|
||
| // Resolve | ||
| const resolvedInitialMas = await ma.resolve() | ||
| const resolvedSecondMas = await Promise.all(resolvedInitialMas.map(nm => { | ||
| // nm.resolvers.set('dnsaddr', resolvers.dnsaddrResolver) | ||
| return nm.resolve() | ||
| })) | ||
| // @ts-ignore | ||
| const resolvedMas = resolvedSecondMas.flat() | ||
|
|
||
| expect(resolvedMas).to.have.length(dnsaddrStub2.length) | ||
| resolvedMas.forEach((ma, index) => { | ||
| const stubAddr = dnsaddrStub2[index][0].split('=')[1] | ||
|
|
||
| expect(ma.equals(multiaddr(stubAddr))).to.equal(true) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.