diff --git a/.eslintignore b/.eslintignore index ee93d59f1..0f2800a1b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ -*.test.js -*.test.ts dist coverage +docs +tmp diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 0dc144301..f01c92e3a 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -20,7 +20,17 @@ module.exports = { fixMixedExportsWithInlineTypeSpecifier: false, }, ], + '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/consistent-type-imports': 'error', + '@typescript-eslint/no-unused-vars': [ + 'error', + { + argsIgnorePattern: '^_', + destructuredArrayIgnorePattern: '^_', + ignoreRestSiblings: true, + varsIgnorePattern: '^_', + }, + ], 'prettier/prettier': 'error', 'jsdoc/require-jsdoc': 'off', 'jsdoc/require-description': 'off', @@ -36,16 +46,12 @@ module.exports = { message: 'Do not import directly from `crypto`, use `src/crypto/crypto` instead.', }, + { + selector: 'ImportDeclaration[source.value=/^\\.\\./]', + message: + 'Relative parent imports are not allowed, use path aliases instead.', + }, ], }, plugins: ['@typescript-eslint', 'prettier', 'jsdoc'], - ignorePatterns: [ - 'dist', - 'node_modules', - 'examples', - 'scripts', - 'src/types', - 'docs', - 'tmp', - ], } diff --git a/bench/decode.ts b/bench/decode.ts index 7e99e2769..598bcf1af 100644 --- a/bench/decode.ts +++ b/bench/decode.ts @@ -1,10 +1,6 @@ -import { - ConversationV1, - ConversationV2, -} from './../src/conversations/Conversation' -import { MessageV1 } from '../src/Message' -import { newLocalHostClient } from '../test/helpers' -import { SignedPublicKeyBundle } from '../src/crypto' +import { ConversationV1, ConversationV2 } from '@/conversations/Conversation' +import { MessageV1 } from '@/Message' +import { newLocalHostClient } from '@test/helpers' import { MESSAGE_SIZES, newPrivateKeyBundle, @@ -13,7 +9,8 @@ import { } from './helpers' import { add } from 'benny' import { fetcher } from '@xmtp/proto' -import { dateToNs } from '../src/utils' +import { dateToNs } from '@/utils/date' +import { SignedPublicKeyBundle } from '@/crypto/PublicKeyBundle' const decodeV1 = () => { return MESSAGE_SIZES.map((size) => diff --git a/bench/encode.ts b/bench/encode.ts index fa712bcb3..4d37849fd 100644 --- a/bench/encode.ts +++ b/bench/encode.ts @@ -1,15 +1,16 @@ -import { ConversationV2 } from './../src/conversations/Conversation' -import { MessageV1 } from '../src/Message' +import { ConversationV2 } from '@/conversations/Conversation' +import { MessageV1 } from '@/Message' import { add } from 'benny' -import { Client, dateToNs } from '../src' -import { newWallet, newLocalHostClient } from '../test/helpers' -import { SignedPublicKeyBundle } from '../src/crypto' +import { newWallet, newLocalHostClient } from '@test/helpers' import { MESSAGE_SIZES, newPrivateKeyBundle, randomBytes, wrapSuite, } from './helpers' +import Client from '@/Client' +import { dateToNs } from '@/utils/date' +import { SignedPublicKeyBundle } from '@/crypto/PublicKeyBundle' const encodeV1 = () => { return MESSAGE_SIZES.map((size) => diff --git a/bench/helpers.ts b/bench/helpers.ts index 08f959bd1..467b03ca7 100644 --- a/bench/helpers.ts +++ b/bench/helpers.ts @@ -1,9 +1,9 @@ import type Benchmark from 'benchmark' import { suite, save, cycle } from 'benny' import type { Config } from 'benny/lib/internal/common-types' -import crypto from '../src/crypto/crypto' -import { PrivateKeyBundleV1 } from '../src/crypto/PrivateKeyBundle' -import { newWallet } from '../test/helpers' +import crypto from '@/crypto/crypto' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import { newWallet } from '@test/helpers' const MAX_RANDOM_BYTES_SIZE = 65536 diff --git a/package.json b/package.json index ed5575e79..2a76f8ce8 100644 --- a/package.json +++ b/package.json @@ -140,10 +140,12 @@ "rollup": "^4.12.1", "rollup-plugin-dts": "^6.1.0", "rollup-plugin-filesize": "^10.0.0", + "rollup-plugin-tsconfig-paths": "^1.5.2", "semantic-release": "^21.1.2", "typedoc": "^0.25.12", "typescript": "^5.4.2", "vite": "5.1.4", + "vite-tsconfig-paths": "^4.3.1", "vitest": "^1.3.1" }, "packageManager": "yarn@4.1.0", diff --git a/rollup.config.bench.js b/rollup.config.bench.js index 9d9060389..b43fdd26a 100644 --- a/rollup.config.bench.js +++ b/rollup.config.bench.js @@ -1,6 +1,7 @@ import { defineConfig } from 'rollup' import typescript from '@rollup/plugin-typescript' import json from '@rollup/plugin-json' +import tsConfigPaths from 'rollup-plugin-tsconfig-paths' const external = [ '@noble/secp256k1', @@ -11,11 +12,13 @@ const external = [ 'benny', 'crypto', 'elliptic', + 'ethers', 'long', 'viem', ] const plugins = [ + tsConfigPaths(), typescript({ declaration: false, declarationMap: false, diff --git a/rollup.config.js b/rollup.config.js index 7bbd085b5..692cd147b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -5,6 +5,7 @@ import filesize from 'rollup-plugin-filesize' import json from '@rollup/plugin-json' import terser from '@rollup/plugin-terser' import { resolveExtensions } from '@xmtp/rollup-plugin-resolve-extensions' +import tsConfigPaths from 'rollup-plugin-tsconfig-paths' const external = [ '@noble/secp256k1', @@ -20,6 +21,7 @@ const external = [ ] const plugins = [ + tsConfigPaths(), typescript({ declaration: false, declarationMap: false, @@ -59,7 +61,7 @@ export default defineConfig([ file: 'dist/index.d.ts', format: 'es', }, - plugins: [dts()], + plugins: [tsConfigPaths(), dts()], }, { input: 'src/index.ts', diff --git a/src/ApiClient.ts b/src/ApiClient.ts index 6ffe2b34f..562af03d7 100644 --- a/src/ApiClient.ts +++ b/src/ApiClient.ts @@ -1,11 +1,14 @@ import { messageApi } from '@xmtp/proto' import type { NotifyStreamEntityArrival } from '@xmtp/proto/ts/dist/types/fetch.pb' -import { b64Decode, retry, sleep, toNanoString } from './utils' import AuthCache from './authn/AuthCache' -import type { Authenticator } from './authn' +import type { Authenticator } from '@/authn/interfaces' +// eslint-disable-next-line no-restricted-syntax import { version } from '../package.json' import { XMTP_DEV_WARNING } from './constants' import type { Flatten } from './utils/typedefs' +import { b64Decode } from '@/utils/bytes' +import { retry, sleep } from '@/utils/async' +import { toNanoString } from '@/utils/date' export const { MessageApi, SortDirection } = messageApi const RETRY_SLEEP_TIME = 100 diff --git a/src/Client.ts b/src/Client.ts index 46524ee76..7e817fdec 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -1,19 +1,6 @@ import { PrivateKeyBundleV1 } from './crypto/PrivateKeyBundle' -import { PublicKeyBundle, SignedPublicKeyBundle } from './crypto' -import type { - EnvelopeMapper, - EnvelopeMapperWithMessage, - EnvelopeWithMessage, -} from './utils' -import { - buildUserContactTopic, - mapPaginatedStream, - buildUserInviteTopic, - isBrowser, - getSigner, -} from './utils' import type { Signer } from './types/Signer' -import { Conversations } from './conversations' +import Conversations from '@/conversations/Conversations' import { ContentTypeText, TextCodec } from './codecs/Text' import type { ContentCodec, EncodedContent } from './MessageContent' import { ContentTypeId } from './MessageContent' @@ -22,30 +9,39 @@ import { content as proto, messageApi } from '@xmtp/proto' import { decodeContactBundle, encodeContactBundle } from './ContactBundle' import type { ApiClient, PublishParams } from './ApiClient' import HttpApiClient, { ApiUrls, SortDirection } from './ApiClient' -import { KeystoreAuthenticator } from './authn' +import KeystoreAuthenticator from '@/authn/KeystoreAuthenticator' import type { Flatten } from './utils/typedefs' import type BackupClient from './message-backup/BackupClient' import { BackupType } from './message-backup/BackupClient' import { createBackupClient } from './message-backup/BackupClientFactory' -import type { KeystoreProvider } from './keystore/providers' -import { - KeyGeneratorKeystoreProvider, - KeystoreProviderUnavailableError, - NetworkKeystoreProvider, - SnapProvider, - StaticKeystoreProvider, -} from './keystore/providers' -import type { Persistence } from './keystore/persistence' -import { - BrowserStoragePersistence, - InMemoryPersistence, -} from './keystore/persistence' +import type { KeystoreProvider } from './keystore/providers/interfaces' import { hasMetamaskWithSnaps } from './keystore/snapHelpers' import { packageName, version } from './snapInfo.json' import type { ExtractDecodedType } from './types/client' import { getAddress, type WalletClient } from 'viem' import { Contacts } from './Contacts' import type { KeystoreInterfaces } from './keystore/rpcDefinitions' +import { isBrowser } from '@/utils/browser' +import { getSigner } from '@/utils/viem' +import { buildUserContactTopic, buildUserInviteTopic } from '@/utils/topic' +import { mapPaginatedStream } from '@/utils/async' +import type { + EnvelopeMapperWithMessage, + EnvelopeMapper, + EnvelopeWithMessage, +} from '@/utils/async' +import type { Persistence } from '@/keystore/persistence/interface' +import BrowserStoragePersistence from '@/keystore/persistence/BrowserStoragePersistence' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import SnapProvider from '@/keystore/providers/SnapProvider' +import StaticKeystoreProvider from '@/keystore/providers/StaticKeystoreProvider' +import NetworkKeystoreProvider from '@/keystore/providers/NetworkKeystoreProvider' +import KeyGeneratorKeystoreProvider from '@/keystore/providers/KeyGeneratorKeystoreProvider' +import { KeystoreProviderUnavailableError } from '@/keystore/providers/errors' +import { + PublicKeyBundle, + SignedPublicKeyBundle, +} from '@/crypto/PublicKeyBundle' const { Compression } = proto // eslint-disable @typescript-eslint/explicit-module-boundary-types diff --git a/src/ContactBundle.ts b/src/ContactBundle.ts index 07d074819..ba2913400 100644 --- a/src/ContactBundle.ts +++ b/src/ContactBundle.ts @@ -1,5 +1,8 @@ +import { + PublicKeyBundle, + SignedPublicKeyBundle, +} from '@/crypto/PublicKeyBundle' import { contact, publicKey } from '@xmtp/proto' -import { PublicKeyBundle, SignedPublicKeyBundle } from './crypto' // Decodes contact bundles from the contact topic. export function decodeContactBundle( diff --git a/src/Contacts.ts b/src/Contacts.ts index f5af16c6f..fab40d378 100644 --- a/src/Contacts.ts +++ b/src/Contacts.ts @@ -1,10 +1,11 @@ import type Client from './Client' import { privatePreferences } from '@xmtp/proto' -import type { EnvelopeWithMessage } from './utils' -import { buildUserPrivatePreferencesTopic, fromNanoString } from './utils' import Stream from './Stream' import type { OnConnectionLostCallback } from './ApiClient' import JobRunner from './conversations/JobRunner' +import type { EnvelopeWithMessage } from '@/utils/async' +import { fromNanoString } from '@/utils/date' +import { buildUserPrivatePreferencesTopic } from '@/utils/topic' export type ConsentState = 'allowed' | 'denied' | 'unknown' diff --git a/src/Invitation.ts b/src/Invitation.ts index efd9ba2d7..18c0f46d3 100644 --- a/src/Invitation.ts +++ b/src/Invitation.ts @@ -4,9 +4,10 @@ import type { messageApi } from '@xmtp/proto' import { invitation } from '@xmtp/proto' import crypto from './crypto/crypto' import Ciphertext from './crypto/Ciphertext' -import { decrypt, encrypt } from './crypto' +import { decrypt, encrypt } from './crypto/encryption' import type { PrivateKeyBundleV2 } from './crypto/PrivateKeyBundle' -import { dateToNs, buildDirectMessageTopicV2 } from './utils' +import { buildDirectMessageTopicV2 } from '@/utils/topic' +import { dateToNs } from '@/utils/date' export type InvitationContext = { conversationId: string diff --git a/src/Message.ts b/src/Message.ts index 9edf0d677..485616241 100644 --- a/src/Message.ts +++ b/src/Message.ts @@ -5,13 +5,14 @@ import type { conversationReference } from '@xmtp/proto' import { message as proto } from '@xmtp/proto' import Long from 'long' import Ciphertext from './crypto/Ciphertext' -import { PublicKeyBundle, PublicKey } from './crypto' import { bytesToHex } from './crypto/utils' import { sha256 } from './crypto/encryption' import type { ContentTypeId } from './MessageContent' -import { dateToNs, nsToDate } from './utils' +import { dateToNs, nsToDate } from './utils/date' import { buildDecryptV1Request, getResultOrThrow } from './utils/keystore' import type { KeystoreInterfaces } from './keystore/rpcDefinitions' +import { PublicKeyBundle } from '@/crypto/PublicKeyBundle' +import { PublicKey } from '@/crypto/PublicKey' const headerBytesAndCiphertext = ( msg: proto.Message diff --git a/src/authn/AuthData.ts b/src/authn/AuthData.ts index 82439b57e..492d1af37 100644 --- a/src/authn/AuthData.ts +++ b/src/authn/AuthData.ts @@ -1,6 +1,6 @@ import { authn as authnProto } from '@xmtp/proto' import type Long from 'long' -import { dateToNs } from '../utils/date' +import { dateToNs } from '@/utils/date' export default class AuthData implements authnProto.AuthData { walletAddr: string diff --git a/src/authn/KeystoreAuthenticator.ts b/src/authn/KeystoreAuthenticator.ts index e0dc3c16b..d93cec06f 100644 --- a/src/authn/KeystoreAuthenticator.ts +++ b/src/authn/KeystoreAuthenticator.ts @@ -1,10 +1,10 @@ import type { authn } from '@xmtp/proto' -import { dateToNs } from '../utils' +import { dateToNs } from '@/utils/date' import Token from './Token' import type { KeystoreInterface, KeystoreInterfaces, -} from '../keystore/rpcDefinitions' +} from '@/keystore/rpcDefinitions' const wrapToken = (token: authn.Token): Token => { if (token instanceof Token) { diff --git a/src/authn/LocalAuthenticator.ts b/src/authn/LocalAuthenticator.ts index 924567688..42f44e20d 100644 --- a/src/authn/LocalAuthenticator.ts +++ b/src/authn/LocalAuthenticator.ts @@ -1,8 +1,8 @@ import { authn, signature, publicKey } from '@xmtp/proto' import AuthData from './AuthData' -import type { PrivateKey } from '../crypto' import Token from './Token' import { hexToBytes, keccak256 } from 'viem' +import type { PrivateKey } from '@/crypto/PrivateKey' export default class LocalAuthenticator { private identityKey: PrivateKey diff --git a/src/authn/index.ts b/src/authn/index.ts deleted file mode 100644 index b44778210..000000000 --- a/src/authn/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export { default as LocalAuthenticator } from './LocalAuthenticator' -export { default as KeystoreAuthenticator } from './KeystoreAuthenticator' -export { default as AuthData } from './AuthData' -export { default as Token } from './Token' -export { default as AuthCache } from './AuthCache' -export * from './interfaces' diff --git a/src/codecs/Composite.ts b/src/codecs/Composite.ts index 88976a236..4fb3c9fb5 100644 --- a/src/codecs/Composite.ts +++ b/src/codecs/Composite.ts @@ -2,8 +2,8 @@ import type { ContentCodec, EncodedContent, CodecRegistry, -} from '../MessageContent' -import { ContentTypeId } from '../MessageContent' +} from '@/MessageContent' +import { ContentTypeId } from '@/MessageContent' import { composite as proto } from '@xmtp/proto' // xmtp.org/composite diff --git a/src/codecs/Text.ts b/src/codecs/Text.ts index 126790586..2cec5668d 100644 --- a/src/codecs/Text.ts +++ b/src/codecs/Text.ts @@ -1,5 +1,5 @@ -import type { ContentCodec, EncodedContent } from '../MessageContent' -import { ContentTypeId } from '../MessageContent' +import type { ContentCodec, EncodedContent } from '@/MessageContent' +import { ContentTypeId } from '@/MessageContent' // xmtp.org/text // diff --git a/src/conversations/Conversation.ts b/src/conversations/Conversation.ts index 7602abd82..7186da781 100644 --- a/src/conversations/Conversation.ts +++ b/src/conversations/Conversation.ts @@ -1,34 +1,30 @@ -import type { OnConnectionLostCallback } from './../ApiClient' -import { - buildUserIntroTopic, - buildDirectMessageTopic, - dateToNs, - concat, - toNanoString, -} from '../utils' -import Stream from '../Stream' +import type { OnConnectionLostCallback } from '@/ApiClient' +import Stream from '@/Stream' import type { ListMessagesOptions, ListMessagesPaginatedOptions, SendOptions, -} from '../Client' -import type Client from '../Client' -import type { InvitationContext } from '../Invitation' -import { DecodedMessage, MessageV1, MessageV2 } from '../Message' +} from '@/Client' +import type Client from '@/Client' +import type { InvitationContext } from '@/Invitation' +import { DecodedMessage, MessageV1, MessageV2 } from '@/Message' import type { messageApi, keystore } from '@xmtp/proto' import { message, content as proto } from '@xmtp/proto' +import { PreparedMessage } from '@/PreparedMessage' +import { sha256 } from '@/crypto/encryption' +import { buildDecryptV1Request, getResultOrThrow } from '@/utils/keystore' +import { ContentTypeText } from '@/codecs/Text' +import type { ConsentState } from '@/Contacts' +import { getAddress } from 'viem' +import { buildDirectMessageTopic, buildUserIntroTopic } from '@/utils/topic' +import { dateToNs, toNanoString } from '@/utils/date' +import { concat } from '@/utils/bytes' import { - SignedPublicKey, - Signature, PublicKeyBundle, SignedPublicKeyBundle, -} from '../crypto' -import { PreparedMessage } from '../PreparedMessage' -import { sha256 } from '../crypto/encryption' -import { buildDecryptV1Request, getResultOrThrow } from '../utils/keystore' -import { ContentTypeText } from '../codecs/Text' -import type { ConsentState } from '../Contacts' -import { getAddress } from 'viem' +} from '@/crypto/PublicKeyBundle' +import { SignedPublicKey } from '@/crypto/PublicKey' +import Signature from '@/crypto/Signature' /** * Conversation represents either a V1 or V2 conversation with a common set of methods. diff --git a/src/conversations/Conversations.ts b/src/conversations/Conversations.ts index 8043d3f5a..f74cef0ea 100644 --- a/src/conversations/Conversations.ts +++ b/src/conversations/Conversations.ts @@ -1,25 +1,26 @@ -import type { OnConnectionLostCallback } from './../ApiClient' +import type { OnConnectionLostCallback } from '@/ApiClient' import type { messageApi, keystore, conversationReference } from '@xmtp/proto' -import { SignedPublicKeyBundle } from './../crypto/PublicKeyBundle' -import type { ListMessagesOptions } from './../Client' -import type { InvitationContext } from './../Invitation' +import { + PublicKeyBundle, + SignedPublicKeyBundle, +} from '@/crypto/PublicKeyBundle' +import type { ListMessagesOptions } from '@/Client' +import type { InvitationContext } from '@/Invitation' import type { Conversation } from './Conversation' import { ConversationV1, ConversationV2 } from './Conversation' -import { MessageV1, DecodedMessage } from '../Message' -import Stream from '../Stream' -import type Client from '../Client' +import { MessageV1, DecodedMessage } from '@/Message' +import Stream from '@/Stream' +import type Client from '@/Client' +import { SortDirection } from '@/ApiClient' +import Long from 'long' +import JobRunner from './JobRunner' +import { dateToNs, nsToDate } from '@/utils/date' import { buildDirectMessageTopic, buildUserIntroTopic, buildUserInviteTopic, - dateToNs, isValidTopic, - nsToDate, -} from '../utils' -import { PublicKeyBundle } from '../crypto' -import { SortDirection } from '../ApiClient' -import Long from 'long' -import JobRunner from './JobRunner' +} from '@/utils/topic' const messageHasHeaders = (msg: MessageV1): boolean => { return Boolean(msg.recipientAddress && msg.senderAddress) diff --git a/src/conversations/JobRunner.ts b/src/conversations/JobRunner.ts index 6a7e0aa99..37592116f 100644 --- a/src/conversations/JobRunner.ts +++ b/src/conversations/JobRunner.ts @@ -1,8 +1,8 @@ import { keystore } from '@xmtp/proto' import { Mutex } from 'async-mutex' import Long from 'long' -import { dateToNs, nsToDate } from '../utils' -import type { KeystoreInterfaces } from '../keystore/rpcDefinitions' +import { dateToNs, nsToDate } from '@/utils/date' +import type { KeystoreInterfaces } from '@/keystore/rpcDefinitions' const CLOCK_SKEW_OFFSET_MS = 10000 diff --git a/src/conversations/index.ts b/src/conversations/index.ts deleted file mode 100644 index 37fd7b94a..000000000 --- a/src/conversations/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { default as Conversations } from './Conversations' -export type { Conversation } from './Conversation' -export { ConversationV1, ConversationV2 } from './Conversation' diff --git a/src/crypto/PrivateKeyBundle.ts b/src/crypto/PrivateKeyBundle.ts index 1b8cf5360..f2ea25113 100644 --- a/src/crypto/PrivateKeyBundle.ts +++ b/src/crypto/PrivateKeyBundle.ts @@ -3,7 +3,7 @@ import { PrivateKey, SignedPrivateKey } from './PrivateKey' import { WalletSigner } from './Signature' import type { PublicKey, SignedPublicKey } from './PublicKey' import { PublicKeyBundle, SignedPublicKeyBundle } from './PublicKeyBundle' -import type { Signer } from '../types/Signer' +import type { Signer } from '@/types/Signer' import { NoMatchingPreKeyError } from './errors' // PrivateKeyBundle bundles the private keys corresponding to a PublicKeyBundle for convenience. diff --git a/src/crypto/PublicKey.ts b/src/crypto/PublicKey.ts index 7f1dc65f5..3383de4ec 100644 --- a/src/crypto/PublicKey.ts +++ b/src/crypto/PublicKey.ts @@ -3,7 +3,7 @@ import * as secp from '@noble/secp256k1' import Long from 'long' import Signature, { WalletSigner } from './Signature' import { computeAddress, equalBytes, splitSignature } from './utils' -import type { Signer } from '../types/Signer' +import type { Signer } from '@/types/Signer' import { sha256 } from './encryption' import type { Hex } from 'viem' import { hashMessage, hexToBytes } from 'viem' diff --git a/src/crypto/Signature.ts b/src/crypto/Signature.ts index 5bcee93b0..2beeeef92 100644 --- a/src/crypto/Signature.ts +++ b/src/crypto/Signature.ts @@ -3,7 +3,7 @@ import Long from 'long' import * as secp from '@noble/secp256k1' import { PublicKey, UnsignedPublicKey, SignedPublicKey } from './PublicKey' import { SignedPrivateKey } from './PrivateKey' -import type { Signer } from '../types/Signer' +import type { Signer } from '@/types/Signer' import { bytesToHex, equalBytes, splitSignature } from './utils' import type { Hex } from 'viem' import { hashMessage, hexToBytes } from 'viem' diff --git a/src/crypto/index.ts b/src/crypto/index.ts deleted file mode 100644 index ac23b0327..000000000 --- a/src/crypto/index.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { PublicKeyBundle, SignedPublicKeyBundle } from './PublicKeyBundle' -import { SignedPrivateKey, PrivateKey } from './PrivateKey' -import type { PrivateKeyBundle } from './PrivateKeyBundle' -import { - PrivateKeyBundleV1, - PrivateKeyBundleV2, - decodePrivateKeyBundle, -} from './PrivateKeyBundle' -import { UnsignedPublicKey, SignedPublicKey, PublicKey } from './PublicKey' -import Signature, { WalletSigner } from './Signature' -import * as utils from './utils' -import { - decrypt, - encrypt, - exportHmacKey, - generateHmacSignature, - hkdfHmacKey, - importHmacKey, - verifyHmacSignature, -} from './encryption' -import Ciphertext from './Ciphertext' -import SignedEciesCiphertext from './SignedEciesCiphertext' - -export type { PrivateKeyBundle } -export { - utils, - encrypt, - decrypt, - exportHmacKey, - generateHmacSignature, - hkdfHmacKey, - importHmacKey, - verifyHmacSignature, - Ciphertext, - UnsignedPublicKey, - SignedPublicKey, - SignedPublicKeyBundle, - PublicKey, - PublicKeyBundle, - PrivateKey, - SignedPrivateKey, - decodePrivateKeyBundle, - PrivateKeyBundleV1, - PrivateKeyBundleV2, - Signature, - SignedEciesCiphertext, - WalletSigner, -} diff --git a/src/crypto/selfEncryption.ts b/src/crypto/selfEncryption.ts index 9917c43f2..dbf13f333 100644 --- a/src/crypto/selfEncryption.ts +++ b/src/crypto/selfEncryption.ts @@ -1,3 +1,4 @@ +import type { PrivateKey } from '@/crypto/PrivateKey' import { // eslint-disable-next-line camelcase generate_private_preferences_topic, @@ -6,7 +7,6 @@ import { // eslint-disable-next-line camelcase user_preferences_encrypt, } from '@xmtp/user-preferences-bindings-wasm' -import type { PrivateKey } from '../crypto' export async function userPreferencesEncrypt( identityKey: PrivateKey, diff --git a/src/index.ts b/src/index.ts index b6d5448f6..4e241590d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,19 @@ export type { Message } from './Message' export { DecodedMessage, MessageV1, MessageV2, decodeContent } from './Message' -export type { PrivateKeyBundle } from './crypto' +export type { PrivateKeyBundle } from './crypto/PrivateKeyBundle' +export { PrivateKey } from './crypto/PrivateKey' export { - Ciphertext, - PublicKey, - PublicKeyBundle, - SignedPublicKey, - SignedPublicKeyBundle, - PrivateKey, PrivateKeyBundleV1, PrivateKeyBundleV2, - Signature, +} from './crypto/PrivateKeyBundle' +export { default as Ciphertext } from './crypto/Ciphertext' +export { PublicKey, SignedPublicKey } from './crypto/PublicKey' +export { + PublicKeyBundle, + SignedPublicKeyBundle, +} from './crypto/PublicKeyBundle' +export { default as Signature } from './crypto/Signature' +export { encrypt, decrypt, exportHmacKey, @@ -18,7 +21,7 @@ export { hkdfHmacKey, importHmacKey, verifyHmacSignature, -} from './crypto' +} from './crypto/encryption' export { default as Stream } from './Stream' export type { Signer } from './types/Signer' export type { @@ -37,8 +40,9 @@ export { defaultKeystoreProviders, Compression, } from './Client' -export type { Conversation } from './conversations' -export { Conversations, ConversationV1, ConversationV2 } from './conversations' +export type { Conversation } from '@/conversations/Conversation' +export { ConversationV1, ConversationV2 } from '@/conversations/Conversation' +export { default as Conversations } from '@/conversations/Conversations' export type { CodecRegistry, ContentCodec, @@ -62,15 +66,11 @@ export type { OnConnectionLostCallback, } from './ApiClient' export { default as HttpApiClient, ApiUrls, SortDirection } from './ApiClient' -export type { Authenticator } from './authn' -export { AuthCache, LocalAuthenticator } from './authn' +export type { Authenticator } from '@/authn/interfaces' +export { default as LocalAuthenticator } from '@/authn/LocalAuthenticator' +export { default as AuthCache } from '@/authn/AuthCache' +export { retry, mapPaginatedStream } from './utils/async' export { - nsToDate, - dateToNs, - retry, - fromNanoString, - toNanoString, - mapPaginatedStream, buildContentTopic, buildDirectMessageTopic, buildDirectMessageTopicV2, @@ -78,9 +78,10 @@ export { buildUserIntroTopic, buildUserInviteTopic, buildUserPrivateStoreTopic, -} from './utils' -export type { Keystore, TopicData } from './keystore' -export { InMemoryKeystore } from './keystore' +} from './utils/topic' +export { nsToDate, dateToNs, fromNanoString, toNanoString } from './utils/date' +export type { Keystore, TopicData } from './keystore/interfaces' +export { default as InMemoryKeystore } from './keystore/InMemoryKeystore' export type { KeystoreApiDefs, KeystoreApiEntries, @@ -106,20 +107,16 @@ export { apiDefs as keystoreApiDefs, snapApiDefs as snapKeystoreApiDefs, } from './keystore/rpcDefinitions' -export type { KeystoreProvider } from './keystore/providers' -export { - KeyGeneratorKeystoreProvider, - NetworkKeystoreProvider, - StaticKeystoreProvider, - SnapProvider, -} from './keystore/providers' -export type { Persistence } from './keystore/persistence' -export { - EncryptedPersistence, - BrowserStoragePersistence, - InMemoryPersistence, - PrefixedPersistence, -} from './keystore/persistence' +export type { KeystoreProvider } from './keystore/providers/interfaces' +export { default as KeyGeneratorKeystoreProvider } from './keystore/providers/KeyGeneratorKeystoreProvider' +export { default as NetworkKeystoreProvider } from './keystore/providers/NetworkKeystoreProvider' +export { default as StaticKeystoreProvider } from './keystore/providers/StaticKeystoreProvider' +export { default as SnapProvider } from './keystore/providers/SnapProvider' +export type { Persistence } from './keystore/persistence/interface' +export { default as EncryptedPersistence } from './keystore/persistence/EncryptedPersistence' +export { default as BrowserStoragePersistence } from './keystore/persistence/BrowserStoragePersistence' +export { default as InMemoryPersistence } from './keystore/persistence/InMemoryPersistence' +export { default as PrefixedPersistence } from './keystore/persistence/PrefixedPersistence' export type { InvitationContext } from './Invitation' export { SealedInvitation, InvitationV1 } from './Invitation' export { decodeContactBundle } from './ContactBundle' diff --git a/src/keystore/InMemoryKeystore.ts b/src/keystore/InMemoryKeystore.ts index a2ee30776..ac2cd8616 100644 --- a/src/keystore/InMemoryKeystore.ts +++ b/src/keystore/InMemoryKeystore.ts @@ -1,9 +1,8 @@ import type { authn, privateKey, signature } from '@xmtp/proto' import { keystore } from '@xmtp/proto' -import type { PrivateKeyBundleV1 } from './../crypto/PrivateKeyBundle' -import { PrivateKeyBundleV2 } from './../crypto/PrivateKeyBundle' -import { InvitationV1, SealedInvitation } from './../Invitation' -import type { PrivateKey, PublicKeyBundle } from '../crypto' +import type { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import { PrivateKeyBundleV2 } from '@/crypto/PrivateKeyBundle' +import { InvitationV1, SealedInvitation } from '@/Invitation' import type { TopicData } from './interfaces' import { decryptV1, encryptV1, encryptV2, decryptV2 } from './encryption' import { KeystoreError } from './errors' @@ -16,30 +15,32 @@ import { getKeyMaterial, topicDataToV2ConversationReference, } from './utils' -import { - nsToDate, - buildDirectMessageTopicV2, - buildDirectMessageTopic, -} from '../utils' import type { AddRequest } from './conversationStores' import { V1Store, V2Store } from './conversationStores' -import type { Persistence } from './persistence' -import LocalAuthenticator from '../authn/LocalAuthenticator' -import { hmacSha256Sign } from '../crypto/ecies' -import crypto from '../crypto/crypto' -import { bytesToHex } from '../crypto/utils' +import type { Persistence } from './persistence/interface' +import LocalAuthenticator from '@/authn/LocalAuthenticator' +import { hmacSha256Sign } from '@/crypto/ecies' +import crypto from '@/crypto/crypto' +import { bytesToHex } from '@/crypto/utils' import Long from 'long' import { userPreferencesDecrypt, userPreferencesEncrypt, generateUserPreferencesTopic, -} from '../crypto/selfEncryption' +} from '@/crypto/selfEncryption' +import type { KeystoreInterface } from '@/keystore/rpcDefinitions' +import { nsToDate } from '@/utils/date' +import { + buildDirectMessageTopic, + buildDirectMessageTopicV2, +} from '@/utils/topic' +import type { PrivateKey } from '@/crypto/PrivateKey' +import type { PublicKeyBundle } from '@/crypto/PublicKeyBundle' import { exportHmacKey, generateHmacSignature, hkdfHmacKey, -} from '../crypto/encryption' -import type { KeystoreInterface } from './rpcDefinitions' +} from '@/crypto/encryption' const { ErrorCode } = keystore diff --git a/src/keystore/SnapKeystore.ts b/src/keystore/SnapKeystore.ts index 708c28c99..96c024e61 100644 --- a/src/keystore/SnapKeystore.ts +++ b/src/keystore/SnapKeystore.ts @@ -1,6 +1,6 @@ import type { SnapMeta } from './snapHelpers' import { snapRPC } from './snapHelpers' -import type { XmtpEnv } from '../Client' +import type { XmtpEnv } from '@/Client' import type { SnapKeystoreApiEntries, SnapKeystoreApiRequestValues, diff --git a/src/keystore/conversationStores.ts b/src/keystore/conversationStores.ts index 129ca4c2a..c2b6a3ced 100644 --- a/src/keystore/conversationStores.ts +++ b/src/keystore/conversationStores.ts @@ -4,7 +4,7 @@ import { keystore } from '@xmtp/proto' import type { Persistence } from './persistence/interface' import { Mutex } from 'async-mutex' import { isCompleteTopicData, topicDataToMap } from './utils' -import { numberToUint8Array, uint8ArrayToNumber } from '../utils' +import { numberToUint8Array, uint8ArrayToNumber } from '@/utils/bytes' export type AddRequest = { topic: string diff --git a/src/keystore/encryption.ts b/src/keystore/encryption.ts index b3ef3dfdc..fa68bf662 100644 --- a/src/keystore/encryption.ts +++ b/src/keystore/encryption.ts @@ -1,5 +1,6 @@ -import type { PublicKeyBundle, PrivateKeyBundleV1 } from '../crypto' -import { encrypt, decrypt } from '../crypto' +import type { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import type { PublicKeyBundle } from '@/crypto/PublicKeyBundle' +import { encrypt, decrypt } from '@/crypto/encryption' import type { ciphertext } from '@xmtp/proto' export const decryptV1 = async ( diff --git a/src/keystore/index.ts b/src/keystore/index.ts deleted file mode 100644 index d5e3787c7..000000000 --- a/src/keystore/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export { default as InMemoryKeystore } from './InMemoryKeystore' -export { SnapKeystore } from './SnapKeystore' -export { V1Store, V2Store } from './conversationStores' -export * from './encryption' -export * from './errors' -export * from './interfaces' diff --git a/src/keystore/interfaces.ts b/src/keystore/interfaces.ts index 1f7cf7837..9ab3d3c78 100644 --- a/src/keystore/interfaces.ts +++ b/src/keystore/interfaces.ts @@ -5,7 +5,7 @@ import type { privateKey, signature, } from '@xmtp/proto' -import type { WithoutUndefined } from '../utils/typedefs' +import type { WithoutUndefined } from '@/utils/typedefs' /** * A Keystore is responsible for holding the user's XMTP private keys and using them to encrypt/decrypt/sign messages. diff --git a/src/keystore/persistence/EncryptedPersistence.ts b/src/keystore/persistence/EncryptedPersistence.ts index 148567564..f26465052 100644 --- a/src/keystore/persistence/EncryptedPersistence.ts +++ b/src/keystore/persistence/EncryptedPersistence.ts @@ -1,8 +1,8 @@ +import type { PrivateKey, SignedPrivateKey } from '@/crypto/PrivateKey' import type { Persistence } from './interface' -import type { Ecies } from '../../crypto/ecies' -import { getPublic, encrypt, decrypt } from '../../crypto/ecies' -import type { PrivateKey, SignedPrivateKey } from '../../crypto' -import { SignedEciesCiphertext } from '../../crypto' +import type { Ecies } from '@/crypto/ecies' +import { getPublic, encrypt, decrypt } from '@/crypto/ecies' +import SignedEciesCiphertext from '@/crypto/SignedEciesCiphertext' /** * EncryptedPersistence is a Persistence implementation that uses ECIES to encrypt all values diff --git a/src/keystore/persistence/TopicPersistence.ts b/src/keystore/persistence/TopicPersistence.ts index da77c1d51..2f0711234 100644 --- a/src/keystore/persistence/TopicPersistence.ts +++ b/src/keystore/persistence/TopicPersistence.ts @@ -1,7 +1,7 @@ import { messageApi } from '@xmtp/proto' -import type { ApiClient } from '../../ApiClient' -import type { Authenticator } from '../../authn' -import { buildUserPrivateStoreTopic } from '../../utils/topic' +import type { ApiClient } from '@/ApiClient' +import type { Authenticator } from '@/authn/interfaces' +import { buildUserPrivateStoreTopic } from '@/utils/topic' import type { Persistence } from './interface' export default class TopicPersistence implements Persistence { diff --git a/src/keystore/persistence/index.ts b/src/keystore/persistence/index.ts deleted file mode 100644 index 15ccee364..000000000 --- a/src/keystore/persistence/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export * from './interface' -export { default as BrowserStoragePersistence } from './BrowserStoragePersistence' -export { default as InMemoryPersistence } from './InMemoryPersistence' -export { default as PrefixedPersistence } from './PrefixedPersistence' -export { default as EncryptedPersistence } from './EncryptedPersistence' diff --git a/src/keystore/providers/KeyGeneratorKeystoreProvider.ts b/src/keystore/providers/KeyGeneratorKeystoreProvider.ts index 0aa28d67f..955d9e54d 100644 --- a/src/keystore/providers/KeyGeneratorKeystoreProvider.ts +++ b/src/keystore/providers/KeyGeneratorKeystoreProvider.ts @@ -1,13 +1,13 @@ -import type { ApiClient } from '../../ApiClient' -import { PrivateKeyBundleV1 } from '../../crypto' -import InMemoryKeystore from '../InMemoryKeystore' -import TopicPersistence from '../persistence/TopicPersistence' +import type { ApiClient } from '@/ApiClient' +import InMemoryKeystore from '@/keystore/InMemoryKeystore' +import TopicPersistence from '@/keystore/persistence/TopicPersistence' import { KeystoreProviderUnavailableError } from './errors' import { buildPersistenceFromOptions } from './helpers' import NetworkKeyManager from './NetworkKeyManager' -import type { Signer } from '../../types/Signer' +import type { Signer } from '@/types/Signer' import type { KeystoreProvider, KeystoreProviderOptions } from './interfaces' -import type { KeystoreInterface } from '../rpcDefinitions' +import type { KeystoreInterface } from '@/keystore/rpcDefinitions' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' /** * KeyGeneratorKeystoreProvider will create a new XMTP `PrivateKeyBundle` and persist it to the network diff --git a/src/keystore/providers/NetworkKeyManager.ts b/src/keystore/providers/NetworkKeyManager.ts index 9120edb1f..65efa5a8c 100644 --- a/src/keystore/providers/NetworkKeyManager.ts +++ b/src/keystore/providers/NetworkKeyManager.ts @@ -1,20 +1,19 @@ -import type { Signer } from '../../types/Signer' -import crypto from '../../crypto/crypto' -import { - PrivateKeyBundleV1, - decodePrivateKeyBundle, - decrypt, - encrypt, - PrivateKeyBundleV2, -} from '../../crypto' -import type { PreEventCallback } from '../../Client' -import { LocalAuthenticator } from '../../authn' -import { bytesToHex } from '../../crypto/utils' -import Ciphertext from '../../crypto/Ciphertext' +import type { Signer } from '@/types/Signer' +import crypto from '@/crypto/crypto' +import type { PreEventCallback } from '@/Client' +import LocalAuthenticator from '@/authn/LocalAuthenticator' +import { bytesToHex } from '@/crypto/utils' +import Ciphertext from '@/crypto/Ciphertext' import { privateKey as proto } from '@xmtp/proto' -import type TopicPersistence from '../persistence/TopicPersistence' +import type TopicPersistence from '@/keystore/persistence/TopicPersistence' import type { Hex } from 'viem' import { getAddress, hexToBytes, verifyMessage } from 'viem' +import { + PrivateKeyBundleV1, + PrivateKeyBundleV2, + decodePrivateKeyBundle, +} from '@/crypto/PrivateKeyBundle' +import { decrypt, encrypt } from '@/crypto/encryption' const KEY_BUNDLE_NAME = 'key_bundle' /** diff --git a/src/keystore/providers/NetworkKeystoreProvider.ts b/src/keystore/providers/NetworkKeystoreProvider.ts index d4073e6fa..68bacaee1 100644 --- a/src/keystore/providers/NetworkKeystoreProvider.ts +++ b/src/keystore/providers/NetworkKeystoreProvider.ts @@ -1,12 +1,12 @@ -import type { Signer } from './../../types/Signer' -import type { ApiClient } from '../../ApiClient' +import type { Signer } from '@/types/Signer' +import type { ApiClient } from '@/ApiClient' import type { KeystoreProvider, KeystoreProviderOptions } from './interfaces' import NetworkKeyLoader from './NetworkKeyManager' import { KeystoreProviderUnavailableError } from './errors' -import TopicPersistence from '../persistence/TopicPersistence' -import InMemoryKeystore from '../InMemoryKeystore' +import TopicPersistence from '@/keystore/persistence/TopicPersistence' +import InMemoryKeystore from '@/keystore/InMemoryKeystore' import { buildPersistenceFromOptions } from './helpers' -import type { KeystoreInterface } from '../rpcDefinitions' +import type { KeystoreInterface } from '@/keystore/rpcDefinitions' /** * NetworkKeystoreProvider will look on the XMTP network for an `EncryptedPrivateKeyBundle` diff --git a/src/keystore/providers/SnapProvider.ts b/src/keystore/providers/SnapProvider.ts index 806057830..099885078 100644 --- a/src/keystore/providers/SnapProvider.ts +++ b/src/keystore/providers/SnapProvider.ts @@ -1,22 +1,25 @@ import { KeystoreProviderUnavailableError } from './errors' import type { KeystoreProvider, KeystoreProviderOptions } from './interfaces' -import { SnapKeystore } from '../SnapKeystore' +import { SnapKeystore } from '@/keystore/SnapKeystore' import { connectSnap, getSnap, getWalletStatus, hasMetamaskWithSnaps, initSnap, -} from '../snapHelpers' +} from '@/keystore/snapHelpers' import { keystore } from '@xmtp/proto' -import type { Signer } from '../../types/Signer' -import type { ApiClient } from '../../ApiClient' +import type { Signer } from '@/types/Signer' +import type { ApiClient } from '@/ApiClient' import NetworkKeystoreProvider from './NetworkKeystoreProvider' -import { PrivateKeyBundleV1, decodePrivateKeyBundle } from '../../crypto' import KeyGeneratorKeystoreProvider from './KeyGeneratorKeystoreProvider' -import type { XmtpEnv } from '../../Client' -import { semverGreaterThan } from '../../utils/semver' -import type { SnapKeystoreInterface } from '../rpcDefinitions' +import type { XmtpEnv } from '@/Client' +import { semverGreaterThan } from '@/utils/semver' +import type { SnapKeystoreInterface } from '@/keystore/rpcDefinitions' +import { + PrivateKeyBundleV1, + decodePrivateKeyBundle, +} from '@/crypto/PrivateKeyBundle' const { GetKeystoreStatusResponse_KeystoreStatus: KeystoreStatus } = keystore export const SNAP_LOCAL_ORIGIN = 'local:http://localhost:8080' diff --git a/src/keystore/providers/StaticKeystoreProvider.ts b/src/keystore/providers/StaticKeystoreProvider.ts index 25b535c41..24a3d9554 100644 --- a/src/keystore/providers/StaticKeystoreProvider.ts +++ b/src/keystore/providers/StaticKeystoreProvider.ts @@ -1,12 +1,12 @@ import { KeystoreProviderUnavailableError } from './errors' import type { KeystoreProvider, KeystoreProviderOptions } from './interfaces' -import InMemoryKeystore from '../InMemoryKeystore' +import InMemoryKeystore from '@/keystore/InMemoryKeystore' import { decodePrivateKeyBundle, PrivateKeyBundleV2, -} from '../../crypto/PrivateKeyBundle' +} from '@/crypto/PrivateKeyBundle' import { buildPersistenceFromOptions } from './helpers' -import type { KeystoreInterface } from '../rpcDefinitions' +import type { KeystoreInterface } from '@/keystore/rpcDefinitions' /** * StaticKeystoreProvider will look for a `privateKeyOverride` in the provided options, diff --git a/src/keystore/providers/helpers.ts b/src/keystore/providers/helpers.ts index 11717a9a8..d08d0d530 100644 --- a/src/keystore/providers/helpers.ts +++ b/src/keystore/providers/helpers.ts @@ -1,9 +1,12 @@ -import type { PrivateKeyBundleV2 } from './../../crypto/PrivateKeyBundle' -import type { PrivateKeyBundleV1 } from '../../crypto/PrivateKeyBundle' -import { EncryptedPersistence, PrefixedPersistence } from '../persistence' +import type { + PrivateKeyBundleV2, + PrivateKeyBundleV1, +} from '@/crypto/PrivateKeyBundle' +import EncryptedPersistence from '@/keystore/persistence/EncryptedPersistence' +import PrefixedPersistence from '@/keystore/persistence/PrefixedPersistence' import type { KeystoreProviderOptions } from './interfaces' -import { buildPersistenceKey } from '../utils' -import EphemeralPersistence from '../persistence/InMemoryPersistence' +import { buildPersistenceKey } from '@/keystore/utils' +import EphemeralPersistence from '@/keystore/persistence/InMemoryPersistence' export const buildPersistenceFromOptions = async ( opts: KeystoreProviderOptions, diff --git a/src/keystore/providers/index.ts b/src/keystore/providers/index.ts deleted file mode 100644 index 4ca535171..000000000 --- a/src/keystore/providers/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export * from './errors' -export * from './interfaces' -export { default as KeyGeneratorKeystoreProvider } from './KeyGeneratorKeystoreProvider' -export { default as NetworkKeystoreProvider } from './NetworkKeystoreProvider' -export { default as StaticKeystoreProvider } from './StaticKeystoreProvider' -export { default as SnapProvider } from './SnapProvider' diff --git a/src/keystore/providers/interfaces.ts b/src/keystore/providers/interfaces.ts index be70d8496..3bf814f2b 100644 --- a/src/keystore/providers/interfaces.ts +++ b/src/keystore/providers/interfaces.ts @@ -1,8 +1,11 @@ -import type { XmtpEnv, PreEventCallbackOptions } from '../../Client' -import type { Signer } from '../../types/Signer' -import type { ApiClient } from '../../ApiClient' -import type { Persistence } from '../persistence' -import type { KeystoreInterface, KeystoreInterfaces } from '../rpcDefinitions' +import type { XmtpEnv, PreEventCallbackOptions } from '@/Client' +import type { Signer } from '@/types/Signer' +import type { ApiClient } from '@/ApiClient' +import type { Persistence } from '@/keystore/persistence/interface' +import type { + KeystoreInterface, + KeystoreInterfaces, +} from '@/keystore/rpcDefinitions' export type KeystoreProviderOptions = { env: XmtpEnv diff --git a/src/keystore/rpcDefinitions.ts b/src/keystore/rpcDefinitions.ts index db73e08cf..185522c7f 100644 --- a/src/keystore/rpcDefinitions.ts +++ b/src/keystore/rpcDefinitions.ts @@ -1,6 +1,6 @@ import { keystore, authn, publicKey, privateKey, signature } from '@xmtp/proto' import type { Reader, Writer } from 'protobufjs/minimal' -import type { Flatten } from '../utils/typedefs' +import type { Flatten } from '@/utils/typedefs' // eslint-disable-next-line @typescript-eslint/no-explicit-any export type KeystoreRPCCodec = { diff --git a/src/keystore/snapHelpers.ts b/src/keystore/snapHelpers.ts index f0ac9c835..b0c8b91ec 100644 --- a/src/keystore/snapHelpers.ts +++ b/src/keystore/snapHelpers.ts @@ -6,12 +6,12 @@ import type { SnapKeystoreApiRequestEncoders, SnapKeystoreApiResponseDecoders, } from './rpcDefinitions' -import { b64Decode, b64Encode } from '../utils/bytes' +import { b64Decode, b64Encode } from '@/utils/bytes' import { KeystoreError } from './errors' -import type { PrivateKeyBundleV1 } from '../crypto' -import { getEthereum } from '../utils/ethereum' -import type { XmtpEnv } from '../Client' -import { isSameMajorVersion } from '../utils/semver' +import { getEthereum } from '@/utils/ethereum' +import type { XmtpEnv } from '@/Client' +import { isSameMajorVersion } from '@/utils/semver' +import type { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' const { GetKeystoreStatusResponse_KeystoreStatus: KeystoreStatus, InitKeystoreRequest, diff --git a/src/keystore/utils.ts b/src/keystore/utils.ts index cc0c29392..a61e893dd 100644 --- a/src/keystore/utils.ts +++ b/src/keystore/utils.ts @@ -1,10 +1,13 @@ import type { TopicData } from './interfaces' import type { conversationReference, publicKey, invitation } from '@xmtp/proto' import { keystore } from '@xmtp/proto' -import { PublicKeyBundle, SignedPublicKeyBundle } from '../crypto' import { KeystoreError } from './errors' -import type { WithoutUndefined } from '../utils/typedefs' -import type { XmtpEnv } from '../Client' +import type { WithoutUndefined } from '@/utils/typedefs' +import type { XmtpEnv } from '@/Client' +import { + PublicKeyBundle, + SignedPublicKeyBundle, +} from '@/crypto/PublicKeyBundle' export const convertError = ( e: Error, diff --git a/src/types/client.ts b/src/types/client.ts index cbff6bcbd..a22d30193 100644 --- a/src/types/client.ts +++ b/src/types/client.ts @@ -1,5 +1,5 @@ -import type Client from '../Client' -import type { ContentCodec } from '../MessageContent' +import type Client from '@/Client' +import type { ContentCodec } from '@/MessageContent' export type GetMessageContentTypeFromClient = C extends Client ? T : never diff --git a/src/types/metamask.ts b/src/types/metamask.ts index 7bedde4b8..7cb22e5f0 100644 --- a/src/types/metamask.ts +++ b/src/types/metamask.ts @@ -1,4 +1,4 @@ -import { MetaMaskInpageProvider } from '@metamask/providers' +import type { MetaMaskInpageProvider } from '@metamask/providers' type EthereumType = MetaMaskInpageProvider & { setProvider?: (provider: MetaMaskInpageProvider) => void diff --git a/src/utils/index.ts b/src/utils/index.ts deleted file mode 100644 index c9c5fb79e..000000000 --- a/src/utils/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -export * from './topic' -export * from './async' -export * from './date' -export * from './bytes' -export * from './browser' -export * from './semver' -export * from './viem' diff --git a/src/utils/keystore.ts b/src/utils/keystore.ts index 47994467f..ff1a9ea00 100644 --- a/src/utils/keystore.ts +++ b/src/utils/keystore.ts @@ -1,7 +1,7 @@ import { keystore } from '@xmtp/proto' -import { PublicKeyBundle } from '../crypto/PublicKeyBundle' -import { KeystoreError } from '../keystore/errors' -import type { MessageV1 } from '../Message' +import { PublicKeyBundle } from '@/crypto/PublicKeyBundle' +import { KeystoreError } from '@/keystore/errors' +import type { MessageV1 } from '@/Message' import type { WithoutUndefined } from './typedefs' type EncryptionResponseResult< diff --git a/src/utils/viem.ts b/src/utils/viem.ts index 8994c13e8..1fd8f1d6d 100644 --- a/src/utils/viem.ts +++ b/src/utils/viem.ts @@ -1,5 +1,5 @@ import type { WalletClient } from 'viem' -import type { Signer } from '../types/Signer' +import type { Signer } from '@/types/Signer' export function getSigner(wallet: Signer | WalletClient | null): Signer | null { if (!wallet) { diff --git a/test/ApiClient.test.ts b/test/ApiClient.test.ts index 5f7031e58..4d9d8b753 100644 --- a/test/ApiClient.test.ts +++ b/test/ApiClient.test.ts @@ -1,19 +1,17 @@ -import { +import type { InitReq, NotifyStreamEntityArrival, } from '@xmtp/proto/ts/dist/types/fetch.pb' -import ApiClient, { - GrpcError, - GrpcStatus, - PublishParams, -} from '../src/ApiClient' +import type { PublishParams } from '@/ApiClient' +import ApiClient, { GrpcError, GrpcStatus } from '@/ApiClient' import { messageApi } from '@xmtp/proto' import { sleep } from './helpers' -import { LocalAuthenticator } from '../src/authn' -import { PrivateKey } from '../src' +import LocalAuthenticator from '@/authn/LocalAuthenticator' +// eslint-disable-next-line no-restricted-syntax import packageJson from '../package.json' -import { dateToNs } from '../src/utils' +import { dateToNs } from '@/utils/date' import { vi } from 'vitest' +import { PrivateKey } from '@/crypto/PrivateKey' const { MessageApi } = messageApi @@ -37,7 +35,7 @@ const mockGetToken = vi.hoisted(() => }) ) ) -vi.mock('../src/authn/LocalAuthenticator', () => { +vi.mock('@/authn/LocalAuthenticator', () => { return { default: vi.fn().mockImplementation(() => { return { createToken: mockGetToken } @@ -260,7 +258,7 @@ describe('Subscribe', () => { it('can subscribe', async () => { const subscribeMock = createSubscribeMock(2) let numEnvelopes = 0 - const cb = (env: messageApi.Envelope) => { + const cb = () => { numEnvelopes++ } const req = { contentTopics: [CONTENT_TOPIC] } @@ -292,20 +290,20 @@ describe('Subscribe', () => { // it is called. The second time it is called, it behaves as expected (the connection // stays open, and two messages are received over the subscription) called++ - if (called == 1) { + if (called === 1) { throw new Error('error') } - let nonErroringSubscribe = subscribeMockImplementation(2) + const nonErroringSubscribe = subscribeMockImplementation(2) return await nonErroringSubscribe(req, cb, initReq) } ) const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {}) let numEnvelopes = 0 - const cb = (env: messageApi.Envelope) => { + const cb = () => { numEnvelopes++ } let numDisconnects = 0 - let onDisconnect = () => { + const onDisconnect = () => { numDisconnects++ } const req = { contentTopics: [CONTENT_TOPIC] } @@ -342,16 +340,16 @@ describe('Subscribe', () => { // it is called. The second time it is called, it behaves as expected (the connection // stays open, and two messages are received over the subscription) called++ - if (called == 1) { + if (called === 1) { return } - let nonAbortingSubscribe = subscribeMockImplementation(2) + const nonAbortingSubscribe = subscribeMockImplementation(2) return await nonAbortingSubscribe(req, cb, initReq) } ) const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {}) let numEnvelopes = 0 - const cb = (env: messageApi.Envelope) => { + const cb = () => { numEnvelopes++ } const req = { contentTopics: [CONTENT_TOPIC] } @@ -374,11 +372,8 @@ describe('Subscribe', () => { }) it('throws when no content topics returned', async () => { - const subscribeMock = createSubscribeMock(2) - let numEnvelopes = 0 - const cb = (env: messageApi.Envelope) => { - numEnvelopes++ - } + createSubscribeMock(2) + const cb = () => {} const req = { contentTopics: [] } const t = () => client.subscribe(req, cb) expect(t).toThrow( @@ -394,7 +389,7 @@ function createQueryMock(envelopes: messageApi.Envelope[], numPages = 1) { .mockImplementation(async (): Promise => { numCalls++ return { - envelopes: envelopes, + envelopes, pagingInfo: { cursor: numCalls >= numPages ? undefined : CURSOR, }, @@ -435,7 +430,7 @@ function createSubscribeMock(numMessages: number) { // The connection stream is expected to stay open until it is closed by an unsubscribe // request (which is reflected by the 'onabort' signal) function subscribeMockImplementation(numMessages: number) { - let subscribe = async ( + const subscribe = async ( req: messageApi.SubscribeRequest, cb: NotifyStreamEntityArrival | undefined, initReq?: InitReq diff --git a/test/ApiClientE2E.test.ts b/test/ApiClientE2E.test.ts index 31d0d09c1..6b692fb09 100644 --- a/test/ApiClientE2E.test.ts +++ b/test/ApiClientE2E.test.ts @@ -1,9 +1,9 @@ -import ApiClient, { ApiUrls, GrpcStatus } from '../src/ApiClient' +import ApiClient, { ApiUrls, GrpcStatus } from '@/ApiClient' import { newWallet } from './helpers' -import { LocalAuthenticator } from '../src/authn' -import { buildUserPrivateStoreTopic } from '../src/utils' -import { Wallet } from 'ethers' -import { PrivateKeyBundleV1 } from '../src/crypto' +import LocalAuthenticator from '@/authn/LocalAuthenticator' +import { buildUserPrivateStoreTopic } from '@/utils/topic' +import type { Wallet } from 'ethers' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' type TestCase = { name: string; api: string } diff --git a/test/BackupClient.test.ts b/test/BackupClient.test.ts index e599d2e0b..22a57e67d 100644 --- a/test/BackupClient.test.ts +++ b/test/BackupClient.test.ts @@ -1,4 +1,4 @@ -import { BackupType } from '../src/message-backup/BackupClient' +import { BackupType } from '@/message-backup/BackupClient' import { newLocalHostClient, newDevClient } from './helpers' describe('Backup configuration', () => { diff --git a/test/Client.test.ts b/test/Client.test.ts index 0289593ee..59c35b2a7 100644 --- a/test/Client.test.ts +++ b/test/Client.test.ts @@ -5,32 +5,28 @@ import { waitForUserContact, newLocalHostClientWithCustomWallet, } from './helpers' -import { EnvelopeWithMessage, buildUserContactTopic } from '../src/utils' -import Client, { ClientOptions } from '../src/Client' -import { - ApiUrls, - CompositeCodec, - Compression, - ContentTypeText, - HttpApiClient, - InMemoryPersistence, - PublishParams, - TextCodec, -} from '../src' -import NetworkKeyManager from '../src/keystore/providers/NetworkKeyManager' -import TopicPersistence from '../src/keystore/persistence/TopicPersistence' -import { PrivateKey, PrivateKeyBundleV1 } from '../src/crypto' +import { buildUserContactTopic } from '@/utils/topic' +import type { ClientOptions } from '@/Client' +import Client, { Compression } from '@/Client' +import NetworkKeyManager from '@/keystore/providers/NetworkKeyManager' +import TopicPersistence from '@/keystore/persistence/TopicPersistence' import { Wallet } from 'ethers' -import { NetworkKeystoreProvider } from '../src/keystore/providers' -import { PublishResponse } from '@xmtp/proto/ts/dist/types/message_api/v1/message_api.pb' -import LocalStoragePonyfill from '../src/keystore/persistence/LocalStoragePonyfill' -import { message } from '@xmtp/proto' +import NetworkKeystoreProvider from '@/keystore/providers/NetworkKeystoreProvider' +import type { PublishResponse } from '@xmtp/proto/ts/dist/types/message_api/v1/message_api.pb' +import LocalStoragePonyfill from '@/keystore/persistence/LocalStoragePonyfill' import { createWalletClient, http } from 'viem' -import { privateKeyToAccount } from 'viem/accounts' +import { privateKeyToAccount, generatePrivateKey } from 'viem/accounts' import { mainnet } from 'viem/chains' -import { generatePrivateKey } from 'viem/accounts' import { vi, assert } from 'vitest' import { ContentTypeTestKey, TestKeyCodec } from './ContentTypeTestKey' +import type { EnvelopeWithMessage } from '@/utils/async' +import { ContentTypeText, TextCodec } from '@/codecs/Text' +import { CompositeCodec } from '@/codecs/Composite' +import HttpApiClient, { ApiUrls } from '@/ApiClient' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import { message } from '@xmtp/proto' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import { PrivateKey } from '@/crypto/PrivateKey' type TestCase = { name: string @@ -38,7 +34,7 @@ type TestCase = { } const mockEthRequest = vi.hoisted(() => vi.fn()) -vi.mock('../src/utils/ethereum', () => { +vi.mock('@/utils/ethereum', () => { return { __esModule: true, getEthereum: vi.fn(() => { @@ -103,11 +99,11 @@ describe('Client', () => { }) it('Check address can be sent to', async () => { - const can_mesg_a = await alice.canMessage('NOT AN ADDRESS') - expect(can_mesg_a).toBe(false) + const canMessageA = await alice.canMessage('NOT AN ADDRESS') + expect(canMessageA).toBe(false) - const can_mesg_b = await alice.canMessage(bob.address) - expect(can_mesg_b).toBe(true) + const canMessageB = await alice.canMessage(bob.address) + expect(canMessageB).toBe(true) const lower = await alice.canMessage(bob.address.toLowerCase()) expect(lower).toBe(true) @@ -364,7 +360,7 @@ describe('publishEnvelopes', () => { const c = await newLocalHostClient() // Set a bogus authenticator so we can have failing publishes c.apiClient.setAuthenticator({ - // @ts-ignore-next-line + // @ts-expect-error mock function createToken: async () => ({ toBase64: () => 'derp!', }), @@ -394,11 +390,11 @@ describe('ClientOptions', () => { } tests.forEach((testCase) => { it('Default/empty options', async () => { - const c = await testCase.newClient() + await testCase.newClient() }) it('Partial specification', async () => { - const c = await testCase.newClient({ + await testCase.newClient({ persistConversations: true, }) }) @@ -411,13 +407,11 @@ describe('ClientOptions', () => { const convo = await client.conversations.newConversation(other.address) expect(convo).toBeTruthy() try { - // Add ts-expect-error so that if we break the type casting someone will notice - // @ts-expect-error + // @ts-expect-error if we break the type casting someone will notice await convo.send(123) const messages = await convo.messages() for (const message of messages) { - // Strings don't have this kind of method - // @ts-expect-error + // @ts-expect-error Strings don't have this kind of method message.toFixed() } } catch (e) { @@ -443,13 +437,13 @@ describe('ClientOptions', () => { it('allows you to specify a custom API client factory', async () => { const expectedError = new Error('CustomApiClient') class CustomApiClient extends HttpApiClient { - publish(messages: PublishParams[]): Promise { + publish(): Promise { return Promise.reject(expectedError) } } const c = newLocalHostClient({ - apiClientFactory: (opts) => { + apiClientFactory: () => { return new CustomApiClient(ApiUrls.local) }, }) @@ -460,7 +454,7 @@ describe('ClientOptions', () => { describe('pluggable persistence', () => { it('allows for an override of the persistence engine', async () => { class MyNewPersistence extends InMemoryPersistence { - getItem(key: string): Promise { + getItem(): Promise { return Promise.reject(new Error('MyNewPersistence')) } } diff --git a/test/Compression.test.ts b/test/Compression.test.ts index faa49e1f5..054aabee6 100644 --- a/test/Compression.test.ts +++ b/test/Compression.test.ts @@ -4,21 +4,21 @@ import { decompress, readStreamFromBytes, writeStreamToBytes, -} from '../src/Compression' -import { ContentTypeText } from '../src/codecs/Text' +} from '@/Compression' +import { ContentTypeText } from '@/codecs/Text' describe('Compression', function () { it('can stream bytes from source to sink', async function () { - let from = new Uint8Array(111).fill(42) + const from = new Uint8Array(111).fill(42) // make sink smaller so that it has to grow a lot - let to = { bytes: new Uint8Array(3) } + const to = { bytes: new Uint8Array(3) } await readStreamFromBytes(from, 23).pipeTo(writeStreamToBytes(to, 1000)) expect(from).toEqual(to.bytes) }) it('will not write beyond limit', () => { - let from = new Uint8Array(111).fill(42) - let to = { bytes: new Uint8Array(10) } + const from = new Uint8Array(111).fill(42) + const to = { bytes: new Uint8Array(10) } expect( readStreamFromBytes(from, 23).pipeTo(writeStreamToBytes(to, 100)) ).rejects.toThrow('maximum output size exceeded') @@ -29,7 +29,7 @@ describe('Compression', function () { const compressed = new Uint8Array([ 120, 156, 211, 210, 34, 11, 0, 0, 252, 223, 9, 7, ]) - let content = { + const content = { type: ContentTypeText, parameters: {}, content: uncompressed, diff --git a/test/ContactBundle.test.ts b/test/ContactBundle.test.ts index d6876b8f1..4702b1869 100644 --- a/test/ContactBundle.test.ts +++ b/test/ContactBundle.test.ts @@ -1,17 +1,19 @@ -import { decodeContactBundle, encodeContactBundle } from '../src/ContactBundle' -import { PublicKeyBundle, SignedPublicKeyBundle } from '../src' +import { decodeContactBundle, encodeContactBundle } from '@/ContactBundle' import { PrivateKeyBundleV1, PrivateKeyBundleV2, -} from '../src/crypto/PrivateKeyBundle' - +} from '@/crypto/PrivateKeyBundle' import { newWallet } from './helpers' +import { + PublicKeyBundle, + SignedPublicKeyBundle, +} from '@/crypto/PublicKeyBundle' describe('ContactBundles', function () { it('roundtrip', async function () { const priv = await PrivateKeyBundleV1.generate() const pub = priv.getPublicKeyBundle() - let bytes = encodeContactBundle(pub) + const bytes = encodeContactBundle(pub) const cb = decodeContactBundle(bytes) expect(cb).toBeInstanceOf(PublicKeyBundle) expect(pub.equals(cb as PublicKeyBundle)).toBeTruthy() @@ -20,7 +22,7 @@ describe('ContactBundles', function () { const wallet = newWallet() const priv = await PrivateKeyBundleV2.generate(wallet) const pub = priv.getPublicKeyBundle() - let bytes = encodeContactBundle(pub) + const bytes = encodeContactBundle(pub) const cb = decodeContactBundle(bytes) expect(cb).toBeInstanceOf(SignedPublicKeyBundle) expect(pub.equals(cb as SignedPublicKeyBundle)).toBeTruthy() diff --git a/test/Contacts.test.ts b/test/Contacts.test.ts index 65b7564ea..42d2f929b 100644 --- a/test/Contacts.test.ts +++ b/test/Contacts.test.ts @@ -1,6 +1,5 @@ -import { privatePreferences } from '@xmtp/proto' -import Client from '../src/Client' -import { Contacts } from '../src/Contacts' +import Client from '@/Client' +import { Contacts } from '@/Contacts' import { newWallet } from './helpers' const alice = newWallet() @@ -180,7 +179,7 @@ describe('Contacts', () => { await aliceClient.conversations.newConversation(bob.address) let numActions = 0 - const actions: privatePreferences.PrivatePreferencesAction[] = [] + // eslint-disable-next-line no-unreachable-loop for await (const action of aliceStream) { numActions++ expect(action.denyAddress).toBeUndefined() diff --git a/test/ContentTypeTestKey.ts b/test/ContentTypeTestKey.ts index 9c30b2191..f31dff6ac 100644 --- a/test/ContentTypeTestKey.ts +++ b/test/ContentTypeTestKey.ts @@ -1,6 +1,7 @@ +import type { ContentCodec, EncodedContent } from '@/MessageContent' +import { ContentTypeId } from '@/MessageContent' +import { PublicKey } from '@/crypto/PublicKey' import { publicKey } from '@xmtp/proto' -import type { ContentCodec, EncodedContent } from '../src' -import { ContentTypeId, PublicKey } from '../src' export const ContentTypeTestKey = new ContentTypeId({ authorityId: 'xmtp.test', diff --git a/test/Invitation.test.ts b/test/Invitation.test.ts index 96fd5f473..a96511849 100644 --- a/test/Invitation.test.ts +++ b/test/Invitation.test.ts @@ -1,15 +1,15 @@ -import crypto from '../src/crypto/crypto' -import { InvitationV1 } from './../src/Invitation' -import { PrivateKeyBundleV2 } from './../src/crypto/PrivateKeyBundle' +import crypto from '@/crypto/crypto' import { + InvitationV1, SealedInvitation, SealedInvitationV1, SealedInvitationHeaderV1, -} from '../src/Invitation' +} from '@/Invitation' +import { PrivateKeyBundleV2 } from '@/crypto/PrivateKeyBundle' import { newWallet } from './helpers' import Long from 'long' -import Ciphertext from '../src/crypto/Ciphertext' -import { NoMatchingPreKeyError } from '../src/crypto/errors' +import Ciphertext from '@/crypto/Ciphertext' +import { NoMatchingPreKeyError } from '@/crypto/errors' const createInvitation = (): InvitationV1 => { return new InvitationV1({ @@ -80,7 +80,7 @@ describe('Invitations', () => { ).rejects.toThrow(NoMatchingPreKeyError) expect(() => { - const sealedInvite = new SealedInvitation({ + const _sealedInvite = new SealedInvitation({ v1: { headerBytes: Uint8Array.from([123]), ciphertext: undefined }, }) }).toThrow() @@ -103,7 +103,7 @@ describe('Invitations', () => { }) const invite = new SealedInvitationV1({ headerBytes: header.toBytes(), - ciphertext: ciphertext, + ciphertext, }) expect( diff --git a/test/Keygen.test.ts b/test/Keygen.test.ts index 379730379..807d3c191 100644 --- a/test/Keygen.test.ts +++ b/test/Keygen.test.ts @@ -1,13 +1,11 @@ -import { decodePrivateKeyBundle } from './../src/crypto/PrivateKeyBundle' -import { ApiUrls } from './../src/ApiClient' -import { newWallet, sleep } from './helpers' -import Client, { defaultOptions } from '../src/Client' -import { Signer } from '../src/types/Signer' -import ApiClient from '../src/ApiClient' -import { PublicKeyBundle } from '../src/crypto/PublicKeyBundle' -import { KeyGeneratorKeystoreProvider } from '../src/keystore/providers' -import NetworkKeyManager from '../src/keystore/providers/NetworkKeyManager' -import TopicPersistence from '../src/keystore/persistence/TopicPersistence' +import { decodePrivateKeyBundle } from '@/crypto/PrivateKeyBundle' +import ApiClient, { ApiUrls } from '@/ApiClient' +import { newWallet } from './helpers' +import Client, { defaultOptions } from '@/Client' +import type { Signer } from '@/types/Signer' +import type { PublicKeyBundle } from '@/crypto/PublicKeyBundle' +import NetworkKeyManager from '@/keystore/providers/NetworkKeyManager' +import TopicPersistence from '@/keystore/persistence/TopicPersistence' describe('Key Generation', () => { let wallet: Signer @@ -39,7 +37,7 @@ describe('Key Generation', () => { const keys = await Client.getKeys(wallet, opts) const manager = new NetworkKeyManager( wallet, - new TopicPersistence(new ApiClient(ApiUrls['local'])) + new TopicPersistence(new ApiClient(ApiUrls.local)) ) expect( diff --git a/test/Message.test.ts b/test/Message.test.ts index 1f9617624..4f61c858a 100644 --- a/test/Message.test.ts +++ b/test/Message.test.ts @@ -1,16 +1,19 @@ -import { ConversationV1 } from './../src/conversations/Conversation' +import { ConversationV1 } from '@/conversations/Conversation' import { newWallet } from './helpers' -import { MessageV1, DecodedMessage } from '../src/Message' -import { PrivateKeyBundleV1 } from '../src/crypto/PrivateKeyBundle' -import { bytesToHex, equalBytes } from '../src/crypto/utils' -import { sha256 } from '../src/crypto/encryption' -import { InMemoryKeystore, KeystoreError } from '../src/keystore' -import { Client, ContentTypeText, InMemoryPersistence } from '../src' -import { Wallet } from 'ethers' +import { MessageV1, DecodedMessage } from '@/Message' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import { bytesToHex, equalBytes } from '@/crypto/utils' +import { sha256 } from '@/crypto/encryption' +import InMemoryKeystore from '@/keystore/InMemoryKeystore' +import type { Wallet } from 'ethers' import { ContentTypeTestKey, TestKeyCodec } from './ContentTypeTestKey' import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts' import { createWalletClient, http } from 'viem' import { mainnet } from 'viem/chains' +import { KeystoreError } from '@/keystore/errors' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import Client from '@/Client' +import { ContentTypeText } from '@/codecs/Text' describe('Message', function () { let aliceWallet: Wallet @@ -126,7 +129,7 @@ describe('Message', function () { ) expect(() => { - msg.recipientAddress + const _ = msg.recipientAddress }).toThrow('key is not signed') }) diff --git a/test/MessageContent.test.ts b/test/MessageContent.test.ts index 7c59abada..d3b17168c 100644 --- a/test/MessageContent.test.ts +++ b/test/MessageContent.test.ts @@ -1,4 +1,4 @@ -import { ContentTypeId } from '../src/MessageContent' +import { ContentTypeId } from '@/MessageContent' describe('ContentTypeId', () => { it('creates a new content type', () => { diff --git a/test/authn/Authn.test.ts b/test/authn/Authn.test.ts index c720744e8..fc90dad1d 100644 --- a/test/authn/Authn.test.ts +++ b/test/authn/Authn.test.ts @@ -1,11 +1,13 @@ import Long from 'long' -import { PrivateKey, PrivateKeyBundleV1, Signature } from '../../src/crypto' -import Authenticator from '../../src/authn/LocalAuthenticator' -import Token from '../../src/authn/Token' -import { newWallet, sleep } from '../helpers' -import { Wallet } from 'ethers' -import AuthCache from '../../src/authn/AuthCache' +import Authenticator from '@/authn/LocalAuthenticator' +import Token from '@/authn/Token' +import { newWallet, sleep } from '@test/helpers' +import type { Wallet } from 'ethers' +import AuthCache from '@/authn/AuthCache' import { hexToBytes, keccak256 } from 'viem' +import { PrivateKey } from '@/crypto/PrivateKey' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import Signature from '@/crypto/Signature' describe('authn', () => { let authenticator: Authenticator diff --git a/test/codecs/Composite.test.ts b/test/codecs/Composite.test.ts index 52f663471..0901f69b7 100644 --- a/test/codecs/Composite.test.ts +++ b/test/codecs/Composite.test.ts @@ -1,9 +1,6 @@ -import { ContentTypeText } from '../../src' -import { - CompositeCodec, - ContentTypeComposite, -} from '../../src/codecs/Composite' -import { CodecRegistry } from '../helpers' +import { CompositeCodec, ContentTypeComposite } from '@/codecs/Composite' +import { CodecRegistry } from '@test/helpers' +import { ContentTypeText } from '@/codecs/Text' describe('CompositeType', () => { const codecs = new CodecRegistry() diff --git a/test/codecs/Text.test.ts b/test/codecs/Text.test.ts index a61c0aeaf..cca1fd80d 100644 --- a/test/codecs/Text.test.ts +++ b/test/codecs/Text.test.ts @@ -1,5 +1,5 @@ -import { CodecRegistry } from '../helpers' -import { ContentTypeText, Encoding } from '../../src/codecs/Text' +import { CodecRegistry } from '@test/helpers' +import { ContentTypeText, Encoding } from '@/codecs/Text' describe('ContentTypeText', () => { const codecs = new CodecRegistry() diff --git a/test/conversations/Conversation.test.ts b/test/conversations/Conversation.test.ts index 1b70d5dcc..7101301ff 100644 --- a/test/conversations/Conversation.test.ts +++ b/test/conversations/Conversation.test.ts @@ -1,14 +1,19 @@ -import { DecodedMessage, MessageV1, MessageV2 } from './../../src/Message' -import { buildDirectMessageTopic } from './../../src/utils' -import { Client, Compression, ContentTypeId, ContentTypeText } from '../../src' -import { SortDirection } from '../../src/ApiClient' -import { sleep } from '../../src/utils' -import { newLocalHostClient, waitForUserContact } from '../helpers' -import { PrivateKey, SignedPublicKeyBundle } from '../../src/crypto' -import { ConversationV2 } from '../../src/conversations/Conversation' -import { ContentTypeTestKey, TestKeyCodec } from '../ContentTypeTestKey' +import type { MessageV2 } from '@/Message' +import { DecodedMessage, MessageV1 } from '@/Message' +import { buildDirectMessageTopic } from '@/utils/topic' +import { SortDirection } from '@/ApiClient' +import { sleep } from '@/utils/async' +import { newLocalHostClient, waitForUserContact } from '@test/helpers' +import { ConversationV2 } from '@/conversations/Conversation' import { content as proto } from '@xmtp/proto' import { assert, vi } from 'vitest' +import type Client from '@/Client' +import { ContentTypeText } from '@/codecs/Text' +import { Compression } from '@/Client' +import { ContentTypeTestKey, TestKeyCodec } from '@test/ContentTypeTestKey' +import { ContentTypeId } from '@/MessageContent' +import { PrivateKey } from '@/crypto/PrivateKey' +import { SignedPublicKeyBundle } from '@/crypto/PublicKeyBundle' describe('conversation', () => { let alice: Client @@ -81,7 +86,7 @@ describe('conversation', () => { expect(messageIds.size).toBe(10) // Test sorting - let lastMessage: DecodedMessage | undefined = undefined + let lastMessage: DecodedMessage | undefined for await (const page of aliceConversation.messagesPaginated({ direction: SortDirection.SORT_DIRECTION_DESCENDING, })) { @@ -188,7 +193,7 @@ describe('conversation', () => { await aliceConversation.send('hello', { ephemeral: true }) await sleep(100) - let result = await stream.next() + const result = await stream.next() const message = result.value expect(message.error).toBeUndefined() @@ -224,7 +229,7 @@ describe('conversation', () => { await aliceConversation.send('hello', { ephemeral: true }) await sleep(100) - let result = await stream.next() + const result = await stream.next() const message = result.value expect(message.error).toBeUndefined() @@ -293,7 +298,7 @@ describe('conversation', () => { await aliceConversation.send('gm to you too') } - let result = await stream.next() + const result = await stream.next() expect(result.done).toBeTruthy() await sleep(100) @@ -444,7 +449,7 @@ describe('conversation', () => { // alice doesn't recognize the type await expect( - // @ts-expect-error + // @ts-expect-error default client doesn't have the right type aliceConvo.send(key, { contentType: ContentTypeTestKey, }) @@ -452,7 +457,7 @@ describe('conversation', () => { // bob doesn't recognize the type alice.registerCodec(new TestKeyCodec()) - // @ts-expect-error + // @ts-expect-error default client doesn't have the right type await aliceConvo.send(key, { contentType: ContentTypeTestKey, }) @@ -474,7 +479,7 @@ describe('conversation', () => { // both recognize the type bob.registerCodec(new TestKeyCodec()) - // @ts-expect-error + // @ts-expect-error default client doesn't have the right type await aliceConvo.send(key, { contentType: ContentTypeTestKey, }) @@ -489,7 +494,7 @@ describe('conversation', () => { ...ContentTypeTestKey, versionMajor: 2, }) - // @ts-expect-error + // @ts-expect-error default client doesn't have the right type expect(aliceConvo.send(key, { contentType: type2 })).rejects.toThrow( 'unknown content type xmtp.test/public-key:2.0' ) diff --git a/test/conversations/Conversations.test.ts b/test/conversations/Conversations.test.ts index 666426f85..614c0ea5d 100644 --- a/test/conversations/Conversations.test.ts +++ b/test/conversations/Conversations.test.ts @@ -1,14 +1,8 @@ -import { - ConversationV1, - ConversationV2, -} from './../../src/conversations/Conversation' -import { newLocalHostClient } from './../helpers' -import { Client } from '../../src' -import { - buildDirectMessageTopic, - buildUserIntroTopic, - sleep, -} from '../../src/utils' +import { ConversationV1, ConversationV2 } from '@/conversations/Conversation' +import { newLocalHostClient } from '@test/helpers' +import { buildDirectMessageTopic, buildUserIntroTopic } from '@/utils/topic' +import { sleep } from '@/utils/async' +import type Client from '@/Client' describe('conversations', () => { describe('listConversations', () => { @@ -298,6 +292,7 @@ describe.sequential('Conversation streams', () => { await conversation.send('hi bob') let numConversations = 0 + // eslint-disable-next-line no-unreachable-loop for await (const conversation of stream) { numConversations++ expect(conversation.peerAddress).toBe(bob.address) diff --git a/test/conversations/JobRunner.test.ts b/test/conversations/JobRunner.test.ts index 20deef2a4..6e3d51924 100644 --- a/test/conversations/JobRunner.test.ts +++ b/test/conversations/JobRunner.test.ts @@ -1,13 +1,11 @@ -import { - InMemoryKeystore, - InMemoryPersistence, - KeystoreInterface, - PrivateKeyBundleV1, - nsToDate, -} from '../../src' import { keystore as keystoreProto } from '@xmtp/proto' -import JobRunner from '../../src/conversations/JobRunner' -import { newWallet, sleep } from '../helpers' +import JobRunner from '@/conversations/JobRunner' +import { newWallet, sleep } from '@test/helpers' +import type { KeystoreInterface } from '@/keystore/rpcDefinitions' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import InMemoryKeystore from '@/keystore/InMemoryKeystore' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import { nsToDate } from '@/utils/date' describe('JobRunner', () => { let keystore: KeystoreInterface @@ -58,7 +56,7 @@ describe('JobRunner', () => { }) it('fails with an invalid job type', async () => { - // @ts-ignore-next-line + // @ts-expect-error test case const v3Runner = new JobRunner('v3', keystore) expect(v3Runner.run(async () => {})).rejects.toThrow('unknown job type: v3') }) diff --git a/test/crypto/PrivateKeyBundle.test.ts b/test/crypto/PrivateKeyBundle.test.ts index dcf4cab67..35c9a6005 100644 --- a/test/crypto/PrivateKeyBundle.test.ts +++ b/test/crypto/PrivateKeyBundle.test.ts @@ -1,13 +1,13 @@ +import { newWallet } from '@test/helpers' +import { storageSigRequestText } from '@/keystore/providers/NetworkKeyManager' +import { hexToBytes } from 'viem' import { - decodePrivateKeyBundle, - PrivateKey, PrivateKeyBundleV1, PrivateKeyBundleV2, - SignedPublicKeyBundle, -} from '../../src/crypto' -import { newWallet } from '../helpers' -import { storageSigRequestText } from '../../src/keystore/providers/NetworkKeyManager' -import { hexToBytes } from 'viem' + decodePrivateKeyBundle, +} from '@/crypto/PrivateKeyBundle' +import { PrivateKey } from '@/crypto/PrivateKey' +import { SignedPublicKeyBundle } from '@/crypto/PublicKeyBundle' describe('Crypto', function () { describe('PrivateKeyBundle', function () { @@ -35,7 +35,7 @@ describe('Crypto', function () { ) expect(pri.secp256k1).toBeTruthy() const wallet = newWallet() - const bundle = await PrivateKeyBundleV1.generate(wallet) + const _bundle = await PrivateKeyBundleV1.generate(wallet) const preKey = hexToBytes( '0xf51bd1da9ec2239723ae2cf6a9f8d0ac37546b27e634002c653d23bacfcc67ad' ) diff --git a/test/crypto/PublicKey.test.ts b/test/crypto/PublicKey.test.ts index c91d28c0a..4797bf182 100644 --- a/test/crypto/PublicKey.test.ts +++ b/test/crypto/PublicKey.test.ts @@ -1,16 +1,11 @@ import Long from 'long' -import { - PrivateKey, - PublicKey, - SignedPublicKey, - SignedPrivateKey, - WalletSigner, - Signature, -} from '../../src/crypto' import { Wallet } from 'ethers' -import { equalBytes } from '../../src/crypto/utils' -import { newWallet } from '../helpers' +import { equalBytes } from '@/crypto/utils' +import { newWallet } from '@test/helpers' import { hexToBytes } from 'viem' +import Signature, { WalletSigner } from '@/crypto/Signature' +import { PrivateKey, SignedPrivateKey } from '@/crypto/PrivateKey' +import { PublicKey, SignedPublicKey } from '@/crypto/PublicKey' describe('Crypto', function () { describe('Signed Keys', function () { diff --git a/test/crypto/Signature.test.ts b/test/crypto/Signature.test.ts index f52b46d85..c8c0793fe 100644 --- a/test/crypto/Signature.test.ts +++ b/test/crypto/Signature.test.ts @@ -1,5 +1,6 @@ -import { PrivateKeyBundleV1, Signature } from '../../src/crypto' -import { newWallet } from '../helpers' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import Signature from '@/crypto/Signature' +import { newWallet } from '@test/helpers' describe('Crypto', function () { describe('Signature', function () { diff --git a/test/crypto/SignedEciesCiphertext.test.ts b/test/crypto/SignedEciesCiphertext.test.ts index f871c6719..fbf17facb 100644 --- a/test/crypto/SignedEciesCiphertext.test.ts +++ b/test/crypto/SignedEciesCiphertext.test.ts @@ -1,8 +1,9 @@ -import { PrivateKeyBundleV1, SignedEciesCiphertext } from '../../src/crypto' -import { newWallet } from '../helpers' -import { encrypt, getPublic } from '../../src/crypto/ecies' -import { equalBytes } from '../../src/crypto/utils' -import crypto from '../../src/crypto/crypto' +import { newWallet } from '@test/helpers' +import { encrypt, getPublic } from '@/crypto/ecies' +import { equalBytes } from '@/crypto/utils' +import crypto from '@/crypto/crypto' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import SignedEciesCiphertext from '@/crypto/SignedEciesCiphertext' describe('SignedEciesCiphertext', () => { let bundle: PrivateKeyBundleV1 diff --git a/test/crypto/encryption.test.ts b/test/crypto/encryption.test.ts index 8c114deff..9caf3519e 100644 --- a/test/crypto/encryption.test.ts +++ b/test/crypto/encryption.test.ts @@ -4,8 +4,8 @@ import { hkdfHmacKey, verifyHmacSignature, generateHmacSignature, -} from '../../src/crypto/encryption' -import crypto from '../../src/crypto/crypto' +} from '@/crypto/encryption' +import crypto from '@/crypto/crypto' describe('HMAC encryption', () => { it('generates and validates HMAC', async () => { diff --git a/test/crypto/index.test.ts b/test/crypto/index.test.ts index 01ac60f89..450ddab0b 100644 --- a/test/crypto/index.test.ts +++ b/test/crypto/index.test.ts @@ -1,11 +1,8 @@ -import crypto from '../../src/crypto/crypto' -import { - PublicKeyBundle, - PrivateKeyBundleV1, - PrivateKey, - encrypt, - decrypt, -} from '../../src/crypto' +import { PrivateKey } from '@/crypto/PrivateKey' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import { PublicKeyBundle } from '@/crypto/PublicKeyBundle' +import crypto from '@/crypto/crypto' +import { decrypt, encrypt } from '@/crypto/encryption' import { assert } from 'vitest' describe('Crypto', function () { diff --git a/test/helpers.ts b/test/helpers.ts index 9c7c2cef5..7b4f10ce1 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -1,13 +1,19 @@ -import { dateToNs, toNanoString } from './../src/utils' +import { dateToNs, toNanoString } from '@/utils/date' import { Wallet } from 'ethers' -import type { ContentCodec, ContentTypeId, ClientOptions } from '../src' -import { PrivateKey, TextCodec, Client } from '../src' -import type { Signer } from '../src/types/Signer' -import type Stream from '../src/Stream' -import { promiseWithTimeout } from '../src/utils' -import type { PublicKeyBundle, SignedPublicKeyBundle } from '../src/crypto' +import type { Signer } from '@/types/Signer' +import type Stream from '@/Stream' +import { promiseWithTimeout } from '@/utils/async' import type { messageApi } from '@xmtp/proto' import { fetcher } from '@xmtp/proto' +import type { ClientOptions } from '@/Client' +import Client from '@/Client' +import type { ContentCodec, ContentTypeId } from '@/MessageContent' +import { TextCodec } from '@/codecs/Text' +import type { + PublicKeyBundle, + SignedPublicKeyBundle, +} from '@/crypto/PublicKeyBundle' +import { PrivateKey } from '@/crypto/PrivateKey' const { b64Encode } = fetcher diff --git a/test/keystore/InMemoryKeystore.test.ts b/test/keystore/InMemoryKeystore.test.ts index c1a03736a..cfe45c109 100644 --- a/test/keystore/InMemoryKeystore.test.ts +++ b/test/keystore/InMemoryKeystore.test.ts @@ -1,32 +1,32 @@ import { keystore, privateKey } from '@xmtp/proto' -import { randomBytes } from './../../bench/helpers' -import { InvitationContext } from './../../src/Invitation' -import { MessageV1 } from './../../src/Message' -import { - PrivateKeyBundleV1, - SignedPublicKeyBundle, - PrivateKeyBundleV2, -} from '../../src/crypto' -import { decryptV1 } from '../../src/keystore/encryption' -import { KeystoreError } from '../../src/keystore/errors' -import InMemoryKeystore from '../../src/keystore/InMemoryKeystore' -import { equalBytes } from '../../src/crypto/utils' -import { InvitationV1, SealedInvitation } from '../../src/Invitation' -import { buildProtoEnvelope, newWallet } from '../helpers' -import { dateToNs, nsToDate } from '../../src/utils/date' -import { InMemoryPersistence } from '../../src/keystore/persistence' -import Token from '../../src/authn/Token' +import { randomBytes } from '@bench/helpers' +import { InvitationV1, SealedInvitation } from '@/Invitation' +import type { InvitationContext } from '@/Invitation' +import { MessageV1 } from '@/Message' +import { decryptV1 } from '@/keystore/encryption' +import { KeystoreError } from '@/keystore/errors' +import InMemoryKeystore from '@/keystore/InMemoryKeystore' +import { equalBytes } from '@/crypto/utils' +import { buildProtoEnvelope, newWallet } from '@test/helpers' +import { dateToNs, nsToDate } from '@/utils/date' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import Token from '@/authn/Token' import Long from 'long' -import { CreateInviteResponse } from '@xmtp/proto/ts/dist/types/keystore_api/v1/keystore.pb' +import type { CreateInviteResponse } from '@xmtp/proto/ts/dist/types/keystore_api/v1/keystore.pb' import { assert } from 'vitest' import { toBytes } from 'viem' -import { getKeyMaterial } from '../../src/keystore/utils' +import { + PrivateKeyBundleV1, + PrivateKeyBundleV2, +} from '@/crypto/PrivateKeyBundle' +import { SignedPublicKeyBundle } from '@/crypto/PublicKeyBundle' +import { getKeyMaterial } from '@/keystore/utils' import { generateHmacSignature, hkdfHmacKey, importHmacKey, verifyHmacSignature, -} from '../../src/crypto/encryption' +} from '@/crypto/encryption' describe('InMemoryKeystore', () => { let aliceKeys: PrivateKeyBundleV1 @@ -110,7 +110,7 @@ describe('InMemoryKeystore', () => { }, ] - // @ts-expect-error + // @ts-expect-error test case const res = await aliceKeystore.encryptV1({ requests }) expect(res.responses).toHaveLength(requests.length) @@ -519,7 +519,7 @@ describe('InMemoryKeystore', () => { await Promise.all( shuffled.map(async (createdAt) => { - let keys = await PrivateKeyBundleV1.generate(newWallet()) + const keys = await PrivateKeyBundleV1.generate(newWallet()) const recipient = SignedPublicKeyBundle.fromLegacyBundle( keys.getPublicKeyBundle() @@ -573,10 +573,11 @@ describe('InMemoryKeystore', () => { const firstResponse: CreateInviteResponse = responses[0] const topicName = firstResponse.conversation!.topic + // eslint-disable-next-line no-control-regex expect(topicName).toMatch(/^[\x00-\x7F]+$/) expect( - responses.filter((response, index, array) => { + responses.filter((response) => { return response.conversation!.topic === topicName }) ).toHaveLength(25) @@ -694,7 +695,7 @@ describe('InMemoryKeystore', () => { const topicName = firstResponse.conversation!.topic expect( - responses.filter((response, index, array) => { + responses.filter((response) => { return response.conversation!.topic === topicName }) ).toHaveLength(25) @@ -877,7 +878,7 @@ describe('InMemoryKeystore', () => { const invites = await Promise.all( [...timestamps].map(async (createdAt) => { - let keys = await PrivateKeyBundleV1.generate(newWallet()) + const keys = await PrivateKeyBundleV1.generate(newWallet()) const recipient = SignedPublicKeyBundle.fromLegacyBundle( keys.getPublicKeyBundle() @@ -960,7 +961,7 @@ describe('InMemoryKeystore', () => { topicHmacs[topic], headerBytes ) - expect(valid).toBe(idx === 1 ? true : false) + expect(valid).toBe(idx === 1) } ) ) @@ -977,7 +978,7 @@ describe('InMemoryKeystore', () => { const invites = await Promise.all( [...timestamps].map(async (createdAt) => { - let keys = await PrivateKeyBundleV1.generate(newWallet()) + const keys = await PrivateKeyBundleV1.generate(newWallet()) const recipient = SignedPublicKeyBundle.fromLegacyBundle( keys.getPublicKeyBundle() @@ -1065,7 +1066,7 @@ describe('InMemoryKeystore', () => { topicHmacs[topic], headerBytes ) - expect(valid).toBe(idx === 1 ? true : false) + expect(valid).toBe(idx === 1) } ) ) diff --git a/test/keystore/conversationStores.test.ts b/test/keystore/conversationStores.test.ts index 37124f5a5..815a0e336 100644 --- a/test/keystore/conversationStores.test.ts +++ b/test/keystore/conversationStores.test.ts @@ -1,10 +1,8 @@ -import crypto from '../../src/crypto/crypto' -import { V2Store } from '../../src/keystore' -import { AddRequest, V1Store } from '../../src/keystore/conversationStores' -import { InMemoryPersistence } from '../../src/keystore/persistence' -import { dateToNs } from '../../src/utils' - -const INVITE_KEY = 'invitations/v1' +import crypto from '@/crypto/crypto' +import type { AddRequest } from '@/keystore/conversationStores' +import { V2Store, V1Store } from '@/keystore/conversationStores' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import { dateToNs } from '@/utils/date' const buildAddRequest = (): AddRequest => { const topic = crypto.getRandomValues(new Uint8Array(32)).toString() @@ -31,9 +29,9 @@ describe('V2Store', () => { const addRequest = buildAddRequest() await store.add([addRequest]) - const result = store.lookup(addRequest.topic) - expect(result).not.toBeNull() const { topic, ...topicData } = addRequest + const result = store.lookup(topic) + expect(result).not.toBeNull() expect(result).toEqual(topicData) }) diff --git a/test/keystore/encryption.test.ts b/test/keystore/encryption.test.ts index 9b0681042..5e848d435 100644 --- a/test/keystore/encryption.test.ts +++ b/test/keystore/encryption.test.ts @@ -1,12 +1,13 @@ -import { Ciphertext } from '../../src/crypto' -import { PrivateKeyBundleV1 } from './../../src/crypto/PrivateKeyBundle' -import { decryptV1, encryptV1 } from '../../src/keystore/encryption' -import { MessageV1 } from '../../src/Message' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import { decryptV1, encryptV1 } from '@/keystore/encryption' +import { MessageV1 } from '@/Message' import { Wallet } from 'ethers' -import { equalBytes } from '../../src/crypto/utils' -import { newWallet } from '../helpers' -import { InMemoryKeystore } from '../../src/keystore' -import { InMemoryPersistence, KeystoreInterface } from '../../src' +import { equalBytes } from '@/crypto/utils' +import { newWallet } from '@test/helpers' +import InMemoryKeystore from '@/keystore/InMemoryKeystore' +import type { KeystoreInterface } from '@/keystore/rpcDefinitions' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import Ciphertext from '@/crypto/Ciphertext' describe('encryption primitives', () => { let aliceKeys: PrivateKeyBundleV1 diff --git a/test/keystore/persistence/EncryptedPersistence.test.ts b/test/keystore/persistence/EncryptedPersistence.test.ts index bb6ac8c80..1886fc000 100644 --- a/test/keystore/persistence/EncryptedPersistence.test.ts +++ b/test/keystore/persistence/EncryptedPersistence.test.ts @@ -1,10 +1,9 @@ -import crypto from '../../../src/crypto/crypto' -import { PrivateKeyBundleV1 } from './../../../src/crypto/PrivateKeyBundle' -import { - EncryptedPersistence, - InMemoryPersistence, -} from '../../../src/keystore/persistence' -import { PrivateKey, SignedEciesCiphertext } from '../../../src/crypto' +import crypto from '@/crypto/crypto' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import EncryptedPersistence from '@/keystore/persistence/EncryptedPersistence' +import type { PrivateKey } from '@/crypto/PrivateKey' +import SignedEciesCiphertext from '@/crypto/SignedEciesCiphertext' const TEST_KEY = 'test-key' const TEST_KEY_2 = 'test-key-2' diff --git a/test/keystore/persistence/LocalStoragePersistence.test.ts b/test/keystore/persistence/LocalStoragePersistence.test.ts index 156bf69e5..9bb96108e 100644 --- a/test/keystore/persistence/LocalStoragePersistence.test.ts +++ b/test/keystore/persistence/LocalStoragePersistence.test.ts @@ -1,5 +1,8 @@ -import { InMemoryPersistence } from '../../../src/keystore/persistence' -import { decodePrivateKeyBundle, PrivateKeyBundleV1 } from '../../../src/crypto' +import { + PrivateKeyBundleV1, + decodePrivateKeyBundle, +} from '@/crypto/PrivateKeyBundle' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' describe('Persistence', () => { describe('LocalStoragePersistence', () => { diff --git a/test/keystore/persistence/PrefixedPersistence.test.ts b/test/keystore/persistence/PrefixedPersistence.test.ts index 6da7f22a8..a1a2964ad 100644 --- a/test/keystore/persistence/PrefixedPersistence.test.ts +++ b/test/keystore/persistence/PrefixedPersistence.test.ts @@ -1,7 +1,5 @@ -import { - InMemoryPersistence, - PrefixedPersistence, -} from '../../../src/keystore/persistence' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import PrefixedPersistence from '@/keystore/persistence/PrefixedPersistence' describe('PrefixedPersistence', () => { it('correctly adds a prefix to keys', async () => { diff --git a/test/keystore/persistence/TopicPersistence.test.ts b/test/keystore/persistence/TopicPersistence.test.ts index a1f3d0f04..5013a995c 100644 --- a/test/keystore/persistence/TopicPersistence.test.ts +++ b/test/keystore/persistence/TopicPersistence.test.ts @@ -1,8 +1,8 @@ -import ApiClient, { ApiUrls } from '../../../src/ApiClient' -import { newWallet } from '../../helpers' -import TopicPersistence from '../../../src/keystore/persistence/TopicPersistence' -import { LocalAuthenticator } from '../../../src/authn' -import { PrivateKeyBundleV1 } from '../../../src/crypto' +import ApiClient, { ApiUrls } from '@/ApiClient' +import { newWallet } from '@test/helpers' +import TopicPersistence from '@/keystore/persistence/TopicPersistence' +import LocalAuthenticator from '@/authn/LocalAuthenticator' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' // We restrict publishing to topics that do not match this pattern const buildValidKey = (walletAddress: string) => `${walletAddress}/key_bundle` @@ -11,7 +11,7 @@ describe('TopicPersistence', () => { let apiClient: ApiClient let bundle: PrivateKeyBundleV1 beforeEach(async () => { - apiClient = new ApiClient(ApiUrls['local']) + apiClient = new ApiClient(ApiUrls.local) bundle = await PrivateKeyBundleV1.generate(newWallet()) }) it('round trips items from the store', async () => { diff --git a/test/keystore/providers/KeyGeneratorKeystoreProvider.test.ts b/test/keystore/providers/KeyGeneratorKeystoreProvider.test.ts index 04d43b953..7ee61a4bc 100644 --- a/test/keystore/providers/KeyGeneratorKeystoreProvider.test.ts +++ b/test/keystore/providers/KeyGeneratorKeystoreProvider.test.ts @@ -1,19 +1,17 @@ -import ApiClient, { ApiUrls } from '../../../src/ApiClient' -import { - KeyGeneratorKeystoreProvider, - KeystoreProviderUnavailableError, -} from '../../../src/keystore/providers' -import { Signer } from '../../../src/types/Signer' -import { newWallet } from '../../helpers' +import KeyGeneratorKeystoreProvider from '@/keystore/providers/KeyGeneratorKeystoreProvider' +import ApiClient, { ApiUrls } from '@/ApiClient' +import type { Signer } from '@/types/Signer' +import { newWallet } from '@test/helpers' import { testProviderOptions } from './helpers' import { vi } from 'vitest' +import { KeystoreProviderUnavailableError } from '@/keystore/providers/errors' describe('KeyGeneratorKeystoreProvider', () => { let wallet: Signer let apiClient: ApiClient beforeEach(() => { wallet = newWallet() - apiClient = new ApiClient(ApiUrls['local']) + apiClient = new ApiClient(ApiUrls.local) }) it('creates a key when wallet supplied', async () => { diff --git a/test/keystore/providers/NetworkKeyManager.test.ts b/test/keystore/providers/NetworkKeyManager.test.ts index dcb606403..880f55441 100644 --- a/test/keystore/providers/NetworkKeyManager.test.ts +++ b/test/keystore/providers/NetworkKeyManager.test.ts @@ -1,13 +1,14 @@ -import { BrowserStoragePersistence, PrefixedPersistence } from '../../../src' -import ApiClient, { ApiUrls } from '../../../src/ApiClient' -import { PrivateKeyBundleV1 } from '../../../src/crypto/PrivateKeyBundle' -import TopicPersistence from '../../../src/keystore/persistence/TopicPersistence' -import { buildPersistenceFromOptions } from '../../../src/keystore/providers/helpers' -import NetworkKeyManager from '../../../src/keystore/providers/NetworkKeyManager' -import { Signer } from '../../../src/types/Signer' -import { newWallet, pollFor, sleep, wrapAsLedgerWallet } from '../../helpers' +import ApiClient, { ApiUrls } from '@/ApiClient' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import TopicPersistence from '@/keystore/persistence/TopicPersistence' +import { buildPersistenceFromOptions } from '@/keystore/providers/helpers' +import NetworkKeyManager from '@/keystore/providers/NetworkKeyManager' +import type { Signer } from '@/types/Signer' +import { newWallet, pollFor, sleep, wrapAsLedgerWallet } from '@test/helpers' import { testProviderOptions } from './helpers' import { vi } from 'vitest' +import BrowserStoragePersistence from '@/keystore/persistence/BrowserStoragePersistence' +import PrefixedPersistence from '@/keystore/persistence/PrefixedPersistence' describe('NetworkKeyManager', () => { let wallet: Signer @@ -15,7 +16,7 @@ describe('NetworkKeyManager', () => { beforeEach(async () => { wallet = newWallet() - persistence = new TopicPersistence(new ApiClient(ApiUrls['local'])) + persistence = new TopicPersistence(new ApiClient(ApiUrls.local)) }) it('round trips', async () => { diff --git a/test/keystore/providers/NetworkKeystoreProvider.test.ts b/test/keystore/providers/NetworkKeystoreProvider.test.ts index ba2449fab..b0891e871 100644 --- a/test/keystore/providers/NetworkKeystoreProvider.test.ts +++ b/test/keystore/providers/NetworkKeystoreProvider.test.ts @@ -1,19 +1,21 @@ import { privateKey } from '@xmtp/proto' -import { KeystoreProviderUnavailableError } from './../../../src/keystore/providers/errors' -import ApiClient, { ApiUrls } from '../../../src/ApiClient' -import { encrypt, PrivateKeyBundleV1 } from '../../../src/crypto' -import NetworkKeystoreProvider from '../../../src/keystore/providers/NetworkKeystoreProvider' -import { Signer } from '../../../src/types/Signer' -import { newWallet } from '../../helpers' +import { KeystoreProviderUnavailableError } from '@/keystore/providers/errors' +import ApiClient, { ApiUrls } from '@/ApiClient' +import NetworkKeystoreProvider from '@/keystore/providers/NetworkKeystoreProvider' +import type { Signer } from '@/types/Signer' +import { newWallet } from '@test/helpers' import { testProviderOptions } from './helpers' import NetworkKeyManager, { storageSigRequestText, -} from '../../../src/keystore/providers/NetworkKeyManager' -import TopicPersistence from '../../../src/keystore/persistence/TopicPersistence' -import { LocalAuthenticator } from '../../../src/authn' -import crypto from '../../../src/crypto/crypto' +} from '@/keystore/providers/NetworkKeyManager' +import TopicPersistence from '@/keystore/persistence/TopicPersistence' +import LocalAuthenticator from '@/authn/LocalAuthenticator' +import crypto from '@/crypto/crypto' import { vi } from 'vitest' -import { Hex, hexToBytes } from 'viem' +import type { Hex } from 'viem' +import { hexToBytes } from 'viem' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' +import { encrypt } from '@/crypto/encryption' describe('NetworkKeystoreProvider', () => { let apiClient: ApiClient @@ -21,7 +23,7 @@ describe('NetworkKeystoreProvider', () => { let wallet: Signer beforeEach(async () => { - apiClient = new ApiClient(ApiUrls['local']) + apiClient = new ApiClient(ApiUrls.local) wallet = newWallet() bundle = await PrivateKeyBundleV1.generate(wallet) }) @@ -58,7 +60,7 @@ describe('NetworkKeystoreProvider', () => { const input = storageSigRequestText(wPreKey) const walletAddr = await wallet.getAddress() - let sig = await wallet.signMessage(input) + const sig = await wallet.signMessage(input) const secret = hexToBytes(sig as Hex) const ciphertext = await encrypt(bytes, secret) const bytesToStore = privateKey.EncryptedPrivateKeyBundleV1.encode({ diff --git a/test/keystore/providers/SnapProvider.test.ts b/test/keystore/providers/SnapProvider.test.ts index 69e8795ae..1e11c904d 100644 --- a/test/keystore/providers/SnapProvider.test.ts +++ b/test/keystore/providers/SnapProvider.test.ts @@ -1,22 +1,22 @@ -import HttpApiClient, { ApiClient, ApiUrls } from '../../../src/ApiClient' -import { - KeystoreProviderOptions, - KeystoreProviderUnavailableError, -} from '../../../src/keystore/providers' -import { newWallet } from '../../helpers' -import SnapKeystoreProvider from '../../../src/keystore/providers/SnapProvider' +import type { ApiClient } from '@/ApiClient' +import HttpApiClient, { ApiUrls } from '@/ApiClient' +import { newWallet } from '@test/helpers' +import SnapKeystoreProvider from '@/keystore/providers/SnapProvider' import { connectSnap, getSnap, getWalletStatus, hasMetamaskWithSnaps, initSnap, -} from '../../../src/keystore/snapHelpers' -import { Signer } from '../../../src/types/Signer' +} from '@/keystore/snapHelpers' +import type { Signer } from '@/types/Signer' import { keystore as keystoreProto } from '@xmtp/proto' -import { vi, Mock } from 'vitest' +import type { Mock } from 'vitest' +import { vi } from 'vitest' +import type { KeystoreProviderOptions } from '@/keystore/providers/interfaces' +import { KeystoreProviderUnavailableError } from '@/keystore/providers/errors' -vi.mock('../../../src/keystore/snapHelpers') +vi.mock('@/keystore/snapHelpers') describe('SnapProvider', () => { const provider = new SnapKeystoreProvider() @@ -25,7 +25,7 @@ describe('SnapProvider', () => { let wallet: Signer beforeEach(async () => { - apiClient = new HttpApiClient(ApiUrls['local']) + apiClient = new HttpApiClient(ApiUrls.local) wallet = newWallet() vi.resetAllMocks() }) diff --git a/test/keystore/providers/StaticKeystoreProvider.test.ts b/test/keystore/providers/StaticKeystoreProvider.test.ts index c46b06288..95178e006 100644 --- a/test/keystore/providers/StaticKeystoreProvider.test.ts +++ b/test/keystore/providers/StaticKeystoreProvider.test.ts @@ -1,9 +1,9 @@ import { privateKey } from '@xmtp/proto' -import { PrivateKeyBundleV1 } from '../../../src/crypto' -import { newWallet } from '../../helpers' +import { newWallet } from '@test/helpers' import { testProviderOptions } from './helpers' -import StaticKeystoreProvider from '../../../src/keystore/providers/StaticKeystoreProvider' -import { KeystoreProviderUnavailableError } from '../../../src/keystore/providers/errors' +import StaticKeystoreProvider from '@/keystore/providers/StaticKeystoreProvider' +import { KeystoreProviderUnavailableError } from '@/keystore/providers/errors' +import { PrivateKeyBundleV1 } from '@/crypto/PrivateKeyBundle' const ENV = 'local' diff --git a/test/keystore/providers/helpers.ts b/test/keystore/providers/helpers.ts index becc4f590..1049da397 100644 --- a/test/keystore/providers/helpers.ts +++ b/test/keystore/providers/helpers.ts @@ -1,5 +1,5 @@ -import { InMemoryPersistence } from '../../../src' -import type { KeystoreProviderOptions } from '../../../src/keystore/providers' +import InMemoryPersistence from '@/keystore/persistence/InMemoryPersistence' +import type { KeystoreProviderOptions } from '@/keystore/providers/interfaces' export const testProviderOptions = ({ privateKeyOverride = undefined, diff --git a/test/keystore/snapHelpers.test.ts b/test/keystore/snapHelpers.test.ts index 520d307c3..0ed9d66b5 100644 --- a/test/keystore/snapHelpers.test.ts +++ b/test/keystore/snapHelpers.test.ts @@ -1,10 +1,7 @@ -import { - getWalletStatus, - hasMetamaskWithSnaps, -} from '../../src/keystore/snapHelpers' +import { getWalletStatus, hasMetamaskWithSnaps } from '@/keystore/snapHelpers' import { keystore } from '@xmtp/proto' -import { b64Encode } from '../../src/utils' -import { SNAP_LOCAL_ORIGIN } from '../../src/keystore/providers/SnapProvider' +import { b64Encode } from '@/utils/bytes' +import { SNAP_LOCAL_ORIGIN } from '@/keystore/providers/SnapProvider' import { vi } from 'vitest' const { GetKeystoreStatusRequest, @@ -14,7 +11,7 @@ const { // Setup the mocks for window.ethereum const mockRequest = vi.hoisted(() => vi.fn()) -vi.mock('../../src/utils/ethereum', () => { +vi.mock('@/utils/ethereum', () => { return { __esModule: true, getEthereum: vi.fn(() => { diff --git a/test/utils/semver.test.ts b/test/utils/semver.test.ts index 9ae823ed7..d77bee603 100644 --- a/test/utils/semver.test.ts +++ b/test/utils/semver.test.ts @@ -1,4 +1,4 @@ -import { isSameMajorVersion, semverGreaterThan } from '../../src/utils/semver' +import { isSameMajorVersion, semverGreaterThan } from '@/utils/semver' describe('semver', () => { describe('isSameMajorVersion', () => { diff --git a/test/utils/topic.test.ts b/test/utils/topic.test.ts index 2baf3d6e0..10143f748 100644 --- a/test/utils/topic.test.ts +++ b/test/utils/topic.test.ts @@ -2,8 +2,8 @@ import { buildContentTopic, buildDirectMessageTopicV2, isValidTopic, -} from '../../src/utils/topic' -import crypto from '../../src/crypto/crypto' +} from '@/utils/topic' +import crypto from '@/crypto/crypto' describe('topic utils', () => { describe('isValidTopic', () => { diff --git a/tsconfig.json b/tsconfig.json index 9940dc80d..b77ca2a45 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,11 @@ "lib": ["dom"], "moduleResolution": "bundler", "noEmit": true, + "paths": { + "@/*": ["./src/*"], + "@bench/*": ["./bench/*"], + "@test/*": ["./test/*"] + }, "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, @@ -16,7 +21,6 @@ "include": [ "src/**/*", "bench/**/*", - "build/**/*", "test/**/*", ".eslintrc.cjs", "commitlint.config.cjs", diff --git a/vitest.config.ts b/vitest.config.ts index 8a4dcd91f..da31aa1d6 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,10 +1,19 @@ /// -import { defineConfig } from 'vitest/config' +import { defineConfig, mergeConfig } from 'vite' +import { defineConfig as defineVitestConfig } from 'vitest/config' +import tsconfigPaths from 'vite-tsconfig-paths' -export default defineConfig({ +// https://vitejs.dev/config/ +const viteConfig = defineConfig({ + plugins: [tsconfigPaths()], +}) + +const vitestConfig = defineVitestConfig({ test: { globals: true, testTimeout: 120000, hookTimeout: 60000, }, }) + +export default mergeConfig(viteConfig, vitestConfig) diff --git a/yarn.lock b/yarn.lock index 749031ee2..8c2b98a84 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2709,11 +2709,13 @@ __metadata: rollup: "npm:^4.12.1" rollup-plugin-dts: "npm:^6.1.0" rollup-plugin-filesize: "npm:^10.0.0" + rollup-plugin-tsconfig-paths: "npm:^1.5.2" semantic-release: "npm:^21.1.2" typedoc: "npm:^0.25.12" typescript: "npm:^5.4.2" viem: "npm:2.7.15" vite: "npm:5.1.4" + vite-tsconfig-paths: "npm:^4.3.1" vitest: "npm:^1.3.1" languageName: unknown linkType: soft @@ -5554,6 +5556,13 @@ __metadata: languageName: node linkType: hard +"globrex@npm:^0.1.2": + version: 0.1.2 + resolution: "globrex@npm:0.1.2" + checksum: 10/81ce62ee6f800d823d6b7da7687f841676d60ee8f51f934ddd862e4057316d26665c4edc0358d4340a923ac00a514f8b67c787e28fe693aae16350f4e60d55e9 + languageName: node + linkType: hard + "gopd@npm:^1.0.1": version: 1.0.1 resolution: "gopd@npm:1.0.1" @@ -8971,6 +8980,17 @@ __metadata: languageName: node linkType: hard +"rollup-plugin-tsconfig-paths@npm:^1.5.2": + version: 1.5.2 + resolution: "rollup-plugin-tsconfig-paths@npm:1.5.2" + dependencies: + typescript-paths: "npm:^1.5.1" + peerDependencies: + rollup: ^2 || ^3 || ^4 + checksum: 10/11e9cb8b096edc0dd17deba660213ec3047ea06790ff034984a3a6687acb0bd910a10ca825184f01a838a5c934021e8858e043c8668d0bf1fde389bf50240b78 + languageName: node + linkType: hard + "rollup@npm:^4.12.1": version: 4.12.1 resolution: "rollup@npm:4.12.1" @@ -10001,6 +10021,20 @@ __metadata: languageName: node linkType: hard +"tsconfck@npm:^3.0.1": + version: 3.0.3 + resolution: "tsconfck@npm:3.0.3" + peerDependencies: + typescript: ^5.0.0 + peerDependenciesMeta: + typescript: + optional: true + bin: + tsconfck: bin/tsconfck.js + checksum: 10/1c17217dc3758e71bebdb223b7cd6e613f8f8c92a225cccc40d459554dfae50cbf9d339c6a4a5a8d04620fe1c21bb6d454b6e10421e3fcd808ea51d0b5039ffd + languageName: node + linkType: hard + "tsconfig-paths@npm:^3.15.0": version: 3.15.0 resolution: "tsconfig-paths@npm:3.15.0" @@ -10178,6 +10212,15 @@ __metadata: languageName: node linkType: hard +"typescript-paths@npm:^1.5.1": + version: 1.5.1 + resolution: "typescript-paths@npm:1.5.1" + peerDependencies: + typescript: ^4.7.2 || ^5 + checksum: 10/755aecb40a1015152c31f1b3b8e587330aca05e8dfb7e4e48df28add152c9cc257a16df98c58f31724d9b210199f56feb2710db3e7da948ab6f607d32fb6a572 + languageName: node + linkType: hard + "typescript@npm:^5.3.3": version: 5.3.3 resolution: "typescript@npm:5.3.3" @@ -10417,6 +10460,22 @@ __metadata: languageName: node linkType: hard +"vite-tsconfig-paths@npm:^4.3.1": + version: 4.3.1 + resolution: "vite-tsconfig-paths@npm:4.3.1" + dependencies: + debug: "npm:^4.1.1" + globrex: "npm:^0.1.2" + tsconfck: "npm:^3.0.1" + peerDependencies: + vite: "*" + peerDependenciesMeta: + vite: + optional: true + checksum: 10/1432f80750f5cbe181c265eb9fc2e9fff8b25a2858f176dc0a02311e3e826333526ee9c16bb0aaaa8555a417ea944d68a2e8225181215cd9502370f913eb3f79 + languageName: node + linkType: hard + "vite@npm:5.1.4, vite@npm:^5.0.0": version: 5.1.4 resolution: "vite@npm:5.1.4"