solana web3js 中是否有任何方法可以检查地址所有权?
例如,在 ETH 中,我使用 PK 签署消息,然后使用消息和签名恢复地址。如果我正在检查的地址等于恢复功能的地址,我知道签署消息的人拥有特定地址。
我需要在 solana 中实现相同的功能。
谢谢您的帮助。
import Accounts from "web3-eth-accounts";
/**
* Check address ownership by signed message
*
* @param {string} address address to check ownership
* @param {string} message message signed by address private key
* @param {string} sign message sign
* @returns true if message was signed with this address private key, false otherwise
*/
export const checkAddressOwnership = (address, message, sign) => {
const accounts = new Accounts();
const recoveredAddress = accounts.recover(message, sign);
return address.toLowerCase() === recoveredAddress.toLowerCase();
};
/**
* Sign message with address private key
*
* @param {string} message message to sign
* @param {string} privateKey private key
* @returns signed message
*/
export const signMessage = (message, privateKey) => {
const accounts = new Accounts();
return accounts.sign(message, privateKey);
};