Skip to content

Commit ac440cb

Browse files
authoredNov 11, 2019
fix: ensure b58 can decode hash (#103)
* fix: ensure b58 can decode hash * chore(test): clean up test names * fix: performance improvement conversion
1 parent 4039f38 commit ac440cb

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ coverage
2727
.lock-wscript
2828

2929
build
30+
docs
3031

3132
# Dependency directory
3233
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git

‎src/convert.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const ip = require('ip')
44
const isIp = require('is-ip')
55
const protocols = require('./protocols-table')
66
const bs58 = require('bs58')
7+
const CID = require('cids')
78
const base32 = require('hi-base32')
89
const varint = require('varint')
910

@@ -125,7 +126,7 @@ function buf2str (buf) {
125126

126127
function mh2buf (hash) {
127128
// the address is a varint prefixed multihash string representation
128-
const mh = Buffer.from(bs58.decode(hash))
129+
const mh = new CID(hash).multihash
129130
const size = Buffer.from(varint.encode(mh.length))
130131
return Buffer.concat([size, mh])
131132
}

‎test/index.spec.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ describe('variants', () => {
356356
expect(addr.toString()).to.equal(str)
357357
})
358358

359+
it('p2p', () => {
360+
const str = '/p2p/bafzbeidt255unskpefjmqb2rc27vjuyxopkxgaylxij6pw35hhys4vnyp4'
361+
const addr = multiaddr(str)
362+
expect(addr).to.have.property('buffer')
363+
expect(addr.toString()).to.equal('/p2p/QmW8rAgaaA6sRydK1k6vonShQME47aDxaFidbtMevWs73t')
364+
})
365+
359366
it('ipfs', () => {
360367
const str = '/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'
361368
const addr = multiaddr(str)
@@ -761,7 +768,7 @@ describe('helpers', () => {
761768
})
762769

763770
describe('.getPeerId should parse id from multiaddr', () => {
764-
it('parses extracts the peer Id from a multiaddr, p2p', () => {
771+
it('extracts the peer Id from a multiaddr, p2p', () => {
765772
expect(
766773
multiaddr('/p2p-circuit/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').getPeerId()
767774
).to.equal('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
@@ -771,16 +778,21 @@ describe('helpers', () => {
771778
multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmZR5a9AAXGqQF2ADqoDdGS8zvqv8n3Pag6TDDnTNMcFW6/p2p-circuit/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').getPeerId()
772779
).to.equal('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
773780
})
774-
it('parses extracts the peer Id from a multiaddr, ipfs', () => {
781+
it('extracts the peer Id from a multiaddr, ipfs', () => {
775782
expect(
776783
multiaddr('/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').getPeerId()
777784
).to.equal('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
778785
})
779-
it('parses extracts the peer Id from a multiaddr, p2p and CIDv1 Base32', () => {
786+
it('extracts the peer Id from a multiaddr, p2p and CIDv1 Base32', () => {
780787
expect(
781788
multiaddr('/p2p-circuit/p2p/bafzbeigweq4zr4x4ky2dvv7nanbkw6egutvrrvzw6g3h2rftp7gidyhtt4').getPeerId()
782789
).to.equal('QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi')
783790
})
791+
it('extracts the peer Id from a multiaddr, p2p and CIDv1 Base32, where Id contains non b58 chars', () => {
792+
expect(
793+
multiaddr('/p2p-circuit/p2p/bafzbeidt255unskpefjmqb2rc27vjuyxopkxgaylxij6pw35hhys4vnyp4').getPeerId()
794+
).to.equal('QmW8rAgaaA6sRydK1k6vonShQME47aDxaFidbtMevWs73t')
795+
})
784796
})
785797

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

0 commit comments

Comments
 (0)