@libp2p/pnet 1.0.0-97ab31c0c → 1.0.0-984f13e42

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,9 @@
1
1
  import type { Source } from 'it-stream-types';
2
+ import type { Uint8ArrayList } from 'uint8arraylist';
2
3
  /**
3
4
  * Creates a stream iterable to encrypt messages in a private network
4
5
  */
5
- export declare function createBoxStream(nonce: Uint8Array, psk: Uint8Array): (source: Source<Uint8Array>) => AsyncIterable<Uint8Array>;
6
+ export declare function createBoxStream(nonce: Uint8Array, psk: Uint8Array): (source: Source<Uint8Array | Uint8ArrayList>) => AsyncGenerator<Uint8Array | Uint8ArrayList>;
6
7
  /**
7
8
  * Creates a stream iterable to decrypt messages in a private network
8
9
  */
@@ -1 +1 @@
1
- {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE7C;;GAEG;AACH,wBAAgB,eAAe,CAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,KAAK,aAAa,CAAC,UAAU,CAAC,CAQ9H;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,YACnD,OAAO,UAAU,CAAC,+CAOnC;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAE,SAAS,EAAE,UAAU,GAAG;IAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,GAAG,EAAE,UAAU,CAAA;CAAE,CAyB/H"}
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD;;GAEG;AACH,wBAAgB,eAAe,CAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,GAAG,cAAc,CAAC,KAAK,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAQjK;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,YACnD,OAAO,UAAU,CAAC,+CAOnC;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAE,SAAS,EAAE,UAAU,GAAG;IAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,GAAG,EAAE,UAAU,CAAA;CAAE,CAyB/H"}
@@ -10,7 +10,7 @@ export function createBoxStream(nonce, psk) {
10
10
  const xor = xsalsa20(nonce, psk);
11
11
  return (source) => (async function* () {
12
12
  for await (const chunk of source) {
13
- yield Uint8Array.from(xor.update(chunk.slice()));
13
+ yield Uint8Array.from(xor.update(chunk.subarray()));
14
14
  }
15
15
  })();
16
16
  }
@@ -21,7 +21,7 @@ export function createUnboxStream(nonce, psk) {
21
21
  return (source) => (async function* () {
22
22
  const xor = xsalsa20(nonce, psk);
23
23
  for await (const chunk of source) {
24
- yield Uint8Array.from(xor.update(chunk.slice()));
24
+ yield Uint8Array.from(xor.update(chunk.subarray()));
25
25
  }
26
26
  })();
27
27
  }
@@ -1 +1 @@
1
- {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,QAAQ,MAAM,UAAU,CAAA;AAC/B,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,EA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interface';
59
- import type { ConnectionProtector } from '@libp2p/interface/connection';
58
+ import type { ComponentLogger, ConnectionProtector } from '@libp2p/interface';
60
59
  export { generateKey } from './key-generator.js';
61
60
  export interface ProtectorInit {
62
- enabled?: boolean;
61
+ /**
62
+ * A pre-shared key. This must be the same byte value for all peers in the
63
+ * swarm in order for them to communicate.
64
+ */
63
65
  psk: Uint8Array;
66
+ /**
67
+ * The initial nonce exchange must complete within this many milliseconds
68
+ * (default: 1000)
69
+ */
70
+ timeout?: number;
64
71
  }
65
72
  export interface ProtectorComponents {
66
73
  logger: ComponentLogger;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAeH,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,mBAAmB,CAAA;AAChE,OAAO,KAAK,EAAE,mBAAmB,EAAuB,MAAM,8BAA8B,CAAA;AAE5F,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,GAAG,EAAE,UAAU,CAAA;CAChB;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,eAAe,CAAA;CACxB;AA6ED,wBAAgB,YAAY,CAAE,IAAI,EAAE,aAAa,GAAG,CAAC,UAAU,EAAE,mBAAmB,KAAK,mBAAmB,CAE3G"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAeH,OAAO,KAAK,EAAE,eAAe,EAAU,mBAAmB,EAAuB,MAAM,mBAAmB,CAAA;AAG1G,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,GAAG,EAAE,UAAU,CAAA;IACf;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,eAAe,CAAA;CACxB;AAyED,wBAAgB,YAAY,CAAE,IAAI,EAAE,aAAa,GAAG,CAAC,UAAU,EAAE,mBAAmB,KAAK,mBAAmB,CAE3G"}
package/dist/src/index.js CHANGED
@@ -56,8 +56,8 @@
56
56
  * ```
57
57
  */
58
58
  import { randomBytes } from '@libp2p/crypto';
59
- import { CodeError } from '@libp2p/interface/errors';
60
- import { handshake } from 'it-handshake';
59
+ import { CodeError } from '@libp2p/interface';
60
+ import { byteStream } from 'it-byte-stream';
61
61
  import map from 'it-map';
62
62
  import { duplexPair } from 'it-pair/duplex';
63
63
  import { pipe } from 'it-pipe';
@@ -69,23 +69,17 @@ class PreSharedKeyConnectionProtector {
69
69
  tag;
70
70
  log;
71
71
  psk;
72
- enabled;
72
+ timeout;
73
73
  /**
74
74
  * Takes a Private Shared Key (psk) and provides a `protect` method
75
75
  * for wrapping existing connections in a private encryption stream.
76
76
  */
77
77
  constructor(components, init) {
78
78
  this.log = components.logger.forComponent('libp2p:pnet');
79
- this.enabled = init.enabled !== false;
80
- if (this.enabled) {
81
- const decodedPSK = decodeV1PSK(init.psk);
82
- this.psk = decodedPSK.psk;
83
- this.tag = decodedPSK.tag ?? '';
84
- }
85
- else {
86
- this.psk = new Uint8Array();
87
- this.tag = '';
88
- }
79
+ this.timeout = init.timeout ?? 1000;
80
+ const decodedPSK = decodeV1PSK(init.psk);
81
+ this.psk = decodedPSK.psk;
82
+ this.tag = decodedPSK.tag ?? '';
89
83
  }
90
84
  /**
91
85
  * Takes a given Connection and creates a private encryption stream
@@ -93,29 +87,29 @@ class PreSharedKeyConnectionProtector {
93
87
  * created with.
94
88
  */
95
89
  async protect(connection) {
96
- if (!this.enabled) {
97
- return connection;
98
- }
99
90
  if (connection == null) {
100
91
  throw new CodeError(Errors.NO_HANDSHAKE_CONNECTION, Errors.ERR_INVALID_PARAMETERS);
101
92
  }
102
93
  // Exchange nonces
103
94
  this.log('protecting the connection');
104
95
  const localNonce = randomBytes(NONCE_LENGTH);
105
- const shake = handshake(connection);
106
- shake.write(localNonce);
107
- const result = await shake.reader.next(NONCE_LENGTH);
108
- if (result.value == null) {
109
- throw new CodeError(Errors.STREAM_ENDED, Errors.ERR_INVALID_PARAMETERS);
110
- }
111
- const remoteNonce = result.value.slice();
112
- shake.rest();
96
+ const signal = AbortSignal.timeout(this.timeout);
97
+ const bytes = byteStream(connection);
98
+ const [, result] = await Promise.all([
99
+ bytes.write(localNonce, {
100
+ signal
101
+ }),
102
+ bytes.read(NONCE_LENGTH, {
103
+ signal
104
+ })
105
+ ]);
106
+ const remoteNonce = result.subarray();
113
107
  // Create the boxing/unboxing pipe
114
108
  this.log('exchanged nonces');
115
109
  const [internal, external] = duplexPair();
116
110
  pipe(external,
117
111
  // Encrypt all outbound traffic
118
- createBoxStream(localNonce, this.psk), shake.stream, (source) => map(source, (buf) => buf.subarray()),
112
+ createBoxStream(localNonce, this.psk), bytes.unwrap(), (source) => map(source, (buf) => buf.subarray()),
119
113
  // Decrypt all inbound traffic
120
114
  createUnboxStream(remoteNonce, this.psk), external).catch(this.log.error);
121
115
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AA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ACF;AAED,MAAM,UAAU,YAAY,CAAE,IAAmB;IAC/C,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,+BAA+B,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AAC9E,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"key-generator.js","sourceRoot":"","sources":["../../src/key-generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAEtE;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAE,KAAsC;IACjE,MAAM,GAAG,GAAG,kBAAkB,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,CAAA;IACjE,MAAM,GAAG,GAAG,oBAAoB,CAAC,mCAAmC,GAAG,GAAG,CAAC,CAAA;IAE3E,IAAI,KAAK,YAAY,UAAU,EAAE;QAC/B,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;KACf;SAAM;QACL,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;KACjB;AACH,CAAC;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAA;AAC9B,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,CAAA"}
1
+ {"version":3,"file":"key-generator.js","sourceRoot":"","sources":["../../src/key-generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAEtE;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAE,KAAsC;IACjE,MAAM,GAAG,GAAG,kBAAkB,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,CAAA;IACjE,MAAM,GAAG,GAAG,oBAAoB,CAAC,mCAAmC,GAAG,GAAG,CAAC,CAAA;IAE3E,IAAI,KAAK,YAAY,UAAU,EAAE,CAAC;QAChC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAChB,CAAC;SAAM,CAAC;QACN,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAClB,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAA;AAC9B,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,CAAA"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@libp2p/pnet",
3
- "version": "1.0.0-97ab31c0c",
3
+ "version": "1.0.0-984f13e42",
4
4
  "description": "Implementation of Connection protection management via a shared secret",
5
5
  "license": "Apache-2.0 OR MIT",
6
- "homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/pnet#readme",
6
+ "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/pnet#readme",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/libp2p/js-libp2p.git"
@@ -11,6 +11,10 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/libp2p/js-libp2p/issues"
13
13
  },
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "provenance": true
17
+ },
14
18
  "type": "module",
15
19
  "types": "./dist/src/index.d.ts",
16
20
  "files": [
@@ -45,20 +49,21 @@
45
49
  "dep-check": "aegir dep-check"
46
50
  },
47
51
  "dependencies": {
48
- "@libp2p/crypto": "2.0.8-97ab31c0c",
49
- "@libp2p/interface": "0.1.6-97ab31c0c",
50
- "it-handshake": "^4.1.3",
52
+ "@libp2p/crypto": "3.0.2-984f13e42",
53
+ "@libp2p/interface": "1.0.2-984f13e42",
54
+ "it-byte-stream": "^1.0.5",
51
55
  "it-map": "^3.0.4",
52
56
  "it-pair": "^2.0.6",
53
57
  "it-pipe": "^3.0.1",
54
58
  "it-stream-types": "^2.0.1",
55
- "uint8arrays": "^4.0.6",
59
+ "uint8arraylist": "^2.4.7",
60
+ "uint8arrays": "^5.0.0",
56
61
  "xsalsa20": "^1.1.0"
57
62
  },
58
63
  "devDependencies": {
59
- "@libp2p/interface-compliance-tests": "4.1.5-97ab31c0c",
60
- "@libp2p/logger": "3.1.0-97ab31c0c",
61
- "@libp2p/peer-id-factory": "3.0.8-97ab31c0c",
64
+ "@libp2p/interface-compliance-tests": "5.0.7-984f13e42",
65
+ "@libp2p/logger": "4.0.2-984f13e42",
66
+ "@libp2p/peer-id-factory": "4.0.1-984f13e42",
62
67
  "@multiformats/multiaddr": "^12.1.10",
63
68
  "@types/xsalsa20": "^1.1.0",
64
69
  "aegir": "^41.0.2",
package/src/crypto.ts CHANGED
@@ -4,16 +4,17 @@ import xsalsa20 from 'xsalsa20'
4
4
  import * as Errors from './errors.js'
5
5
  import { KEY_LENGTH } from './key-generator.js'
6
6
  import type { Source } from 'it-stream-types'
7
+ import type { Uint8ArrayList } from 'uint8arraylist'
7
8
 
8
9
  /**
9
10
  * Creates a stream iterable to encrypt messages in a private network
10
11
  */
11
- export function createBoxStream (nonce: Uint8Array, psk: Uint8Array): (source: Source<Uint8Array>) => AsyncIterable<Uint8Array> {
12
+ export function createBoxStream (nonce: Uint8Array, psk: Uint8Array): (source: Source<Uint8Array | Uint8ArrayList>) => AsyncGenerator<Uint8Array | Uint8ArrayList> {
12
13
  const xor = xsalsa20(nonce, psk)
13
14
 
14
- return (source: Source<Uint8Array>) => (async function * () {
15
+ return (source: Source<Uint8Array | Uint8ArrayList>) => (async function * () {
15
16
  for await (const chunk of source) {
16
- yield Uint8Array.from(xor.update(chunk.slice()))
17
+ yield Uint8Array.from(xor.update(chunk.subarray()))
17
18
  }
18
19
  })()
19
20
  }
@@ -26,7 +27,7 @@ export function createUnboxStream (nonce: Uint8Array, psk: Uint8Array) {
26
27
  const xor = xsalsa20(nonce, psk)
27
28
 
28
29
  for await (const chunk of source) {
29
- yield Uint8Array.from(xor.update(chunk.slice()))
30
+ yield Uint8Array.from(xor.update(chunk.subarray()))
30
31
  }
31
32
  })()
32
33
  }
package/src/index.ts CHANGED
@@ -57,8 +57,8 @@
57
57
  */
58
58
 
59
59
  import { randomBytes } from '@libp2p/crypto'
60
- import { CodeError } from '@libp2p/interface/errors'
61
- import { handshake } from 'it-handshake'
60
+ import { CodeError } from '@libp2p/interface'
61
+ import { byteStream } from 'it-byte-stream'
62
62
  import map from 'it-map'
63
63
  import { duplexPair } from 'it-pair/duplex'
64
64
  import { pipe } from 'it-pipe'
@@ -69,14 +69,22 @@ import {
69
69
  } from './crypto.js'
70
70
  import * as Errors from './errors.js'
71
71
  import { NONCE_LENGTH } from './key-generator.js'
72
- import type { ComponentLogger, Logger } from '@libp2p/interface'
73
- import type { ConnectionProtector, MultiaddrConnection } from '@libp2p/interface/connection'
72
+ import type { ComponentLogger, Logger, ConnectionProtector, MultiaddrConnection } from '@libp2p/interface'
73
+ import type { Uint8ArrayList } from 'uint8arraylist'
74
74
 
75
75
  export { generateKey } from './key-generator.js'
76
76
 
77
77
  export interface ProtectorInit {
78
- enabled?: boolean
78
+ /**
79
+ * A pre-shared key. This must be the same byte value for all peers in the
80
+ * swarm in order for them to communicate.
81
+ */
79
82
  psk: Uint8Array
83
+ /**
84
+ * The initial nonce exchange must complete within this many milliseconds
85
+ * (default: 1000)
86
+ */
87
+ timeout?: number
80
88
  }
81
89
 
82
90
  export interface ProtectorComponents {
@@ -87,7 +95,7 @@ class PreSharedKeyConnectionProtector implements ConnectionProtector {
87
95
  public tag: string
88
96
  private readonly log: Logger
89
97
  private readonly psk: Uint8Array
90
- private readonly enabled: boolean
98
+ private readonly timeout: number
91
99
 
92
100
  /**
93
101
  * Takes a Private Shared Key (psk) and provides a `protect` method
@@ -95,16 +103,11 @@ class PreSharedKeyConnectionProtector implements ConnectionProtector {
95
103
  */
96
104
  constructor (components: ProtectorComponents, init: ProtectorInit) {
97
105
  this.log = components.logger.forComponent('libp2p:pnet')
98
- this.enabled = init.enabled !== false
99
-
100
- if (this.enabled) {
101
- const decodedPSK = decodeV1PSK(init.psk)
102
- this.psk = decodedPSK.psk
103
- this.tag = decodedPSK.tag ?? ''
104
- } else {
105
- this.psk = new Uint8Array()
106
- this.tag = ''
107
- }
106
+ this.timeout = init.timeout ?? 1000
107
+
108
+ const decodedPSK = decodeV1PSK(init.psk)
109
+ this.psk = decodedPSK.psk
110
+ this.tag = decodedPSK.tag ?? ''
108
111
  }
109
112
 
110
113
  /**
@@ -113,10 +116,6 @@ class PreSharedKeyConnectionProtector implements ConnectionProtector {
113
116
  * created with.
114
117
  */
115
118
  async protect (connection: MultiaddrConnection): Promise<MultiaddrConnection> {
116
- if (!this.enabled) {
117
- return connection
118
- }
119
-
120
119
  if (connection == null) {
121
120
  throw new CodeError(Errors.NO_HANDSHAKE_CONNECTION, Errors.ERR_INVALID_PARAMETERS)
122
121
  }
@@ -125,26 +124,31 @@ class PreSharedKeyConnectionProtector implements ConnectionProtector {
125
124
  this.log('protecting the connection')
126
125
  const localNonce = randomBytes(NONCE_LENGTH)
127
126
 
128
- const shake = handshake(connection)
129
- shake.write(localNonce)
127
+ const signal = AbortSignal.timeout(this.timeout)
130
128
 
131
- const result = await shake.reader.next(NONCE_LENGTH)
129
+ const bytes = byteStream(connection)
132
130
 
133
- if (result.value == null) {
134
- throw new CodeError(Errors.STREAM_ENDED, Errors.ERR_INVALID_PARAMETERS)
135
- }
131
+ const [
132
+ , result
133
+ ] = await Promise.all([
134
+ bytes.write(localNonce, {
135
+ signal
136
+ }),
137
+ bytes.read(NONCE_LENGTH, {
138
+ signal
139
+ })
140
+ ])
136
141
 
137
- const remoteNonce = result.value.slice()
138
- shake.rest()
142
+ const remoteNonce = result.subarray()
139
143
 
140
144
  // Create the boxing/unboxing pipe
141
145
  this.log('exchanged nonces')
142
- const [internal, external] = duplexPair<Uint8Array>()
146
+ const [internal, external] = duplexPair<Uint8Array | Uint8ArrayList>()
143
147
  pipe(
144
148
  external,
145
149
  // Encrypt all outbound traffic
146
150
  createBoxStream(localNonce, this.psk),
147
- shake.stream,
151
+ bytes.unwrap(),
148
152
  (source) => map(source, (buf) => buf.subarray()),
149
153
  // Decrypt all inbound traffic
150
154
  createUnboxStream(remoteNonce, this.psk),