Skip to content

feat: update to new libp2p interfaces #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .aegir.js → .aegir.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const path = require('path')

/** @type {import('aegir').PartialOptions} */
const config = {
tsRepo: true,
docs: {
entryPoint: "src/index.ts"
},
build: {
bundlesizeMax: '231KB'
}
}

Expand Down
24 changes: 11 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ jobs:
- run: yarn --prefer-offline --frozen-lockfile
- run: yarn run build
- run: yarn run check
- uses: ipfs/aegir/actions/bundle-size@master
name: Check bundle size
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

test-node:
name: Test Nodejs
Expand All @@ -59,7 +55,8 @@ jobs:
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn --prefer-offline --frozen-lockfile
- run: npx aegir test -t node --bail --cov
- run: yarn run build
- run: npm run test:node -- --bail --cov
- uses: codecov/codecov-action@v1

test-chrome:
Expand All @@ -82,7 +79,8 @@ jobs:
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn --prefer-offline --frozen-lockfile
- run: npx aegir test -t browser -t webworker --bail
- run: yarn run build
- run: npm run test:browser -- --bail

test-firefox:
name: Test Firefox
Expand All @@ -104,7 +102,8 @@ jobs:
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn --prefer-offline --frozen-lockfile
- run: npx aegir test -t browser -t webworker --bail -- --browser firefox
- run: yarn run build
- run: npm run test:browser -- --bail -- --browser firefox

test-webkit:
name: Test Webkit
Expand All @@ -127,7 +126,8 @@ jobs:
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn --prefer-offline --frozen-lockfile
- run: npx aegir test -t browser -t webworker --bail -- --browser webkit
- run: yarn run build
- run: npm run test:browser -- --bail -- --browser webkit

test-electron-main:
name: Test Electron Main
Expand All @@ -149,7 +149,8 @@ jobs:
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn --prefer-offline --frozen-lockfile
- run: npx xvfb-maybe aegir test -t electron-main --bail
- run: yarn run build
- run: npx xvfb-maybe npm run test:electron-main -- --bail

test-electron-renderer:
name: Test Electron Renderer
Expand All @@ -170,7 +171,4 @@ jobs:
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn --prefer-offline --frozen-lockfile
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
env:
ESBUILD_WORKER_THREADS: 0
- run: yarn --prefer-offline --frozen-lockfile
14 changes: 7 additions & 7 deletions benchmarks/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-disable */

const { Noise } = require('../dist/src/index')
const benchmark = require('benchmark')
const DuplexPair = require('it-pair/duplex')
const PeerId = require('peer-id')
import { Noise } from '../dist/src/index.js'
import benchmark from 'benchmark'
import { duplexPair } from 'it-pair/duplex'
import { createFromJSON } from '@libp2p/peer-id-factory'

