Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda