Skip to content

Commit 732e468

Browse files
authored
Merge pull request #134 from achingbrain/feat/update-to-new-interfaces-and-esm
feat: update to new libp2p interfaces
2 parents 790b039 + c04e40d commit 732e468

39 files changed

+3360
-1534
lines changed

.aegir.js renamed to .aegir.cjs

-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
const path = require('path')
21

32
/** @type {import('aegir').PartialOptions} */
43
const config = {
5-
tsRepo: true,
64
docs: {
75
entryPoint: "src/index.ts"
8-
},
9-
build: {
10-
bundlesizeMax: '231KB'
116
}
127
}
138

.github/workflows/ci.yml

+11-13
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ jobs:
3030
- run: yarn --prefer-offline --frozen-lockfile
3131
- run: yarn run build
3232
- run: yarn run check
33-
- uses: ipfs/aegir/actions/bundle-size@master
34-
name: Check bundle size
35-
with:
36-
github_token: ${{ secrets.GITHUB_TOKEN }}
3733

3834
test-node:
3935
name: Test Nodejs
@@ -59,7 +55,8 @@ jobs:
5955
restore-keys: |
6056
${{ runner.os }}-yarn-
6157
- run: yarn --prefer-offline --frozen-lockfile
62-
- run: npx aegir test -t node --bail --cov
58+
- run: yarn run build
59+
- run: npm run test:node -- --bail --cov
6360
- uses: codecov/codecov-action@v1
6461

6562
test-chrome:
@@ -82,7 +79,8 @@ jobs:
8279
restore-keys: |
8380
${{ runner.os }}-yarn-
8481
- run: yarn --prefer-offline --frozen-lockfile
85-
- run: npx aegir test -t browser -t webworker --bail
82+
- run: yarn run build
83+
- run: npm run test:browser -- --bail
8684

8785
test-firefox:
8886
name: Test Firefox
@@ -104,7 +102,8 @@ jobs:
104102
restore-keys: |
105103
${{ runner.os }}-yarn-
106104
- run: yarn --prefer-offline --frozen-lockfile
107-
- run: npx aegir test -t browser -t webworker --bail -- --browser firefox
105+
- run: yarn run build
106+
- run: npm run test:browser -- --bail -- --browser firefox
108107

109108
test-webkit:
110109
name: Test Webkit
@@ -127,7 +126,8 @@ jobs:
127126
restore-keys: |
128127
${{ runner.os }}-yarn-
129128
- run: yarn --prefer-offline --frozen-lockfile
130-
- run: npx aegir test -t browser -t webworker --bail -- --browser webkit
129+
- run: yarn run build
130+
- run: npm run test:browser -- --bail -- --browser webkit
131131

132132
test-electron-main:
133133
name: Test Electron Main
@@ -149,7 +149,8 @@ jobs:
149149
restore-keys: |
150150
${{ runner.os }}-yarn-
151151
- run: yarn --prefer-offline --frozen-lockfile
152-
- run: npx xvfb-maybe aegir test -t electron-main --bail
152+
- run: yarn run build
153+
- run: npx xvfb-maybe npm run test:electron-main -- --bail
153154

154155
test-electron-renderer:
155156
name: Test Electron Renderer
@@ -170,7 +171,4 @@ jobs:
170171
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
171172
restore-keys: |
172173
${{ runner.os }}-yarn-
173-
- run: yarn --prefer-offline --frozen-lockfile
174-
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
175-
env:
176-
ESBUILD_WORKER_THREADS: 0
174+
- run: yarn --prefer-offline --frozen-lockfile

benchmarks/benchmark.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/* eslint-disable */
22

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

