本页目录
签名
发送请求前先连接钱包。将示例地址和 manifest URL 替换为有效值。用于身份验证时,使用服务器生成的一次性挑战,并在服务器验证证明。
了解如何签署数据用于链下验证。
签署任意数据
签署数据 cell 用于链下验证:
const result = await provider.send({
method: 'signData',
id: Date.now().toString(),
params: [
JSON.stringify({
schema_crc: 0, // Custom schema identifier
cell: cellBoc.toString('base64'), // Base64 encoded cell to sign
})
]
})
if ('result' in result) {
console.log('Signature:', result.result.signature) // Base64 signature
console.log('Timestamp:', result.result.timestamp) // UNIX timestamp
}签署证明(身份验证)
在连接时请求证明用于身份验证:
const connectEvent = await provider.connect(2, {
manifestUrl: 'https://yourapp.com/tonconnect-manifest.json',
items: [
{ name: 'ton_addr' },
{
name: 'ton_proof',
payload: 'your-server-generated-challenge' // Nonce for replay protection
}
]
})
if (connectEvent.event === 'connect') {
const proofItem = connectEvent.payload.items.find(i => i.name === 'ton_proof')
if (proofItem && "proof" in proofItem) {
// Send proof to your backend for verification
const proof = proofItem.proof
console.log({
signature: proof.signature,
timestamp: proof.timestamp,
domain: proof.domain,
payload: proof.payload,
})
}
}错误处理
const result = await provider.send({
method: 'sendTransaction',
id: Date.now().toString(),
params: [/* ... */]
})
if ('error' in result) {
switch (result.error.code) {
case 300:
console.log('User rejected the transaction')
break
case 1:
console.log('Bad request - check parameters')
break
case 400:
console.log('Method not supported')
break
default:
console.error('Error:', result.error.message)
}
} else {
console.log('Success:', result.result)
}错误码
| 错误码 | 描述 |
|---|---|
| 0 | 未知错误 |
| 1 | 请求错误 |
| 100 | 未知应用 |
| 300 | 用户拒绝请求 |
| 400 | 不支持的方法 |
最后更新于