simolus3 / web3dart

Ethereum library, written in Dart.
MIT License
441 stars 270 forks source link

Unable to verify Signature from `signPersonalMessage` method #207

Open eye-dee opened 2 years ago

eye-dee commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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