0

I'm currently stucked by this strange thing: I started by exporting my private key(fdba67e41f7c6767100969ae3f045d10a59e35d380acf5b37a3b208bd2969347) from my metamask account, which i can use to login with MEW without problem (and gives me the address 0xa6CDA44CEA3Ac87435d9fDF548B051dDE90D128F):

enter image description here

But when I use the function web3.eth.accounts.privateKeyToAccount() with this private key as input, it's not creating an account with the corresponding public key as address.

var account_1_priv = 'fdba67e41f7c6767100969ae3f045d10a59e35d380acf5b37a3b208bd2969347';

var acc = web3.eth.accounts.privateKeyToAccount(account_1_priv);
console.log(acc)

>>> { address: '0xaF6d10CbEcd036f713cA504d3e952F2A31845EB9',
privateKey:'fdba67e41f7c6767100969ae3f045d10a59e35d380acf5b37a3b208bd2969347',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt] }

The new address showned here became 0xaF6d10CbEcd036f713cA504d3e952F2A31845EB9.

Same thing with the sign function

I need to sign a transaction locally with my private key privKey, and I used it in the following way: (last line with the sign function from web3.eth.accounts)

var account_1_priv = 'fdba67e41f7c6767100969ae3f045d10a59e35d380acf5b37a3b208bd2969347';
var hashdata = "0x2ff5a3650ca0391b59cf6614820cac78fd7564302c5614349990117b21df0eeb";

var result = web3.eth.accounts.sign(hashdata, account_1_priv)
console.log(result)

>> { message: '0x2ff5a3650ca0391b59cf6614820cac78fd7564302c5614349990117b21df0eeb',    
messageHash: '0x5c9aac8744ec24ede5fc8e3125cf4e388d14afb8e809575fdd9477ecee44db33',
v: '0x1b',
r: '0x93291c697b8e30350f93e7b13e27b6dd232d6d4d9cb5edb8a32c06554ed2bafd',
s: '0x79f2d63e1225f4f7d4f0caff85f5af39599b8630c81abd9b758ad1915b001b49',
signature: '0x93291c697b8e30350f93e7b13e27b6dd232d6d4d9cb5edb8a32c06554ed2bafd79f2d63e1225f4f7d4f0caff85f5af39599b8630c81abd9b758ad1915b001b491b' 
}

but later when I want to verify the public key with hashdata, v, r, s on solidity with ercrecovery like this:

  ecrecover(bytes32 hashdata, uint8 v, bytes32 r, bytes32 s);

I got the output of 0xaF6d10CbEcd036f713cA504d3e952F2A31845EB9, which is the same as the privateKeyToAccount() gave me, but different from my metamask account. Am I using any of these functions wrong? how can I sign the message correctly with a private key on behavior of my metamask account? I was thinking about maybe the problem is related to the HD wallet structure, but haven't found any related topics yet.

1

Use 0xfdba67e41f7c6767100969ae3f045d10a59e35d380acf5b37a3b208bd2969347 instead (with a 0x prefix) so web3 knows the format of the private key:

web3.eth.accounts.privateKeyToAccount('0xfdba67e41f7c6767100969ae3f045d10a59e35d380acf5b37a3b208bd2969347').address
"0xa6CDA44CEA3Ac87435d9fDF548B051dDE90D128F"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.