本页目录
Tron
Tron 离线签名使用相同的动画二维码传输模式。
数据类型
- 请求 UR:
tron-sign-request - 响应 UR:
tron-signature
示例直接使用 Tron 注册表包。请导入该包重新导出的路径类,确保注册表对象彼此兼容。
流程
- 构建 Tron 签名请求(交易/消息 + BIP-44 路径)
- 编码为 UR,显示为动画二维码
- 扫描设备响应,解码签名
- 广播到 Tron 网络
示例
安装 @keystonehq/bc-ur-registry-tron 和 @ngraveio/bc-ur,并提供 Buffer 实现。能够编码注册表对象不代表设备支持该操作;请确认目标固件接受的 UR 类型。
钱包应用负责提供未签名交易数据,以及设备导出的实际账户路径和主指纹。二维码渲染器应通过定时器调用 nextFrame(),并在签名对话框关闭时停止。以下片段只演示传输数据的编码与解码,不会提交交易。
构建 Tron 签名请求(UR)
import { TronSignRequest, DataType, CryptoKeypath, PathComponent } from '@keystonehq/bc-ur-registry-tron';
// BIP44: m/44'/195'/0'/0/0
const keypath = new CryptoKeypath([
new PathComponent({ index: 44, hardened: true }),
new PathComponent({ index: 195, hardened: true }),
new PathComponent({ index: 0, hardened: true }),
new PathComponent({ index: 0, hardened: false }),
new PathComponent({ index: 0, hardened: false }),
], Buffer.from(masterFingerprintHex, 'hex'));
const requestId = globalThis.crypto.randomUUID();
const requestIdBytes = Buffer.from(requestId.replace(/-/g, ''), 'hex');
const req = new TronSignRequest({
requestId: requestIdBytes,
signData: Buffer.from(rawTxHex, 'hex'),
dataType: DataType.transaction,
derivationPath: keypath,
});
const encoder = req.toUREncoder(200);
const nextFrame = () => encoder.nextPart(); // Call from your QR animation timer.解码 Tron 签名(UR)
import { URDecoder } from '@ngraveio/bc-ur';
import { TronSignature } from '@keystonehq/bc-ur-registry-tron';
const dec = new URDecoder();
// dec.receivePart(frameString) for each scanned frame
if (dec.isError()) throw new Error('QR decoding failed');
if (dec.isSuccess()) {
const ur = dec.resultUR();
if (ur.type !== 'tron-signature') throw new Error('Expected tron-signature');
const sig = TronSignature.fromCBOR(ur.cbor);
const returnedId = sig.getRequestId();
if (!returnedId || !returnedId.equals(requestIdBytes)) {
throw new Error('Signature request ID does not match');
}
const signature = sig.getSignature();
// Verify the signer and signature against the original transaction before broadcasting.
}最后更新于