Skip to content

Commit

Permalink
fix: make it very explicitly clear that the dbencryption key is required
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Apr 24, 2024
1 parent c691501 commit 9ff1528
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import { DecodedMessage } from '../index'

declare const Buffer

export type GetMessageContentTypeFromClient<C> =
C extends Client<infer T> ? T : never
export type GetMessageContentTypeFromClient<C> = C extends Client<infer T>
? T
: never

export type ExtractDecodedType<C> =
C extends XMTPModule.ContentCodec<infer T> ? T : never
export type ExtractDecodedType<C> = C extends XMTPModule.ContentCodec<infer T>
? T
: never

export class Client<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
Expand All @@ -48,9 +50,11 @@ export class Client<
ContentCodecs extends DefaultContentTypes = DefaultContentTypes,
>(
wallet: Signer | WalletClient | null,
opts?: Partial<ClientOptions> & { codecs?: ContentCodecs }
options: ClientOptions & { codecs?: ContentCodecs }
): Promise<Client<ContentCodecs>> {
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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -136,9 +140,11 @@ export class Client<
* @returns {Promise<Client>} A Promise that resolves to a new Client instance with a random address.
*/
static async createRandom<ContentTypes extends DefaultContentTypes>(
opts?: Partial<ClientOptions> & { codecs?: ContentTypes }
options: ClientOptions & { codecs?: ContentTypes }
): Promise<Client<ContentTypes>> {
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(
Expand All @@ -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 || [])
}

/**
Expand All @@ -170,9 +176,11 @@ export class Client<
ContentCodecs extends DefaultContentTypes = [],
>(
keyBundle: string,
opts?: Partial<ClientOptions> & { codecs?: ContentCodecs }
options: ClientOptions & { codecs?: ContentCodecs }
): Promise<Client<DefaultContentTypes>> {
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,
Expand All @@ -181,7 +189,7 @@ export class Client<
options.dbEncryptionKey,
options.dbPath
)
return new Client(address, opts?.codecs || [])
return new Client(address, options?.codecs || [])
}

/**
Expand Down Expand Up @@ -227,9 +235,11 @@ export class Client<
*/
static async canMessage(
peerAddress: string,
opts?: Partial<ClientOptions>
options: ClientOptions
): Promise<boolean> {
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,
Expand Down Expand Up @@ -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
*/
Expand All @@ -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>): ClientOptions {
const _defaultOptions: ClientOptions = {
env: 'dev',
enableAlphaMls: false,
dbEncryptionKey: undefined,
dbPath: undefined,
}

return { ..._defaultOptions, ...opts } as ClientOptions
}

0 comments on commit 9ff1528

Please sign in to comment.