88
const bench = async function () {
99
console.log('Initializing handshake benchmark')
1010
const initiator = new Noise()
11-
const initiatorPeer = await PeerId.createFromJSON({
11+
const initiatorPeer = await createFromJSON({
1212
id: '12D3KooWH45PiqBjfnEfDfCD6TqJrpqTBJvQDwGHvjGpaWwms46D',
1313
privKey: 'CAESYBtKXrMwawAARmLScynQUuSwi/gGSkwqDPxi15N3dqDHa4T4iWupkMe5oYGwGH3Hyfvd/QcgSTqg71oYZJadJ6prhPiJa6mQx7mhgbAYfcfJ+939ByBJOqDvWhhklp0nqg==',
1414
pubKey: 'CAESIGuE+IlrqZDHuaGBsBh9x8n73f0HIEk6oO9aGGSWnSeq'
1515
})
1616
const responder = new Noise()
17-
const responderPeer = await PeerId.createFromJSON({
17+
const responderPeer = await createFromJSON({
1818
id: '12D3KooWP63uzL78BRMpkQ7augMdNi1h3VBrVWZucKjyhzGVaSi1',
1919
privKey: 'CAESYPxO3SHyfc2578hDmfkGGBY255JjiLuVavJWy+9ivlpsxSyVKf36ipyRGL6szGzHuFs5ceEuuGVrPMg/rW2Ch1bFLJUp/fqKnJEYvqzMbMe4Wzlx4S64ZWs8yD+tbYKHVg==',
2020
pubKey: 'CAESIMUslSn9+oqckRi+rMxsx7hbOXHhLrhlazzIP61tgodW'
@@ -23,7 +23,7 @@ const bench = async function () {
2323
const bench = new benchmark('handshake', {
2424
defer: true,
2525
fn: async function (deffered) {
26-
const [inboundConnection, outboundConnection] = DuplexPair()
26+
const [inboundConnection, outboundConnection] = duplexPair()
2727
await Promise.all([
2828
initiator.secureOutbound(initiatorPeer, outboundConnection, responderPeer),
2929
responder.secureInbound(responderPeer, inboundConnection, initiatorPeer)

package.json

+32-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
22
"name": "@chainsafe/libp2p-noise",
33
"version": "5.0.3",
4-
"main": "dist/src/index.js",
4+
"type": "module",
55
"types": "dist/src/index.d.ts",
6+
"exports": {
7+
".": {
8+
"import": "./dist/src/index.js"
9+
}
10+
},
611
"files": [
712
"dist",
813
"src"
@@ -22,49 +27,49 @@
2227
"bench": "node benchmarks/benchmark.js",
2328
"clean": "rm -rf dist",
2429
"check": "aegir dep-check && aegir ts -p check",
25-
"build": "aegir build",
30+
"build": "tsc",
31+
"postbuild": "cp -R src/proto dist/src",
2632
"lint": "aegir lint",
2733
"lint:fix": "aegir lint --fix",
2834
"pretest": "yarn run check",
29-
"test": "aegir test",
30-
"test:node": "aegir test -t node",
31-
"test:browser": "aegir test -t browser",
35+
"test": "aegir test -f \"./dist/test/**/*.spec.js\"",
36+
"test:node": "npm run test -- -t node",
37+
"test:browser": "npm run test -- -t browser -t webworker",
38+
"test:electron-main": "npm run test -- -t electron-main",
3239
"docs": "aegir docs",
33-
"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"
40+
"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"
3441
},
3542
"browser": {
3643
"./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js",
3744
"util": false
3845
},
39-
"devDependencies": {
40-
"@types/bl": "^5.0.2",
41-
"aegir": "^36.1.1",
42-
"benchmark": "^2.1.4",
43-
"buffer": "^6.0.3",
44-
"chai": "^4.3.4",
45-
"events": "^3.3.0",
46-
"libp2p-crypto": "^0.21.0",
47-
"microtime": "^3.0.0",
48-
"mocha": "^9.0.2",
49-
"sinon": "^12.0.1",
50-
"util": "^0.12.4"
51-
},
5246
"dependencies": {
47+
"@libp2p/interfaces": "^1.3.14",
48+
"@libp2p/logger": "^1.1.2",
49+
"@libp2p/peer-collections": "^1.0.0",
50+
"@libp2p/peer-id": "^1.1.8",
5351
"@stablelib/chacha20poly1305": "^1.0.1",
5452
"@stablelib/hkdf": "^1.0.1",
5553
"@stablelib/sha256": "^1.0.1",
5654
"@stablelib/x25519": "^1.0.1",
57-
"bl": "^5.0.0",
58-
"debug": "^4.3.1",
59-
"it-buffer": "^0.1.3",
60-
"it-length-prefixed": "^5.0.3",
61-
"it-pair": "^1.0.0",
62-
"it-pb-rpc": "^0.2.0",
63-
"it-pipe": "^1.1.0",
64-
"peer-id": "^0.16.0",
55+
"it-length-prefixed": "^7.0.1",
56+
"it-pair": "^2.0.2",
57+
"it-pb-stream": "^1.0.2",
58+
"it-pipe": "^2.0.3",
6559
"protobufjs": "^6.11.2",
60+
"uint8arraylist": "^1.4.0",
6661
"uint8arrays": "^3.0.0"
6762
},
63+
"devDependencies": {
64+
"@libp2p/crypto": "^0.22.9",
65+
"@libp2p/peer-id-factory": "^1.0.8",
66+
"aegir": "^36.1.3",
67+
"benchmark": "^2.1.4",
68+
"events": "^3.3.0",
69+
"microtime": "^3.0.0",
70+
"sinon": "^13.0.1",
71+
"util": "^0.12.4"
72+
},
6873
"resolutions": {
6974
"bn.js": "4.4.0"
7075
},
File renamed without changes.

src/@types/handshake-interface.d.ts renamed to src/@types/handshake-interface.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { bytes } from './basic'
2-
import { NoiseSession } from './handshake'
3-
import PeerId from 'peer-id'
1+
import type { bytes } from './basic.js'
2+
import type { NoiseSession } from './handshake.js'
3+
import type { PeerId } from '@libp2p/interfaces/peer-id'
44

55
export interface IHandshake {
66
session: NoiseSession

src/@types/handshake.d.ts renamed to src/@types/handshake.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { bytes, bytes32, uint64 } from './basic'
2-
import { KeyPair } from './libp2p'
1+
import type { bytes, bytes32, uint64 } from './basic.js'
2+
import type { KeyPair } from './libp2p.js'
33

44
export type Hkdf = [bytes, bytes, bytes]
55

src/@types/it-length-prefixed/index.d.ts

-37
This file was deleted.

src/@types/it-pair/index.d.ts

-8
This file was deleted.

src/@types/libp2p.d.ts

-19
This file was deleted.

src/@types/libp2p.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { bytes, bytes32 } from './basic.js'
2+
import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
3+
4+
export interface KeyPair {
5+
publicKey: bytes32
6+
privateKey: bytes32
7+
}
8+
9+
export interface INoiseConnection extends ConnectionEncrypter {
10+
remoteEarlyData?: () => bytes
11+
}

src/crypto.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { IHandshake } from './@types/handshake-interface'
2-
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from './constants'
3-
4-
interface IReturnEncryptionWrapper {
5-
(source: Iterable<Uint8Array>): AsyncIterableIterator<Uint8Array>
6-
}
1+
import type { Transform } from 'it-stream-types'
2+
import type { IHandshake } from './@types/handshake-interface.js'
3+
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from './constants.js'
74

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

2522
// Decrypt received payload to the user
26-
export function decryptStream (handshake: IHandshake): IReturnEncryptionWrapper {
23+
export function decryptStream (handshake: IHandshake): Transform<Uint8Array> {
2724
return async function * (source) {
2825
for await (const chunk of source) {
2926
for (let i = 0; i < chunk.length; i += NOISE_MSG_MAX_LENGTH_BYTES) {

src/encoder.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { bytes } from './@types/basic'
2-
import { MessageBuffer } from './@types/handshake'
3-
import BufferList from 'bl/BufferList'
1+
import type { bytes } from './@types/basic.js'
2+
import type { MessageBuffer } from './@types/handshake.js'
3+
import type { Uint8ArrayList } from 'uint8arraylist'
44
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
55

66
const allocUnsafe = (len: number): Uint8Array => {
@@ -11,21 +11,21 @@ const allocUnsafe = (len: number): Uint8Array => {
1111
return new Uint8Array(len)
1212
}
1313

14-
export const uint16BEEncode = (value: number, target: Uint8Array, offset: number): Uint8Array => {
15-
target = target || allocUnsafe(2)
16-
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(offset, value, false)
14+
export const uint16BEEncode = (value: number, target?: Uint8Array, offset?: number): Uint8Array => {
15+
target = target ?? allocUnsafe(2)
16+
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(offset ?? 0, value, false)
1717
return target
1818
}
1919
uint16BEEncode.bytes = 2
2020

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

24-
if (data instanceof BufferList) {
25-
return data.readUInt16BE(0)
24+
if (data instanceof Uint8Array) {
25+
return new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0, false)
2626
}
2727

28-
return new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0, false)
28+
return data.getUint16(0)
2929
}
3030
uint16BEDecode.bytes = 2
3131

src/errors.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import BufferList from 'bl'
1+
import type { Uint8ArrayList } from 'uint8arraylist'
22

33
export class FailedIKError extends Error {
4-
public initialMsg: string|BufferList|Uint8Array
4+
public initialMsg: string|Uint8ArrayList|Uint8Array
55

6-
constructor (initialMsg: string|BufferList|Uint8Array, message?: string) {
6+
constructor (initialMsg: string|Uint8ArrayList|Uint8Array, message?: string) {
77
super(message)
88

99
this.initialMsg = initialMsg

0 commit comments

Comments
 (0)