I'm new to blockchain programming.

In my application I need to work with keys. I am writing tests in JS and the keys are presented in this form:

Buffer.from('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', 'hex')

I need to convert the keys to string format. The toString() method does not work with different parameters. Nothing is output to the console and the length value is 0 when using toString().

Please tell me, perhaps I am initially using the wrong key format?

  • Why are you converting the private key to string ? Apr 6 at 15:35
  • this is needed for some encryption operations. I need to halve the public key and private key Apr 6 at 18:53
  • So, do you want to derive the public key from the private key ? Apr 7 at 7:28

1 Answer 1


You can use the ethereumjs-util library of Javascript, and import the same in your JS code:

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

If you just want to halve the private key (or any key in hex format) into two parts:

const keyBuffer = ethUtil.toBuffer('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80');

// Calculate half of the length of the buffer
const halfLength = Math.ceil(keyBuffer.length / 2);

// Slice the buffer into two halves
const firstHalf = keyBuffer.subarray(0, halfLength);
const secondHalf = keyBuffer.subarray(halfLength);

console.log('First Half: ', firstHalf.toString('hex'));

console.log('Second Half: ', secondHalf.toString('hex'));

To derive the public key from private key, and then the address from the public key:

const keyBuffer = ethUtil.toBuffer('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80')

// Derive the Public Key From Private Key
const publicKey = ethUtil.privateToPublic(keyBuffer);

console.log('Public Key: ', ethUtil.bufferToHex(publicKey));

// Derive the Address From Public Key
const addressFromPublic = ethUtil.publicToAddress(publicKey);

console.log('Address (Derived from Public Key): ', ethUtil.bufferToHex(addressFromPublic));

// Derive the Address From Private Key
const addressFromPrivate = ethUtil.privateToAddress(keyBuffer);

console.log('Address (Derived from Private Key): ', ethUtil.bufferToHex(addressFromPrivate));
  • Thanks for the suggested option. There is a function that takes a private and public key as an argument. It is assumed that the user provides them from his Metamask account. The function works with shortened keys, so I need a function to shorten the keys. During tests, I discovered that the function for compressing a key always returns the same value, even if called with different keys I started looking for the reason for this and discovered that there is a key in the tests represented in the form Buffer.from("string", "hex"), the function cannot read the key. Apr 8 at 12:43
  • When I try to output it to the console - nothing is output, cast to a string - also nothing is output to the console, output the length - I get zero. But if I remove the "hex" or the "0x" prefix from the key representation, then I can cast it to a string and shorten it. Perhaps I'm representing the key incorrectly? Perhaps there is no need to use Buffer? Apr 8 at 12:46
  • You can try shorten the keys using ethereumjs-util library as I've shared in my answer. Apr 8 at 12:50
  • By the way, the user is never supposed to provide keys from his MetaMask account, instead on your frontend dApp, the user connects his MetaMask account which will be connected using the window.ethereum object that acts as an injected provider. Apr 8 at 12:50
  • Yes, sure. I understand it. The problem will need to be resolved in the future. But now I need to deal with the key compress Apr 8 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.