Allow publicKeyToAddress
to accept compressed public keys
#2044
Unanswered
resetko-zeal
asked this question in
Idea / Feature Request
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Firstly, thanks for great work done to bring
viem
to this world! It's really great!Would be nice to have
publicKeyToAddress
util to work with compressed public keys.The use case appeared during migration from
ethereumjs-wallet
:We work with Trezor hardware wallet, and to achieve the best UX when enumerating addresses, because Trezor opens popup for each request to HW key, we query extended public key for "parent" derivation path and then derive child keys to get addresses.
High level, this may look like:
Where
extendedPublicKey
is the extended public key for pathm/44'/60'/0'/0
we get from the hardware device, and then we're deriving "locally" addresses form/44'/60'/0'/0/0
,m/44'/60'/0'/0/1
, etc.Moving to
view
I found the couple of functions which may help, it'sHDKey
re-export andhdKeyToAccount
. The latter does not work really well, because its purpose to create an "account" means it's expecting to extract private key, and looking at its source code - it is callinghdKey.privateKey
andprivateKeyToAccount
on the derived key.So the workaround for that was to still use
HDKey
re-export to derive:And then I thought I can use
publicKeyToAddress
to get the address from this public key, but unfortunately it didn't work, because it accepts only uncompressed public keys.So to get this working I need to import
@noble/secp256k1
and do some additional magic around this public key to uncompress it and finally pass topublicKeyToAddress
.Since
viem
already depends on@noble/curves
I thought it would be nice forpublicKeyToAddress
to accept both compressed and uncompressed public keys.I'm not sure if I'm fully aware about all the edge cases, but to my limited understanding, we can detect compressed/uncompressed public keys based on length, and then uncompress if needed.
Let me know if my though process is correct, and I maybe can create a PR for such improvement
Beta Was this translation helpful? Give feedback.
All reactions