Skip to content

Commit

Permalink
chore: update to new multiformats (#948)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: uses the CID class from the new multiformats module

Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
  • Loading branch information
achingbrain and vasco-santos authored Jul 9, 2021
1 parent 39b0358 commit 13cf476
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 78 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14
- run: npm install
- run: npx aegir lint
- uses: gozala/typescript-error-reporter-action@v1.0.8
Expand Down
10 changes: 5 additions & 5 deletions examples/libp2p-in-the-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"dependencies": {
"@babel/preset-env": "^7.13.0",
"libp2p": "../../",
"libp2p-bootstrap": "^0.12.1",
"libp2p-mplex": "^0.10.0",
"libp2p-noise": "^2.0.0",
"libp2p-webrtc-star": "^0.22.0",
"libp2p-websockets": "^0.15.0"
"libp2p-bootstrap": "^0.13.0",
"libp2p-mplex": "^0.10.4",
"libp2p-noise": "^4.0.0",
"libp2p-webrtc-star": "^0.23.0",
"libp2p-websockets": "^0.16.1"
},
"devDependencies": {
"@babel/cli": "^7.13.10",
Expand Down
6 changes: 3 additions & 3 deletions examples/peer-and-content-routing/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const CID = require('cids')
const { CID } = require('multiformats/cid')
const KadDHT = require('libp2p-kad-dht')

const all = require('it-all')
Expand Down Expand Up @@ -51,10 +51,10 @@ const createNode = async () => {
// Wait for onConnect handlers in the DHT
await delay(100)

const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
const cid = CID.parse('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
await node1.contentRouting.provide(cid)

console.log('Node %s is providing %s', node1.peerId.toB58String(), cid.toBaseEncodedString())
console.log('Node %s is providing %s', node1.peerId.toB58String(), cid.toString())

// wait for propagation
await delay(300)
Expand Down
2 changes: 1 addition & 1 deletion examples/peer-and-content-routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Instead of calling `peerRouting.findPeer`, we will use `contentRouting.provide`

```JavaScript
await node1.contentRouting.provide(cid)
console.log('Node %s is providing %s', node1.peerId.toB58String(), cid.toBaseEncodedString())
console.log('Node %s is providing %s', node1.peerId.toB58String(), cid.toString())

const provs = await all(node3.contentRouting.findProviders(cid, { timeout: 5000 }))

Expand Down
10 changes: 5 additions & 5 deletions examples/webrtc-direct/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
},
"dependencies": {
"libp2p": "../../",
"libp2p-bootstrap": "^0.12.1",
"libp2p-mplex": "^0.10.1",
"libp2p-noise": "^2.0.1",
"libp2p-webrtc-direct": "^0.6.0",
"peer-id": "^0.14.3"
"libp2p-bootstrap": "^0.13.0",
"libp2p-mplex": "^0.10.4",
"libp2p-noise": "^4.0.0",
"libp2p-webrtc-direct": "^0.7.0",
"peer-id": "^0.15.0"
},
"browser": {
"ipfs": "ipfs/dist/index.min.js"
Expand Down
52 changes: 26 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@
"aggregate-error": "^3.1.0",
"any-signal": "^2.1.1",
"bignumber.js": "^9.0.1",
"cids": "^1.1.5",
"class-is": "^1.1.0",
"debug": "^4.3.1",
"err-code": "^3.0.0",
"es6-promisify": "^6.1.1",
"events": "^3.3.0",
"hashlru": "^2.3.0",
"interface-datastore": "^4.0.0",
"interface-datastore": "^5.1.1",
"it-all": "^1.0.4",
"it-buffer": "^0.1.2",
"it-drain": "^1.0.3",
Expand All @@ -100,31 +99,31 @@
"it-handshake": "^2.0.0",
"it-length-prefixed": "^5.0.2",
"it-map": "^1.0.4",
"it-merge": "1.0.0",
"it-merge": "^1.0.0",
"it-pipe": "^1.1.0",
"it-take": "1.0.0",
"it-take": "^1.0.0",
"libp2p-crypto": "^0.19.4",
"libp2p-interfaces": "^0.10.4",
"libp2p-utils": "^0.3.1",
"mafmt": "^9.0.0",
"libp2p-interfaces": "^1.0.0",
"libp2p-interfaces-compliance-tests": "^1.0.0",
"libp2p-utils": "^0.4.0",
"mafmt": "^10.0.0",
"merge-options": "^3.0.4",
"multiaddr": "^9.0.1",
"multicodec": "^3.0.1",
"multihashing-async": "^2.1.2",
"multiaddr": "^10.0.0",
"multiformats": "^9.0.0",
"multistream-select": "^2.0.0",
"mutable-proxy": "^1.0.0",
"node-forge": "^0.10.0",
"p-any": "^3.0.0",
"p-fifo": "^1.0.0",
"p-retry": "^4.4.0",
"p-settle": "^4.1.1",
"peer-id": "^0.14.2",
"peer-id": "^0.15.0",
"private-ip": "^2.1.0",
"protobufjs": "^6.10.2",
"retimer": "^3.0.0",
"sanitize-filename": "^1.6.3",
"set-delayed-interval": "^1.0.0",
"streaming-iterables": "^5.0.2",
"streaming-iterables": "^6.0.0",
"timeout-abort-controller": "^1.1.1",
"varint": "^6.0.0",
"wherearewe": "^1.0.0",
Expand All @@ -133,7 +132,8 @@
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"@types/es6-promisify": "^6.0.0",
"@types/node-forge": "^0.9.7",
"@types/node": "^16.0.1",
"@types/node-forge": "^0.10.1",
"@types/varint": "^6.0.0",
"abortable-iterator": "^3.0.0",
"aegir": "^33.1.1",
Expand All @@ -142,29 +142,29 @@
"interop-libp2p": "^0.4.0",
"into-stream": "^6.0.0",
"ipfs-http-client": "^50.1.1",
"it-concat": "^1.0.0",
"it-concat": "^2.0.0",
"it-pair": "^1.0.0",
"it-pushable": "^1.4.0",
"libp2p": ".",
"libp2p-bootstrap": "^0.12.3",
"libp2p-delegated-content-routing": "^0.10.0",
"libp2p-delegated-peer-routing": "^0.9.0",
"libp2p-floodsub": "^0.25.0",
"libp2p-gossipsub": "^0.9.0",
"libp2p-kad-dht": "^0.22.0",
"libp2p-mdns": "^0.16.0",
"libp2p-bootstrap": "^0.13.0",
"libp2p-delegated-content-routing": "^0.11.0",
"libp2p-delegated-peer-routing": "^0.10.0",
"libp2p-floodsub": "^0.26.0",
"libp2p-gossipsub": "^0.10.0",
"libp2p-kad-dht": "^0.23.0",
"libp2p-mdns": "^0.17.0",
"libp2p-mplex": "^0.10.1",
"libp2p-noise": "^3.0.0",
"libp2p-tcp": "^0.16.0",
"libp2p-webrtc-star": "^0.22.2",
"libp2p-websockets": "^0.15.8",
"libp2p-noise": "^4.0.0",
"libp2p-tcp": "^0.17.0",
"libp2p-webrtc-star": "^0.23.0",
"libp2p-websockets": "^0.16.0",
"multihashes": "^4.0.2",
"nock": "^13.0.3",
"p-defer": "^3.0.0",
"p-times": "^3.0.0",
"p-wait-for": "^3.2.0",
"rimraf": "^3.0.2",
"sinon": "^10.0.0",
"sinon": "^11.1.1",
"uint8arrays": "^2.1.3",
"util": "^0.12.3"
},
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/auto-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class AutoRelay {
continue
}

const peerId = PeerId.createFromCID(id)
const peerId = PeerId.createFromB58String(id)
const connection = this._connectionManager.get(peerId)

// If not connected, store for possible later use.
Expand Down
4 changes: 2 additions & 2 deletions src/circuit/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ class Circuit {
throw errCode(new Error(errMsg), codes.ERR_RELAYED_DIAL)
}

const relayPeer = PeerId.createFromCID(relayId)
const destinationPeer = PeerId.createFromCID(destinationId)
const relayPeer = PeerId.createFromB58String(relayId)
const destinationPeer = PeerId.createFromB58String(destinationId)

let disconnectOnFailure = false
let relayConnection = this._connectionManager.get(relayPeer)
Expand Down
8 changes: 4 additions & 4 deletions src/circuit/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const CID = require('cids')
const multihashing = require('multihashing-async')
const { CID } = require('multiformats/cid')
const { sha256 } = require('multiformats/hashes/sha2')

/**
* Convert a namespace string into a cid.
Expand All @@ -11,7 +11,7 @@ const multihashing = require('multihashing-async')
*/
module.exports.namespaceToCid = async (namespace) => {
const bytes = new TextEncoder().encode(namespace)
const hash = await multihashing(bytes, 'sha2-256')
const hash = await sha256.digest(bytes)

return new CID(hash)
return CID.createV0(hash)
}
2 changes: 1 addition & 1 deletion src/content-routing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { pipe } = require('it-pipe')
/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('multiaddr').Multiaddr} Multiaddr
* @typedef {import('cids')} CID
* @typedef {import('multiformats/cid').CID} CID
* @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule
*/

Expand Down
2 changes: 1 addition & 1 deletion src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class PeerStore extends EventEmitter {

const peersData = new Map()
storedPeers.forEach((idStr) => {
peersData.set(idStr, this.get(PeerId.createFromCID(idStr)))
peersData.set(idStr, this.get(PeerId.createFromB58String(idStr)))
})

return peersData
Expand Down
5 changes: 3 additions & 2 deletions src/peer-store/persistent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const log = Object.assign(debug('libp2p:persistent-peer-store'), {
const { Key } = require('interface-datastore')
const { Multiaddr } = require('multiaddr')
const PeerId = require('peer-id')
const { base32 } = require('multiformats/bases/base32')

const PeerStore = require('..')

Expand Down Expand Up @@ -195,7 +196,7 @@ class PersistentPeerStore extends PeerStore {
const batch = this._datastore.batch()
for (const peerIdStr of commitPeers) {
// PeerId
const peerId = this.keyBook.data.get(peerIdStr) || PeerId.createFromCID(peerIdStr)
const peerId = this.keyBook.data.get(peerIdStr) || PeerId.createFromB58String(peerIdStr)

// Address Book
this._batchAddressBook(peerId, batch)
Expand Down Expand Up @@ -346,7 +347,7 @@ class PersistentPeerStore extends PeerStore {
async _processDatastoreEntry ({ key, value }) {
try {
const keyParts = key.toString().split('/')
const peerId = PeerId.createFromCID(keyParts[3])
const peerId = PeerId.createFromBytes(base32.decode(keyParts[3]))

let decoded
switch (keyParts[2]) {
Expand Down
4 changes: 1 addition & 3 deletions src/record/peer-record/consts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

const multicodec = require('multicodec')

// The domain string used for peer records contained in a Envelope.
const domain = multicodec.getName(multicodec.LIBP2P_PEER_RECORD) || 'libp2p-peer-record'
const domain = 'libp2p-peer-record'

// The type hint used to identify peer records in a Envelope.
// Defined in https://github.com/multiformats/multicodec/blob/master/table.csv
Expand Down
10 changes: 5 additions & 5 deletions test/content-routing/content-routing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const sinon = require('sinon')
const pDefer = require('p-defer')
const mergeOptions = require('merge-options')

const CID = require('cids')
const { CID } = require('multiformats/cid')
const ipfsHttpClient = require('ipfs-http-client')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
const { Multiaddr } = require('multiaddr')
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('content-routing', () => {
})

it('should be able to register as a provider', async () => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'

const mockBlockApi = nock('http://0.0.0.0:60197')
Expand All @@ -191,7 +191,7 @@ describe('content-routing', () => {
})

it('should handle errors when registering as a provider', async () => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const mockApi = nock('http://0.0.0.0:60197')
// mock the block/stat call
.post('/api/v0/block/stat')
Expand All @@ -205,7 +205,7 @@ describe('content-routing', () => {
})

it('should be able to find providers', async () => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'

const mockApi = nock('http://0.0.0.0:60197')
Expand All @@ -227,7 +227,7 @@ describe('content-routing', () => {
})

it('should handle errors when finding providers', async () => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const cid = CID.parse('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const mockApi = nock('http://0.0.0.0:60197')
.post('/api/v0/dht/findprovs')
.query(true)
Expand Down
2 changes: 1 addition & 1 deletion test/insecure/compliance.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
/* eslint-env mocha */

const tests = require('libp2p-interfaces/src/crypto/tests')
const tests = require('libp2p-interfaces-compliance-tests/src/crypto')
const plaintext = require('../../src/insecure/plaintext')

describe('plaintext compliance', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/keychain/peerid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const { expect } = require('aegir/utils/chai')
const PeerId = require('peer-id')
const multihash = require('multihashes')
const { base58btc } = require('multiformats/bases/base58')
const crypto = require('libp2p-crypto')
const rsaUtils = require('libp2p-crypto/src/keys/rsa-utils')
const rsaClass = require('libp2p-crypto/src/keys/rsa-class')
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('peer ID', () => {
const jwk = rsaUtils.pkixToJwk(publicKeyDer)
const rsa = new rsaClass.RsaPublicKey(jwk)
const keyId = await rsa.hash()
const kids = multihash.toB58String(keyId)
const kids = base58btc.encode(keyId).substring(1)
expect(kids).to.equal(peer.toB58String())
})

Expand All @@ -54,7 +54,7 @@ describe('peer ID', () => {
}
const rsa = new rsaClass.RsaPublicKey(jwk)
const keyId = await rsa.hash()
const kids = multihash.toB58String(keyId)
const kids = base58btc.encode(keyId).substring(1)
expect(kids).to.equal(peer.toB58String())
})

Expand Down
2 changes: 1 addition & 1 deletion test/record/peer-record.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const { expect } = require('aegir/utils/chai')

const tests = require('libp2p-interfaces/src/record/tests')
const tests = require('libp2p-interfaces-compliance-tests/src/record')
const { Multiaddr } = require('multiaddr')
const PeerId = require('peer-id')

Expand Down
2 changes: 1 addition & 1 deletion test/relay/relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Dialing (via relay, TCP)', () => {
await libp2p.stop()
// Clear the peer stores
for (const peerIdStr of libp2p.peerStore.peers.keys()) {
const peerId = PeerId.createFromCID(peerIdStr)
const peerId = PeerId.createFromB58String(peerIdStr)
libp2p.peerStore.delete(peerId)
}
}))
Expand Down
Loading

0 comments on commit 13cf476

Please sign in to comment.