Skip to content

Commit

Permalink
fix: refactor user preferences exports for web
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Dec 14, 2023
1 parent 4b51dd3 commit fc794e3
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 32 deletions.
35 changes: 35 additions & 0 deletions src/crypto/selfEncryption.ts
Original file line number Diff line number Diff line change
@@ -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)
}
47 changes: 47 additions & 0 deletions src/crypto/selfEncryption.web.ts
Original file line number Diff line number Diff line change
@@ -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)
}
23 changes: 16 additions & 7 deletions src/keystore/InMemoryKeystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -255,8 +263,9 @@ export default class InMemoryKeystore implements Keystore {
}

async getPrivatePreferencesTopicIdentifier(): Promise<keystore.GetPrivatePreferencesTopicIdentifierResponse> {
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,
})
Expand Down
25 changes: 0 additions & 25 deletions src/keystore/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

0 comments on commit fc794e3

Please sign in to comment.