much improved
This commit is contained in:
@@ -3,27 +3,30 @@ import { Buffer } from 'buffer';
|
||||
|
||||
// code from https://docs.metamask.io/wallet/how-to/sign-data/#use-personal_sign
|
||||
|
||||
export const signMessage = async (provider: EIP1193Provider,userAccount: string) => {
|
||||
export const getPublicKey = async (provider: EIP1193Provider, userAccount: string) => {
|
||||
const publicKey = await provider.request({
|
||||
method: 'eth_getEncryptionPublicKey',
|
||||
params: [userAccount],
|
||||
})
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
export const signMessage = async (message: string,provider: EIP1193Provider,userAccount: string) => {
|
||||
try {
|
||||
// For historical reasons, you must submit the message to sign in hex-encoded UTF-8.
|
||||
// This uses a Node.js-style buffer shim in the browser.
|
||||
|
||||
const publicKey = await provider.request({
|
||||
method: 'eth_getEncryptionPublicKey',
|
||||
params: [userAccount],
|
||||
})
|
||||
|
||||
// const bytes = new TextEncoder().encode(String(publicKey));
|
||||
// const msg = '0x' + Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
const msg = `0x${Buffer.from(String(publicKey), "utf8").toString("hex")}`
|
||||
const sign = await provider.request({
|
||||
const msg = `0x${Buffer.from(message, "utf8").toString("hex")}`
|
||||
const sig = await provider.request({
|
||||
method: "personal_sign",
|
||||
params: [msg, userAccount],
|
||||
})
|
||||
|
||||
console.log("Encoded Public Key: ", msg);
|
||||
|
||||
return { publicKey, sign };
|
||||
return sig;
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user