How can I get the public key of msg.sender
from inside the smart contract?
function getPublicKey() returns (string publicKey) {}
From Solidity docs, ecrecover
returns an address:
ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns
(address): recover the address associated with the public key from
elliptic curve signature or return zero on error
From ethereumjs-util library, ecrecover
returns a public key:
/**
* ECDSA public key recovery from signature
* @param {Buffer} msgHash
* @param {Number} v
* @param {Buffer} r
* @param {Buffer} s
* @return {Buffer} publicKey
*/
exports.ecrecover = function (msgHash, v, r, s) {
var signature = Buffer.concat([exports.setLength(r, 32), exports.setLength(s, 32)], 64)
var recovery = v - 27
if (recovery !== 0 && recovery !== 1) {
throw new Error('Invalid signature v value')
}
var senderPubKey = secp256k1.recover(msgHash, signature, recovery)
return secp256k1.publicKeyConvert(senderPubKey, false).slice(1)
}
I assume, that when the transaction is sent, there is all information necessary to derive a public key for the sender's address, but I'm struggling to find any clear documentation on how to do it.
My current approach is:
function getPublicKey(account) {
return new Promise((resolve, reject) => {
web3.eth.sendTransaction({
from: account,
to: account,
value: 0,
}, (err, txId) => {
if (err) return reject(err)
web3.eth.getTransaction(txId, (err, tx) => {
if (err) return reject(err)
return resolve(tx.publicKey)
})
})
})
}
msg.sender
. @TjadenHess could you advice how this could be done?