From 9ff1528c39f74f7376fd54d19a4f97bb9fd94c9b Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 23 Apr 2024 21:58:47 -0700 Subject: [PATCH] fix: make it very explicitly clear that the dbencryption key is required --- src/lib/Client.ts | 60 +++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/src/lib/Client.ts b/src/lib/Client.ts index 714c4886c..f8ddeb756 100644 --- a/src/lib/Client.ts +++ b/src/lib/Client.ts @@ -19,11 +19,13 @@ import { DecodedMessage } from '../index' declare const Buffer -export type GetMessageContentTypeFromClient = - C extends Client ? T : never +export type GetMessageContentTypeFromClient = C extends Client + ? T + : never -export type ExtractDecodedType = - C extends XMTPModule.ContentCodec ? T : never +export type ExtractDecodedType = C extends XMTPModule.ContentCodec + ? T + : never export class Client< ContentTypes extends DefaultContentTypes = DefaultContentTypes, @@ -48,9 +50,11 @@ export class Client< ContentCodecs extends DefaultContentTypes = DefaultContentTypes, >( wallet: Signer | WalletClient | null, - opts?: Partial & { codecs?: ContentCodecs } + options: ClientOptions & { codecs?: ContentCodecs } ): Promise> { - const options = defaultOptions(opts) + if (options.dbEncryptionKey.length !== 32) { + throw new Error('The encryption key must be exactly 32 bytes.') + } const { enableSubscription, createSubscription } = this.setupSubscriptions(options) const signer = getSigner(wallet) @@ -96,7 +100,7 @@ export class Client< this.removeSignSubscription() this.removeAuthSubscription() const address = await signer.getAddress() - resolve(new Client(address, opts?.codecs || [])) + resolve(new Client(address, options?.codecs || [])) } ) await XMTPModule.auth( @@ -136,9 +140,11 @@ export class Client< * @returns {Promise} A Promise that resolves to a new Client instance with a random address. */ static async createRandom( - opts?: Partial & { codecs?: ContentTypes } + options: ClientOptions & { codecs?: ContentTypes } ): Promise> { - const options = defaultOptions(opts) + if (options.dbEncryptionKey.length !== 32) { + throw new Error('The encryption key must be exactly 32 bytes.') + } const { enableSubscription, createSubscription } = this.setupSubscriptions(options) const address = await XMTPModule.createRandom( @@ -153,7 +159,7 @@ export class Client< this.removeSubscription(enableSubscription) this.removeSubscription(createSubscription) - return new Client(address, opts?.codecs || []) + return new Client(address, options?.codecs || []) } /** @@ -170,9 +176,11 @@ export class Client< ContentCodecs extends DefaultContentTypes = [], >( keyBundle: string, - opts?: Partial & { codecs?: ContentCodecs } + options: ClientOptions & { codecs?: ContentCodecs } ): Promise> { - const options = defaultOptions(opts) + if (options.dbEncryptionKey.length !== 32) { + throw new Error('The encryption key must be exactly 32 bytes.') + } const address = await XMTPModule.createFromKeyBundle( keyBundle, options.env, @@ -181,7 +189,7 @@ export class Client< options.dbEncryptionKey, options.dbPath ) - return new Client(address, opts?.codecs || []) + return new Client(address, options?.codecs || []) } /** @@ -227,9 +235,11 @@ export class Client< */ static async canMessage( peerAddress: string, - opts?: Partial + options: ClientOptions ): Promise { - const options = defaultOptions(opts) + if (options.dbEncryptionKey.length !== 32) { + throw new Error('The encryption key must be exactly 32 bytes.') + } return await XMTPModule.staticCanMessage( peerAddress, options.env, @@ -439,9 +449,9 @@ export type ClientOptions = { */ enableAlphaMls?: boolean /** - * OPTIONAL specify the encryption key for the database + * REQUIRED specify the encryption key for the database. The encryption key must be exactly 32 bytes. */ - dbEncryptionKey?: Uint8Array + dbEncryptionKey: Uint8Array /** * OPTIONAL specify the XMTP managed database path */ @@ -452,19 +462,3 @@ export type KeyType = { kind: 'identity' | 'prekey' prekeyIndex?: number } - -/** - * Provide a default client configuration. These settings can be used on their own, or as a starting point for custom configurations - * - * @param opts additional options to override the default settings - */ -export function defaultOptions(opts?: Partial): ClientOptions { - const _defaultOptions: ClientOptions = { - env: 'dev', - enableAlphaMls: false, - dbEncryptionKey: undefined, - dbPath: undefined, - } - - return { ..._defaultOptions, ...opts } as ClientOptions -}