XRP转账函数异常:tefBAD_AUTH与LastLedgerSequence过期问题排查
XRPL转账交易报错tefBAD_AUTH与LastLedgerSequence过期问题
问题描述
开发类Web3的XRPL DApp时,使用官方教程提供的转账函数,执行后返回tefBAD_AUTH错误,同时触发XrplError:最新账本序列36750915大于交易的LastLedgerSequence(36750914)。所用地址在钱包中可正常使用。
代码
const xrpl = require("xrpl"); const poolSecret = "sEd**********************"; const poolWallet = xrpl.Wallet.fromSeed(poolSecret) async function main(addr) { console.log("Connecting to Testnet...") const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233') await client.connect() // Prepare transaction ------------------------------------------------------- const prepared = await client.autofill({ "TransactionType": "Payment", "Account": addr, "Amount": xrpl.xrpToDrops("1"), "Destination": poolWallet.address }) const max_ledger = prepared.LastLedgerSequence console.log("Prepared transaction instructions:", prepared) console.log("Transaction cost:", xrpl.dropsToXrp(prepared.Fee), "XRP") console.log("Transaction expires after ledger:", max_ledger) // Sign prepared instructions ------------------------------------------------ const signed = poolWallet.sign(prepared) console.log("Identifying hash:", signed.hash) console.log("Signed blob:", signed.tx_blob) // Submit signed blob -------------------------------------------------------- try { const tx = await client.submitAndWait(signed.tx_blob) // Wait for validation ------------------------------------------------------- // submitAndWait() handles this automatically, but it can take 4-7s. // Check transaction results ------------------------------------------------- console.log("Transaction result:", tx.result.meta.TransactionResult) console.log("Balance changes:", JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)) // End of main() client.disconnect() } catch (e) { console.log(e) } } main("rf*******************************");
完整日志
Connecting to Testnet... Prepared transaction instructions: { TransactionType: 'Payment', Account: 'rf*******************************', Amount: '1000000', Destination: 'ra*******************************', Flags: 0, Sequence: 36726053, Fee: '12', LastLedgerSequence: 36750914 } Transaction cost: 0.000012 XRP Transaction expires after ledger: 36750914 Identifying hash: 79D3030E5C7F53B04C7FD9C4BC0D7D65BEEEAFA2787BB72C87CA8F6B553BA32B Signed blob: 12000022000000002402306525201B0230C6426140000000000F424068400000000000000C7321EDA47EA21FCF73BF28D57230BCECB075B2A710C0A15E1D49DA2CA30AE672D667067440B5B294834715D70A90DEFECCD66C7AF839FEE250ED078249C2D85315F66D562856025B2812F1285065D4BABC638DC6488F604C4381D9EFF91E6C8795E09E810581144A0CD16399904DE78F0B132D8CC7105091BB911A83143FF554299C3A982782E1201E87E73B0284D73F79 **XrplError: The latest ledger sequence 36750915 is greater than the transaction's LastLedgerSequence (36750914).** Preliminary result: tefBAD_AUTH at /home/barnyjake/work/xrp/node_modules/xrpl/dist/npm/sugar/submit.js:65:19 at Generator.next (<anonymous>) at fulfilled (/home/barnyjake/work/xrp/node_modules/xrpl/dist/npm/sugar/submit.js:5:58) { data: undefined }
环境信息
- 系统:Ubuntu 20.04
- Node.js版本:v14、v16均已测试
问题原因及解决方案
核心原因
- 签名主体错误:交易的
Account字段是付款方地址,但代码中用收款方的poolWallet签名,权限校验不通过导致tefBAD_AUTH。 - LastLedgerSequence过期:交易准备到签名提交的间隙,XRPL账本已更新,导致设置的过期账本序列小于当前最新序列,交易直接失效。
修复步骤
- 修正签名主体:使用付款方的钱包实例签名交易,而非收款方钱包。需要获取付款方的密钥,创建对应
Wallet对象。 - 优化交易生命周期:
- 减少交易准备到提交之间的延迟操作,避免账本序列过期;
- 若存在延迟风险,可手动在
autofill返回的LastLedgerSequence基础上增加10-20,延长交易有效期; - 提交前重新调用
autofill刷新账本序列信息。
修复后代码片段
// 替换为付款方的密钥 const payerSecret = "sEd**********************"; const payerWallet = xrpl.Wallet.fromSeed(payerSecret) // ... 其他代码保持不变 // 使用付款方钱包签名交易 const signed = payerWallet.sign(prepared)
额外建议
- 可在捕获
XrplError后,自动重试交易准备、签名、提交流程,适配网络延迟场景。
内容的提问来源于stack exchange,提问作者John Alim
相关产品推荐
相关产品推荐

