@libp2p/pnet 1.0.0-6625a27fc → 1.0.0-68db79f6b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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;AA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';
59
59
  import type { ConnectionProtector } from '@libp2p/interface/connection';
60
60
  export { generateKey } from './key-generator.js';
61
61
  export interface ProtectorInit {
62
- enabled?: boolean;
62
+ /**
63
+ * A pre-shared key. This must be the same byte value for all peers in the
64
+ * swarm in order for them to communicate.
65
+ */
63
66
  psk: Uint8Array;
67
+ /**
68
+ * The initial nonce exchange must complete within this many milliseconds
69
+ * (default: 1000)
70
+ */
71
+ timeout?: number;
64
72
  }
65
73
  export interface ProtectorComponents {
66
74
  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,MAAM,mBAAmB,CAAA;AAChE,OAAO,KAAK,EAAE,mBAAmB,EAAuB,MAAM,8BAA8B,CAAA;AAG5F,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;AAqED,wBAAgB,YAAY,CAAE,IAAI,EAAE,aAAa,GAAG,CAAC,UAAU,EAAE,mBAAmB,KAAK,mBAAmB,CAE3G"}
package/dist/src/index.js CHANGED
@@ -57,7 +57,7 @@
57
57
  */
58
58
  import { randomBytes } from '@libp2p/crypto';
59
59
  import { CodeError } from '@libp2p/interface/errors';
60
- import { handshake } from 'it-handshake';
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,27 @@ 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
+ await bytes.write(localNonce, {
99
+ signal
100
+ });
101
+ const result = await bytes.read(NONCE_LENGTH, {
102
+ signal
103
+ });
104
+ const remoteNonce = result.subarray();
113
105
  // Create the boxing/unboxing pipe
114
106
  this.log('exchanged nonces');
115
107
  const [internal, external] = duplexPair();
116
108
  pipe(external,
117
109
  // Encrypt all outbound traffic
118
- createBoxStream(localNonce, this.psk), shake.stream, (source) => map(source, (buf) => buf.subarray()),
110
+ createBoxStream(localNonce, this.psk), bytes.unwrap(), (source) => map(source, (buf) => buf.subarray()),
119
111
  // Decrypt all inbound traffic
120
112
  createUnboxStream(remoteNonce, this.psk), external).catch(this.log.error);
121
113
  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;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,E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AE,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-6625a27fc",
3
+ "version": "1.0.0-68db79f6b",
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-6625a27fc",
49
- "@libp2p/interface": "0.1.6-6625a27fc",
50
- "it-handshake": "^4.1.3",
52
+ "@libp2p/crypto": "2.0.8-68db79f6b",
53
+ "@libp2p/interface": "0.1.6-68db79f6b",
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",
59
+ "uint8arraylist": "^2.4.7",
55
60
  "uint8arrays": "^4.0.6",
56
61
  "xsalsa20": "^1.1.0"
57
62
  },
