From 5de124efc748ae4bc0d69c411ce3d81d57eb1ac8 Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Mon, 6 Jun 2022 11:05:44 +0200 Subject: [PATCH] make pbkdf2 async --- packages/status-js/src/client.ts | 4 ++-- packages/status-js/src/encryption.ts | 30 +++++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/status-js/src/client.ts b/packages/status-js/src/client.ts index c30d394c..f09f2dc6 100644 --- a/packages/status-js/src/client.ts +++ b/packages/status-js/src/client.ts @@ -191,13 +191,13 @@ class Community { ) } - private observeChannelMessages(chats: string[]) { + private async observeChannelMessages(chats: string[]) { const contentTopics: string[] = [] for (const chatId of chats) { const id = `${this.communityPublicKey}${chatId}` const channelContentTopic = idToContentTopic(id) - const symKey = createSymKeyFromPassword(id) + const symKey = await createSymKeyFromPassword(id) contentTopics.push(channelContentTopic) diff --git a/packages/status-js/src/encryption.ts b/packages/status-js/src/encryption.ts index 80df1548..c2d7a8f4 100644 --- a/packages/status-js/src/encryption.ts +++ b/packages/status-js/src/encryption.ts @@ -1,13 +1,25 @@ -import pbkdf2 from 'pbkdf2' +import { utf8ToBytes } from 'ethereum-cryptography/utils' +import { pbkdf2 } from 'pbkdf2' const AESKeyLength = 32 // bytes -export function createSymKeyFromPassword(password: string): Uint8Array { - return pbkdf2.pbkdf2Sync( - Buffer.from(password, 'utf-8'), - '', - 65356, - AESKeyLength, - 'sha256' - ) +export async function createSymKeyFromPassword( + password: string +): Promise { + return new Promise((resolve, reject) => { + pbkdf2( + utf8ToBytes(password), + '', + 65356, + AESKeyLength, + 'sha256', + (err, buf) => { + if (err) { + reject(err) + } else { + resolve(buf) + } + } + ) + }) }