On this page
Signing
The signing examples below use connectSuiAccount() from Getting Started. It returns a Wallet Standard account and its active chain. The examples use @mysten/[email protected]. Even the legacy *TransactionBlock methods now declare Transaction inputs in @mysten/[email protected].
Request permission before signing, select a connected account, and read the active chain. These examples use @mysten/[email protected], matching @mysten/[email protected] accepted by the current provider.
Learn how to sign messages for authentication.
Initialize the account context before the following examples:
const { account, chain } = await connectSuiAccount()Sign Personal Message
Sign arbitrary messages for authentication:
const message = new TextEncoder().encode('Hello, Sui!')
const result = await provider.signPersonalMessage({
account,
chain,
message,
})
console.log({
signature: result.signature, // Base64 encoded signature
bytes: result.bytes, // Signed message bytes
})Sign Message (Legacy)
const message = new TextEncoder().encode('Hello, Sui!')
const result = await provider.signMessage({
account,
message,
})
console.log({
signature: result.signature,
messageBytes: result.messageBytes,
})Verify Signature
import { verifyPersonalMessageSignature } from '@mysten/sui/verify'
const message = new TextEncoder().encode('Hello, Sui!')
const { signature } = await provider.signPersonalMessage({ message, account, chain })
const signerKey = await verifyPersonalMessageSignature(message, signature)
const isExpectedSigner = signerKey.toSuiAddress() === account.address
console.log('Expected signer:', isExpectedSigner)Error Handling
try {
await provider.requestPermissions()
} catch (error) {
if (error.code === 4001) {
console.log('User rejected the connection request')
} else {
console.error('Connection error:', error.message)
}
}Common Error Codes
| Code | Description |
|---|---|
| 4001 | User rejected request |
| 4100 | Unauthorized |
| -32603 | Internal error |
Last updated on