Skip to content

Commit

Permalink
fix: use multibase and add buffer
Browse files Browse the repository at this point in the history
Adds buffer related to ipfs/js-ipfs#2924

Uses multibase to encode.
  • Loading branch information
hugomrdias committed Mar 13, 2020
1 parent 072326e commit dbc8607
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ test/test-data/go-ipfs-repo/LOG.old

# while testing npm5
package-lock.json
yarn.lock
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
"bugs": "https://github.com/multiformats/js-multiaddr/issues",
"homepage": "https://github.com/multiformats/js-multiaddr",
"dependencies": {
"bs58": "^4.0.1",
"buffer": "^5.5.0",
"cids": "~0.7.1",
"class-is": "^1.1.0",
"hi-base32": "~0.5.0",
"ip": "^1.1.5",
"is-ip": "^3.1.0",
"multibase": "^0.6.0",
"varint": "^5.0.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/codec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { Buffer } = require('buffer')
const convert = require('./convert')
const protocols = require('./protocols-table')
const varint = require('varint')
Expand Down
14 changes: 7 additions & 7 deletions src/convert.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

const ip = require('ip')
const { Buffer } = require('buffer')
const isIp = require('is-ip')
const protocols = require('./protocols-table')
const bs58 = require('bs58')
const CID = require('cids')
const base32 = require('hi-base32')
const multibase = require('multibase')
const varint = require('varint')

module.exports = Convert
Expand Down Expand Up @@ -138,8 +138,7 @@ function buf2mh (buf) {
if (address.length !== size) {
throw new Error('inconsistent lengths')
}

return bs58.encode(address)
return multibase.encode('base58btc', address).toString().slice(1)
}

function onion2buf (str) {
Expand All @@ -150,7 +149,8 @@ function onion2buf (str) {
if (addr[0].length !== 16) {
throw new Error('failed to parse onion addr: ' + addr[0] + ' not a Tor onion address.')
}
const buf = Buffer.from(base32.decode.asBytes(addr[0].toUpperCase()))

const buf = multibase.decode('b' + addr[0])

// onion port number
const port = parseInt(addr[1], 10)
Expand All @@ -169,7 +169,7 @@ function onion32buf (str) {
if (addr[0].length !== 56) {
throw new Error('failed to parse onion addr: ' + addr[0] + ' not a Tor onion3 address.')
}
const buf = Buffer.from(base32.decode.asBytes(addr[0].toUpperCase()))
const buf = multibase.decode('b' + addr[0])

// onion port number
const port = parseInt(addr[1], 10)
Expand All @@ -183,7 +183,7 @@ function onion32buf (str) {
function buf2onion (buf) {
const addrBytes = buf.slice(0, buf.length - 2)
const portBytes = buf.slice(buf.length - 2)
const addr = base32.encode(addrBytes).toLowerCase()
const addr = multibase.encode('base32', addrBytes).toString().slice(1)
const port = buf2port(portBytes)
return addr + ':' + port
}
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const codec = require('./codec')
const { Buffer } = require('buffer')
const protocols = require('./protocols-table')
const varint = require('varint')
const bs58 = require('bs58')
const multibase = require('multibase')
const CID = require('cids')
const withIs = require('class-is')
const inspect = Symbol.for('nodejs.util.inspect.custom')
Expand Down Expand Up @@ -308,7 +309,7 @@ Multiaddr.prototype.getPeerId = function getPeerId () {
// Get the last id
b58str = tuples.pop()[1]
// Get multihash, unwrap from CID if needed
b58str = bs58.encode(new CID(b58str).multihash)
b58str = multibase.encode('base58btc', new CID(b58str).multihash).toString().slice(1)
} catch (e) {
b58str = null
}
Expand Down

0 comments on commit dbc8607

Please sign in to comment.