Skip to content

Instantly share code, notes, and snippets.

Last active November 2, 2020 16:48
What would you like to do?
Get Ethereum Address from ASN1 DER encoded public key
function getEthereumAddress(publicKey: Buffer): string {
console.log("Encoded Pub Key: " + publicKey.toString('hex'));
// The public key is ASN1 encoded in a format according to
// I used to figure out how to parse this
// and defined the schema in the EcdsaPubKey object
let res = EcdsaPubKey.decode(publicKey, 'der');
let pubKeyBuffer : Buffer =;
console.log("Pub Key Buffer: " + pubKeyBuffer.toString('hex'));
// The public key starts with a 0x04 prefix that needs to be removed
// more info:
pubKeyBuffer = pubKeyBuffer.slice(1, pubKeyBuffer.length);
const address = keccak256(pubKeyBuffer) // keccak256 hash of publicKey
const buf2 = Buffer.from(address, 'hex');
const EthAddr = "0x" + buf2.slice(-20).toString('hex'); // take last 20 bytes as ethereum adress
console.log("Generated Ethreum address: " + EthAddr);
return EthAddr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment