Skip to Content
dApps

Signing

Connect the provider before signing and confirm the selected network. The direct injected provider and the Aptos Wallet Standard adapter have different argument types; do not interchange their examples. Replace abbreviated addresses and provide an SDK client for read-only calls.

Learn how to sign messages for authentication or verification.


Sign Message

Sign arbitrary messages for authentication or verification:

const message = 'Welcome to MyApp!\n\nClick to sign in.\n\nNonce: abc123' const response = await provider.signMessage({ message, nonce: 12345, // Example only; obtain a single-use challenge from your server }) console.log({ signature: response.signature, // Hex-encoded signature fullMessage: response.fullMessage, // Full signed message prefix: response.prefix, // Message prefix used })

Sign Message Options

const response = await provider.signMessage({ message: 'Hello Aptos', nonce: 12345, address: true, // Include address in message application: true, // Include app info chainId: true, // Include chain ID })

Verify Message (Off-chain)

import nacl from 'tweetnacl' const { signature, fullMessage } = await provider.signMessage({ message: 'Hello Aptos', nonce: 12345, }) const account = await provider.account() const pubKeyBytes = Buffer.from(account.publicKey.slice(2), 'hex') const signatureBytes = Buffer.from(signature.slice(2), 'hex') const messageBytes = Buffer.from(fullMessage) const isValid = nacl.sign.detached.verify(messageBytes, signatureBytes, pubKeyBytes) console.log('Signature valid:', isValid)

Sign In (AIP Standard)

For standardized sign-in flow:

const signInInput = { // Sign-in parameters } const result = await provider.signIn(signInInput) console.log({ address: result.account.address, publicKey: result.account.publicKey, signature: result.signature, })

Error Handling

try { await provider.signMessage({ message: 'Hello', nonce: 12345 }) } catch (error) { switch (error.code) { case 4001: console.log('User rejected the request') break case 4100: console.log('The account or method is not authorized') break case 4200: console.log('Unsupported method') break default: console.error('Error:', error.message) } }

The direct signMessage method uses a numeric nonce. signMessageCompatible and Wallet Standard integrations use a string nonce. Verify the returned fullMessage, signer, and challenge, and invalidate the challenge after use. The sign-in input above is a schematic placeholder; construct the full AptosSignInInput required by your adapter version.

Last updated on