web3dart icon indicating copy to clipboard operation
web3dart copied to clipboard

Unable to verify Signature from `signPersonalMessage` method

Open eye-dee opened this issue 4 years ago • 3 comments

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)

eye-dee avatar Oct 08 '21 14:10 eye-dee

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

WhiteCjy avatar Oct 28 '21 07:10 WhiteCjy

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

eye-dee avatar Oct 29 '21 16:10 eye-dee

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

ludowkm avatar Nov 03 '21 03:11 ludowkm