mirror of
https://github.com/status-im/dappconnect-sdks.git
synced 2025-01-12 17:54:21 +00:00
add private key to address function
This commit is contained in:
parent
9227a83810
commit
6fb97a5614
10
packages/status-js/src/utils/private-key-to-address.test.ts
Normal file
10
packages/status-js/src/utils/private-key-to-address.test.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { privateKeyToAddress } from './private-key-to-address'
|
||||||
|
|
||||||
|
describe('privateKeyToAddress', () => {
|
||||||
|
it('should return public address', () => {
|
||||||
|
const privateKey =
|
||||||
|
'0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709'
|
||||||
|
const address = privateKeyToAddress(privateKey)
|
||||||
|
expect(address).toEqual('0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01')
|
||||||
|
})
|
||||||
|
})
|
13
packages/status-js/src/utils/private-key-to-address.ts
Normal file
13
packages/status-js/src/utils/private-key-to-address.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { keccak256 } from 'ethereum-cryptography/keccak'
|
||||||
|
import { getPublicKey } from 'ethereum-cryptography/secp256k1'
|
||||||
|
import { bytesToHex } from 'ethereum-cryptography/utils'
|
||||||
|
|
||||||
|
export function privateKeyToAddress(privateKey: string): string {
|
||||||
|
const publicKey = getPublicKey(privateKey)
|
||||||
|
const publicKeyWithoutPrefix = publicKey.slice(1) // uncompressed public key has 04 prefix
|
||||||
|
const hash = keccak256(publicKeyWithoutPrefix)
|
||||||
|
const address = bytesToHex(hash.slice(12))
|
||||||
|
|
||||||
|
// TODO: add checksum
|
||||||
|
return address
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user