From 8552fe86899a776aadc653c98e86ec906faee4fb Mon Sep 17 00:00:00 2001 From: Cayman Date: Tue, 5 Dec 2023 15:59:30 -0500 Subject: [PATCH 1/7] feat: add private key to libp2p components --- packages/libp2p/src/components.ts | 4 +++- packages/libp2p/src/config.ts | 6 +++++- packages/libp2p/src/index.ts | 7 ++++++- packages/libp2p/src/libp2p.ts | 6 ++++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/libp2p/src/components.ts b/packages/libp2p/src/components.ts index f27da1ec7a..f4a3dda27f 100644 --- a/packages/libp2p/src/components.ts +++ b/packages/libp2p/src/components.ts @@ -1,11 +1,12 @@ import { CodeError } from '@libp2p/interface' -import { isStartable, type Startable, type Libp2pEvents, type ComponentLogger, type NodeInfo, type ConnectionProtector, type ConnectionGater, type ContentRouting, type TypedEventTarget, type Metrics, type PeerId, type PeerRouting, type PeerStore, type Upgrader } from '@libp2p/interface' +import { isStartable, type Startable, type Libp2pEvents, type ComponentLogger, type NodeInfo, type ConnectionProtector, type ConnectionGater, type ContentRouting, type TypedEventTarget, type Metrics, type PeerId, type PeerRouting, type PeerStore, type PrivateKey, type Upgrader } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' import type { AddressManager, ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal' import type { Datastore } from 'interface-datastore' export interface Components extends Record, Startable { peerId: PeerId + privateKey: PrivateKey nodeInfo: NodeInfo logger: ComponentLogger events: TypedEventTarget @@ -25,6 +26,7 @@ export interface Components extends Record, Startable { export interface ComponentsInit { peerId?: PeerId + privateKey?: PrivateKey nodeInfo?: NodeInfo logger?: ComponentLogger events?: TypedEventTarget diff --git a/packages/libp2p/src/config.ts b/packages/libp2p/src/config.ts index 410d31de5a..1c4d1eef19 100644 --- a/packages/libp2p/src/config.ts +++ b/packages/libp2p/src/config.ts @@ -25,12 +25,16 @@ const DefaultConfig: Partial = { } } -export function validateConfig > (opts: RecursivePartial>): Libp2pInit { +export async function validateConfig > (opts: RecursivePartial>): Promise> { const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, opts) if (resultingOptions.connectionProtector === null && globalThis.process?.env?.LIBP2P_FORCE_PNET != null) { // eslint-disable-line no-undef throw new CodeError(messages.ERR_PROTECTOR_REQUIRED, codes.ERR_PROTECTOR_REQUIRED) } + if (await resultingOptions.privateKey.id() !== resultingOptions.peerId.toString()) { + throw new CodeError('Private key doesn\'t match peer id', codes.ERR_INVALID_KEY) + } + return resultingOptions } diff --git a/packages/libp2p/src/index.ts b/packages/libp2p/src/index.ts index 72fa49b1f8..0ff6aaafaa 100644 --- a/packages/libp2p/src/index.ts +++ b/packages/libp2p/src/index.ts @@ -19,7 +19,7 @@ import type { AddressManagerInit } from './address-manager/index.js' import type { Components } from './components.js' import type { ConnectionManagerInit } from './connection-manager/index.js' import type { TransportManagerInit } from './transport-manager.js' -import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger, NodeInfo, ConnectionProtector, ConnectionEncrypter, ConnectionGater, ContentRouting, Metrics, PeerDiscovery, PeerId, PeerRouting, StreamMuxerFactory, Transport } from '@libp2p/interface' +import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger, NodeInfo, ConnectionProtector, ConnectionEncrypter, ConnectionGater, ContentRouting, Metrics, PeerDiscovery, PeerId, PeerRouting, StreamMuxerFactory, Transport, PrivateKey } from '@libp2p/interface' import type { PersistentPeerStoreInit } from '@libp2p/peer-store' import type { Datastore } from 'interface-datastore' @@ -36,6 +36,11 @@ export interface Libp2pInit */ peerId: PeerId + /** + * Private key associated with the peerId + */ + privateKey: PrivateKey + /** * Metadata about the node - implementation name, version number, etc */ diff --git a/packages/libp2p/src/libp2p.ts b/packages/libp2p/src/libp2p.ts index c38109909c..4439272c82 100644 --- a/packages/libp2p/src/libp2p.ts +++ b/packages/libp2p/src/libp2p.ts @@ -1,4 +1,4 @@ -import { unmarshalPublicKey } from '@libp2p/crypto/keys' +import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys' import { contentRoutingSymbol, CodeError, TypedEventEmitter, CustomEvent, setMaxListeners, peerDiscoverySymbol, peerRoutingSymbol } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' import { PeerSet } from '@libp2p/peer-collections' @@ -67,6 +67,7 @@ export class Libp2pNode> extends this.services = {} const components = this.components = defaultComponents({ peerId: init.peerId, + privateKey: init.privateKey, nodeInfo: init.nodeInfo ?? { name: pkg.name, version: pkg.version @@ -387,6 +388,7 @@ export class Libp2pNode> extends */ export async function createLibp2pNode > (options: Libp2pOptions = {}): Promise> { options.peerId ??= await createEd25519PeerId() + options.privateKey ??= await unmarshalPrivateKey(options.peerId.privateKey as Uint8Array) - return new Libp2pNode(validateConfig(options)) + return new Libp2pNode(await validateConfig(options)) } From 7c2ea66d15b6f89c95b15b016e75393178828471 Mon Sep 17 00:00:00 2001 From: Cayman Date: Tue, 5 Dec 2023 16:32:18 -0500 Subject: [PATCH 2/7] fix!: update RecordEnvelope to use keys --- packages/peer-record/src/envelope/index.ts | 39 +++++++------------ packages/peer-record/test/envelope.spec.ts | 12 ++++-- packages/peer-record/test/peer-record.spec.ts | 8 ++-- packages/peer-store/test/index.spec.ts | 15 ++++--- packages/protocol-identify/src/identify.ts | 8 ++-- packages/protocol-identify/src/index.ts | 3 +- .../protocol-identify/test/fixtures/index.ts | 3 +- packages/protocol-identify/test/index.spec.ts | 13 +++++-- .../src/server/index.ts | 7 +++- 9 files changed, 60 insertions(+), 48 deletions(-) diff --git a/packages/peer-record/src/envelope/index.ts b/packages/peer-record/src/envelope/index.ts index 8770af487c..008bceffb9 100644 --- a/packages/peer-record/src/envelope/index.ts +++ b/packages/peer-record/src/envelope/index.ts @@ -1,4 +1,4 @@ -import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys' +import { unmarshalPublicKey } from '@libp2p/crypto/keys' import { CodeError } from '@libp2p/interface' import { peerIdFromKeys } from '@libp2p/peer-id' import * as varint from 'uint8-varint' @@ -7,10 +7,11 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals' import { fromString as uint8arraysFromString } from 'uint8arrays/from-string' import { codes } from '../errors.js' import { Envelope as Protobuf } from './envelope.js' -import type { PeerId, Record, Envelope } from '@libp2p/interface' +import type { Record, Envelope, PrivateKey, PublicKey, PeerId } from '@libp2p/interface' export interface RecordEnvelopeInit { peerId: PeerId + publicKey: PublicKey payloadType: Uint8Array payload: Uint8Array signature: Uint8Array @@ -23,9 +24,11 @@ export class RecordEnvelope implements Envelope { static createFromProtobuf = async (data: Uint8Array | Uint8ArrayList): Promise => { const envelopeData = Protobuf.decode(data) const peerId = await peerIdFromKeys(envelopeData.publicKey) + const publicKey = await unmarshalPublicKey(envelopeData.publicKey) return new RecordEnvelope({ peerId, + publicKey, payloadType: envelopeData.payloadType, payload: envelopeData.payload, signature: envelopeData.signature @@ -34,22 +37,18 @@ export class RecordEnvelope implements Envelope { /** * Seal marshals the given Record, places the marshaled bytes inside an Envelope - * and signs it with the given peerId's private key + * and signs it with the given private key */ - static seal = async (record: Record, peerId: PeerId): Promise => { - if (peerId.privateKey == null) { - throw new Error('Missing private key') - } - + static seal = async (record: Record, privateKey: PrivateKey): Promise => { const domain = record.domain const payloadType = record.codec const payload = record.marshal() const signData = formatSignaturePayload(domain, payloadType, payload) - const key = await unmarshalPrivateKey(peerId.privateKey) - const signature = await key.sign(signData.subarray()) + const signature = await privateKey.sign(signData.subarray()) return new RecordEnvelope({ - peerId, + peerId: await peerIdFromKeys(privateKey.public.bytes), + publicKey: privateKey.public, payloadType, payload, signature @@ -72,6 +71,7 @@ export class RecordEnvelope implements Envelope { } public peerId: PeerId + public publicKey: PublicKey public payloadType: Uint8Array public payload: Uint8Array public signature: Uint8Array @@ -82,9 +82,10 @@ export class RecordEnvelope implements Envelope { * by a libp2p peer. */ constructor (init: RecordEnvelopeInit) { - const { peerId, payloadType, payload, signature } = init + const { peerId, publicKey, payloadType, payload, signature } = init this.peerId = peerId + this.publicKey = publicKey this.payloadType = payloadType this.payload = payload this.signature = signature @@ -94,13 +95,9 @@ export class RecordEnvelope implements Envelope { * Marshal the envelope content */ marshal (): Uint8Array { - if (this.peerId.publicKey == null) { - throw new Error('Missing public key') - } - if (this.marshaled == null) { this.marshaled = Protobuf.encode({ - publicKey: this.peerId.publicKey, + publicKey: this.publicKey.bytes, payloadType: this.payloadType, payload: this.payload.subarray(), signature: this.signature @@ -123,13 +120,7 @@ export class RecordEnvelope implements Envelope { async validate (domain: string): Promise { const signData = formatSignaturePayload(domain, this.payloadType, this.payload) - if (this.peerId.publicKey == null) { - throw new Error('Missing public key') - } - - const key = unmarshalPublicKey(this.peerId.publicKey) - - return key.verify(signData.subarray(), this.signature) + return this.publicKey.verify(signData.subarray(), this.signature) } } diff --git a/packages/peer-record/test/envelope.spec.ts b/packages/peer-record/test/envelope.spec.ts index 9da7b5295d..969cb36aea 100644 --- a/packages/peer-record/test/envelope.spec.ts +++ b/packages/peer-record/test/envelope.spec.ts @@ -1,10 +1,11 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { expect } from 'aegir/chai' import { equals as uint8ArrayEquals } from 'uint8arrays/equals' import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' import { RecordEnvelope } from '../src/envelope/index.js' import { codes as ErrorCodes } from '../src/errors.js' -import type { PeerId, Record } from '@libp2p/interface' +import type { PeerId, PrivateKey, Record } from '@libp2p/interface' const domain = 'libp2p-testing' const codec = uint8arrayFromString('/libp2p/testdata') @@ -32,10 +33,12 @@ class TestRecord implements Record { describe('Envelope', () => { const payloadType = codec let peerId: PeerId + let privateKey: PrivateKey let testRecord: TestRecord before(async () => { peerId = await createEd25519PeerId() + privateKey = await unmarshalPrivateKey(peerId.privateKey) testRecord = new TestRecord('test-data') }) @@ -45,6 +48,7 @@ describe('Envelope', () => { const envelope = new RecordEnvelope({ peerId, + publicKey: privateKey.public, payloadType, payload, signature @@ -58,7 +62,7 @@ describe('Envelope', () => { }) it('can seal a record', async () => { - const envelope = await RecordEnvelope.seal(testRecord, peerId) + const envelope = await RecordEnvelope.seal(testRecord, privateKey) expect(envelope).to.exist() expect(envelope.peerId.equals(peerId)).to.eql(true) expect(envelope.payloadType).to.eql(payloadType) @@ -67,7 +71,7 @@ describe('Envelope', () => { }) it('can open and verify a sealed record', async () => { - const envelope = await RecordEnvelope.seal(testRecord, peerId) + const envelope = await RecordEnvelope.seal(testRecord, privateKey) const rawEnvelope = envelope.marshal() const unmarshalledEnvelope = await RecordEnvelope.openAndCertify(rawEnvelope, testRecord.domain) @@ -78,7 +82,7 @@ describe('Envelope', () => { }) it('throw on open and verify when a different domain is used', async () => { - const envelope = await RecordEnvelope.seal(testRecord, peerId) + const envelope = await RecordEnvelope.seal(testRecord, privateKey) const rawEnvelope = envelope.marshal() await expect(RecordEnvelope.openAndCertify(rawEnvelope, '/bad-domain')) diff --git a/packages/peer-record/test/peer-record.spec.ts b/packages/peer-record/test/peer-record.spec.ts index edc623cccc..5bddec70f2 100644 --- a/packages/peer-record/test/peer-record.spec.ts +++ b/packages/peer-record/test/peer-record.spec.ts @@ -7,7 +7,7 @@ import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' import { RecordEnvelope } from '../src/envelope/index.js' import { PeerRecord } from '../src/peer-record/index.js' -import type { PeerId } from '@libp2p/interface' +import type { PeerId, PrivateKey } from '@libp2p/interface' describe('PeerRecord', () => { let peerId: PeerId @@ -30,7 +30,7 @@ describe('PeerRecord', () => { // The payload isn't going to match because of how the protobuf encodes uint64 values // They are marshalled correctly on both sides, but will be off by 1 value // Signatures will still be validated - const jsEnv = await RecordEnvelope.seal(record, peerId) + const jsEnv = await RecordEnvelope.seal(record, key) expect(env.payloadType).to.eql(jsEnv.payloadType) }) @@ -117,10 +117,12 @@ describe('PeerRecord', () => { describe('PeerRecord inside Envelope', () => { let peerId: PeerId + let privateKey: PrivateKey let peerRecord: PeerRecord before(async () => { peerId = await createEd25519PeerId() + privateKey = await unmarshalPrivateKey(peerId.privateKey) const multiaddrs = [ multiaddr('/ip4/127.0.0.1/tcp/2000') ] @@ -129,7 +131,7 @@ describe('PeerRecord inside Envelope', () => { }) it('creates an envelope with the PeerRecord and can unmarshal it', async () => { - const e = await RecordEnvelope.seal(peerRecord, peerId) + const e = await RecordEnvelope.seal(peerRecord, privateKey) const byteE = e.marshal() const decodedE = await RecordEnvelope.openAndCertify(byteE, PeerRecord.DOMAIN) diff --git a/packages/peer-store/test/index.spec.ts b/packages/peer-store/test/index.spec.ts index 60ddfbee9a..d5f9a0b2c6 100644 --- a/packages/peer-store/test/index.spec.ts +++ b/packages/peer-store/test/index.spec.ts @@ -1,7 +1,8 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { TypedEventEmitter, type TypedEventTarget, type Libp2pEvents, type PeerId } from '@libp2p/interface' +import { unmarshalPrivateKey } from '@libp2p/crypto' +import { TypedEventEmitter, type TypedEventTarget, type Libp2pEvents, type PeerId, type PrivateKey } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record' @@ -15,12 +16,14 @@ const addr1 = multiaddr('/ip4/127.0.0.1/tcp/8000') describe('PersistentPeerStore', () => { let peerId: PeerId + let privateKey: PrivateKey let otherPeerId: PeerId let peerStore: PersistentPeerStore let events: TypedEventTarget beforeEach(async () => { peerId = await createEd25519PeerId() + privateKey = await unmarshalPrivateKey(peerId.privateKey) otherPeerId = await createEd25519PeerId() events = new TypedEventEmitter() peerStore = new PersistentPeerStore({ @@ -176,7 +179,7 @@ describe('PersistentPeerStore', () => { multiaddr('/ip4/127.0.0.1/tcp/1234') ] }) - const signedPeerRecord = await RecordEnvelope.seal(peerRecord, peerId) + const signedPeerRecord = await RecordEnvelope.seal(peerRecord, privateKey) await expect(peerStore.has(peerId)).to.eventually.be.false() await peerStore.consumePeerRecord(signedPeerRecord.marshal()) @@ -205,7 +208,7 @@ describe('PersistentPeerStore', () => { multiaddr('/ip4/127.0.0.1/tcp/4567') ] }) - const signedPeerRecord = await RecordEnvelope.seal(peerRecord, peerId) + const signedPeerRecord = await RecordEnvelope.seal(peerRecord, privateKey) await peerStore.consumePeerRecord(signedPeerRecord.marshal()) @@ -228,7 +231,7 @@ describe('PersistentPeerStore', () => { multiaddr('/ip4/127.0.0.1/tcp/1234') ], seqNumber: 1n - }), peerId) + }), privateKey) const newSignedPeerRecord = await RecordEnvelope.seal(new PeerRecord({ peerId, @@ -236,7 +239,7 @@ describe('PersistentPeerStore', () => { multiaddr('/ip4/127.0.0.1/tcp/4567') ], seqNumber: 2n - }), peerId) + }), privateKey) await expect(peerStore.consumePeerRecord(newSignedPeerRecord.marshal())).to.eventually.equal(true) await expect(peerStore.consumePeerRecord(oldSignedPeerRecord.marshal())).to.eventually.equal(false) @@ -257,7 +260,7 @@ describe('PersistentPeerStore', () => { multiaddrs: [ multiaddr('/ip4/127.0.0.1/tcp/4567') ] - }), peerId) + }), privateKey) await expect(peerStore.has(peerId)).to.eventually.be.false() await expect(peerStore.consumePeerRecord(signedPeerRecord.marshal(), otherPeerId)).to.eventually.equal(false) diff --git a/packages/protocol-identify/src/identify.ts b/packages/protocol-identify/src/identify.ts index 836ee7eb1e..f18caec032 100644 --- a/packages/protocol-identify/src/identify.ts +++ b/packages/protocol-identify/src/identify.ts @@ -18,7 +18,7 @@ import { } from './consts.js' import { Identify as IdentifyMessage } from './pb/message.js' import type { Identify as IdentifyInterface, IdentifyComponents, IdentifyInit } from './index.js' -import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions, Logger, Connection, Stream, TypedEventTarget, PeerId, Peer, PeerData, PeerStore, Startable } from '@libp2p/interface' +import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions, Logger, Connection, Stream, TypedEventTarget, PeerId, Peer, PeerData, PeerStore, Startable, PrivateKey } from '@libp2p/interface' import type { AddressManager, ConnectionManager, IncomingStreamData, Registrar } from '@libp2p/interface-internal' // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L52 @@ -49,6 +49,7 @@ export class Identify implements Startable, IdentifyInterface { private started: boolean private readonly timeout: number private readonly peerId: PeerId + private readonly privateKey: PrivateKey private readonly peerStore: PeerStore private readonly registrar: Registrar private readonly connectionManager: ConnectionManager @@ -66,6 +67,7 @@ export class Identify implements Startable, IdentifyInterface { constructor (components: IdentifyComponents, init: IdentifyInit = {}) { this.started = false this.peerId = components.peerId + this.privateKey = components.privateKey this.peerStore = components.peerStore this.registrar = components.registrar this.addressManager = components.addressManager @@ -167,7 +169,7 @@ export class Identify implements Startable, IdentifyInterface { peerId: this.peerId, multiaddrs: listenAddresses }) - const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.peerId) + const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.privateKey) const supportedProtocols = this.registrar.getProtocols() const peer = await this.peerStore.get(this.peerId) const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') ?? uint8ArrayFromString(this.host.agentVersion)) @@ -339,7 +341,7 @@ export class Identify implements Startable, IdentifyInterface { multiaddrs }) - const envelope = await RecordEnvelope.seal(peerRecord, this.peerId) + const envelope = await RecordEnvelope.seal(peerRecord, this.privateKey) signedPeerRecord = envelope.marshal().subarray() } diff --git a/packages/protocol-identify/src/index.ts b/packages/protocol-identify/src/index.ts index 61361f9edf..cc23e13fdd 100644 --- a/packages/protocol-identify/src/index.ts +++ b/packages/protocol-identify/src/index.ts @@ -23,7 +23,7 @@ import { MULTICODEC_IDENTIFY_PUSH } from './consts.js' import { Identify as IdentifyClass } from './identify.js' -import type { AbortOptions, IdentifyResult, Libp2pEvents, ComponentLogger, NodeInfo, TypedEventTarget, PeerId, PeerStore, Connection } from '@libp2p/interface' +import type { AbortOptions, IdentifyResult, Libp2pEvents, ComponentLogger, NodeInfo, TypedEventTarget, PeerId, PeerStore, Connection, PrivateKey } from '@libp2p/interface' import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal' export interface IdentifyInit { @@ -67,6 +67,7 @@ export interface IdentifyInit { export interface IdentifyComponents { peerId: PeerId + privateKey: PrivateKey peerStore: PeerStore connectionManager: ConnectionManager registrar: Registrar diff --git a/packages/protocol-identify/test/fixtures/index.ts b/packages/protocol-identify/test/fixtures/index.ts index 61f45fde25..705e8d03d9 100644 --- a/packages/protocol-identify/test/fixtures/index.ts +++ b/packages/protocol-identify/test/fixtures/index.ts @@ -5,7 +5,7 @@ import Sinon from 'sinon' import { stubInterface, type StubbedInstance } from 'sinon-ts' import { Uint8ArrayList } from 'uint8arraylist' import { Identify as IdentifyMessage } from '../../src/pb/message.js' -import type { ComponentLogger, Libp2pEvents, NodeInfo, TypedEventTarget, PeerId, PeerStore, Connection, Stream } from '@libp2p/interface' +import type { ComponentLogger, Libp2pEvents, NodeInfo, TypedEventTarget, PeerId, PeerStore, Connection, Stream, PrivateKey } from '@libp2p/interface' import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal' export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher { @@ -14,6 +14,7 @@ export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher { export interface StubbedIdentifyComponents { peerId: PeerId + privateKey: PrivateKey peerStore: StubbedInstance connectionManager: StubbedInstance registrar: StubbedInstance diff --git a/packages/protocol-identify/test/index.spec.ts b/packages/protocol-identify/test/index.spec.ts index 5aab641c05..6c5cf817d8 100644 --- a/packages/protocol-identify/test/index.spec.ts +++ b/packages/protocol-identify/test/index.spec.ts @@ -1,3 +1,4 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { TypedEventEmitter, start, stop } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -24,8 +25,10 @@ describe('identify', () => { let identify: Identify beforeEach(async () => { + const peerId = await createEd25519PeerId() components = { - peerId: await createEd25519PeerId(), + peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey), peerStore: stubInterface(), connectionManager: stubInterface(), registrar: stubInterface(), @@ -215,6 +218,7 @@ describe('identify', () => { await start(identify) const remotePeer = await createEd25519PeerId() + const remotePrivateKey = await unmarshalPrivateKey(remotePeer.privateKey) const oldPeerRecord = await RecordEnvelope.seal(new PeerRecord({ peerId: remotePeer, @@ -222,7 +226,7 @@ describe('identify', () => { multiaddr('/ip4/127.0.0.1/tcp/1234') ], seqNumber: BigInt(1n) - }), remotePeer) + }), remotePrivateKey) const connection = identifyConnection(remotePeer, { listenAddrs: [], @@ -238,7 +242,7 @@ describe('identify', () => { multiaddr('/ip4/127.0.0.1/tcp/1234') ], seqNumber: BigInt(Date.now() * 2) - }), remotePeer) + }), remotePrivateKey) components.peerStore.get.resolves({ id: remotePeer, @@ -298,6 +302,7 @@ describe('identify', () => { await start(identify) const remotePeer = await createEd25519PeerId() + const remotePrivateKey = await unmarshalPrivateKey(remotePeer.privateKey) const signedPeerRecord = await RecordEnvelope.seal(new PeerRecord({ peerId: remotePeer, @@ -305,7 +310,7 @@ describe('identify', () => { multiaddr('/ip4/127.0.0.1/tcp/5678') ], seqNumber: BigInt(Date.now() * 2) - }), remotePeer) + }), remotePrivateKey) const peerRecordEnvelope = signedPeerRecord.marshal() const message = { diff --git a/packages/transport-circuit-relay-v2/src/server/index.ts b/packages/transport-circuit-relay-v2/src/server/index.ts index 36eb9fdfa8..e9e39132a2 100644 --- a/packages/transport-circuit-relay-v2/src/server/index.ts +++ b/packages/transport-circuit-relay-v2/src/server/index.ts @@ -18,7 +18,7 @@ import { AdvertService, type AdvertServiceComponents, type AdvertServiceInit } f import { ReservationStore, type ReservationStoreInit } from './reservation-store.js' import { ReservationVoucherRecord } from './reservation-voucher.js' import type { CircuitRelayService, RelayReservation } from '../index.js' -import type { ComponentLogger, Logger, Connection, Stream, ConnectionGater, PeerId, PeerStore, Startable } from '@libp2p/interface' +import type { ComponentLogger, Logger, Connection, Stream, ConnectionGater, PeerId, PeerStore, Startable, PrivateKey } from '@libp2p/interface' import type { AddressManager, ConnectionManager, IncomingStreamData, Registrar } from '@libp2p/interface-internal' import type { PeerMap } from '@libp2p/peer-collections' @@ -75,6 +75,7 @@ export interface CircuitRelayServerComponents extends AdvertServiceComponents { peerStore: PeerStore addressManager: AddressManager peerId: PeerId + privateKey: PrivateKey connectionManager: ConnectionManager connectionGater: ConnectionGater logger: ComponentLogger @@ -95,6 +96,7 @@ class CircuitRelayServer extends TypedEventEmitter implements private readonly peerStore: PeerStore private readonly addressManager: AddressManager private readonly peerId: PeerId + private readonly privateKey: PrivateKey private readonly connectionManager: ConnectionManager private readonly connectionGater: ConnectionGater private readonly reservationStore: ReservationStore @@ -118,6 +120,7 @@ class CircuitRelayServer extends TypedEventEmitter implements this.peerStore = components.peerStore this.addressManager = components.addressManager this.peerId = components.peerId + this.privateKey = components.privateKey this.connectionManager = components.connectionManager this.connectionGater = components.connectionGater this.started = false @@ -303,7 +306,7 @@ class CircuitRelayServer extends TypedEventEmitter implements peer: remotePeer, relay: this.peerId, expiration: Number(expire) - }), this.peerId) + }), this.privateKey) return { addrs, From 44c42e6455bad486fd9cfa668b947790e2540fdd Mon Sep 17 00:00:00 2001 From: Cayman Date: Tue, 5 Dec 2023 16:32:48 -0500 Subject: [PATCH 3/7] fix!: update PubSubBaseProtocol to use private key --- packages/pubsub/src/index.ts | 5 +++-- packages/pubsub/src/sign.ts | 17 ++++------------- packages/pubsub/test/sign.spec.ts | 10 ++++++---- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/pubsub/src/index.ts b/packages/pubsub/src/index.ts index f46a7fccf2..845910921f 100644 --- a/packages/pubsub/src/index.ts +++ b/packages/pubsub/src/index.ts @@ -15,7 +15,7 @@ */ import { CodeError, TypedEventEmitter, CustomEvent } from '@libp2p/interface' -import { type PubSub, type Message, type StrictNoSign, type StrictSign, type PubSubInit, type PubSubEvents, type PeerStreams, type PubSubRPCMessage, type PubSubRPC, type PubSubRPCSubscription, type SubscriptionChangeData, type PublishResult, type TopicValidatorFn, TopicValidatorResult, type ComponentLogger, type Logger, type Connection, type PeerId } from '@libp2p/interface' +import { type PubSub, type Message, type StrictNoSign, type StrictSign, type PubSubInit, type PubSubEvents, type PeerStreams, type PrivateKey, type PubSubRPCMessage, type PubSubRPC, type PubSubRPCSubscription, type SubscriptionChangeData, type PublishResult, type TopicValidatorFn, TopicValidatorResult, type ComponentLogger, type Logger, type Connection, type PeerId } from '@libp2p/interface' import { PeerMap, PeerSet } from '@libp2p/peer-collections' import { pipe } from 'it-pipe' import Queue from 'p-queue' @@ -31,6 +31,7 @@ import type { Uint8ArrayList } from 'uint8arraylist' export interface PubSubComponents { peerId: PeerId + privateKey: PrivateKey registrar: Registrar logger: ComponentLogger } @@ -614,7 +615,7 @@ export abstract class PubSubBaseProtocol = Pu const signaturePolicy = this.globalSignaturePolicy switch (signaturePolicy) { case 'StrictSign': - return signMessage(this.components.peerId, message, this.encodeMessage.bind(this)) + return signMessage(this.components.privateKey, message, this.encodeMessage.bind(this)) case 'StrictNoSign': return Promise.resolve({ type: 'unsigned', diff --git a/packages/pubsub/src/sign.ts b/packages/pubsub/src/sign.ts index 41de3e7bbc..2215c1d24e 100644 --- a/packages/pubsub/src/sign.ts +++ b/packages/pubsub/src/sign.ts @@ -3,29 +3,21 @@ import { peerIdFromKeys } from '@libp2p/peer-id' import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toRpcMessage } from './utils.js' -import type { PeerId, PubSubRPCMessage, SignedMessage } from '@libp2p/interface' +import type { PeerId, PrivateKey, PubSubRPCMessage, SignedMessage } from '@libp2p/interface' export const SignPrefix = uint8ArrayFromString('libp2p-pubsub:') /** * Signs the provided message with the given `peerId` */ -export async function signMessage (peerId: PeerId, message: { from: PeerId, topic: string, data: Uint8Array, sequenceNumber: bigint }, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise { - if (peerId.privateKey == null) { - throw new Error('Cannot sign message, no private key present') - } - - if (peerId.publicKey == null) { - throw new Error('Cannot sign message, no public key present') - } - +export async function signMessage (privateKey: PrivateKey, message: { from: PeerId, topic: string, data: Uint8Array, sequenceNumber: bigint }, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise { // @ts-expect-error signature field is missing, added below const outputMessage: SignedMessage = { type: 'signed', topic: message.topic, data: message.data, sequenceNumber: message.sequenceNumber, - from: peerId + from: message.from } // Get the message in bytes, and prepend with the pubsub prefix @@ -34,9 +26,8 @@ export async function signMessage (peerId: PeerId, message: { from: PeerId, topi encode(toRpcMessage(outputMessage)).subarray() ]) - const privateKey = await keys.unmarshalPrivateKey(peerId.privateKey) outputMessage.signature = await privateKey.sign(bytes) - outputMessage.key = peerId.publicKey + outputMessage.key = privateKey.public.bytes return outputMessage } diff --git a/packages/pubsub/test/sign.spec.ts b/packages/pubsub/test/sign.spec.ts index ffcf571104..3c3d778414 100644 --- a/packages/pubsub/test/sign.spec.ts +++ b/packages/pubsub/test/sign.spec.ts @@ -10,7 +10,7 @@ import { } from '../src/sign.js' import { randomSeqno, toRpcMessage } from '../src/utils.js' import { RPC } from './message/rpc.js' -import type { PeerId, PubSubRPCMessage } from '@libp2p/interface' +import type { PeerId, PrivateKey, PubSubRPCMessage } from '@libp2p/interface' function encodeMessage (message: PubSubRPCMessage): Uint8Array { return RPC.Message.encode(message) @@ -18,11 +18,13 @@ function encodeMessage (message: PubSubRPCMessage): Uint8Array { describe('message signing', () => { let peerId: PeerId + let privateKey: PrivateKey before(async () => { peerId = await PeerIdFactory.createRSAPeerId({ bits: 1024 }) + privateKey = await keys.unmarshalPrivateKey(peerId.privateKey) }) it('should be able to sign and verify a message', async () => { @@ -44,7 +46,7 @@ describe('message signing', () => { const privateKey = await keys.unmarshalPrivateKey(peerId.privateKey) const expectedSignature = await privateKey.sign(bytesToSign) - const signedMessage = await signMessage(peerId, message, encodeMessage) + const signedMessage = await signMessage(privateKey, message, encodeMessage) // Check the signature and public key expect(signedMessage.signature).to.equalBytes(expectedSignature) @@ -79,7 +81,7 @@ describe('message signing', () => { const privateKey = await keys.unmarshalPrivateKey(secPeerId.privateKey) const expectedSignature = await privateKey.sign(bytesToSign) - const signedMessage = await signMessage(secPeerId, message, encodeMessage) + const signedMessage = await signMessage(privateKey, message, encodeMessage) // Check the signature and public key expect(signedMessage.signature).to.eql(expectedSignature) @@ -113,7 +115,7 @@ describe('message signing', () => { const privateKey = await keys.unmarshalPrivateKey(peerId.privateKey) const expectedSignature = await privateKey.sign(bytesToSign) - const signedMessage = await signMessage(peerId, message, encodeMessage) + const signedMessage = await signMessage(privateKey, message, encodeMessage) // Check the signature and public key expect(signedMessage.signature).to.equalBytes(expectedSignature) From 453e91bf860314959117332f70951dc5a858164f Mon Sep 17 00:00:00 2001 From: Cayman Date: Tue, 5 Dec 2023 22:41:29 -0500 Subject: [PATCH 4/7] chore: fix build errors --- .../src/mocks/connection-manager.ts | 3 ++- .../interface-compliance-tests/src/pubsub/index.ts | 3 ++- .../interface-compliance-tests/src/pubsub/utils.ts | 5 ++++- packages/kad-dht/test/utils/test-dht.ts | 6 +++++- packages/peer-record/test/envelope.spec.ts | 2 +- packages/peer-record/test/peer-record.spec.ts | 2 +- packages/peer-store/test/index.spec.ts | 4 ++-- packages/protocol-identify/test/index.spec.ts | 6 +++--- packages/protocol-identify/test/push.spec.ts | 5 ++++- packages/pubsub-floodsub/test/floodsub.spec.ts | 5 ++++- packages/pubsub/test/emit-self.spec.ts | 3 +++ packages/pubsub/test/instance.spec.ts | 3 +++ packages/pubsub/test/lifecycle.spec.ts | 4 ++++ packages/pubsub/test/message.spec.ts | 2 ++ packages/pubsub/test/pubsub.spec.ts | 11 +++++++++++ packages/pubsub/test/sign.spec.ts | 4 +--- packages/pubsub/test/topic-validators.spec.ts | 2 ++ packages/transport-circuit-relay-v2/test/hop.spec.ts | 6 +++++- 18 files changed, 59 insertions(+), 17 deletions(-) diff --git a/packages/interface-compliance-tests/src/mocks/connection-manager.ts b/packages/interface-compliance-tests/src/mocks/connection-manager.ts index 5d0b4297d4..eb8ec094a1 100644 --- a/packages/interface-compliance-tests/src/mocks/connection-manager.ts +++ b/packages/interface-compliance-tests/src/mocks/connection-manager.ts @@ -1,5 +1,5 @@ import { CodeError } from '@libp2p/interface' -import { isPeerId, type PeerId, type ComponentLogger, type Libp2pEvents, type PendingDial, type Connection, type TypedEventTarget, type PubSub, type Startable } from '@libp2p/interface' +import { isPeerId, type PeerId, type ComponentLogger, type Libp2pEvents, type PendingDial, type Connection, type TypedEventTarget, type PrivateKey, type PubSub, type Startable } from '@libp2p/interface' import { PeerMap } from '@libp2p/peer-collections' import { peerIdFromString } from '@libp2p/peer-id' import { isMultiaddr, type Multiaddr } from '@multiformats/multiaddr' @@ -8,6 +8,7 @@ import type { ConnectionManager, Registrar } from '@libp2p/interface-internal' export interface MockNetworkComponents { peerId: PeerId + privateKey: PrivateKey registrar: Registrar connectionManager: ConnectionManager events: TypedEventTarget diff --git a/packages/interface-compliance-tests/src/pubsub/index.ts b/packages/interface-compliance-tests/src/pubsub/index.ts index 7e5fa3fe95..bedc3f3c72 100644 --- a/packages/interface-compliance-tests/src/pubsub/index.ts +++ b/packages/interface-compliance-tests/src/pubsub/index.ts @@ -5,11 +5,12 @@ import messagesTest from './messages.js' import multipleNodesTest from './multiple-nodes.js' import twoNodesTest from './two-nodes.js' import type { TestSetup } from '../index.js' -import type { ComponentLogger, PeerId, PubSub, PubSubInit } from '@libp2p/interface' +import type { ComponentLogger, PeerId, PrivateKey, PubSub, PubSubInit } from '@libp2p/interface' import type { ConnectionManager, Registrar } from '@libp2p/interface-internal' export interface PubSubComponents { peerId: PeerId + privateKey: PrivateKey registrar: Registrar connectionManager: ConnectionManager pubsub?: PubSub diff --git a/packages/interface-compliance-tests/src/pubsub/utils.ts b/packages/interface-compliance-tests/src/pubsub/utils.ts index b3e626da90..215a40e997 100644 --- a/packages/interface-compliance-tests/src/pubsub/utils.ts +++ b/packages/interface-compliance-tests/src/pubsub/utils.ts @@ -1,3 +1,4 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { TypedEventEmitter } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -16,8 +17,10 @@ export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId): Promise< } export async function createComponents (): Promise { + const peerId = await createEd25519PeerId() const components: any = { - peerId: await createEd25519PeerId(), + peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: mockRegistrar(), events: new TypedEventEmitter(), logger: defaultLogger() diff --git a/packages/kad-dht/test/utils/test-dht.ts b/packages/kad-dht/test/utils/test-dht.ts index 742eae1357..ad70ab9895 100644 --- a/packages/kad-dht/test/utils/test-dht.ts +++ b/packages/kad-dht/test/utils/test-dht.ts @@ -1,3 +1,4 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { TypedEventEmitter, start, stop } from '@libp2p/interface' import { mockRegistrar, mockConnectionManager, mockNetwork } from '@libp2p/interface-compliance-tests/mocks' import { defaultLogger } from '@libp2p/logger' @@ -23,8 +24,10 @@ export class TestDHT { async spawn (options: Partial = {}, autoStart = true): Promise { const events = new TypedEventEmitter() + const peerId = await createPeerId() + const privateKey = await unmarshalPrivateKey(peerId.privateKey as Uint8Array) const components: KadDHTComponents = { - peerId: await createPeerId(), + peerId, datastore: new MemoryDatastore(), registrar: mockRegistrar(), // connectionGater: mockConnectionGater(), @@ -47,6 +50,7 @@ export class TestDHT { mockNetwork.addNode({ ...components, + privateKey, events }) diff --git a/packages/peer-record/test/envelope.spec.ts b/packages/peer-record/test/envelope.spec.ts index 969cb36aea..36fe60cbbe 100644 --- a/packages/peer-record/test/envelope.spec.ts +++ b/packages/peer-record/test/envelope.spec.ts @@ -38,7 +38,7 @@ describe('Envelope', () => { before(async () => { peerId = await createEd25519PeerId() - privateKey = await unmarshalPrivateKey(peerId.privateKey) + privateKey = await unmarshalPrivateKey(peerId.privateKey as Uint8Array) testRecord = new TestRecord('test-data') }) diff --git a/packages/peer-record/test/peer-record.spec.ts b/packages/peer-record/test/peer-record.spec.ts index 5bddec70f2..9d217a390b 100644 --- a/packages/peer-record/test/peer-record.spec.ts +++ b/packages/peer-record/test/peer-record.spec.ts @@ -122,7 +122,7 @@ describe('PeerRecord inside Envelope', () => { before(async () => { peerId = await createEd25519PeerId() - privateKey = await unmarshalPrivateKey(peerId.privateKey) + privateKey = await unmarshalPrivateKey(peerId.privateKey as Uint8Array) const multiaddrs = [ multiaddr('/ip4/127.0.0.1/tcp/2000') ] diff --git a/packages/peer-store/test/index.spec.ts b/packages/peer-store/test/index.spec.ts index d5f9a0b2c6..bf5f1102f5 100644 --- a/packages/peer-store/test/index.spec.ts +++ b/packages/peer-store/test/index.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 6] */ -import { unmarshalPrivateKey } from '@libp2p/crypto' +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { TypedEventEmitter, type TypedEventTarget, type Libp2pEvents, type PeerId, type PrivateKey } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -23,7 +23,7 @@ describe('PersistentPeerStore', () => { beforeEach(async () => { peerId = await createEd25519PeerId() - privateKey = await unmarshalPrivateKey(peerId.privateKey) + privateKey = await unmarshalPrivateKey(peerId.privateKey as Uint8Array) otherPeerId = await createEd25519PeerId() events = new TypedEventEmitter() peerStore = new PersistentPeerStore({ diff --git a/packages/protocol-identify/test/index.spec.ts b/packages/protocol-identify/test/index.spec.ts index 6c5cf817d8..3bd2735cab 100644 --- a/packages/protocol-identify/test/index.spec.ts +++ b/packages/protocol-identify/test/index.spec.ts @@ -28,7 +28,7 @@ describe('identify', () => { const peerId = await createEd25519PeerId() components = { peerId, - privateKey: await unmarshalPrivateKey(peerId.privateKey), + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), peerStore: stubInterface(), connectionManager: stubInterface(), registrar: stubInterface(), @@ -218,7 +218,7 @@ describe('identify', () => { await start(identify) const remotePeer = await createEd25519PeerId() - const remotePrivateKey = await unmarshalPrivateKey(remotePeer.privateKey) + const remotePrivateKey = await unmarshalPrivateKey(remotePeer.privateKey as Uint8Array) const oldPeerRecord = await RecordEnvelope.seal(new PeerRecord({ peerId: remotePeer, @@ -302,7 +302,7 @@ describe('identify', () => { await start(identify) const remotePeer = await createEd25519PeerId() - const remotePrivateKey = await unmarshalPrivateKey(remotePeer.privateKey) + const remotePrivateKey = await unmarshalPrivateKey(remotePeer.privateKey as Uint8Array) const signedPeerRecord = await RecordEnvelope.seal(new PeerRecord({ peerId: remotePeer, diff --git a/packages/protocol-identify/test/push.spec.ts b/packages/protocol-identify/test/push.spec.ts index 7e4ed7c489..61cd4dff67 100644 --- a/packages/protocol-identify/test/push.spec.ts +++ b/packages/protocol-identify/test/push.spec.ts @@ -1,3 +1,4 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { TypedEventEmitter, start, stop } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -17,8 +18,10 @@ describe('identify (push)', () => { let identify: Identify beforeEach(async () => { + const peerId = await createEd25519PeerId() components = { - peerId: await createEd25519PeerId(), + peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), peerStore: stubInterface(), connectionManager: stubInterface(), registrar: stubInterface(), diff --git a/packages/pubsub-floodsub/test/floodsub.spec.ts b/packages/pubsub-floodsub/test/floodsub.spec.ts index cae44d0f7e..887be1a684 100644 --- a/packages/pubsub-floodsub/test/floodsub.spec.ts +++ b/packages/pubsub-floodsub/test/floodsub.spec.ts @@ -1,5 +1,6 @@ /* eslint-env mocha */ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { type Message, type PubSubRPC, StrictNoSign } from '@libp2p/interface' import { mockRegistrar } from '@libp2p/interface-compliance-tests/mocks' import { defaultLogger } from '@libp2p/logger' @@ -23,8 +24,10 @@ describe('floodsub', () => { before(async () => { expect(multicodec).to.exist() + const peerId = await createEd25519PeerId() floodsub = new FloodSub({ - peerId: await createEd25519PeerId(), + peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: mockRegistrar(), logger: defaultLogger() }, { diff --git a/packages/pubsub/test/emit-self.spec.ts b/packages/pubsub/test/emit-self.spec.ts index b5abf96785..dfff14c224 100644 --- a/packages/pubsub/test/emit-self.spec.ts +++ b/packages/pubsub/test/emit-self.spec.ts @@ -1,3 +1,4 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { defaultLogger } from '@libp2p/logger' import { expect } from 'aegir/chai' import delay from 'delay' @@ -22,6 +23,7 @@ describe('emitSelf', () => { pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { @@ -78,6 +80,7 @@ describe('emitSelf', () => { pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { diff --git a/packages/pubsub/test/instance.spec.ts b/packages/pubsub/test/instance.spec.ts index 8f96356d0b..cf9beaea5c 100644 --- a/packages/pubsub/test/instance.spec.ts +++ b/packages/pubsub/test/instance.spec.ts @@ -1,3 +1,4 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { expect } from 'aegir/chai' @@ -38,10 +39,12 @@ describe('pubsub instance', () => { it('should accept valid parameters', async () => { const peerId = await createEd25519PeerId() + const privateKey = await unmarshalPrivateKey(peerId.privateKey as Uint8Array) expect(() => { return new PubsubProtocol({ peerId, + privateKey, registrar: new MockRegistrar(), logger: defaultLogger() }, { // eslint-disable-line no-new diff --git a/packages/pubsub/test/lifecycle.spec.ts b/packages/pubsub/test/lifecycle.spec.ts index 787588700b..ed4ef1f847 100644 --- a/packages/pubsub/test/lifecycle.spec.ts +++ b/packages/pubsub/test/lifecycle.spec.ts @@ -1,3 +1,4 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { defaultLogger } from '@libp2p/logger' import { expect } from 'aegir/chai' import delay from 'delay' @@ -53,6 +54,7 @@ describe('pubsub base lifecycle', () => { pubsub = new PubsubProtocol({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: sinonMockRegistrar, logger: defaultLogger() }, { @@ -113,6 +115,7 @@ describe('pubsub base lifecycle', () => { pubsubA = new PubsubImplementation({ peerId: peerIdA, + privateKey: await unmarshalPrivateKey(peerIdA.privateKey as Uint8Array), registrar: registrarA, logger: defaultLogger() }, { @@ -120,6 +123,7 @@ describe('pubsub base lifecycle', () => { }) pubsubB = new PubsubImplementation({ peerId: peerIdB, + privateKey: await unmarshalPrivateKey(peerIdB.privateKey as Uint8Array), registrar: registrarB, logger: defaultLogger() }, { diff --git a/packages/pubsub/test/message.spec.ts b/packages/pubsub/test/message.spec.ts index 137d875ad2..f901db8b38 100644 --- a/packages/pubsub/test/message.spec.ts +++ b/packages/pubsub/test/message.spec.ts @@ -1,4 +1,5 @@ /* eslint-env mocha */ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { defaultLogger } from '@libp2p/logger' import { expect } from 'aegir/chai' import sinon from 'sinon' @@ -19,6 +20,7 @@ describe('pubsub base messages', () => { peerId = await createPeerId() pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { diff --git a/packages/pubsub/test/pubsub.spec.ts b/packages/pubsub/test/pubsub.spec.ts index bd4da454b9..49aa4228d1 100644 --- a/packages/pubsub/test/pubsub.spec.ts +++ b/packages/pubsub/test/pubsub.spec.ts @@ -1,4 +1,5 @@ /* eslint max-nested-callbacks: ["error", 6] */ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { defaultLogger } from '@libp2p/logger' import { PeerSet } from '@libp2p/peer-collections' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -31,6 +32,7 @@ describe('pubsub base implementation', () => { const peerId = await createPeerId() pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { @@ -105,6 +107,7 @@ describe('pubsub base implementation', () => { const peerId = await createPeerId() pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { @@ -138,6 +141,7 @@ describe('pubsub base implementation', () => { pubsubA = new PubsubImplementation({ peerId: peerIdA, + privateKey: await unmarshalPrivateKey(peerIdA.privateKey as Uint8Array), registrar: registrarA, logger: defaultLogger() }, { @@ -145,6 +149,7 @@ describe('pubsub base implementation', () => { }) pubsubB = new PubsubImplementation({ peerId: peerIdB, + privateKey: await unmarshalPrivateKey(peerIdB.privateKey as Uint8Array), registrar: registrarB, logger: defaultLogger() }, { @@ -210,6 +215,7 @@ describe('pubsub base implementation', () => { const peerId = await createPeerId() pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { @@ -247,6 +253,7 @@ describe('pubsub base implementation', () => { pubsubA = new PubsubImplementation({ peerId: peerIdA, + privateKey: await unmarshalPrivateKey(peerIdA.privateKey as Uint8Array), registrar: registrarA, logger: defaultLogger() }, { @@ -254,6 +261,7 @@ describe('pubsub base implementation', () => { }) pubsubB = new PubsubImplementation({ peerId: peerIdB, + privateKey: await unmarshalPrivateKey(peerIdB.privateKey as Uint8Array), registrar: registrarB, logger: defaultLogger() }, { @@ -343,6 +351,7 @@ describe('pubsub base implementation', () => { peerId = await createPeerId() pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { @@ -373,6 +382,7 @@ describe('pubsub base implementation', () => { peerId = await createPeerId() pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { @@ -447,6 +457,7 @@ describe('pubsub base implementation', () => { peerId = await createPeerId() pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { diff --git a/packages/pubsub/test/sign.spec.ts b/packages/pubsub/test/sign.spec.ts index 3c3d778414..3a3d8f9f11 100644 --- a/packages/pubsub/test/sign.spec.ts +++ b/packages/pubsub/test/sign.spec.ts @@ -10,7 +10,7 @@ import { } from '../src/sign.js' import { randomSeqno, toRpcMessage } from '../src/utils.js' import { RPC } from './message/rpc.js' -import type { PeerId, PrivateKey, PubSubRPCMessage } from '@libp2p/interface' +import type { PeerId, PubSubRPCMessage } from '@libp2p/interface' function encodeMessage (message: PubSubRPCMessage): Uint8Array { return RPC.Message.encode(message) @@ -18,13 +18,11 @@ function encodeMessage (message: PubSubRPCMessage): Uint8Array { describe('message signing', () => { let peerId: PeerId - let privateKey: PrivateKey before(async () => { peerId = await PeerIdFactory.createRSAPeerId({ bits: 1024 }) - privateKey = await keys.unmarshalPrivateKey(peerId.privateKey) }) it('should be able to sign and verify a message', async () => { diff --git a/packages/pubsub/test/topic-validators.spec.ts b/packages/pubsub/test/topic-validators.spec.ts index 153d322de7..cd4c4d8117 100644 --- a/packages/pubsub/test/topic-validators.spec.ts +++ b/packages/pubsub/test/topic-validators.spec.ts @@ -1,3 +1,4 @@ +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { type PubSubRPC, TopicValidatorResult, type PeerId } from '@libp2p/interface' import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -25,6 +26,7 @@ describe('topic validators', () => { pubsub = new PubsubImplementation({ peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), registrar: new MockRegistrar(), logger: defaultLogger() }, { diff --git a/packages/transport-circuit-relay-v2/test/hop.spec.ts b/packages/transport-circuit-relay-v2/test/hop.spec.ts index dd2aef249d..eee649f903 100644 --- a/packages/transport-circuit-relay-v2/test/hop.spec.ts +++ b/packages/transport-circuit-relay-v2/test/hop.spec.ts @@ -1,6 +1,7 @@ /* eslint-disable max-nested-callbacks */ -import { TypedEventEmitter, type TypedEventTarget, type ComponentLogger, type Libp2pEvents, type Connection, type Stream, type ConnectionGater, type ContentRouting, type PeerId, type PeerStore, type Transport, type Upgrader } from '@libp2p/interface' +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' +import { TypedEventEmitter, type TypedEventTarget, type ComponentLogger, type Libp2pEvents, type Connection, type Stream, type ConnectionGater, type ContentRouting, type PeerId, type PeerStore, type Transport, type Upgrader, type PrivateKey } from '@libp2p/interface' import { isStartable } from '@libp2p/interface' import { mockRegistrar, mockUpgrader, mockNetwork, mockConnectionManager, mockConnectionGater } from '@libp2p/interface-compliance-tests/mocks' import { defaultLogger } from '@libp2p/logger' @@ -23,6 +24,7 @@ export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher { interface Node { peerId: PeerId + privateKey: PrivateKey multiaddr: Multiaddr registrar: Registrar peerStore: StubbedInstance @@ -88,6 +90,7 @@ describe('circuit-relay hop protocol', function () { contentRouting: stubInterface(), connectionManager, peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), peerStore, registrar, connectionGater, @@ -118,6 +121,7 @@ describe('circuit-relay hop protocol', function () { const node: Node = { peerId, + privateKey: await unmarshalPrivateKey(peerId.privateKey as Uint8Array), multiaddr: ma, registrar, circuitRelayService: service, From 0c2501b859971a4462b41195237c71bec90c1a09 Mon Sep 17 00:00:00 2001 From: Cayman Date: Tue, 5 Dec 2023 22:53:19 -0500 Subject: [PATCH 5/7] chore: fix linter error --- packages/peer-record/src/envelope/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/peer-record/src/envelope/index.ts b/packages/peer-record/src/envelope/index.ts index 008bceffb9..f81056ecee 100644 --- a/packages/peer-record/src/envelope/index.ts +++ b/packages/peer-record/src/envelope/index.ts @@ -24,7 +24,7 @@ export class RecordEnvelope implements Envelope { static createFromProtobuf = async (data: Uint8Array | Uint8ArrayList): Promise => { const envelopeData = Protobuf.decode(data) const peerId = await peerIdFromKeys(envelopeData.publicKey) - const publicKey = await unmarshalPublicKey(envelopeData.publicKey) + const publicKey = unmarshalPublicKey(envelopeData.publicKey) return new RecordEnvelope({ peerId, From 13e00b6d01844117f901e4f4329e4c98b090905a Mon Sep 17 00:00:00 2001 From: Cayman Date: Wed, 6 Dec 2023 09:48:29 -0500 Subject: [PATCH 6/7] chore: fix dep-check --- packages/interface-compliance-tests/package.json | 1 + packages/peer-store/package.json | 1 + packages/protocol-identify/package.json | 1 + packages/pubsub-floodsub/package.json | 1 + packages/transport-circuit-relay-v2/package.json | 1 + 5 files changed, 5 insertions(+) diff --git a/packages/interface-compliance-tests/package.json b/packages/interface-compliance-tests/package.json index 3e34d7659c..56a7ba9094 100644 --- a/packages/interface-compliance-tests/package.json +++ b/packages/interface-compliance-tests/package.json @@ -103,6 +103,7 @@ "test:electron-main": "aegir test -t electron-main" }, "dependencies": { + "@libp2p/crypto": "^3.0.1", "@libp2p/interface": "^1.0.1", "@libp2p/interface-internal": "^1.0.1", "@libp2p/logger": "^4.0.1", diff --git a/packages/peer-store/package.json b/packages/peer-store/package.json index 08fe9bd62e..14cc93387f 100644 --- a/packages/peer-store/package.json +++ b/packages/peer-store/package.json @@ -73,6 +73,7 @@ "uint8arrays": "^4.0.6" }, "devDependencies": { + "@libp2p/crypto": "^3.0.1", "@libp2p/logger": "^4.0.1", "@types/sinon": "^17.0.0", "aegir": "^41.0.2", diff --git a/packages/protocol-identify/package.json b/packages/protocol-identify/package.json index fccef7399e..c701c40c95 100644 --- a/packages/protocol-identify/package.json +++ b/packages/protocol-identify/package.json @@ -65,6 +65,7 @@ "wherearewe": "^2.0.1" }, "devDependencies": { + "@libp2p/crypto": "^3.0.1", "@libp2p/logger": "^4.0.1", "@libp2p/peer-id-factory": "^4.0.0", "aegir": "^41.0.2", diff --git a/packages/pubsub-floodsub/package.json b/packages/pubsub-floodsub/package.json index 8b39dbc2d2..6c09b9af9d 100644 --- a/packages/pubsub-floodsub/package.json +++ b/packages/pubsub-floodsub/package.json @@ -66,6 +66,7 @@ "uint8arrays": "^4.0.6" }, "devDependencies": { + "@libp2p/crypto": "^3.0.1", "@libp2p/interface-compliance-tests": "^5.0.5", "@libp2p/logger": "^4.0.1", "@libp2p/peer-collections": "^5.0.0", diff --git a/packages/transport-circuit-relay-v2/package.json b/packages/transport-circuit-relay-v2/package.json index 3c06de46d3..37f1927d9e 100644 --- a/packages/transport-circuit-relay-v2/package.json +++ b/packages/transport-circuit-relay-v2/package.json @@ -72,6 +72,7 @@ "uint8arrays": "^4.0.6" }, "devDependencies": { + "@libp2p/crypto": "^3.0.1", "@libp2p/interface-compliance-tests": "^5.0.5", "@libp2p/logger": "^4.0.1", "@libp2p/peer-id-factory": "^4.0.0", From db8564933c04611388755b0ededd647986f3cfcd Mon Sep 17 00:00:00 2001 From: Cayman Date: Wed, 6 Dec 2023 09:51:24 -0500 Subject: [PATCH 7/7] chore: change key and peer id comparison --- packages/libp2p/src/config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/libp2p/src/config.ts b/packages/libp2p/src/config.ts index 1c4d1eef19..568f1bd985 100644 --- a/packages/libp2p/src/config.ts +++ b/packages/libp2p/src/config.ts @@ -1,4 +1,5 @@ import { CodeError, FaultTolerance } from '@libp2p/interface' +import { peerIdFromKeys } from '@libp2p/peer-id' import { defaultAddressSort } from '@libp2p/utils/address-sort' import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers' import mergeOptions from 'merge-options' @@ -32,7 +33,7 @@ export async function validateConfig