跳到主要内容

Tron

Tron 离线签名同样使用动画二维码传递请求和响应。应用负责准备交易或消息数据,UKey Wallet 在离线状态下完成签名确认。

数据项

  • 请求 UR:tron-sign-request
  • 响应 UR:tron-signature

Tron 相关助手遵循标准 UR 传输模式,可与二维码扫描和多帧解码流程一起使用。

步骤

  1. 构建 Tron 签名请求,包含交易或消息数据以及 BIP-44 路径。
  2. 将请求编码为 UR,并显示为动画二维码。
  3. 扫描设备返回的二维码,解析签名结果。
  4. 将签名结果用于广播交易或完成业务校验。

示范

构建 Tron 签名请求(UR)

import { TronSignRequest } from "@keystonehq/bc-ur-registry-tron";
import { CryptoKeypath, PathComponent } from "@keystonehq/bc-ur-registry";
import { airGapUrUtils } from "@keystonehq/keystone-sdk";

// path note: 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 }),
]);

const req = new TronSignRequest({
signData: Buffer.from(rawTxHex, "hex"),
derivationPath: keypath,
});

const ur = req.toUR();
const frames = airGapUrUtils.urToQrcode(ur);

解码 Tron 签名(UR)

import { URDecoder } from "@ngraveio/bc-ur";
import { TronSignature } from "@keystonehq/bc-ur-registry-tron";

const dec = new URDecoder();
// 每扫到一帧二维码,就调用一次 dec.receivePart(frameString)
if (dec.isComplete()) {
const ur = dec.resultUR(); // expected UR type: 'tron-signature'
const signatureData = TronSignature.fromCBOR(ur.cbor);
const signature = signatureData.getSignature();
// 把签好的交易发往 Tron 网络
}