4

I currently have a generated 64-byte public key that has been recovered from an Ethereum transaction.

How do I convert this key into a 33-byte compressed public-key? The problem I'm having seems pretty trivial but I cannot for the life of me find a solution! I'm currently working with javascript.

Any help would be greatly appreciated.

4

There's a package that contains suitable functions, here -> pubkey/eth-crypto

See:

The compress code is below. (Copy + pasted from this file.)

var _secp256k = require('secp256k1');

function compress(startsWith04) {

    // add trailing 04 if not done before
    var testBuffer = Buffer.from(startsWith04, 'hex');
    if (testBuffer.length === 64) startsWith04 = '04' + startsWith04;

    return (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith04), true));
}
2
  • 1
    Richard you beauty thanks brother this is exactly the behavior I need.
    – cheethas
    Mar 13 at 21:50
  • 1
    Glad to help :-) Mar 13 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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