Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
feat: simpler peer id (#117)
Browse files Browse the repository at this point in the history
Refactors `PeerId`s with the aim of making them easier to use in places like API clients where you generally don't do any cryptographic operations.

Splits the existing `peer-id` functionality into `libp2p-peer-id` for everyday activities like turning strings/byte arrays into `PeerId`s, and `libp2p-peer-id-factory` for generating new keypairs and creating `PeerId`s from them, also creating them from protobuf bytes, json objects, and other esoteric formats.

Depending on use of those formats we may move the methods into `libp2p-peer-id` in future but it's a reasonable start.

Tries to remove some of the duplication in parsing `PeerID`s out of strings, and also tries to not create lots of `CID` objects when trying to parse `PeerID`s out of byte arrays.
  • Loading branch information
achingbrain committed Jan 1, 2022
1 parent 9d52223 commit fa2c4f5
Show file tree
Hide file tree
Showing 54 changed files with 1,471 additions and 646 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm run build
- run: npm run test:node -- -- --bail
- uses: codecov/codecov-action@v1
test-chrome:
Expand All @@ -44,6 +45,7 @@ jobs:
with:
node-version: 16
- run: npm install
- run: npm run build
- run: npm run test:browser -- -- --bail
test-firefox:
needs: check
Expand All @@ -54,4 +56,5 @@ jobs:
with:
node-version: 16
- run: npm install
- run: npm run build
- run: npm run test:browser -- -- --bail --browser firefox
6 changes: 3 additions & 3 deletions packages/libp2p-connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
"dependencies": {
"err-code": "^3.0.1",
"libp2p-interfaces": "^3.0.0",
"multiaddr": "^10.0.1",
"peer-id": "^0.16.0"
"multiaddr": "^10.0.1"
},
"devDependencies": {
"aegir": "^36.0.0"
"aegir": "^36.0.0",
"libp2p-peer-id-factory": "^0.0.1"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p-connection/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Connection } from '../src/index.js'
import PeerIdFactory from 'peer-id'
import * as PeerIdFactory from 'libp2p-peer-id-factory'
// @ts-expect-error no types
import pair from 'it-pair'
import { Multiaddr } from 'multiaddr'
Expand Down
3 changes: 3 additions & 0 deletions packages/libp2p-connection/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"references": [
{
"path": "../libp2p-interfaces"
},
{
"path": "../libp2p-peer-id-factory"
}
]
}
7 changes: 2 additions & 5 deletions packages/libp2p-interfaces-compliance-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@
"libp2p-connection": "^0.2.0",
"libp2p-crypto": "^0.21.0",
"libp2p-interfaces": "^3.0.0",
"libp2p-peer-id": "^0.0.1",
"libp2p-peer-id-factory": "^0.0.1",
"libp2p-pubsub": "^0.8.0",
"libp2p-topology": "^0.2.0",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.10",
"p-defer": "^3.0.0",
"p-limit": "^3.1.0",
"p-wait-for": "^4.1.0",
"peer-id": "^0.16.0",
"sinon": "^11.1.1",
"streaming-iterables": "^6.0.0",
"uint8arrays": "^3.0.0",
Expand All @@ -86,10 +87,6 @@
"import": "./dist/src/peer-discovery/index.js",
"types": "./dist/src/peer-discovery/index.d.ts"
},
"./peer-id": {
"import": "./dist/src/peer-id/index.js",
"types": "./dist/src/peer-id/index.d.ts"
},
"./pubsub": {
"import": "./dist/src/pubsub/index.js",
"types": "./dist/src/pubsub/index.d.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'aegir/utils/chai.js'
// @ts-expect-error no types
import duplexPair from 'it-pair/duplex.js'
import { pipe } from 'it-pipe'
import PeerIdFactory from 'peer-id'
import * as PeerIdFactory from 'libp2p-peer-id-factory'
import { collect } from 'streaming-iterables'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import peers from '../utils/peers.js'
Expand Down Expand Up @@ -84,9 +84,9 @@ export default (common: TestSetup<Crypto>) => {
])

