From 70de4ec76989a6168025ccb4af7c820021a7af71 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Mon, 18 Mar 2024 21:35:37 +0900 Subject: [PATCH] add `publicKeyToETHAddress` fn (#536) --- .../src/utils/public-key-to-eth-address.test.ts | 11 +++++++++++ .../status-js/src/utils/public-key-to-eth-address.ts | 12 ++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 packages/status-js/src/utils/public-key-to-eth-address.test.ts create mode 100644 packages/status-js/src/utils/public-key-to-eth-address.ts diff --git a/packages/status-js/src/utils/public-key-to-eth-address.test.ts b/packages/status-js/src/utils/public-key-to-eth-address.test.ts new file mode 100644 index 00000000..11f582fa --- /dev/null +++ b/packages/status-js/src/utils/public-key-to-eth-address.test.ts @@ -0,0 +1,11 @@ +import { expect, test } from 'vitest' + +import { publicKeyToETHAddress } from './public-key-to-eth-address' + +test('should return ETH address from public key', () => { + expect( + publicKeyToETHAddress( + '0x02bcbe39785b55a22383f82ac631ea7500e204627369c4ea01d9296af0ea573f57' + ) + ).toEqual('0x0A1ec0002dDB927B03049F1aD8D589aBEA4Ba4b3') +}) diff --git a/packages/status-js/src/utils/public-key-to-eth-address.ts b/packages/status-js/src/utils/public-key-to-eth-address.ts new file mode 100644 index 00000000..b1ed58cf --- /dev/null +++ b/packages/status-js/src/utils/public-key-to-eth-address.ts @@ -0,0 +1,12 @@ +import { ethers } from 'ethers' + +import { deserializePublicKey } from './deserialize-public-key' + +export function publicKeyToETHAddress(publicKey: string): string { + const key = deserializePublicKey(publicKey, { + compress: false, + }) + const address = ethers.computeAddress(key) + + return address +}