Signing
Algorand message signing is suitable for login verification, proof of address ownership, and off-chain authorization. Add the application name, nonce and expiration time before signing.
Sign any message
const message = new TextEncoder().encode("Algorand sample message");
const { signature, address } = await provider.signMessage(
message,
"utf8", // 也可以改成 'base64'
);
console.log("Generated signature:", signature);
console.log("Recovered signer:", address);
Verification
import nacl from "tweetnacl";
const message = new TextEncoder().encode("Algorand sample message");
const { signature } = await provider.signMessage(message);
// Retrieve public key from address
const publicKey = algosdk.decodeAddress(provider.address).publicKey;
const isValid = nacl.sign.detached.verify(message, signature, publicKey);
console.log("Validation result:", isValid);
Handle Errors
try {
const signedTxns = await provider.signTxns(walletTxns);
} catch (error) {
if (error.code === 4001) {
console.log("The user declined the request");
} else if (error.code === 4100) {
console.log("Wallet is locked");
} else {
console.error("Operation error:", error.message);
}
}
Common error codes
| Code | Details |
|---|---|
| 4001 | User rejected the request |
| 4100 | Authorization required |
| 4200 | Action is unavailable |
| 4300 | Input value is invalid |