eth-sig-util icon indicating copy to clipboard operation
eth-sig-util copied to clipboard

Public Key format difference between `extractPublicKey` and `encrypt` question

Open shawnmitchell opened this issue 3 years ago • 2 comments

Hi, probably missing something obvious and sorry to bother you but I'm trying to use the extracted public key from a personal_sign'ed message to encrypt a return message to a user... but the public key returned by extractPublicKey is a 130 byte hex-encoded string (starting with 0x4) whereas the encrypt method requires the public key be a 44 character (base64 encoded?) string. Am I missing something terribly obvious? I cannot figure out how to convert the hex-encoded to the 44 character version.

try {
      console.log("Account: ", connector.accounts[0]);
      const msg = `0x${Buffer.from("Hello World", "utf8").toString("hex")}`;
      console.log("MessageHashBytes: ", msg);
      const signature = await connector.signPersonalMessage([
        msg,
        connector.accounts[0],
      ]);
      console.log("Signature: ", signature);
      const result = recoverPersonalSignature({ data: msg, signature });
      console.log("Result: ", result);
      const publicKey = extractPublicKey({
        data: msg,
        signature
      });
      console.log('Public Key: ', publicKey)
      console.log('Public Key Length: ', publicKey.length);
      
      const questionMark = Buffer.from(publicKey).toString('base64');
      console.log('Hmm: ', questionMark, questionMark.length)
      
    } catch (error) {
      if (error instanceof Error) console.log(error.message);
    }

produces the following output:

Account:  0x0C0Bd9c93596F6FC9F7E3dd8F53EF73a6759873b
MessageHashBytes:  0x48656c6c6f20576f726c64
Signature:  0x618ebc96d5a7ea456ecbf3bc938e28a5329e1dc063ef26b94c8a8a11cfca0ff26783259b56921dec86bfa2115765624b58ce4a9799ac43f5321c2ac2b7af7f2a1c
Result:  0x0c0bd9c93596f6fc9f7e3dd8f53ef73a6759873b
Public Key:  0x4d979e20ed15805a4b9aec2d9b8dfe256a174844f20ecd26ca5a571e70907a04bbc9be04b47bdc3a73cfdfe7b5536d4bb15fec07e7d9db076816e7f80cd28200
Public Key Length:  130
Hmm:  MHg0ZDk3OWUyMGVkMTU4MDVhNGI5YWVjMmQ5YjhkZmUyNTZhMTc0ODQ0ZjIwZWNkMjZjYTVhNTcxZTcwOTA3YTA0YmJjOWJlMDRiNDdiZGMzYTczY2ZkZmU3YjU1MzZkNGJiMTVmZWMwN2U3ZDlkYjA3NjgxNmU3ZjgwY2QyODIwMA== 176

When I export my public key from metamask I receive +5ar2xujBQQPv+RTRU2tzO6sjSuTMZSosAx5NAdfkw0=

Thanks in advance anyone who can point me in the right direction! <3

shawnmitchell avatar Mar 29 '22 19:03 shawnmitchell

Run into this issue when using the encrypt method, namely the error "Uncaught Error: bad public key size". I was told Buffer.from({128charHexPubKey} 'hex')).toString('base64') but it doesn't work.

idavidgeo avatar Apr 23 '22 22:04 idavidgeo

Run into this issue when using the encrypt method, namely the error "Uncaught Error: bad public key size". I was told Buffer.from({128charHexPubKey} 'hex')).toString('base64') but it doesn't work.

encrypt in eth-sig-util uses the encryptionPublicKey, not the wallet's public key. It's a different encryption scheme.

shawnmitchell avatar Apr 23 '22 23:04 shawnmitchell