I am using google cloud key management service to generate and manage keys. I have generated the HSM key for Asymmetric signing using Elliptic Curve secp256k1 - SHA256 Digest. The public key is something as below -
{
pem: '-----BEGIN PUBLIC KEY-----\n' +
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n' +
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n' +
'-----END PUBLIC KEY-----\n',
algorithm: 'EC_SIGN_SECP256K1_SHA256',
pemCrc32c: { value: '12345678' },
name: 'PATH-TO-KEY-ON-KMS/cryptoKeyVersions/1',
protectionLevel: 'HSM'
}
I am looking to derive Ethereum address from this so that I can fund the wallet and perform signing. For the same I have written a function as below -
const deriveEthAddress = async () => {
const publicKey = await getPublicKey(); // this returns same key as show above snippet
const address = keccak256(publicKey.pem);
const hexAddress = address.toString('hex');
return '0x' + hexAddress.toString('hex').substring(hexAddress.length - 40, hexAddress.length)
}
This function gives me ethereum checksum verified address, but not sure is it the correct way to do this. Is this solution correct or needs improvement?