From 527040b1076b3b1218d04e8aa95c8334c86bb81c Mon Sep 17 00:00:00 2001 From: addievo Date: Tue, 24 Oct 2023 14:17:29 +1100 Subject: [PATCH 1/4] fix: change PrivateKey type to a more generic type --- src/PolykeyAgent.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/PolykeyAgent.ts b/src/PolykeyAgent.ts index aefb48289..b80a92d64 100644 --- a/src/PolykeyAgent.ts +++ b/src/PolykeyAgent.ts @@ -2,7 +2,12 @@ import type { DeepPartial, FileSystem } from './types'; import type { PolykeyWorkerManagerInterface } from './workers/types'; import type { TLSConfig } from './network/types'; import type { SeedNodes } from './nodes/types'; -import type { Key, PasswordOpsLimit, PasswordMemLimit, PrivateKey } from "./keys/types"; +import type { + Key, + PasswordOpsLimit, + PasswordMemLimit, + PrivateKey, +} from './keys/types'; import path from 'path'; import process from 'process'; import Logger from '@matrixai/logger'; @@ -62,7 +67,7 @@ type PolykeyAgentOptions = { certRenewLeadTime: number; recoveryCode: string; privateKeyPath: string; - privateKey: PrivateKey; + privateKey: Buffer; }; client: { keepAliveTimeoutTime: number; From 9f1772a7ce2b8bac7b364fc5105a31061bf6da28 Mon Sep 17 00:00:00 2001 From: Aditya <38064122+bettercallav@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:02:19 +1100 Subject: [PATCH 2/4] fix: add recoveryCode, privateKey and privateKeyPath to PkAgent start and consequently to keyring.start --- src/PolykeyAgent.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/PolykeyAgent.ts b/src/PolykeyAgent.ts index b80a92d64..b53a3829f 100644 --- a/src/PolykeyAgent.ts +++ b/src/PolykeyAgent.ts @@ -1,4 +1,4 @@ -import type { DeepPartial, FileSystem } from './types'; +import type { DeepPartial, FileSystem, ObjectEmpty } from './types'; import type { PolykeyWorkerManagerInterface } from './workers/types'; import type { TLSConfig } from './network/types'; import type { SeedNodes } from './nodes/types'; @@ -6,7 +6,6 @@ import type { Key, PasswordOpsLimit, PasswordMemLimit, - PrivateKey, } from './keys/types'; import path from 'path'; import process from 'process'; @@ -66,8 +65,8 @@ type PolykeyAgentOptions = { certDuration: number; certRenewLeadTime: number; recoveryCode: string; - privateKeyPath: string; privateKey: Buffer; + privateKeyPath: string; }; client: { keepAliveTimeoutTime: number; @@ -85,7 +84,12 @@ type PolykeyAgentOptions = { rpcCallTimeoutTime: number; rpcParserBufferSize: number; }; -}; +} & ( + | ObjectEmpty + | { recoveryCode: string } + | { privateKey: Buffer } + | { privateKeyPath: string } +); interface PolykeyAgent extends CreateDestroyStartStop {} @CreateDestroyStartStop( @@ -495,6 +499,7 @@ class PolykeyAgent { public readonly fs: FileSystem; public readonly logger: Logger; public readonly clientService: ClientService; + public readonly privateKeyPath: string; protected workerManager: PolykeyWorkerManagerInterface | undefined; protected handleEventCertManagerCertChange = async ( @@ -611,13 +616,18 @@ class PolykeyAgent { fresh = false, }: { password: string; - options?: Partial<{ + options?: DeepPartial<{ clientServiceHost: string; clientServicePort: number; agentServiceHost: string; agentServicePort: number; ipv6Only: boolean; workers: number; + keys: { + recoveryCode: string; + privateKey: Buffer; + privateKeyPath: string; + }; }>; workers?: number; fresh?: boolean; @@ -643,6 +653,9 @@ class PolykeyAgent { await this.keyRing.start({ password, fresh, + recoveryCode: optionsDefaulted.keys.recoveryCode, + privateKey: optionsDefaulted.keys.privateKey, + privateKeyPath: optionsDefaulted.keys.privateKeyPath, }); await this.db.start({ crypto: { From 0fce357eb8334960f15c76b8dcc9f9d7a82958a6 Mon Sep 17 00:00:00 2001 From: Aditya <38064122+bettercallav@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:04:26 +1100 Subject: [PATCH 3/4] lintfix --- src/PolykeyAgent.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/PolykeyAgent.ts b/src/PolykeyAgent.ts index b53a3829f..e9246c96f 100644 --- a/src/PolykeyAgent.ts +++ b/src/PolykeyAgent.ts @@ -2,11 +2,7 @@ import type { DeepPartial, FileSystem, ObjectEmpty } from './types'; import type { PolykeyWorkerManagerInterface } from './workers/types'; import type { TLSConfig } from './network/types'; import type { SeedNodes } from './nodes/types'; -import type { - Key, - PasswordOpsLimit, - PasswordMemLimit, -} from './keys/types'; +import type { Key, PasswordOpsLimit, PasswordMemLimit } from './keys/types'; import path from 'path'; import process from 'process'; import Logger from '@matrixai/logger'; From 231cbf68fceddff7c50a4ca676f07ae86833bed1 Mon Sep 17 00:00:00 2001 From: Brian Botha Date: Tue, 24 Oct 2023 18:22:33 +1100 Subject: [PATCH 4/4] fix: final fixup and review All tests are passing and linted. --- src/PolykeyAgent.ts | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/PolykeyAgent.ts b/src/PolykeyAgent.ts index e9246c96f..8038e48d3 100644 --- a/src/PolykeyAgent.ts +++ b/src/PolykeyAgent.ts @@ -61,9 +61,12 @@ type PolykeyAgentOptions = { certDuration: number; certRenewLeadTime: number; recoveryCode: string; - privateKey: Buffer; - privateKeyPath: string; - }; + } & ( + | ObjectEmpty + | { recoveryCode: string } + | { privateKey: Buffer } + | { privateKeyPath: string } + ); client: { keepAliveTimeoutTime: number; keepAliveIntervalTime: number; @@ -80,12 +83,7 @@ type PolykeyAgentOptions = { rpcCallTimeoutTime: number; rpcParserBufferSize: number; }; -} & ( - | ObjectEmpty - | { recoveryCode: string } - | { privateKey: Buffer } - | { privateKeyPath: string } -); +}; interface PolykeyAgent extends CreateDestroyStartStop {} @CreateDestroyStartStop( @@ -169,7 +167,7 @@ class PolykeyAgent { connectionHolePunchIntervalTime: config.defaultsSystem.nodesConnectionHolePunchIntervalTime, }, - }) as PolykeyAgentOptions; + }); // This can only happen if the caller didn't specify the node path and the // automatic detection failed if (optionsDefaulted.nodePath == null) { @@ -227,13 +225,16 @@ class PolykeyAgent { }); keyRing = await KeyRing.createKeyRing({ keysPath, - passwordOpsLimit: optionsDefaulted.keys.passwordOpsLimit, - passwordMemLimit: optionsDefaulted.keys.passwordMemLimit, - strictMemoryLock: optionsDefaulted.keys.strictMemoryLock, fs, fresh, password, logger: logger.getChild(KeyRing.name), + passwordMemLimit: optionsDefaulted.keys.passwordMemLimit, + passwordOpsLimit: optionsDefaulted.keys.passwordOpsLimit, + privateKey: optionsDefaulted.keys.privateKey, + privateKeyPath: optionsDefaulted.keys.privateKeyPath, + recoveryCode: optionsDefaulted.keys.recoveryCode, + strictMemoryLock: optionsDefaulted.keys.strictMemoryLock, }); db = await DB.createDB({ dbPath, @@ -619,11 +620,11 @@ class PolykeyAgent { agentServicePort: number; ipv6Only: boolean; workers: number; - keys: { - recoveryCode: string; - privateKey: Buffer; - privateKeyPath: string; - }; + keys: + | ObjectEmpty + | { recoveryCode: string } + | { privateKey: Buffer } + | { privateKeyPath: string }; }>; workers?: number; fresh?: boolean; @@ -649,9 +650,9 @@ class PolykeyAgent { await this.keyRing.start({ password, fresh, - recoveryCode: optionsDefaulted.keys.recoveryCode, - privateKey: optionsDefaulted.keys.privateKey, - privateKeyPath: optionsDefaulted.keys.privateKeyPath, + recoveryCode: optionsDefaulted.keys?.recoveryCode, + privateKey: optionsDefaulted.keys?.privateKey, + privateKeyPath: optionsDefaulted.keys?.privateKeyPath, }); await this.db.start({ crypto: {