本页目录
签名
签名前先连接 provider。交易示例使用 algosdk 2 的 from / to 字段,请安装匹配的 SDK 主版本。运行示例前,需要准备好 algodClient、地址和分组交易。在浏览器中使用 Buffer 时,应显式导入 buffer,或使用等效的 base64 编码工具。
了解如何在 Algorand 上签署消息用于身份验证。
签署任意消息
const message = new TextEncoder().encode('Hello Algorand!')
const { signature, address } = await provider.signMessage(
message,
'utf8' // or 'hex'
)
console.log('Signature:', signature)
console.log('Signer:', address)验证签名
import nacl from 'tweetnacl'
const message = new TextEncoder().encode('Hello Algorand!')
const { signature } = await provider.signMessage(message)
// Get public key from address
const publicKey = algosdk.decodeAddress(provider.address).publicKey
const isValid = nacl.sign.detached.verify(message, signature, publicKey)
console.log('Valid:', isValid)错误处理
try {
const signedTxns = await provider.signTxns(walletTxns)
} catch (error) {
if (error.code === 4001) {
console.log('User rejected the request')
} else if (error.code === 4100) {
console.log('The account or method is not authorized')
} else {
console.error('Error:', error.message)
}
}常见错误码
| 错误码 | 说明 |
|---|---|
| 4001 | 用户拒绝请求 |
| 4100 | 未授权 |
| 4200 | 不支持的方法 |
| -32602 | 参数无效 |
最后更新于