Signing
Sui message signature is suitable for login authentication, address ownership proof and off-chain authorization. Before signing, please ensure that the message contains the nonce, application source and necessary context to avoid the signature being reused.
Sign personal message
Use signPersonalMessage to initiate personal message signature:
const message = new TextEncoder().encode("Sui sample payload");
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
const callResult = await provider.signPersonalMessage({
message,
account,
chain,
});
console.log({
signature: callResult.signature, // Note: Base64 encoded signature
bytes: callResult.bytes, // Note: Signed message bytes
});
Signed message (old version)
const message = new TextEncoder().encode("Sui sample payload");
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
const callResult = await provider.signMessage({
message,
account,
chain,
});
console.log({
signature: callResult.signature,
messageBytes: callResult.messageBytes,
});
Verification
import { verifyPersonalMessage } from "@mysten/sui.js/verify";
const message = new TextEncoder().encode("Sui sample payload");
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
const { signature } = await provider.signPersonalMessage({
message,
account,
chain,
});
const accounts = await provider.getAccounts();
const publicKey = accounts[0].publicKey;
const isValid = await verifyPersonalMessage(message, signature);
console.log("Signature check passed:", isValid);
Handle Errors
try {
await provider.requestPermissions();
} catch (error) {
if (error.code === 4001) {
console.log("The connection request was declined");
} else {
console.error("Connection issue:", error.message);
}
}
Common error codes
| Code | Details |
|---|---|
| 4001 | User rejected the request |
| 4100 | Authorization required |
| -32603 | Provider internal failure |