From c8ddbe1908f7b3f59241a6ac9eae06517170b89d Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Tue, 5 Dec 2023 12:02:50 -0600 Subject: [PATCH 1/2] chore: remove initial self encryption code --- src/crypto/SelfEncryption.ts | 32 ---------------------------- test/crypto/P4Encryption.test.ts | 36 -------------------------------- 2 files changed, 68 deletions(-) delete mode 100644 src/crypto/SelfEncryption.ts delete mode 100644 test/crypto/P4Encryption.test.ts diff --git a/src/crypto/SelfEncryption.ts b/src/crypto/SelfEncryption.ts deleted file mode 100644 index e30d30a0d..000000000 --- a/src/crypto/SelfEncryption.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { - // eslint-disable-next-line camelcase - ecies_decrypt_k256_sha3_256, - // eslint-disable-next-line camelcase - ecies_encrypt_k256_sha3_256, -} from '@xmtp/ecies-bindings-wasm' -import { PrivateKey } from '.' - -// Uses ECIES to encrypt messages where the sender and recipient are the same -export default class SelfEncryption { - privateKey: PrivateKey - - constructor(identityKey: PrivateKey) { - this.privateKey = identityKey - } - - encrypt(data: Uint8Array): Uint8Array { - return ecies_encrypt_k256_sha3_256( - this.privateKey.publicKey.secp256k1Uncompressed.bytes, - this.privateKey.secp256k1.bytes, - data - ) - } - - decrypt(message: Uint8Array): Uint8Array { - return ecies_decrypt_k256_sha3_256( - this.privateKey.publicKey.secp256k1Uncompressed.bytes, - this.privateKey.secp256k1.bytes, - message - ) - } -} diff --git a/test/crypto/P4Encryption.test.ts b/test/crypto/P4Encryption.test.ts deleted file mode 100644 index 17d16e015..000000000 --- a/test/crypto/P4Encryption.test.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { PrivateKeyBundleV1 } from '../../src/crypto/PrivateKeyBundle' -import SelfEncryption from '../../src/crypto/SelfEncryption' -import { newWallet } from '../helpers' -import { equalBytes } from '../../src/crypto/utils' - -describe('SelfEncryption', () => { - let bundle: PrivateKeyBundleV1 - - beforeEach(async () => { - bundle = await PrivateKeyBundleV1.generate(newWallet()) - }) - - it('round trips data', async () => { - const message = new TextEncoder().encode('hello world') - const encryptor = new SelfEncryption(bundle.identityKey) - - const ciphertext = encryptor.encrypt(message) - expect(ciphertext).toBeDefined() - - const decrypted = encryptor.decrypt(ciphertext) - expect(equalBytes(decrypted, message)).toBeTruthy() - }) - - it('throws on decryption failure', async () => { - const message = new TextEncoder().encode('hello world') - const encryptor = new SelfEncryption(bundle.identityKey) - - const ciphertext = encryptor.encrypt(message) - expect(ciphertext).toBeDefined() - - const differentEncryptor = new SelfEncryption( - (await PrivateKeyBundleV1.generate(newWallet())).identityKey - ) - expect(() => differentEncryptor.decrypt(ciphertext)).toThrow() - }) -}) From d8b343d4e330cd6fd8c3e69afaee6100997b2bae Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Tue, 5 Dec 2023 12:05:36 -0600 Subject: [PATCH 2/2] fix: update PPPP encryption --- package-lock.json | 12 ++++++------ package.json | 2 +- src/keystore/InMemoryKeystore.ts | 2 +- src/keystore/encryption.ts | 10 +++++----- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 539ffeb96..b62fd75a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,8 @@ "license": "MIT", "dependencies": { "@noble/secp256k1": "^1.5.2", - "@xmtp/ecies-bindings-wasm": "^0.1.7", "@xmtp/proto": "^3.34.0", + "@xmtp/user-preferences-bindings-wasm": "^0.2.1", "async-mutex": "^0.4.0", "elliptic": "^6.5.4", "ethers": "^5.5.3", @@ -4820,11 +4820,6 @@ "url": "https://opencollective.com/eslint" } }, - "node_modules/@xmtp/ecies-bindings-wasm": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/@xmtp/ecies-bindings-wasm/-/ecies-bindings-wasm-0.1.7.tgz", - "integrity": "sha512-+bwI5koXneyRLVUh9Mpm9Md7A1w8GdEKqPPEVhaszfWyGr1eSeMOnkLZ0JCXMxCirYJcmiC/aua96LiuAQpACQ==" - }, "node_modules/@xmtp/proto": { "version": "3.34.0", "resolved": "https://registry.npmjs.org/@xmtp/proto/-/proto-3.34.0.tgz", @@ -4836,6 +4831,11 @@ "undici": "^5.8.1" } }, + "node_modules/@xmtp/user-preferences-bindings-wasm": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@xmtp/user-preferences-bindings-wasm/-/user-preferences-bindings-wasm-0.2.1.tgz", + "integrity": "sha512-I43rXIB9Ys9XeWfVywtxZpMw4IJhkpRr5/umuBlSzS3O9oExzYlrNYr1hCl34KfswHvgtbteh7rChd3ndkBtJA==" + }, "node_modules/abab": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.6.tgz", diff --git a/package.json b/package.json index 883dba0b9..8d79b6d50 100644 --- a/package.json +++ b/package.json @@ -84,8 +84,8 @@ }, "dependencies": { "@noble/secp256k1": "^1.5.2", - "@xmtp/ecies-bindings-wasm": "^0.1.7", "@xmtp/proto": "^3.34.0", + "@xmtp/user-preferences-bindings-wasm": "^0.2.1", "async-mutex": "^0.4.0", "elliptic": "^6.5.4", "ethers": "^5.5.3", diff --git a/src/keystore/InMemoryKeystore.ts b/src/keystore/InMemoryKeystore.ts index 174c59ddf..8f46de982 100644 --- a/src/keystore/InMemoryKeystore.ts +++ b/src/keystore/InMemoryKeystore.ts @@ -31,7 +31,7 @@ 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/ecies-bindings-wasm' +import { generate_private_preferences_topic } from '@xmtp/user-preferences-bindings-wasm' const { ErrorCode } = keystore diff --git a/src/keystore/encryption.ts b/src/keystore/encryption.ts index 15d349bed..3a6f58837 100644 --- a/src/keystore/encryption.ts +++ b/src/keystore/encryption.ts @@ -8,10 +8,10 @@ import { import { ciphertext } from '@xmtp/proto' import { // eslint-disable-next-line camelcase - ecies_decrypt_k256_sha3_256, + user_preferences_decrypt, // eslint-disable-next-line camelcase - ecies_encrypt_k256_sha3_256, -} from '@xmtp/ecies-bindings-wasm' + user_preferences_encrypt, +} from '@xmtp/user-preferences-bindings-wasm' export const decryptV1 = async ( myKeys: PrivateKeyBundleV1, @@ -62,7 +62,7 @@ export async function selfEncrypt( ) { const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes const privateKey = identityKey.secp256k1.bytes - return ecies_encrypt_k256_sha3_256(publicKey, privateKey, payload) + return user_preferences_encrypt(publicKey, privateKey, payload) } export async function selfDecrypt( @@ -71,5 +71,5 @@ export async function selfDecrypt( ) { const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes const privateKey = identityKey.secp256k1.bytes - return ecies_decrypt_k256_sha3_256(publicKey, privateKey, payload) + return user_preferences_decrypt(publicKey, privateKey, payload) }