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

Commit fa2c4f5

Browse files
authored
feat: simpler peer id (#117)
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.
1 parent 9d52223 commit fa2c4f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1471
-646
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
with:
3434
node-version: ${{ matrix.node }}
3535
- run: npm install
36+
- run: npm run build
3637
- run: npm run test:node -- -- --bail
3738
- uses: codecov/codecov-action@v1
3839
test-chrome:
@@ -44,6 +45,7 @@ jobs:
4445
with:
4546
node-version: 16
4647
- run: npm install
48+
- run: npm run build
4749
- run: npm run test:browser -- -- --bail
4850
test-firefox:
4951
needs: check
@@ -54,4 +56,5 @@ jobs:
5456
with:
5557
node-version: 16
5658
- run: npm install
59+
- run: npm run build
5760
- run: npm run test:browser -- -- --bail --browser firefox

packages/libp2p-connection/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@
4545
"dependencies": {
4646
"err-code": "^3.0.1",
4747
"libp2p-interfaces": "^3.0.0",
48-
"multiaddr": "^10.0.1",
49-
"peer-id": "^0.16.0"
48+
"multiaddr": "^10.0.1"
5049
},
5150
"devDependencies": {
52-
"aegir": "^36.0.0"
51+
"aegir": "^36.0.0",
52+
"libp2p-peer-id-factory": "^0.0.1"
5353
},
5454
"exports": {
5555
".": {

packages/libp2p-connection/test/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Connection } from '../src/index.js'
2-
import PeerIdFactory from 'peer-id'
2+
import * as PeerIdFactory from 'libp2p-peer-id-factory'
33
// @ts-expect-error no types
44
import pair from 'it-pair'
55
import { Multiaddr } from 'multiaddr'

packages/libp2p-connection/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"references": [
1313
{
1414
"path": "../libp2p-interfaces"
15+
},
16+
{
17+
"path": "../libp2p-peer-id-factory"
1518
}
1619
]
1720
}

packages/libp2p-interfaces-compliance-tests/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@
5353
"libp2p-connection": "^0.2.0",
5454
"libp2p-crypto": "^0.21.0",
5555
"libp2p-interfaces": "^3.0.0",
56+
"libp2p-peer-id": "^0.0.1",
57+
"libp2p-peer-id-factory": "^0.0.1",
5658
"libp2p-pubsub": "^0.8.0",
5759
"libp2p-topology": "^0.2.0",
5860
"multiaddr": "^10.0.0",
5961
"multiformats": "^9.4.10",
6062
"p-defer": "^3.0.0",
6163
"p-limit": "^3.1.0",
6264
"p-wait-for": "^4.1.0",
63-
"peer-id": "^0.16.0",
6465
"sinon": "^11.1.1",
6566
"streaming-iterables": "^6.0.0",
6667
"uint8arrays": "^3.0.0",
@@ -86,10 +87,6 @@
8687
"import": "./dist/src/peer-discovery/index.js",
8788
"types": "./dist/src/peer-discovery/index.d.ts"
8889
},
89-
"./peer-id": {
90-
"import": "./dist/src/peer-id/index.js",
91-
"types": "./dist/src/peer-id/index.d.ts"
92-
},
9390
"./pubsub": {
9491
"import": "./dist/src/pubsub/index.js",
9592
"types": "./dist/src/pubsub/index.d.ts"

packages/libp2p-interfaces-compliance-tests/src/crypto/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from 'aegir/utils/chai.js'
22
// @ts-expect-error no types
33
import duplexPair from 'it-pair/duplex.js'
44
import { pipe } from 'it-pipe'
5-
import PeerIdFactory from 'peer-id'
5+
import * as PeerIdFactory from 'libp2p-peer-id-factory'
66
import { collect } from 'streaming-iterables'
77
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
88
import peers from '../utils/peers.js'
@@ -84,9 +84,9 @@ export default (common: TestSetup<Crypto>) => {
8484
])
8585

8686
// Inbound should return the initiator (local) peer
87-
expect(inboundResult.remotePeer.id).to.eql(localPeer.id)
87+
expect(inboundResult.remotePeer.toBytes()).to.equalBytes(localPeer.toBytes())
8888
// Outbound should return the receiver (remote) peer
89-
expect(outboundResult.remotePeer.id).to.eql(remotePeer.id)
89+
expect(outboundResult.remotePeer.toBytes()).to.equalBytes(remotePeer.toBytes())
9090
})
9191

9292
it('inbound connections should verify peer integrity if known', async () => {

packages/libp2p-interfaces-compliance-tests/src/peer-discovery/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { expect } from 'aegir/utils/chai.js'
22
import { Multiaddr } from 'multiaddr'
3-
import PeerIdFactory from 'peer-id'
43
import delay from 'delay'
54
import pDefer from 'p-defer'
65
import type { TestSetup } from '../index.js'
@@ -47,7 +46,7 @@ export default (common: TestSetup<PeerDiscovery & Startable>) => {
4746

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

5352
multiaddrs.forEach((m) => expect(Multiaddr.isMultiaddr(m)).to.eql(true))

packages/libp2p-interfaces-compliance-tests/src/peer-id/index.ts

Lines changed: 0 additions & 401 deletions
This file was deleted.

packages/libp2p-interfaces-compliance-tests/src/pubsub/connection-handlers.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ export default (common: TestSetup<PubSub & Startable>) => {
5757

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

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

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

64-
expectSet(psA.topics.get('Zb'), [psB.peerId.toB58String()])
64+
expectSet(psA.topics.get('Zb'), [psB.peerId.toString()])
6565
})
6666
})
6767

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

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

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

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

327327
// Wait for subscriptions to occur
328328
await pWaitFor(() => {
329-
return psA.getSubscribers(topic).includes(psB.peerId.toB58String()) &&
330-
psB.getSubscribers(topic).includes(psA.peerId.toB58String())
329+
return psA.getSubscribers(topic).includes(psB.peerId.toString()) &&
330+
psB.getSubscribers(topic).includes(psA.peerId.toString())
331331
})
332332

333333
// Verify messages go both ways

packages/libp2p-interfaces-compliance-tests/src/pubsub/messages.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'aegir/utils/chai.js'
22
import sinon from 'sinon'
3-
import PeerIdFactory from 'peer-id'
3+
import * as PeerIdFactory from 'libp2p-peer-id-factory'
44
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
55
import * as utils from 'libp2p-pubsub/utils'
66
import { PeerStreams } from 'libp2p-pubsub/peer-streams'
@@ -51,13 +51,13 @@ export default (common: TestSetup<PubSub & Startable>) => {
5151
sinon.spy(pubsub, 'validate')
5252

5353
const peerStream = new PeerStreams({
54-
id: await PeerIdFactory.create(),
54+
id: await PeerIdFactory.createEd25519PeerId(),
5555
protocol: 'test'
5656
})
5757
const rpc = {
5858
subscriptions: [],
5959
msgs: [{
60-
receivedFrom: peerStream.id.toB58String(),
60+
receivedFrom: peerStream.id.toString(),
6161
from: peerStream.id.toBytes(),
6262
data,
6363
seqno: utils.randomSeqno(),
@@ -67,7 +67,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
6767

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

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

8787
const peerStream = new PeerStreams({
88-
id: await PeerIdFactory.create(),
88+
id: await PeerIdFactory.createEd25519PeerId(),
8989
protocol: 'test'
9090
})
9191

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

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

104104
expect(pubsub.validate).to.have.property('callCount', 1)
105105
// @ts-expect-error protected field

0 commit comments

Comments
 (0)