2

I want to manually create ethereum addres from any str message like that:

const msg   = '00'
const pk    = bytesToHex(sha256(msg))
//pk is f1534392279bddbf9d43dde8701cb5be14b82f76ec6607bf8d6ad557f60f304e
const pub   = secp256k1.getPublicKey(pk).substr(2)
//pub is 8fda47407e9a9ba47c4dbab6608ef73240530bb5e246261ec99d757832f5edf44e7e7eb6fc75b45e4ec1ca912b8f5e2c832d39dbf6abda76873e952a7fd13208
const rawAddress = bytesToHex(keccak_256(pub))
//bd5dd0b32f73c17fb0974b61dd1ca428ad2bd09d7f7a561d24099c4eb53fc0c3

here is a problem. According to this site https://www.royalfork.org/2017/12/10/eth-graphical-address/ my address must be this:

b91ef47f51856881969cbd13ef107311125acecff0fb6d989eca30d570683e46

but i`ve got this:

bd5dd0b32f73c17fb0974b61dd1ca428ad2bd09d7f7a561d24099c4eb53fc0c3

So where is the trick?

p.s. function keccak256 tested and works as expected.

p.s.s using pub key without substr(2) result into another incorrect address:

279f256123b10727bd8930f672d3bf6d82111b963a8182dfa6386836d42a578b
gazoblock
  • 336
  • 2
  • 5
  • check this website -> https://emn178.github.io/online-tools/keccak_256.html. Note that, if you keccak with string selected, you will get bd5dd0b32f73c17fb0974b61dd1ca428ad2bd09d7f7a561d24099c4eb53fc0c3, and if you keccak with hex selected, you will get b91ef47f51856881969cbd13ef107311125acecff0fb6d989eca30d570683e46. Maybe you need to invert bytesToHex(keccak_256(pub)) to keccac_256(bytesToHex(pub)) – João Paulo Morais Mar 19 '22 at 02:25

1 Answers1

1

I also needed to get addresses from public keys few months ago and I used eth-crypto. Follow this code:

const privateKey = '<YOUR_PRIVATE_KEY>';
const publicKey = EthCrypto.publicKeyByPrivateKey(privateKey);
const address = EthCrypto.publicKey.toAddress(publicKey);
Miroslav Nedelchev
  • 3,519
  • 2
  • 10
  • 12