0.1.6 • Published 2 years ago
@spidexjs/metamask-sign-recover-pubkey v0.1.6
metamask-sign-recover-pubkey
How to recover pubkey by metamask signature,
Metamask eth_getEncryptionPublicKey function cannot use
ethereum
.request({
method: 'eth_getEncryptionPublicKey',
params: [accounts[0]], // you must have access to the specified account
})
So we need get pubkey from signature
img_1.png
example
import {getPubkey} from "@spidexjs/metamask-sign-recover-pubkey";
const pubkey = await getPubkey(account.address);
console.log(pubkey);
// AiJbGH7xjbW7PoDClLDb3On9YgiWXQMq2cHIA1v4lfPj
const {ethereum}: any = window;
// message hex
let message = arrayify(hashMessage('generate_pubkey'));
let 0xhex = "0x" + ua2hex(message)
const sign = await ethereum.request({method: "eth_sign", params: [address, 0xhex]});
const pk = signatureToPubkey(
sign,
Buffer.from(message)
)
console.log('pubkey:', pk)
//pubkey : AiJbGH7xjbW7PoDClLDb3On9YgiWXQMq2cHIA1v4lfPj
return pk;