diff --git a/package.json b/package.json index 86f35f7..82dfaf8 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,8 @@ "@libp2p/peer-id-factory": "^2.0.0", "@libp2p/tcp": "^7.0.0", "@multiformats/multiaddr": "^12.1.0", - "aegir": "^38.1.7", + "@types/sinon": "^10.0.14", + "aegir": "^39.0.5", "benchmark": "^2.1.4", "execa": "^7.0.0", "go-libp2p": "^1.0.3", @@ -105,8 +106,7 @@ "mkdirp": "^3.0.0", "p-defer": "^4.0.0", "protons": "^7.0.0", - "sinon": "^15.0.0", - "util": "^0.12.4" + "sinon": "^15.0.0" }, "browser": { "./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js", diff --git a/src/@types/handshake-interface.ts b/src/@types/handshake-interface.ts index c88628a..0edc201 100644 --- a/src/@types/handshake-interface.ts +++ b/src/@types/handshake-interface.ts @@ -1,7 +1,7 @@ -import type { PeerId } from '@libp2p/interface-peer-id' import type { bytes } from './basic.js' import type { NoiseSession } from './handshake.js' import type { NoiseExtensions } from '../proto/payload.js' +import type { PeerId } from '@libp2p/interface-peer-id' export interface IHandshake { session: NoiseSession diff --git a/src/@types/libp2p.ts b/src/@types/libp2p.ts index 16596cf..8fa110d 100644 --- a/src/@types/libp2p.ts +++ b/src/@types/libp2p.ts @@ -1,6 +1,6 @@ -import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter' -import type { NoiseExtensions } from '../proto/payload.js' import type { bytes32 } from './basic.js' +import type { NoiseExtensions } from '../proto/payload.js' +import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter' export interface KeyPair { publicKey: bytes32 diff --git a/src/crypto/js.ts b/src/crypto/js.ts index 4bbcdee..2a50102 100644 --- a/src/crypto/js.ts +++ b/src/crypto/js.ts @@ -1,8 +1,8 @@ -import * as x25519 from '@stablelib/x25519' -import { sha256 } from '@noble/hashes/sha256' import { hkdf } from '@noble/hashes/hkdf' +import { sha256 } from '@noble/hashes/sha256' import { ChaCha20Poly1305 } from '@stablelib/chacha20poly1305' -import type { bytes32, bytes } from '../@types/basic.js' +import * as x25519 from '@stablelib/x25519' +import type { bytes, bytes32 } from '../@types/basic.js' import type { Hkdf } from '../@types/handshake.js' import type { KeyPair } from '../@types/libp2p.js' import type { ICryptoInterface } from '../crypto.js' diff --git a/src/crypto/streaming.ts b/src/crypto/streaming.ts index 7c704db..e785a64 100644 --- a/src/crypto/streaming.ts +++ b/src/crypto/streaming.ts @@ -1,10 +1,10 @@ import { TAG_LENGTH } from '@stablelib/chacha20poly1305' -import type { Transform } from 'it-stream-types' -import type { Uint8ArrayList } from 'uint8arraylist' -import type { IHandshake } from '../@types/handshake-interface.js' -import type { MetricsRegistry } from '../metrics.js' import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from '../constants.js' import { uint16BEEncode } from '../encoder.js' +import type { IHandshake } from '../@types/handshake-interface.js' +import type { MetricsRegistry } from '../metrics.js' +import type { Transform } from 'it-stream-types' +import type { Uint8ArrayList } from 'uint8arraylist' // Returns generator that encrypts payload from the user export function encryptStream (handshake: IHandshake, metrics?: MetricsRegistry): Transform> { diff --git a/src/encoder.ts b/src/encoder.ts index 30c87d7..f1f963f 100644 --- a/src/encoder.ts +++ b/src/encoder.ts @@ -1,8 +1,8 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat' -import type { Uint8ArrayList } from 'uint8arraylist' import type { bytes } from './@types/basic.js' import type { MessageBuffer } from './@types/handshake.js' import type { LengthDecoderFunction } from 'it-length-prefixed' +import type { Uint8ArrayList } from 'uint8arraylist' const allocUnsafe = (len: number): Uint8Array => { if (globalThis.Buffer) { diff --git a/src/handshake-xx.ts b/src/handshake-xx.ts index 9f1632e..2c74415 100644 --- a/src/handshake-xx.ts +++ b/src/handshake-xx.ts @@ -1,11 +1,4 @@ -import type { PeerId } from '@libp2p/interface-peer-id' import { InvalidCryptoExchangeError, UnexpectedPeerError } from '@libp2p/interface-connection-encrypter/errors' -import type { ProtobufStream } from 'it-pb-stream' -import type { bytes, bytes32 } from './@types/basic.js' -import type { CipherState, NoiseSession } from './@types/handshake.js' -import type { KeyPair } from './@types/libp2p.js' -import type { IHandshake } from './@types/handshake-interface.js' -import type { ICryptoInterface } from './crypto.js' import { decode0, decode1, decode2, encode0, encode1, encode2 } from './encoder.js' import { XX } from './handshakes/xx.js' import { @@ -21,7 +14,14 @@ import { getPeerIdFromPayload, verifySignedPayload } from './utils.js' +import type { bytes, bytes32 } from './@types/basic.js' +import type { IHandshake } from './@types/handshake-interface.js' +import type { CipherState, NoiseSession } from './@types/handshake.js' +import type { KeyPair } from './@types/libp2p.js' +import type { ICryptoInterface } from './crypto.js' import type { NoiseExtensions } from './proto/payload.js' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { ProtobufStream } from 'it-pb-stream' export class XXHandshake implements IHandshake { public isInitiator: boolean diff --git a/src/handshakes/abstract-handshake.ts b/src/handshakes/abstract-handshake.ts index af3927f..714f814 100644 --- a/src/handshakes/abstract-handshake.ts +++ b/src/handshakes/abstract-handshake.ts @@ -1,11 +1,11 @@ -import { equals as uint8ArrayEquals } from 'uint8arrays/equals' -import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { fromString as uint8ArrayFromString } from 'uint8arrays' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import { equals as uint8ArrayEquals } from 'uint8arrays/equals' +import { logger } from '../logger.js' +import { Nonce } from '../nonce.js' import type { bytes, bytes32 } from '../@types/basic.js' import type { CipherState, MessageBuffer, SymmetricState } from '../@types/handshake.js' import type { ICryptoInterface } from '../crypto.js' -import { logger } from '../logger.js' -import { Nonce } from '../nonce.js' export interface DecryptedResult { plaintext: bytes diff --git a/src/handshakes/xx.ts b/src/handshakes/xx.ts index 3138a13..44d26fa 100644 --- a/src/handshakes/xx.ts +++ b/src/handshakes/xx.ts @@ -1,8 +1,8 @@ -import type { bytes32, bytes } from '../@types/basic.js' -import type { KeyPair } from '../@types/libp2p.js' import { isValidPublicKey } from '../utils.js' +import { AbstractHandshake, type DecryptedResult } from './abstract-handshake.js' +import type { bytes32, bytes } from '../@types/basic.js' import type { CipherState, HandshakeState, MessageBuffer, NoiseSession } from '../@types/handshake.js' -import { AbstractHandshake, DecryptedResult } from './abstract-handshake.js' +import type { KeyPair } from '../@types/libp2p.js' export class XX extends AbstractHandshake { private initializeInitiator (prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32): HandshakeState { diff --git a/src/index.ts b/src/index.ts index ebb27b2..8c15571 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ -import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter' import { Noise } from './noise.js' import type { NoiseInit } from './noise.js' import type { NoiseExtensions } from './proto/payload.js' +import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter' export type { ICryptoInterface } from './crypto.js' export { pureJsCrypto } from './crypto/js.js' diff --git a/src/logger.ts b/src/logger.ts index 4cac511..b44ca7b 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,8 +1,8 @@ -import { Logger, logger } from '@libp2p/logger' +import { type Logger, logger } from '@libp2p/logger' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' +import { DUMP_SESSION_KEYS } from './constants.js' import type { NoiseSession } from './@types/handshake.js' import type { KeyPair } from './@types/libp2p.js' -import { DUMP_SESSION_KEYS } from './constants.js' const log = logger('libp2p:noise') diff --git a/src/noise.ts b/src/noise.ts index 8a2c576..72f70f8 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -1,23 +1,23 @@ -import type { PeerId } from '@libp2p/interface-peer-id' -import type { SecuredConnection } from '@libp2p/interface-connection-encrypter' -import { pbStream, ProtobufStream } from 'it-pb-stream' +import { decode } from 'it-length-prefixed' import { duplexPair } from 'it-pair/duplex' +import { pbStream, type ProtobufStream } from 'it-pb-stream' import { pipe } from 'it-pipe' -import { decode } from 'it-length-prefixed' -import type { Duplex, Source } from 'it-stream-types' -import type { bytes } from './@types/basic.js' -import type { IHandshake } from './@types/handshake-interface.js' -import type { INoiseConnection, KeyPair } from './@types/libp2p.js' import { NOISE_MSG_MAX_LENGTH_BYTES } from './constants.js' -import type { ICryptoInterface } from './crypto.js' import { pureJsCrypto } from './crypto/js.js' import { decryptStream, encryptStream } from './crypto/streaming.js' import { uint16BEDecode, uint16BEEncode } from './encoder.js' import { XXHandshake } from './handshake-xx.js' +import { type MetricsRegistry, registerMetrics } from './metrics.js' import { getPayload } from './utils.js' +import type { bytes } from './@types/basic.js' +import type { IHandshake } from './@types/handshake-interface.js' +import type { INoiseConnection, KeyPair } from './@types/libp2p.js' +import type { ICryptoInterface } from './crypto.js' import type { NoiseExtensions } from './proto/payload.js' +import type { SecuredConnection } from '@libp2p/interface-connection-encrypter' import type { Metrics } from '@libp2p/interface-metrics' -import { MetricsRegistry, registerMetrics } from './metrics.js' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { Duplex, Source } from 'it-stream-types' interface HandshakeParams { connection: ProtobufStream @@ -136,7 +136,7 @@ export class Noise implements INoiseConnection { const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.extensions) // run XX handshake - return await this.performXXHandshake(params, payload) + return this.performXXHandshake(params, payload) } private async performXXHandshake ( diff --git a/src/utils.ts b/src/utils.ts index b4e09ba..50e7ce4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,10 +1,10 @@ import { unmarshalPublicKey, unmarshalPrivateKey } from '@libp2p/crypto/keys' -import type { PeerId } from '@libp2p/interface-peer-id' import { peerIdFromKeys } from '@libp2p/peer-id' import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { type NoiseExtensions, NoiseHandshakePayload } from './proto/payload.js' import type { bytes } from './@types/basic.js' -import { NoiseExtensions, NoiseHandshakePayload } from './proto/payload.js' +import type { PeerId } from '@libp2p/interface-peer-id' export async function getPayload ( localPeer: PeerId, @@ -43,11 +43,11 @@ export async function signPayload (peerId: PeerId, payload: bytes): Promise { - return await peerIdFromKeys(payload.identityKey) + return peerIdFromKeys(payload.identityKey) } export function decodePayload (payload: bytes | Uint8Array): NoiseHandshakePayload { diff --git a/test/fixtures/peer.ts b/test/fixtures/peer.ts index 38d1ff8..07550e2 100644 --- a/test/fixtures/peer.ts +++ b/test/fixtures/peer.ts @@ -1,5 +1,5 @@ -import type { PeerId } from '@libp2p/interface-peer-id' import { createEd25519PeerId, createFromJSON } from '@libp2p/peer-id-factory' +import type { PeerId } from '@libp2p/interface-peer-id' // ed25519 keys const peers = [{ @@ -21,8 +21,8 @@ const peers = [{ }] export async function createPeerIdsFromFixtures (length: number): Promise { - return await Promise.all( - Array.from({ length }).map(async (_, i) => await createFromJSON(peers[i])) + return Promise.all( + Array.from({ length }).map(async (_, i) => createFromJSON(peers[i])) ) } diff --git a/test/handshakes/xx.spec.ts b/test/handshakes/xx.spec.ts index 4918d62..69e2733 100644 --- a/test/handshakes/xx.spec.ts +++ b/test/handshakes/xx.spec.ts @@ -1,13 +1,13 @@ import { Buffer } from 'buffer' import { expect, assert } from 'aegir/chai' -import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { equals as uint8ArrayEquals } from 'uint8arrays/equals' -import type { KeyPair } from '../../src/@types/libp2p.js' -import type { NoiseSession } from '../../src/@types/handshake.js' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { pureJsCrypto } from '../../src/crypto/js.js' import { XX } from '../../src/handshakes/xx.js' import { createHandshakePayload, getHandshakePayload } from '../../src/utils.js' import { generateEd25519Keys } from '../utils.js' +import type { NoiseSession } from '../../src/@types/handshake.js' +import type { KeyPair } from '../../src/@types/libp2p.js' describe('XX Handshake', () => { const prologue = Buffer.alloc(0) diff --git a/test/index.spec.ts b/test/index.spec.ts index 84af2f9..255b81d 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,12 +1,12 @@ -import type { Metrics } from '@libp2p/interface-metrics' import { expect } from 'aegir/chai' import { duplexPair } from 'it-pair/duplex' import { pbStream } from 'it-pb-stream' import sinon from 'sinon' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { noise } from '../src/index.js' import { Noise } from '../src/noise.js' import { createPeerIdsFromFixtures } from './fixtures/peer.js' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import type { Metrics } from '@libp2p/interface-metrics' function createCounterSpy (): ReturnType { return sinon.spy({ diff --git a/test/interop.ts b/test/interop.ts index 14257e4..e664115 100644 --- a/test/interop.ts +++ b/test/interop.ts @@ -1,20 +1,20 @@ -import { connectInteropTests } from '@libp2p/interop' -import type { SpawnOptions, Daemon, DaemonFactory } from '@libp2p/interop' -import { createServer } from '@libp2p/daemon-server' +import fs from 'fs' +import { yamux } from '@chainsafe/libp2p-yamux' +import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import { createClient } from '@libp2p/daemon-client' -import { createLibp2p, Libp2pOptions } from 'libp2p' +import { createServer } from '@libp2p/daemon-server' +import { connectInteropTests } from '@libp2p/interop' +import { logger } from '@libp2p/logger' +import { peerIdFromKeys } from '@libp2p/peer-id' import { tcp } from '@libp2p/tcp' import { multiaddr } from '@multiformats/multiaddr' -import { path as p2pd } from 'go-libp2p' import { execa } from 'execa' +import { path as p2pd } from 'go-libp2p' +import { createLibp2p, type Libp2pOptions } from 'libp2p' import pDefer from 'p-defer' -import { logger } from '@libp2p/logger' -import { yamux } from '@chainsafe/libp2p-yamux' -import fs from 'fs' -import { unmarshalPrivateKey } from '@libp2p/crypto/keys' -import type { PeerId } from '@libp2p/interface-peer-id' -import { peerIdFromKeys } from '@libp2p/peer-id' import { noise } from '../src/index.js' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { SpawnOptions, Daemon, DaemonFactory } from '@libp2p/interop' async function createGoPeer (options: SpawnOptions): Promise { const controlPort = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000 @@ -98,10 +98,10 @@ async function main (): Promise { const factory: DaemonFactory = { async spawn (options: SpawnOptions) { if (options.type === 'go') { - return await createGoPeer(options) + return createGoPeer(options) } - return await createJsPeer(options) + return createJsPeer(options) } } diff --git a/test/noise.spec.ts b/test/noise.spec.ts index c3cc608..3311b2d 100644 --- a/test/noise.spec.ts +++ b/test/noise.spec.ts @@ -1,22 +1,22 @@ -import { randomBytes } from 'iso-random-stream' -import type { PeerId } from '@libp2p/interface-peer-id' import { Buffer } from 'buffer' import { assert, expect } from 'aegir/chai' +import { randomBytes } from 'iso-random-stream' import { duplexPair } from 'it-pair/duplex' import { pbStream } from 'it-pb-stream' +import sinon from 'sinon' import { equals as uint8ArrayEquals } from 'uint8arrays/equals' -import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import sinon from 'sinon' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { NOISE_MSG_MAX_LENGTH_BYTES } from '../src/constants.js' import { pureJsCrypto } from '../src/crypto/js.js' import { decode0, decode2, encode1, uint16BEDecode, uint16BEEncode } from '../src/encoder.js' -import { XX } from '../src/handshakes/xx.js' import { XXHandshake } from '../src/handshake-xx.js' +import { XX } from '../src/handshakes/xx.js' import { Noise } from '../src/noise.js' import { createHandshakePayload, getHandshakePayload, getPayload, signPayload } from '../src/utils.js' import { createPeerIdsFromFixtures } from './fixtures/peer.js' import { getKeyPairFromPeerId } from './utils.js' +import type { PeerId } from '@libp2p/interface-peer-id' describe('Noise', () => { let remotePeer: PeerId, localPeer: PeerId diff --git a/test/utils.ts b/test/utils.ts index a03a5b5..e36e337 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,10 +1,10 @@ import { keys } from '@libp2p/crypto' +import type { KeyPair } from '../src/@types/libp2p.js' import type { PrivateKey } from '@libp2p/interface-keys' import type { PeerId } from '@libp2p/interface-peer-id' -import type { KeyPair } from '../src/@types/libp2p.js' export async function generateEd25519Keys (): Promise { - return await keys.generateKeyPair('Ed25519', 32) + return keys.generateKeyPair('Ed25519', 32) } export function getKeyPairFromPeerId (peerId: PeerId): KeyPair { diff --git a/test/xx-handshake.spec.ts b/test/xx-handshake.spec.ts index af4f4d0..7e03900 100644 --- a/test/xx-handshake.spec.ts +++ b/test/xx-handshake.spec.ts @@ -1,4 +1,3 @@ -import type { PeerId } from '@libp2p/interface-peer-id' import { Buffer } from 'buffer' import { assert, expect } from 'aegir/chai' import { duplexPair } from 'it-pair/duplex' @@ -8,6 +7,7 @@ import { pureJsCrypto } from '../src/crypto/js.js' import { XXHandshake } from '../src/handshake-xx.js' import { getPayload } from '../src/utils.js' import { createPeerIdsFromFixtures } from './fixtures/peer.js' +import type { PeerId } from '@libp2p/interface-peer-id' describe('XX Handshake', () => { let peerA: PeerId, peerB: PeerId, fakePeer: PeerId