From 345dc97f2a61b9bf944a06f9ab634dbbe2d16728 Mon Sep 17 00:00:00 2001 From: austin Date: Sun, 20 Dec 2020 18:12:07 -0500 Subject: [PATCH 1/5] enforce config.keys in TS and log an error when no keys are provided --- src/plugins/shs.ts | 7 ++++++- src/types.ts | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/plugins/shs.ts b/src/plugins/shs.ts index fd935c2..dbf24ce 100644 --- a/src/plugins/shs.ts +++ b/src/plugins/shs.ts @@ -22,6 +22,11 @@ export = { name: 'multiserver-shs', version: '1.0.0', init (api: any, config: Config) { + const keys = config.keys && toSodiumKeys(config.keys) + if (!keys) { + console.error(new Error('Config object should contains SHS keys')) + } + let timeoutHandshake: number | undefined if (!isNaN(config.timers?.handshake as any)) { timeoutHandshake = config.timers?.handshake! @@ -40,7 +45,7 @@ export = { } const shs = Shs({ - keys: config.keys && toSodiumKeys(config.keys), + keys, seed: config.seed, appKey: toBuffer(shsCap), timeout: timeoutHandshake, diff --git a/src/types.ts b/src/types.ts index b773911..fbea0ff 100644 --- a/src/types.ts +++ b/src/types.ts @@ -32,10 +32,10 @@ export type Config = { appKey?: Buffer | string; // Cryptographic keys - keys?: { - public?: string; - private?: string; - id?: string; + keys: { + public: string; + private: string; + id: string; }; seed?: unknown; From 5458be54025b3efc20cb5825ce70158edd09b30f Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 21 Dec 2020 15:22:31 -0500 Subject: [PATCH 2/5] throw error for missing keys instead of logging --- src/plugins/shs.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/plugins/shs.ts b/src/plugins/shs.ts index dbf24ce..1a30e0f 100644 --- a/src/plugins/shs.ts +++ b/src/plugins/shs.ts @@ -22,9 +22,8 @@ export = { name: 'multiserver-shs', version: '1.0.0', init (api: any, config: Config) { - const keys = config.keys && toSodiumKeys(config.keys) - if (!keys) { - console.error(new Error('Config object should contains SHS keys')) + if (!config.keys) { + throw new Error('Config object should contains SHS keys') } let timeoutHandshake: number | undefined @@ -45,7 +44,7 @@ export = { } const shs = Shs({ - keys, + keys: toSodiumKeys(config.keys), seed: config.seed, appKey: toBuffer(shsCap), timeout: timeoutHandshake, From f2938384e1532171113c6994e14cb67c59073b8a Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 21 Dec 2020 17:46:13 -0500 Subject: [PATCH 3/5] allow a seed in lieu of keys --- package-lock.json | 12 ++++++------ package.json | 1 + src/plugins/shs.ts | 6 +++++- test/server.js | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2268f80..8520a0e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -452,7 +452,8 @@ "deep-equal": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", - "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=" + "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", + "dev": true }, "deep-is": { "version": "0.1.3", @@ -2331,12 +2332,11 @@ "dev": true }, "secret-handshake": { - "version": "1.1.16", - "resolved": "https://registry.npmjs.org/secret-handshake/-/secret-handshake-1.1.16.tgz", - "integrity": "sha512-iJgGEykTXa8772vmYMGM20jYifTV7lg96bFeitGjly99aIEkIKHkiJWb+3KZ98dg4gwtF/6L+XhL/76iBgKhpA==", + "version": "1.1.20", + "resolved": "https://registry.npmjs.org/secret-handshake/-/secret-handshake-1.1.20.tgz", + "integrity": "sha512-sDtmZDpibGH2ixj3FOmsC3Z/b08eaB2/KAvy2oSp4qvcGdhatBSfb1RdVpwjQl5c3J83WbBo1HSZ7DBtMu43lA==", "requires": { - "chloride": "^2.2.7", - "deep-equal": "~1.0.0", + "chloride": "^2.2.8", "explain-error": "^1.0.4", "pull-box-stream": "^1.0.13", "pull-handshake": "^1.1.1", diff --git a/package.json b/package.json index 53a5fb3..5c20c4a 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "pull-inactivity": "~2.1.1", "pull-rate": "^1.0.2", "pull-stream": "^3.4.5", + "secret-handshake": "^1.1.20", "to-camel-case": "^1.0.0" }, "engines": { diff --git a/src/plugins/shs.ts b/src/plugins/shs.ts index 1a30e0f..49ddf24 100644 --- a/src/plugins/shs.ts +++ b/src/plugins/shs.ts @@ -1,6 +1,7 @@ import * as u from '../util' import { Config } from '../types' const Shs = require('multiserver/plugins/shs') +const {toKeys} = require('secret-handshake') function toBuffer (base64: string | Buffer): Buffer { if (Buffer.isBuffer(base64)) return base64 @@ -22,8 +23,11 @@ export = { name: 'multiserver-shs', version: '1.0.0', init (api: any, config: Config) { + if (config.seed) { + config.keys = toKeys(config.seed) + } if (!config.keys) { - throw new Error('Config object should contains SHS keys') + throw new Error('Config object should contains SHS keys or a seed') } let timeoutHandshake: number | undefined diff --git a/test/server.js b/test/server.js index efecee0..f3292dd 100644 --- a/test/server.js +++ b/test/server.js @@ -31,6 +31,45 @@ var create = SecretStack({ var alice = create({ seed: seeds.alice }) var bob = create({ seed: seeds.bob }) +tape('throw error when no seed or keys are supplied', function (t) { + var noop = () => {} + var shsPlugin = require('../lib/plugins/shs') + var api = { + multiserver: { + transform: noop + } + } + var config = { + caps: {shs: appkey} + } + + t.throws( + () => shsPlugin.init(api, config), + /Config object should contains SHS keys/, + 'SHS plugin throws without seed or keys' + ) + t.end() +}) + +tape('populate config.keys from seed', function (t) { + var shsPlugin = require('../lib/plugins/shs') + var api = { + multiserver: { + transform: (ms) => { + var {publicKey} = ms.create() + t.ok(publicKey, 'public key has been populated from seed') + t.end() + } + } + } + var config = { + caps: {shs: appkey}, + seed: seeds.bob + } + + shsPlugin.init(api, config) +}) + tape('alice connects to bob', function (t) { alice.connect(bob.address(), function (err, rpc) { if (err) throw err From 4e0e1b3377784fd41d518b83d3d406ce51735699 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 21 Dec 2020 20:26:05 -0500 Subject: [PATCH 4/5] remove unnecessary seed to key conversion --- package.json | 1 - src/plugins/shs.ts | 8 ++------ test/server.js | 19 ------------------- 3 files changed, 2 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 5c20c4a..53a5fb3 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "pull-inactivity": "~2.1.1", "pull-rate": "^1.0.2", "pull-stream": "^3.4.5", - "secret-handshake": "^1.1.20", "to-camel-case": "^1.0.0" }, "engines": { diff --git a/src/plugins/shs.ts b/src/plugins/shs.ts index 49ddf24..451c512 100644 --- a/src/plugins/shs.ts +++ b/src/plugins/shs.ts @@ -1,7 +1,6 @@ import * as u from '../util' import { Config } from '../types' const Shs = require('multiserver/plugins/shs') -const {toKeys} = require('secret-handshake') function toBuffer (base64: string | Buffer): Buffer { if (Buffer.isBuffer(base64)) return base64 @@ -23,10 +22,7 @@ export = { name: 'multiserver-shs', version: '1.0.0', init (api: any, config: Config) { - if (config.seed) { - config.keys = toKeys(config.seed) - } - if (!config.keys) { + if (!config.keys && !config.seed) { throw new Error('Config object should contains SHS keys or a seed') } @@ -48,7 +44,7 @@ export = { } const shs = Shs({ - keys: toSodiumKeys(config.keys), + keys: config.keys && toSodiumKeys(config.keys), seed: config.seed, appKey: toBuffer(shsCap), timeout: timeoutHandshake, diff --git a/test/server.js b/test/server.js index f3292dd..63b04d3 100644 --- a/test/server.js +++ b/test/server.js @@ -51,25 +51,6 @@ tape('throw error when no seed or keys are supplied', function (t) { t.end() }) -tape('populate config.keys from seed', function (t) { - var shsPlugin = require('../lib/plugins/shs') - var api = { - multiserver: { - transform: (ms) => { - var {publicKey} = ms.create() - t.ok(publicKey, 'public key has been populated from seed') - t.end() - } - } - } - var config = { - caps: {shs: appkey}, - seed: seeds.bob - } - - shsPlugin.init(api, config) -}) - tape('alice connects to bob', function (t) { alice.connect(bob.address(), function (err, rpc) { if (err) throw err From dfbe7af199ebe4f1ce9471cb405ab428295b87ef Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 21 Dec 2020 20:38:29 -0500 Subject: [PATCH 5/5] update type Config to use keys OR seed --- src/types.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/types.ts b/src/types.ts index fbea0ff..e9ea6df 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,21 +24,13 @@ export type Transform = { create: () => unknown; }; -export type Config = { +export interface BaseConfig { // Cryptographic capability key caps?: { shs?: Buffer | string; }; appKey?: Buffer | string; - // Cryptographic keys - keys: { - public: string; - private: string; - id: string; - }; - seed?: unknown; - // Multiserver connections?: { incoming?: { @@ -59,4 +51,21 @@ export type Config = { // Legacy but still supported host?: string; port?: number; -}; \ No newline at end of file +}; + +export interface SeedConfig extends BaseConfig { + seed: unknown; + keys: never; +}; + +export interface KeysConfig extends BaseConfig { + seed: never; + // Cryptographic keys + keys: { + public: string; + private: string; + id: string; + }; +}; + +export type Config = KeysConfig | SeedConfig; \ No newline at end of file