Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
TypeError: Cannot read properties of undefined (reading 'bufferToHex') - Metamask

I am trying to encrypt a message by using a method from the Metamask RPC API (here). I first use 'eth_getEncryptionPublicKey' to get the encryption key for the account that I am connected to. I then use the following code to encrypt my message. However, I am getting the following error and I do not understand how to address it. Please can someone advise?

I do not know if this has anything to do with my errors but '@metamask/eth-sig-util' is written in TypeScript.

import ethUtil from 'ethereumjs-util';
import sigUtil from '@metamask/eth-sig-util';

const encryptString = (encryptionKey, text) => {
console.log('encryptString.encryptionKey: ' + encryptionKey);
console.log('encryptString.text: ' + text);

const encryptedMessage = ethUtil.bufferToHex(
  Buffer.from(
    JSON.stringify(
      sigUtil.encrypt({
        publicKey: encryptionKey,
        data: text,
        version: 'x25519-xsalsa20-poly1305',
      })
    ),
    'utf-8'
  )
);

};

console:

encryptString.publicKey: Rb1/QuAkQ7qpyo9wzY5+E0Kw2AkL1Vipb8LObOGkkNw=
encryptString.text: Hello World

Error:

Uncaught TypeError: Cannot read properties of undefined (reading 'bufferToHex')
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I had the same error, using these imports worked for me:

import { bufferToHex } from 'ethereumjs-util';
import { encrypt } from '@metamask/eth-sig-util';

Full code adapted for your case:

import { bufferToHex } from 'ethereumjs-util';
import { encrypt } from '@metamask/eth-sig-util';

const encryptedMessage = bufferToHex(
    Buffer.from(
        JSON.stringify(
            encrypt({
                publicKey: encryptionKey,
                data: 'hello world!',
                version: 'x25519-xsalsa20-poly1305',
            }),
        ),
        'utf8',
    ),
);

console.log(`Encrypted message: ${encryptedMessage}`);

const decryptedMessage = await window as.ethereum.request({
    method: 'eth_decrypt',
    params: [encryptedMessage, account],
});

console.log(`Decrypted message: ${decryptedMessage}`);

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!