// Inbound should return the initiator (local) peer
expect(inboundResult.remotePeer.id).to.eql(localPeer.id)
expect(inboundResult.remotePeer.toBytes()).to.equalBytes(localPeer.toBytes())
// Outbound should return the receiver (remote) peer
expect(outboundResult.remotePeer.id).to.eql(remotePeer.id)
expect(outboundResult.remotePeer.toBytes()).to.equalBytes(remotePeer.toBytes())
})

it('inbound connections should verify peer integrity if known', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from 'aegir/utils/chai.js'
import { Multiaddr } from 'multiaddr'
import PeerIdFactory from 'peer-id'
import delay from 'delay'
import pDefer from 'p-defer'
import type { TestSetup } from '../index.js'
Expand Down Expand Up @@ -47,7 +46,7 @@ export default (common: TestSetup<PeerDiscovery & Startable>) => {

discovery.on('peer', ({ id, multiaddrs }) => {
expect(id).to.exist()
expect(PeerIdFactory.isPeerId(id)).to.eql(true)
expect(id).to.have.property('type').that.is.oneOf(['RSA', 'Ed25519', 'secp256k1'])
expect(multiaddrs).to.exist()

multiaddrs.forEach((m) => expect(Multiaddr.isMultiaddr(m)).to.eql(true))
Expand Down
401 changes: 0 additions & 401 deletions packages/libp2p-interfaces-compliance-tests/src/peer-id/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export default (common: TestSetup<PubSub & Startable>) => {

expectSet(psA.subscriptions, ['Za'])

expectSet(psB.topics.get('Za'), [psA.peerId.toB58String()])
expectSet(psB.topics.get('Za'), [psA.peerId.toString()])

expectSet(psB.subscriptions, ['Zb'])

expectSet(psA.topics.get('Zb'), [psB.peerId.toB58String()])
expectSet(psA.topics.get('Zb'), [psB.peerId.toString()])
})
})

Expand Down Expand Up @@ -115,7 +115,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
// wait for psB to know about psA subscription
await pWaitFor(() => {
const subscribedPeers = psB.getSubscribers(topic)
return subscribedPeers.includes(psA.peerId.toB58String())
return subscribedPeers.includes(psA.peerId.toString())
})
void psB.publish(topic, data)

Expand Down Expand Up @@ -186,7 +186,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
// wait for psB to know about psA subscription
await pWaitFor(() => {
const subscribedPeers = psB.getSubscribers(topic)
return subscribedPeers.includes(psA.peerId.toB58String())
return subscribedPeers.includes(psA.peerId.toString())
})
void psB.publish(topic, data)

Expand Down Expand Up @@ -216,7 +216,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
it('should receive pubsub messages after a node restart', async function () {
const topic = 'test-topic'
const data = uint8ArrayFromString('hey!')
const psAid = psA.peerId.toB58String()
const psAid = psA.peerId.toString()

let counter = 0
const defer1 = pDefer()
Expand Down Expand Up @@ -322,12 +322,12 @@ export default (common: TestSetup<PubSub & Startable>) => {
// @ts-expect-error protected fields
await psA._libp2p.dialer.connectToPeer(psB.peerId)
// @ts-expect-error protected fields
expect(psA._libp2p.connections.get(psB.peerId.toB58String())).to.have.length(2)
expect(psA._libp2p.connections.get(psB.peerId.toString())).to.have.length(2)

// Wait for subscriptions to occur
await pWaitFor(() => {
return psA.getSubscribers(topic).includes(psB.peerId.toB58String()) &&
psB.getSubscribers(topic).includes(psA.peerId.toB58String())
return psA.getSubscribers(topic).includes(psB.peerId.toString()) &&
psB.getSubscribers(topic).includes(psA.peerId.toString())
})

// Verify messages go both ways
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'aegir/utils/chai.js'
import sinon from 'sinon'
import PeerIdFactory from 'peer-id'
import * as PeerIdFactory from 'libp2p-peer-id-factory'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import * as utils from 'libp2p-pubsub/utils'
import { PeerStreams } from 'libp2p-pubsub/peer-streams'
Expand Down Expand Up @@ -51,13 +51,13 @@ export default (common: TestSetup<PubSub & Startable>) => {
sinon.spy(pubsub, 'validate')

const peerStream = new PeerStreams({
id: await PeerIdFactory.create(),
id: await PeerIdFactory.createEd25519PeerId(),
protocol: 'test'
})
const rpc = {
subscriptions: [],
msgs: [{
receivedFrom: peerStream.id.toB58String(),
receivedFrom: peerStream.id.toString(),
from: peerStream.id.toBytes(),
data,
seqno: utils.randomSeqno(),
Expand All @@ -67,7 +67,7 @@ export default (common: TestSetup<PubSub & Startable>) => {

pubsub.subscribe(topic)
// @ts-expect-error protected field
await pubsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)

expect(pubsub.validate).to.have.property('callCount', 1)
// @ts-expect-error protected field
Expand All @@ -85,7 +85,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
sinon.spy(pubsub, 'validate')

const peerStream = new PeerStreams({
id: await PeerIdFactory.create(),
id: await PeerIdFactory.createEd25519PeerId(),
protocol: 'test'
})

Expand All @@ -99,7 +99,7 @@ export default (common: TestSetup<PubSub & Startable>) => {

pubsub.subscribe(topic)
// @ts-expect-error protected field
await pubsub._processRpc(peerStream.id.toB58String(), peerStream, rpc)
await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)

expect(pubsub.validate).to.have.property('callCount', 1)
// @ts-expect-error protected field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
await new Promise((resolve) => psB.once('pubsub:subscription-change', resolve))
expect(psB.peers.size).to.equal(2)

const aPeerId = psA.peerId.toB58String()
const aPeerId = psA.peerId.toString()
expectSet(psB.topics.get(topic), [aPeerId])

expect(psC.peers.size).to.equal(1)
Expand All @@ -81,10 +81,10 @@ export default (common: TestSetup<PubSub & Startable>) => {
])

expect(psA.peers.size).to.equal(1)
expectSet(psA.topics.get(topic), [psB.peerId.toB58String()])
expectSet(psA.topics.get(topic), [psB.peerId.toString()])

expect(psC.peers.size).to.equal(1)
expectSet(psC.topics.get(topic), [psB.peerId.toB58String()])
expectSet(psC.topics.get(topic), [psB.peerId.toString()])
})

it('subscribe to the topic on node c', async () => {
Expand All @@ -97,7 +97,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
psB.once('pubsub:subscription-change', () => {
expect(psA.peers.size).to.equal(1)
expect(psB.peers.size).to.equal(2)
expectSet(psB.topics.get(topic), [psC.peerId.toB58String()])
expectSet(psB.topics.get(topic), [psC.peerId.toString()])

defer.resolve()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
psB.once('pubsub:subscription-change', ({ peerId: changedPeerId, subscriptions: changedSubs }) => {
expectSet(psA.subscriptions, [topic])
expect(psB.peers.size).to.equal(1)
expectSet(psB.topics.get(topic), [psA.peerId.toB58String()])
expect(changedPeerId.toB58String()).to.equal(first(psB.peers).id.toB58String())
expectSet(psB.topics.get(topic), [psA.peerId.toString()])
expect(changedPeerId.toString()).to.equal(first(psB.peers).id.toString())
expect(changedSubs).to.have.lengthOf(1)
expect(changedSubs[0].topicID).to.equal(topic)
expect(changedSubs[0].subscribe).to.equal(true)
Expand Down Expand Up @@ -117,7 +117,7 @@ export default (common: TestSetup<PubSub & Startable>) => {

function receivedMsg (msg: Message) {
expect(uint8ArrayToString(msg.data)).to.equal('banana')
expect(msg.from).to.be.eql(psB.peerId.toB58String())
expect(msg.from).to.be.eql(psB.peerId.toString())
expect(msg.seqno).to.be.a('Uint8Array')
expect(msg.topicIDs).to.be.eql([topic])

Expand All @@ -143,7 +143,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
psB.once('pubsub:subscription-change', ({ peerId: changedPeerId, subscriptions: changedSubs }) => {
expect(psB.peers.size).to.equal(1)
expectSet(psB.topics.get(topic), [])
expect(changedPeerId.toB58String()).to.equal(first(psB.peers).id.toB58String())
expect(changedPeerId.toString()).to.equal(first(psB.peers).id.toString())
expect(changedSubs).to.have.lengthOf(1)
expect(changedSubs[0].topicID).to.equal(topic)
expect(changedSubs[0].subscribe).to.equal(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'aegir/utils/chai.js'
import sinon from 'sinon'
import PeerIdFactory from 'peer-id'
import * as PeerIdFactory from 'libp2p-peer-id-factory'
import peers from '../utils/peers.js'
import type { TestSetup } from '../index.js'
import type { MulticodecTopology } from 'libp2p-interfaces/topology'
Expand Down Expand Up @@ -43,7 +43,7 @@ export default (test: TestSetup<MulticodecTopology>) => {
const peerStore = topology._registrar.peerStore

const id2 = await PeerIdFactory.createFromJSON(peers[1])
peerStore.peers.set(id2.toB58String(), {
peerStore.peers.set(id2.toString(), {
id: id2,
protocols: Array.from(topology.multicodecs)
})
Expand All @@ -66,7 +66,7 @@ export default (test: TestSetup<MulticodecTopology>) => {
const peerStore = topology._registrar.peerStore

const id2 = await PeerIdFactory.createFromJSON(peers[1])
peerStore.peers.set(id2.toB58String(), {
peerStore.peers.set(id2.toString(), {
id: id2,
protocols: Array.from(topology.multicodecs)
})
Expand All @@ -78,7 +78,7 @@ export default (test: TestSetup<MulticodecTopology>) => {

expect(topology.peers.size).to.eql(1)

peerStore.peers.set(id2.toB58String(), {
peerStore.peers.set(id2.toString(), {
id: id2,
protocols: []
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'aegir/utils/chai.js'
import sinon from 'sinon'
import PeerIdFactory from 'peer-id'
import * as PeerIdFactory from 'libp2p-peer-id-factory'
import peers from '../utils/peers.js'
import type { TestSetup } from '../index.js'
import type { Topology } from 'libp2p-interfaces/topology'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { Connection, StreamData } from 'libp2p-interfaces/connection'
import type { MuxedStream } from 'libp2p-interfaces/stream-muxer'
// @ts-expect-error no types
import pair from 'it-pair'
import PeerId from 'peer-id'
import { PeerId } from 'libp2p-peer-id'
import * as PeerIdFactory from 'libp2p-peer-id-factory'
/**
* A tick is considered valid if it happened between now
* and `ms` milliseconds ago
Expand Down Expand Up @@ -55,8 +56,8 @@ async function createConnection (maConn: MultiaddrConnection, direction: 'inboun

const localPeerIdStr = localAddr.getPeerId()
const remotePeerIdStr = remoteAddr.getPeerId()
const localPeer = localPeerIdStr != null ? PeerId.parse(localPeerIdStr) : await PeerId.create({ keyType: 'Ed25519' })
const remotePeer = remotePeerIdStr != null ? PeerId.parse(remotePeerIdStr) : await PeerId.create({ keyType: 'Ed25519' })
const localPeer = localPeerIdStr != null ? PeerId.fromString(localPeerIdStr) : await PeerIdFactory.createEd25519PeerId()
const remotePeer = remotePeerIdStr != null ? PeerId.fromString(remotePeerIdStr) : await PeerIdFactory.createEd25519PeerId()

const streams: Array<MuxedStream<Uint8Array>> = []
let streamId = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tests from '../../src/connection/index.js'
import { Connection } from 'libp2p-connection'
import peers from '../../src/utils/peers.js'
import PeerIdFactory from 'peer-id'
import * as PeerIdFactory from 'libp2p-peer-id-factory'
import { Multiaddr } from 'multiaddr'
// @ts-expect-error no types
import pair from 'it-pair'
Expand Down
Loading

0 comments on commit fa2c4f5

Please sign in to comment.