Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromPrivateKey issue #85

Open
brightsider opened this issue Feb 27, 2019 · 1 comment
Open

fromPrivateKey issue #85

brightsider opened this issue Feb 27, 2019 · 1 comment

Comments

@brightsider
Copy link

I can import wallet with privateKey + 1 random character.
How to fix it?

let key = privateKey
if (key.startsWith('0x')) {
    key = key.substring(2)
}
let buf = Buffer.from(key, 'hex')
let result = ethereumjs.fromPrivateKey(buf)
let publicKey = '0x' + result.getAddress().toString('hex')
if (publicKey.toLowerCase() === address.toLowerCase()) {
    return privateKey
}
@michaelsbradleyjr
Copy link
Contributor

michaelsbradleyjr commented Aug 26, 2019

This is owing to the behavior of Node's built in Buffer.from. In a Node REPL:

> Buffer.from('ff', 'hex')
<Buffer ff>
> Buffer.from('fF' + 'f', 'hex')
<Buffer ff>
> Buffer.from('ff' + 'Z', 'hex')
<Buffer ff>
> Buffer.from('ffpff', 'hex')
<Buffer ff>
> Buffer.from('Rffff', 'hex')
<Buffer >
> Buffer.from('frf', 'hex')
<Buffer >

That is, the function trims off the trailing character so that the input is the longest possible even number of hex characters. Note that Buffer.from also normalizes to lower case and the trimmed character/s may not even be valid hex. If the first two characters are not valid hex then you get an empty buffer, i.e. the same as Buffer.from('', 'hex').

There's nothing I see that ethereumjs-wallet can do to help in this situation since it's trimmed before fromPrivateKey is invoked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@michaelsbradleyjr @brightsider and others