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 2 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
24 changes: 19 additions & 5 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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 varint = require('varint')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')
Expand Down Expand Up @@ -163,8 +163,22 @@ function bytes2str (buf) {
* @param {string | Uint8Array | CID} hash
*/
function mh2bytes (hash) {
let cid

if (typeof hash === 'string') {
Copy link
Member

Choose a reason for hiding this comment

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

We have this same logic in PeerId as well. Thinking if cid could offer a way of doing it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Could probably add it to CID.asCID, though I think that's supposed to convert between old and new CID instances, not try to parse whatever we throw at it. This is quite a common use case though.

Copy link
Member

Choose a reason for hiding this comment

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

This is quite a common use case though

Can we export an util static function for this in multiformats then?

Copy link
Member Author

Choose a reason for hiding this comment

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

cid = CID.parse(hash)
} else if (hash instanceof Uint8Array) {
cid = CID.decode(hash)
} else {
cid = CID.asCID(cid)
}

if (!cid) {
throw new Error('Invalid CID')
}

// the address is a varint prefixed multihash string representation
const mh = new CID(hash).multihash
const mh = cid.multihash.bytes
const size = Uint8Array.from(varint.encode(mh.length))
return uint8ArrayConcat([size, mh], size.length + mh.length)
}
Expand Down Expand Up @@ -199,7 +213,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 +236,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
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const codec = require('./codec')
const protocols = require('./protocols-table')
const varint = require('varint')
const CID = require('cids')
const { CID } = require('multiformats/cid')
const errCode = require('err-code')
const inspect = Symbol.for('nodejs.util.inspect.custom')
const uint8ArrayToString = require('uint8arrays/to-string')
Expand Down Expand Up @@ -314,7 +314,7 @@ class Multiaddr {
const tuple = tuples.pop()
if (tuple && tuple[1]) {
// Get multihash, unwrap from CID if needed
return uint8ArrayToString(new CID(tuple[1]).multihash, 'base58btc')
return uint8ArrayToString(CID.parse(tuple[1]).multihash.bytes, 'base58btc')
} else {
return null
}
Expand Down