add `publicKeyToETHAddress` fn (#536)

This commit is contained in:
Felicio Mununga 2024-03-18 21:35:37 +09:00 committed by GitHub
parent 02abf3417b
commit 70de4ec769
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 0 deletions

View File

@ -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')
})

View File

@ -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
}