Skip to content

feat: adding circuit addresses #14

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 11 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ dist
coverage

*.log

.vscode
.history
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mafmt",
"version": "2.1.6",
"version": "2.1.7",
"description": "A multiaddr validator",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -33,17 +33,18 @@
},
"homepage": "https://github.com/whyrusleeping/js-mafmt#readme",
"devDependencies": {
"aegir": "^9.3.3",
"aegir": "^11.0.0",
"chai": "^3.5.0",
"pre-commit": "^1.2.2"
},
"dependencies": {
"multiaddr": "^2.2.0"
"multiaddr": "^2.2.2"
},
"contributors": [
"David Dias <[email protected]>",
"Jeromy <[email protected]>",
"Jeromy Johnson <[email protected]>",
"dignifiedquire <[email protected]>"
"dignifiedquire <[email protected]>",
"dmitriy ryajov <[email protected]>"
]
}
42 changes: 37 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const multiaddr = require('multiaddr')
*/
const DNS4 = base('dns4')
const DNS6 = base('dns6')
const DNS = or(
const _DNS = or(
base('dns'),
DNS4,
DNS6
Expand All @@ -18,6 +18,11 @@ const TCP = and(IP, base('tcp'))
const UDP = and(IP, base('udp'))
const UTP = and(UDP, base('utp'))

const DNS = or(
and(_DNS, base('tcp')),
_DNS
)

const WebSockets = or(
and(TCP, base('ws')),
and(DNS, base('ws'))
Expand Down Expand Up @@ -51,9 +56,34 @@ const Reliable = or(
UTP
)

const IPFS = or(
let _IPFS = or(
and(Reliable, base('ipfs')),
WebRTCStar
WebRTCStar,
base('ipfs')
)

const _Circuit = or(
and(_IPFS, base('p2p-circuit'), _IPFS),
and(_IPFS, base('p2p-circuit')),
and(base('p2p-circuit'), _IPFS),
and(Reliable, base('p2p-circuit')),
and(base('p2p-circuit'), Reliable),
base('p2p-circuit')
)

const CircuitRecursive = () => or(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need to be a function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mm, that's just to make it recursive, the idea is that if a function is encountered during n match, it will be called, which will expand into addidional match conditions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing, the reason it has to be a function, is because I can reference variables that haven't finished being declared yet... I.e. I can reference circuit from it's own declaration block.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I know what your talking about, we can just do () => Circuit when we need recursivity there.

and(_Circuit, CircuitRecursive),
_Circuit
)

const Circuit = CircuitRecursive()

const IPFS = or(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference on circuit addresses take a look at - https://github.com/libp2p/specs/tree/master/relay#addressing. The above seems to take care of most of the cases, but it hasn't been cleaned up, I'll try to do it once I've got everything ironed out.

and(Circuit, _IPFS, Circuit),
and(_IPFS, Circuit),
and(Circuit, _IPFS),
Circuit,
_IPFS
)

exports.DNS = DNS
Expand All @@ -69,6 +99,7 @@ exports.WebSocketsSecure = WebSocketsSecure
exports.WebRTCStar = WebRTCStar
exports.WebRTCDirect = WebRTCDirect
exports.Reliable = Reliable
exports.Circuit = Circuit
exports.IPFS = IPFS

/*
Expand All @@ -94,7 +125,7 @@ function and () {
return null
}
args.some(function (arg) {
a = arg.partialMatch(a)
a = typeof arg === 'function' ? arg().partialMatch(a) : arg.partialMatch(a)
if (a === null) {
return true
}
Expand Down Expand Up @@ -127,7 +158,7 @@ function or () {
function partialMatch (a) {
let out = null
args.some(function (arg) {
const res = arg.partialMatch(a)
const res = typeof arg === 'function' ? arg().partialMatch(a) : arg.partialMatch(a)
if (res) {
out = res
return true
Expand All @@ -149,6 +180,7 @@ function or () {

function base (n) {
const name = n

function matches (a) {
if (typeof a === 'string') {
a = multiaddr(a)
Expand Down
36 changes: 33 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ describe('multiaddr validation', function () {
'/dns/ipfs.io',
'/dns4/ipfs.io',
'/dns4/libp2p.io',
'/dns6/protocol.ai'
'/dns6/protocol.ai',
'/dns4/protocol.ai/tcp/80',
'/dns6/protocol.ai/tcp/80',
'/dns/protocol.ai/tcp/80'
]

const badDNS = [
Expand Down Expand Up @@ -92,11 +95,33 @@ describe('multiaddr validation', function () {
'/ip6/::/ip4/0.0.0.0/udp/1234/wss'
]

const goodCircuit = [
'/p2p-circuit',
'/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/p2p-circuit/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/p2p-circuit/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/p2p-circuit/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/p2p-circuit/ip4/127.0.0.1/tcp/4002/ipfs/QmddWMcQX6orJGHpETYMyPgXrCXCtYANMFVDCvhKoDwLqA',
'/p2p-circuit/ipfs/QmddWMcQX6orJGHpETYMyPgXrCXCtYANMFVDCvhKoDwLqA',
'/p2p-circuit/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/' +
'p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whyrusleeping check this examples, @dryajov pretty much covered them all from @lgierth notes.

]

const badCircuit = [
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss',
'/ip4/0.0.7.6/udp/1234',
'/ip6/::/udp/0/utp',
'/dns/ipfs.io/ws',
'/libp2p-webrtc-direct/ip4/1.2.3.4/tcp/3456/http'
]

const goodIPFS = [
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit',
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj'
].concat(goodCircuit)

function assertMatches (p) {
const tests = Array.from(arguments).slice(1)
Expand Down Expand Up @@ -166,6 +191,11 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.WebRTCDirect, goodIP, goodUDP, badWS)
})

it('Circuit validation', function () {
assertMatches(mafmt.Circuit, goodCircuit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have tests for mismatches as well.

assertMismatches(mafmt.Circuit, badCircuit)
})

it('IPFS validation', function () {
assertMatches(mafmt.IPFS, goodIPFS)
})
Expand Down