const bench = async function () {
console.log('Initializing handshake benchmark')
const initiator = new Noise()
const initiatorPeer = await PeerId.createFromJSON({
const initiatorPeer = await createFromJSON({
id: '12D3KooWH45PiqBjfnEfDfCD6TqJrpqTBJvQDwGHvjGpaWwms46D',
privKey: 'CAESYBtKXrMwawAARmLScynQUuSwi/gGSkwqDPxi15N3dqDHa4T4iWupkMe5oYGwGH3Hyfvd/QcgSTqg71oYZJadJ6prhPiJa6mQx7mhgbAYfcfJ+939ByBJOqDvWhhklp0nqg==',
pubKey: 'CAESIGuE+IlrqZDHuaGBsBh9x8n73f0HIEk6oO9aGGSWnSeq'
})
const responder = new Noise()
const responderPeer = await PeerId.createFromJSON({
const responderPeer = await createFromJSON({
id: '12D3KooWP63uzL78BRMpkQ7augMdNi1h3VBrVWZucKjyhzGVaSi1',
privKey: 'CAESYPxO3SHyfc2578hDmfkGGBY255JjiLuVavJWy+9ivlpsxSyVKf36ipyRGL6szGzHuFs5ceEuuGVrPMg/rW2Ch1bFLJUp/fqKnJEYvqzMbMe4Wzlx4S64ZWs8yD+tbYKHVg==',
pubKey: 'CAESIMUslSn9+oqckRi+rMxsx7hbOXHhLrhlazzIP61tgodW'
Expand All @@ -23,7 +23,7 @@ const bench = async function () {
const bench = new benchmark('handshake', {
defer: true,
fn: async function (deffered) {
const [inboundConnection, outboundConnection] = DuplexPair()
const [inboundConnection, outboundConnection] = duplexPair()
await Promise.all([
initiator.secureOutbound(initiatorPeer, outboundConnection, responderPeer),
responder.secureInbound(responderPeer, inboundConnection, initiatorPeer)
Expand Down
59 changes: 32 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"name": "@chainsafe/libp2p-noise",
"version": "5.0.3",
"main": "dist/src/index.js",
"type": "module",
"types": "dist/src/index.d.ts",
"exports": {
".": {
"import": "./dist/src/index.js"
}
},
"files": [
"dist",
"src"
Expand All @@ -22,49 +27,49 @@
"bench": "node benchmarks/benchmark.js",
"clean": "rm -rf dist",
"check": "aegir dep-check && aegir ts -p check",
"build": "aegir build",
"build": "tsc",
"postbuild": "cp -R src/proto dist/src",
"lint": "aegir lint",
"lint:fix": "aegir lint --fix",
"pretest": "yarn run check",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"test": "aegir test -f \"./dist/test/**/*.spec.js\"",
"test:node": "npm run test -- -t node",
"test:browser": "npm run test -- -t browser -t webworker",
"test:electron-main": "npm run test -- -t electron-main",
"docs": "aegir docs",
"proto:gen": "pbjs -t static-module -r libp2p-noise -o ./src/proto/payload.js ./src/proto/payload.proto && pbts -o ./src/proto/payload.d.ts ./src/proto/payload.js && yarn run lint --fix"
"proto:gen": "pbjs -t static-module -w es6 -r libp2p-noise -o ./src/proto/payload.js ./src/proto/payload.proto && pbts -o ./src/proto/payload.d.ts ./src/proto/payload.js"
},
"browser": {
"./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js",
"util": false
},
"devDependencies": {
"@types/bl": "^5.0.2",
"aegir": "^36.1.1",
"benchmark": "^2.1.4",
"buffer": "^6.0.3",
"chai": "^4.3.4",
"events": "^3.3.0",
"libp2p-crypto": "^0.21.0",
"microtime": "^3.0.0",
"mocha": "^9.0.2",
"sinon": "^12.0.1",
"util": "^0.12.4"
},
"dependencies": {
"@libp2p/interfaces": "^1.3.14",
"@libp2p/logger": "^1.1.2",
"@libp2p/peer-collections": "^1.0.0",
"@libp2p/peer-id": "^1.1.8",
"@stablelib/chacha20poly1305": "^1.0.1",
"@stablelib/hkdf": "^1.0.1",
"@stablelib/sha256": "^1.0.1",
"@stablelib/x25519": "^1.0.1",
"bl": "^5.0.0",
"debug": "^4.3.1",
"it-buffer": "^0.1.3",
"it-length-prefixed": "^5.0.3",
"it-pair": "^1.0.0",
"it-pb-rpc": "^0.2.0",
"it-pipe": "^1.1.0",
"peer-id": "^0.16.0",
"it-length-prefixed": "^7.0.1",
"it-pair": "^2.0.2",
"it-pb-stream": "^1.0.2",
"it-pipe": "^2.0.3",
"protobufjs": "^6.11.2",
"uint8arraylist": "^1.4.0",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@libp2p/crypto": "^0.22.9",
"@libp2p/peer-id-factory": "^1.0.8",
"aegir": "^36.1.3",
"benchmark": "^2.1.4",
"events": "^3.3.0",
"microtime": "^3.0.0",
"sinon": "^13.0.1",
"util": "^0.12.4"
},
"resolutions": {
"bn.js": "4.4.0"
},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bytes } from './basic'
import { NoiseSession } from './handshake'
import PeerId from 'peer-id'
import type { bytes } from './basic.js'
import type { NoiseSession } from './handshake.js'
import type { PeerId } from '@libp2p/interfaces/peer-id'

export interface IHandshake {
session: NoiseSession
Expand Down
4 changes: 2 additions & 2 deletions src/@types/handshake.d.ts → src/@types/handshake.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bytes, bytes32, uint64 } from './basic'
import { KeyPair } from './libp2p'
import type { bytes, bytes32, uint64 } from './basic.js'
import type { KeyPair } from './libp2p.js'

export type Hkdf = [bytes, bytes, bytes]

Expand Down
37 changes: 0 additions & 37 deletions src/@types/it-length-prefixed/index.d.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/@types/it-pair/index.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/@types/libp2p.d.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/@types/libp2p.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { bytes, bytes32 } from './basic.js'
import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'

export interface KeyPair {
publicKey: bytes32
privateKey: bytes32
}

export interface INoiseConnection extends ConnectionEncrypter {
remoteEarlyData?: () => bytes
}
13 changes: 5 additions & 8 deletions src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { IHandshake } from './@types/handshake-interface'
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from './constants'

interface IReturnEncryptionWrapper {
(source: Iterable<Uint8Array>): AsyncIterableIterator<Uint8Array>
}
import type { Transform } from 'it-stream-types'
import type { IHandshake } from './@types/handshake-interface.js'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so weird. Is there a reason why .d.ts is not ok anymore like it should not be compiled to js

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the way I understand it, .d.ts files are meant to be machine generated so they aren't parsed and validated like regular .ts files.

We've certainly shipped a lot of broken types in the past due to hand-rolled .d.ts files - things like import paths being wrong etc don't trigger errors so it's a lot safer this way.

import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from './constants.js'

// Returns generator that encrypts payload from the user
export function encryptStream (handshake: IHandshake): IReturnEncryptionWrapper {
export function encryptStream (handshake: IHandshake): Transform<Uint8Array> {
return async function * (source) {
for await (const chunk of source) {
for (let i = 0; i < chunk.length; i += NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG) {
Expand All @@ -23,7 +20,7 @@ export function encryptStream (handshake: IHandshake): IReturnEncryptionWrapper
}

// Decrypt received payload to the user
export function decryptStream (handshake: IHandshake): IReturnEncryptionWrapper {
export function decryptStream (handshake: IHandshake): Transform<Uint8Array> {
return async function * (source) {
for await (const chunk of source) {
for (let i = 0; i < chunk.length; i += NOISE_MSG_MAX_LENGTH_BYTES) {
Expand Down
20 changes: 10 additions & 10 deletions src/encoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bytes } from './@types/basic'
import { MessageBuffer } from './@types/handshake'
import BufferList from 'bl/BufferList'
import type { bytes } from './@types/basic.js'
import type { MessageBuffer } from './@types/handshake.js'
import type { Uint8ArrayList } from 'uint8arraylist'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'

const allocUnsafe = (len: number): Uint8Array => {
Expand All @@ -11,21 +11,21 @@ const allocUnsafe = (len: number): Uint8Array => {
return new Uint8Array(len)
}

export const uint16BEEncode = (value: number, target: Uint8Array, offset: number): Uint8Array => {
target = target || allocUnsafe(2)
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(offset, value, false)
export const uint16BEEncode = (value: number, target?: Uint8Array, offset?: number): Uint8Array => {
target = target ?? allocUnsafe(2)
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(offset ?? 0, value, false)
return target
}
uint16BEEncode.bytes = 2

export const uint16BEDecode = (data: Uint8Array | BufferList): number => {
export const uint16BEDecode = (data: Uint8Array | Uint8ArrayList): number => {
if (data.length < 2) throw RangeError('Could not decode int16BE')

if (data instanceof BufferList) {
return data.readUInt16BE(0)
if (data instanceof Uint8Array) {
return new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0, false)
}

return new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0, false)
return data.getUint16(0)
}
uint16BEDecode.bytes = 2

Expand Down
6 changes: 3 additions & 3 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import BufferList from 'bl'
import type { Uint8ArrayList } from 'uint8arraylist'

export class FailedIKError extends Error {
public initialMsg: string|BufferList|Uint8Array
public initialMsg: string|Uint8ArrayList|Uint8Array

constructor (initialMsg: string|BufferList|Uint8Array, message?: string) {
constructor (initialMsg: string|Uint8ArrayList|Uint8Array, message?: string) {
super(message)

this.initialMsg = initialMsg
Expand Down
Loading