Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Unable to verify Signature from signPersonalMessage method #207

Open
eye-dee opened this issue Oct 8, 2021 · 3 comments
Open

Unable to verify Signature from signPersonalMessage method #207

eye-dee opened this issue Oct 8, 2021 · 3 comments

Comments

@eye-dee
Copy link

eye-dee commented Oct 8, 2021

Hello,

I'm using the following method to sign messages:
https://github.com/simolus3/web3dart/blob/master/lib/src/credentials/credentials.dart#L51

The method itself works fine and other parties are able to verify signature generated by it.
But it's not clear how to verify it using web3dart library:

Signing:

    final hash = keccak256(Uint8List.fromList(utf8.encode("Hello")));
    final key = EthPrivateKey(hexToBytes(<private key>));
    final signature = await key.signPersonalMessage(hash);

Verifying

    final sig = MsgSignature(
        bytesToInt(signature.getRange(0, 32).toList()),
        bytesToInt(signature.getRange(32, 64).toList()),
        signature.elementAt(64)
    );

    final pk = ecRecover(hash, sig);

    print(bytesToHex(pk));
    print(bytesToHex(key.encodedPublicKey));

last two print values return different results
(r, s, v) creation looks also correct for me(I checked in other languages where I'm able to verify signature)

@WhiteCjy
Copy link

me too
final private = EthPrivateKey.fromHex(privakey);

final sign =await  private.signPersonalMessage(Uint8List.fromList(utf8.encode(message)));
final signed =await  private.signToSignature(Uint8List.fromList(utf8.encode(message)));

var pubKey=bytesToHex(private.publicKey.getEncoded().toList());

bool isV= isValidSignature(Uint8List.fromList(utf8.encode(message)),signed,hexToBytes(pubKey));
final adds= ecRecover(Uint8List.fromList(utf8.encode(message)), signed);

print("--验证签名--$isV----恢复数据签名帐户---"+bytesToHex(adds));
print("--publicKey--=----"+pubKey);

the result of isV is false

@eye-dee
Copy link
Author

eye-dee commented Oct 29, 2021

Later I found out, there is a ethereum prefix that's automatically beeing added inside ´signPersonalMessage´.
And if you use that library, you need to manuallly add it during verification

static const _messagePrefix = '\u0019Ethereum Signed Message:\n';
final prefix = _messagePrefix + payload.length.toString();

But I would still be interested if that can be included in the web3dart library

@ludowkm
Copy link
Contributor

ludowkm commented Nov 3, 2021

hi @SausageRoll , you can use this one to solve your issue https://pub.dev/packages/eth_sig_util

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

No branches or pull requests

3 participants