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

TypeError: Cannot read properties of undefined (reading 'importKey') #6485

Closed
Muhammad-Altabba opened this issue Oct 6, 2023 · 6 comments
Closed
Labels
Stale Has not received enough activity

Comments

@Muhammad-Altabba
Copy link
Contributor

Description

Unclear error is thrown when calling encrypt from web3-eth-accounts in an in-secured environment. This could happen when using the library with a remote website with http (and not https).
However, I checked this using Jest with jsdom; And in this case it is also an in-secured environment. So, this issue was detected using jsdom but it applies to other scenarios (remote http websites).

It also worth noting that it could be implemented for jsdom later, and there are polyfiles for it, see this issue for more: jsdom/jsdom#1612

Expected behavior

To throw a meaningful error in case some functionality is not available.

Actual behavior

Throws: TypeError: Cannot read properties of undefined (reading 'importKey')

Steps to reproduce the behavior

  1. At test/unit/account.test.ts inside the package web3-eth-accounts:
  • Add this to the very first line of the file (before the existing comment):
/**
 * @jest-environment jsdom
 */
  • Add polyfills for TextEncoder before the imports:
import { TextEncoder } from 'util';
global.TextEncoder = TextEncoder;
  • Search for describe('encrypt', () => { and replace with: describe.only('encrypt', () => {
  1. cd packages/web3-eth-accounts && yarn test account.test.ts

Logs

FAIL  test/unit/account.test.ts (7.943 s)
 ● accounts › encrypt › valid cases › [
 '0x67f476289210e3bef3c1c75e4de993ff0a00663df00def84e73aa7411eac18a6',
  '123',
  [Object]
]

    TypeError: Cannot read properties of undefined (reading 'importKey')

      552 |     }
      553 |
    > 554 |     const cipher = await createCipheriv(
          |                          ^
      555 |             privateKeyUint8Array,
      556 |             derivedKey.slice(0, 16),
      557 |             initializationVector,

      at getBrowserKey (node_modules/ethereum-cryptography/aes.js:33:42)
      at encrypt (node_modules/ethereum-cryptography/aes.js:41:36)
      at src/account.ts:554:23
      at src/account.ts:5226:39
      at Object.<anonymous>.__awaiter (src/account.ts:5169:10)
      at encrypt (src/account.ts:487:24)
      at test/unit/account.test.ts:169:33
      at test/unit/account.test.ts:8:71
      at Object.<anonymous>.__awaiter (test/unit/account.test.ts:4:12)
      at test/unit/account.test.ts:168:60
Copy link

github-actions bot commented Dec 6, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Dec 6, 2023
@Muhammad-Altabba Muhammad-Altabba added Discussion and removed Stale Has not received enough activity Discussion labels Dec 6, 2023
@Muhammad-Altabba
Copy link
Contributor Author

So I suggest just to give the user a clear error message indicating that encrypt does not work inside unsecured environment. That is when he browse a remote http website.

Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Feb 21, 2024
@HenriqueVazquez
Copy link

So I suggest just to give the user a clear error message indicating that encrypt does not work inside unsecured environment. That is when he browse a remote http website.

I agree with you

@github-actions github-actions bot removed the Stale Has not received enough activity label Feb 23, 2024
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Apr 23, 2024
@github-actions github-actions bot closed this as completed May 8, 2024
@mrtn-c
Copy link

mrtn-c commented Dec 18, 2024

So I suggest just to give the user a clear error message indicating that encrypt does not work inside unsecured environment. That is when he browse a remote http website.

I totally agree with you my friend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Has not received enough activity
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@HenriqueVazquez @Muhammad-Altabba @mrtn-c and others