Skip to content
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

chore: update to new multiformats #200

Merged
merged 4 commits into from
Jul 6, 2021
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
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test": "npm run test:node && npm run test:browser",
"test:node": "aegir test --ts -t node",
"test:browser": "aegir test -t browser",
"build": "aegir build",
"prepare": "aegir build",
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be reverted

Copy link
Member Author

Choose a reason for hiding this comment

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

Without this you can't depend on a branch of this repo as otherwise it has no types after install. prepare is run during release so it shouldn't break anything?

Copy link
Member

Choose a reason for hiding this comment

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

We can keep it, but I would not remove the build script. Perhaps just add a new one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can do, but then it'll build twice during release since npm runs prepare and aegir runs build

"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
Expand All @@ -34,11 +34,10 @@
"./src/resolvers/dns.js": "./src/resolvers/dns.browser.js"
},
"dependencies": {
"cids": "^1.0.0",
"dns-over-http-resolver": "^1.0.0",
"err-code": "^3.0.1",
"is-ip": "^3.1.0",
"multibase": "^4.0.2",
"multiformats": "^9.0.2",
"uint8arrays": "^2.1.3",
"varint": "^6.0.0"
},
Expand Down
23 changes: 16 additions & 7 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

const ip = require('./ip')
const protocols = require('./protocols-table')
const CID = require('cids')
const multibase = require('multibase')
const { CID } = require('multiformats/cid')
const { base32 } = require('multiformats/bases/base32')
const { base58btc } = require('multiformats/bases/base58')
const Digest = require('multiformats/hashes/digest')
const varint = require('varint')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')
Expand Down Expand Up @@ -160,11 +162,18 @@ function bytes2str (buf) {
}

/**
* @param {string | Uint8Array | CID} hash
* @param {string} hash - base58btc string
*/
function mh2bytes (hash) {
let mh

if (hash[0] === 'Q' || hash[0] === '1') {
mh = Digest.decode(base58btc.decode(`z${hash}`)).bytes
} else {
mh = CID.parse(hash).multihash.bytes
}

// the address is a varint prefixed multihash string representation
const mh = new CID(hash).multihash
const size = Uint8Array.from(varint.encode(mh.length))
return uint8ArrayConcat([size, mh], size.length + mh.length)
}
Expand All @@ -173,7 +182,7 @@ function mh2bytes (hash) {
* Converts bytes to bas58btc string
*
* @param {Uint8Array} buf
* @returns {string} bas58btc string
* @returns {string} base58btc string
*/
function bytes2mh (buf) {
const size = varint.decode(buf)
Expand All @@ -199,7 +208,7 @@ function onion2bytes (str) {
}

// onion addresses do not include the multibase prefix, add it before decoding
const buf = multibase.decode('b' + addr[0])
const buf = base32.decode('b' + addr[0])

// onion port number
const port = parseInt(addr[1], 10)
Expand All @@ -222,7 +231,7 @@ function onion32bytes (str) {
throw new Error('failed to parse onion addr: ' + addr[0] + ' not a Tor onion3 address.')
}
// onion addresses do not include the multibase prefix, add it before decoding
const buf = multibase.decode('b' + addr[0])
const buf = base32.decode('b' + addr[0])

// onion port number
const port = parseInt(addr[1], 10)
Expand Down
19 changes: 14 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const codec = require('./codec')
const protocols = require('./protocols-table')
const varint = require('varint')
const CID = require('cids')
const { CID } = require('multiformats/cid')
const { base58btc } = require('multiformats/bases/base58')
const errCode = require('err-code')
const inspect = Symbol.for('nodejs.util.inspect.custom')
const uint8ArrayToString = require('uint8arrays/to-string')
Expand Down Expand Up @@ -313,11 +314,19 @@ class Multiaddr {
// Get the last ipfs tuple ['ipfs', 'peerid string']
const tuple = tuples.pop()
if (tuple && tuple[1]) {
// Get multihash, unwrap from CID if needed
return uint8ArrayToString(new CID(tuple[1]).multihash, 'base58btc')
} else {
return null
const peerIdStr = tuple[1]

// peer id is base58btc encoded string but not multibase encoded so add the `z`
// prefix so we can validate that it is correctly encoded
if (peerIdStr[0] === 'Q' || peerIdStr[0] === '1') {
return uint8ArrayToString(base58btc.decode(`z${peerIdStr}`), 'base58btc')
}

// try to parse peer id as CID
return uint8ArrayToString(CID.parse(peerIdStr).multihash.bytes, 'base58btc')
}

return null
} catch (e) {
return null
}
Expand Down
2 changes: 1 addition & 1 deletion src/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const toBytes = function (ip, buff, offset) {
let i
for (i = 0; i < sections.length; i++) {
const isv4 = isV4(sections[i])
var v4Buffer
let v4Buffer

if (isv4) {
v4Buffer = toBytes(sections[i])
Expand Down
5 changes: 5 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,11 @@ describe('helpers', () => {
new Multiaddr('/p2p-circuit/p2p/bafzbeidt255unskpefjmqb2rc27vjuyxopkxgaylxij6pw35hhys4vnyp4').getPeerId()
).to.equal('QmW8rAgaaA6sRydK1k6vonShQME47aDxaFidbtMevWs73t')
})
it('extracts the peer Id from a multiaddr, p2p and base58btc encoded identity multihash', () => {
expect(
new Multiaddr('/p2p-circuit/p2p/12D3KooWNvSZnPi3RrhrTwEY4LuuBeB6K6facKUCJcyWG1aoDd2p').getPeerId()
).to.equal('12D3KooWNvSZnPi3RrhrTwEY4LuuBeB6K6facKUCJcyWG1aoDd2p')
})
})

describe('.getPeerId should return null on missing peer id in multiaddr', () => {
Expand Down