diff --git a/packages/status-js/src/utils/generate-key-from-password.test.ts b/packages/status-js/src/utils/generate-key-from-password.test.ts new file mode 100644 index 0000000..3308b64 --- /dev/null +++ b/packages/status-js/src/utils/generate-key-from-password.test.ts @@ -0,0 +1,24 @@ +import { bytesToHex } from 'ethereum-cryptography/utils' + +import { generateKeyFromPassword } from './generate-key-from-password' + +describe('createSymKeyFromPassword', () => { + it('should create symmetric key from password', async () => { + const password = 'password' + const symKey = await generateKeyFromPassword(password) + + expect(bytesToHex(symKey)).toEqual( + 'c49ad65ebf2a7b7253bf400e3d27719362a91b2c9b9f54d50a69117021666c33' + ) + }) + + it('should generate symmetric key from chat ID', async () => { + const chatId = + '0x02dcec6041fb999d65f1d33363e08c93d3c1f6f0fbbb26add383e2cf46c2b921f41dc14fd8-9a8b-4df5-a358-2c3067be5439' + const symKey = await generateKeyFromPassword(chatId) + + expect(bytesToHex(symKey)).toEqual( + '76ff5bf0a74a8e724367c7fc003f066d477641f468768a8da2817addf5c2ce76' + ) + }) +}) diff --git a/packages/status-js/src/utils/generate-key-from-password.ts b/packages/status-js/src/utils/generate-key-from-password.ts new file mode 100644 index 0000000..564d4c7 --- /dev/null +++ b/packages/status-js/src/utils/generate-key-from-password.ts @@ -0,0 +1,19 @@ +import { pbkdf2 } from 'ethereum-cryptography/pbkdf2' +import { hexToBytes, utf8ToBytes } from 'ethereum-cryptography/utils' + +const AES_KEY_LENGTH = 32 // bytes + +/** + * status-go: https://github.com/status-im/status-go/blob/a471fed6a64e01a1aba8d925377fba045a5aa9f9/wakuv2/waku.go#L713 + */ +export async function generateKeyFromPassword( + password: string +): Promise { + return await pbkdf2( + hexToBytes(password), + utf8ToBytes(''), + 65356, + AES_KEY_LENGTH, + 'sha256' + ) +}