Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use built-in PBKDF2 implementation #295

Merged
merged 9 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-apes-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@status-im/js': patch
---

use built-in crypto for pbkdf2
33 changes: 33 additions & 0 deletions packages/status-js/src/crypto/pbkdf2.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { pbkdf2 as pbkdf2Type } from 'ethereum-cryptography/pbkdf2'

type PBKDF2 = typeof pbkdf2Type

export const pbkdf2: PBKDF2 = async (
password: Uint8Array,
salt: Uint8Array,
iterations: number,
keylen: number
): Promise<Uint8Array> => {
const cryptoKey = await window.crypto.subtle.importKey(
'raw',
password,
{ name: 'PBKDF2' },
false,
['deriveBits']
)

const derivedKey = await window.crypto.subtle.deriveBits(
{
name: 'PBKDF2',
salt,
iterations,
hash: {
name: 'SHA-256',
},
},
cryptoKey,
keylen << 3
)

return new Uint8Array(derivedKey)
}
3 changes: 2 additions & 1 deletion packages/status-js/src/utils/generate-key-from-password.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { pbkdf2 } from 'ethereum-cryptography/pbkdf2'
import { utf8ToBytes } from 'ethereum-cryptography/utils'

import { pbkdf2 } from '../crypto/pbkdf2.browser'

const AES_KEY_LENGTH = 32 // bytes

/**
Expand Down
22 changes: 20 additions & 2 deletions packages/status-js/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import { defineConfig } from 'vite'

import { dependencies } from './package.json'

import type { Alias } from 'vite'

const isTest = process.env.NODE_ENV === 'test'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do it the same, but I remember reading this from their docs:

Use process.env.VITEST or mode property on defineConfig (will be set to test if not overridden) to conditionally apply different configuration in vite.config.ts

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx 👍, done.


const external = [
...Object.keys(dependencies || {}),
// ...Object.keys(peerDependencies || {}),
].map(name => new RegExp(`^${name}(/.*)?`))

export default defineConfig(({ mode }) => {
const alias: Alias[] = []

if (isTest) {
alias.push({
/**
* Note: `happy-dom` nor `jsdom` have Crypto implemented (@see https://github.com/jsdom/jsdom/issues/1612)
*/
find: /^.*\/crypto\/pbkdf2.browser$/,
replacement: 'ethereum-cryptography/pbkdf2',
})
}

return {
build: {
target: 'es2020',
Expand All @@ -24,9 +40,11 @@ export default defineConfig(({ mode }) => {
external,
},
},

resolve: {
alias,
},
test: {
// environment: 'happy-dom',
environment: 'happy-dom',
},
}
})