diff --git a/src/crypto/selfEncryption.ts b/src/crypto/selfEncryption.ts new file mode 100644 index 000000000..f6a5cefa3 --- /dev/null +++ b/src/crypto/selfEncryption.ts @@ -0,0 +1,35 @@ +import { + // eslint-disable-next-line camelcase + generate_private_preferences_topic, + // eslint-disable-next-line camelcase + user_preferences_decrypt, + // eslint-disable-next-line camelcase + user_preferences_encrypt, +} from '@xmtp/user-preferences-bindings-wasm' +import { PrivateKey } from '../crypto' + +export async function userPreferencesEncrypt( + identityKey: PrivateKey, + payload: Uint8Array +) { + const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return user_preferences_encrypt(publicKey, privateKey, payload) +} + +export async function userPreferencesDecrypt( + identityKey: PrivateKey, + payload: Uint8Array +) { + const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return user_preferences_decrypt(publicKey, privateKey, payload) +} + +export async function generateUserPreferencesTopic(identityKey: PrivateKey) { + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return generate_private_preferences_topic(privateKey) +} diff --git a/src/crypto/selfEncryption.web.ts b/src/crypto/selfEncryption.web.ts new file mode 100644 index 000000000..5b2c6dac0 --- /dev/null +++ b/src/crypto/selfEncryption.web.ts @@ -0,0 +1,47 @@ +/*********************************************************************************************** + * DO NOT IMPORT THIS FILE DIRECTLY + ***********************************************************************************************/ + +import init, { + // eslint-disable-next-line camelcase + generate_private_preferences_topic, + // eslint-disable-next-line camelcase + user_preferences_decrypt, + // eslint-disable-next-line camelcase + user_preferences_encrypt, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore +} from '@xmtp/user-preferences-bindings-wasm/web' +import { PrivateKey } from './PrivateKey' + +export async function userPreferencesEncrypt( + identityKey: PrivateKey, + payload: Uint8Array +) { + // wait for WASM to be initialized + await init() + const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return user_preferences_encrypt(publicKey, privateKey, payload) +} + +export async function userPreferencesDecrypt( + identityKey: PrivateKey, + payload: Uint8Array +) { + // wait for WASM to be initialized + await init() + const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return user_preferences_decrypt(publicKey, privateKey, payload) +} + +export async function generateUserPreferencesTopic(identityKey: PrivateKey) { + // wait for WASM to be initialized + await init() + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return generate_private_preferences_topic(privateKey) +} diff --git a/src/keystore/InMemoryKeystore.ts b/src/keystore/InMemoryKeystore.ts index 8f46de982..39ee8f687 100644 --- a/src/keystore/InMemoryKeystore.ts +++ b/src/keystore/InMemoryKeystore.ts @@ -29,9 +29,11 @@ import { hmacSha256Sign } from '../crypto/ecies' import crypto from '../crypto/crypto' import { bytesToHex } from '../crypto/utils' import Long from 'long' -import { selfDecrypt, selfEncrypt } from '../keystore/encryption' -// eslint-disable-next-line camelcase -import { generate_private_preferences_topic } from '@xmtp/user-preferences-bindings-wasm' +import { + userPreferencesDecrypt, + userPreferencesEncrypt, + generateUserPreferencesTopic, +} from '../crypto/selfEncryption' const { ErrorCode } = keystore @@ -216,7 +218,10 @@ export default class InMemoryKeystore implements Keystore { } return { - encrypted: await selfEncrypt(this.v1Keys.identityKey, payload), + encrypted: await userPreferencesEncrypt( + this.v1Keys.identityKey, + payload + ), } }, ErrorCode.ERROR_CODE_INVALID_INPUT @@ -243,7 +248,10 @@ export default class InMemoryKeystore implements Keystore { } return { - decrypted: await selfDecrypt(this.v1Keys.identityKey, payload), + decrypted: await userPreferencesDecrypt( + this.v1Keys.identityKey, + payload + ), } }, ErrorCode.ERROR_CODE_INVALID_INPUT @@ -255,8 +263,9 @@ export default class InMemoryKeystore implements Keystore { } async getPrivatePreferencesTopicIdentifier(): Promise { - const privateKey = this.v1Keys.identityKey.secp256k1.bytes - const identifier = generate_private_preferences_topic(privateKey).toString() + const identifier = await generateUserPreferencesTopic( + this.v1Keys.identityKey + ) return keystore.GetPrivatePreferencesTopicIdentifierResponse.fromPartial({ identifier, }) diff --git a/src/keystore/encryption.ts b/src/keystore/encryption.ts index 3a6f58837..c0f6e5143 100644 --- a/src/keystore/encryption.ts +++ b/src/keystore/encryption.ts @@ -3,15 +3,8 @@ import { encrypt, PrivateKeyBundleV1, decrypt, - PrivateKey, } from '../crypto' import { ciphertext } from '@xmtp/proto' -import { - // eslint-disable-next-line camelcase - user_preferences_decrypt, - // eslint-disable-next-line camelcase - user_preferences_encrypt, -} from '@xmtp/user-preferences-bindings-wasm' export const decryptV1 = async ( myKeys: PrivateKeyBundleV1, @@ -55,21 +48,3 @@ export const encryptV2 = ( secret: Uint8Array, headerBytes: Uint8Array ) => encrypt(payload, secret, headerBytes) - -export async function selfEncrypt( - identityKey: PrivateKey, - payload: Uint8Array -) { - const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes - const privateKey = identityKey.secp256k1.bytes - return user_preferences_encrypt(publicKey, privateKey, payload) -} - -export async function selfDecrypt( - identityKey: PrivateKey, - payload: Uint8Array -) { - const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes - const privateKey = identityKey.secp256k1.bytes - return user_preferences_decrypt(publicKey, privateKey, payload) -}