web3.js
web3.js copied to clipboard
TypeError: Cannot read properties of undefined (reading 'importKey')
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: https://github.com/jsdom/jsdom/issues/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
- At
test/unit/account.test.tsinside the packageweb3-eth-accounts:
- Add this to the very first line of the file (before the existing comment):
/**
* @jest-environment jsdom
*/
- Add polyfills for
TextEncoderbefore the imports:
import { TextEncoder } from 'util';
global.TextEncoder = TextEncoder;
- Search for
describe('encrypt', () => {and replace with:describe.only('encrypt', () => {
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
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.
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.
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.
So I suggest just to give the user a clear error message indicating that
encryptdoes not work inside unsecured environment. That is when he browse a remotehttpwebsite.
I agree with you
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.