58
63
  "devDependencies": {
59
- "@libp2p/interface-compliance-tests": "4.1.5-6625a27fc",
60
- "@libp2p/logger": "3.1.0-6625a27fc",
61
- "@libp2p/peer-id-factory": "3.0.8-6625a27fc",
64
+ "@libp2p/interface-compliance-tests": "4.1.5-68db79f6b",
65
+ "@libp2p/logger": "3.1.0-68db79f6b",
66
+ "@libp2p/peer-id-factory": "3.0.8-68db79f6b",
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
@@ -58,7 +58,7 @@
58
58
 
59
59
  import { randomBytes } from '@libp2p/crypto'
60
60
  import { CodeError } from '@libp2p/interface/errors'
61
- import { handshake } from 'it-handshake'
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'
@@ -71,12 +71,21 @@ import * as Errors from './errors.js'
71
71
  import { NONCE_LENGTH } from './key-generator.js'
72
72
  import type { ComponentLogger, Logger } from '@libp2p/interface'
73
73
  import type { ConnectionProtector, MultiaddrConnection } from '@libp2p/interface/connection'
74
+ import type { Uint8ArrayList } from 'uint8arraylist'
74
75
 
75
76
  export { generateKey } from './key-generator.js'
76
77
 
77
78
  export interface ProtectorInit {
78
- enabled?: boolean
79
+ /**
80
+ * A pre-shared key. This must be the same byte value for all peers in the
81
+ * swarm in order for them to communicate.
82
+ */
79
83
  psk: Uint8Array
84
+ /**
85
+ * The initial nonce exchange must complete within this many milliseconds
86
+ * (default: 1000)
87
+ */
88
+ timeout?: number
80
89
  }
81
90
 
82
91
  export interface ProtectorComponents {
@@ -87,7 +96,7 @@ class PreSharedKeyConnectionProtector implements ConnectionProtector {
87
96
  public tag: string
88
97
  private readonly log: Logger
89
98
  private readonly psk: Uint8Array
90
- private readonly enabled: boolean
99
+ private readonly timeout: number
91
100
 
92
101
  /**
93
102
  * Takes a Private Shared Key (psk) and provides a `protect` method
@@ -95,16 +104,11 @@ class PreSharedKeyConnectionProtector implements ConnectionProtector {
95
104
  */
96
105
  constructor (components: ProtectorComponents, init: ProtectorInit) {
97
106
  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
- }
107
+ this.timeout = init.timeout ?? 1000
108
+
109
+ const decodedPSK = decodeV1PSK(init.psk)
110
+ this.psk = decodedPSK.psk
111
+ this.tag = decodedPSK.tag ?? ''
108
112
  }
109
113
 
110
114
  /**
@@ -113,10 +117,6 @@ class PreSharedKeyConnectionProtector implements ConnectionProtector {
113
117
  * created with.
114
118
  */
115
119
  async protect (connection: MultiaddrConnection): Promise<MultiaddrConnection> {
116
- if (!this.enabled) {
117
- return connection
118
- }
119
-
120
120
  if (connection == null) {
121
121
  throw new CodeError(Errors.NO_HANDSHAKE_CONNECTION, Errors.ERR_INVALID_PARAMETERS)
122
122
  }
@@ -125,26 +125,27 @@ class PreSharedKeyConnectionProtector implements ConnectionProtector {
125
125
  this.log('protecting the connection')
126
126
  const localNonce = randomBytes(NONCE_LENGTH)
127
127
 
128
- const shake = handshake(connection)
129
- shake.write(localNonce)
128
+ const signal = AbortSignal.timeout(this.timeout)
130
129
 
131
- const result = await shake.reader.next(NONCE_LENGTH)
130
+ const bytes = byteStream(connection)
131
+ await bytes.write(localNonce, {
132
+ signal
133
+ })
132
134
 
133
- if (result.value == null) {
134
- throw new CodeError(Errors.STREAM_ENDED, Errors.ERR_INVALID_PARAMETERS)
135
- }
135
+ const result = await bytes.read(NONCE_LENGTH, {
136
+ signal
137
+ })
136
138
 
137
- const remoteNonce = result.value.slice()
138
- shake.rest()
139
+ const remoteNonce = result.subarray()
139
140
 
140
141
  // Create the boxing/unboxing pipe
141
142
  this.log('exchanged nonces')
142
- const [internal, external] = duplexPair<Uint8Array>()
143
+ const [internal, external] = duplexPair<Uint8Array | Uint8ArrayList>()
143
144
  pipe(
144
145
  external,
145
146
  // Encrypt all outbound traffic
146
147
  createBoxStream(localNonce, this.psk),
147
- shake.stream,
148
+ bytes.unwrap(),
148
149
  (source) => map(source, (buf) => buf.subarray()),
149
150
  // Decrypt all inbound traffic
150
151
  createUnboxStream(remoteNonce, this.psk),