How to encrypt message by metamask encryptionPublicKey

In development document, PCR API, Metamask provided an encrypting example:

const ethUtil = require('ethereumjs-util');

const encryptedMessage = ethUtil.bufferToHex(
  Buffer.from(
    JSON.stringify(
      sigUtil.encrypt(
        encryptionPublicKey,
        { data: 'Hello world!' },
        'x25519-xsalsa20-poly1305'
      )
    ),
    'utf8'
  )
);

But I don’t know what is sigUtil? It isn’t in ‘ethereumjs-util’. I can’t find this package in npm and the eth-sig-util package can’t be used in brower. So how can I use the encrypt function of Metamask in brower.

2 Likes

hello sir I’m new account open I send deposit but not open pay account please check it

Package is called eth-sig-util

const { encrypt } = require("eth-sig-util");
const { bufferToHex } = require('ethereumjs-util');

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