Skip to content

Comments

Fix phpEcRecover#5

Open
LinkedDestiny wants to merge 1 commit intodigitaldonkey:masterfrom
LinkedDestiny:master
Open

Fix phpEcRecover#5
LinkedDestiny wants to merge 1 commit intodigitaldonkey:masterfrom
LinkedDestiny:master

Conversation

@LinkedDestiny
Copy link

copy code from nodejs web3 library

copy code from nodejs web3 libraty
@digitaldonkey
Copy link
Owner

digitaldonkey commented Jun 22, 2022

Sorry. I'm pretty out of this. Can you reference the changes made in web3 requiring this?
Can you provide a context what fails?

@LinkedDestiny
Copy link
Author

link

I fix the code by the web3.js implement.

When I use the origin code to recover a sign from a hard-drive wallet, I got an error.

@LinkedDestiny
Copy link
Author

LinkedDestiny commented Jul 1, 2022

export const recover = (hash, signature) => {
  const vals = decodeSignature(signature);
  const vrs = { v: Bytes.toNumber(vals[0]), r: vals[1].slice(2), s: vals[2].slice(2) };
  const ecPublicKey = secp256k1.recoverPubKey(
    Buffer.from(hash.slice(2), 'hex'),
    vrs,
    vrs.v < 2 ? vrs.v : 1 - (vrs.v % 2),
  ); // because odd vals mean v=0... sadly that means v=0 means v=1... I hate that
  const publicKey = '0x' + ecPublicKey.encode('hex', false).slice(2);
  const publicHash = keccak256(publicKey);
  const address = toChecksum('0x' + publicHash.slice(-40));
  return address;
};

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

Successfully merging this pull request may close these issues.

2 participants