本页目录
Bitcoin (PSBT)
使用动画 UR 码进行 PSBT 离线签名。
数据类型
- 请求/响应 UR 类型:
crypto-psbt - 另见:基础 API(UR 原语和数据结构)
流程
- 使用钱包后端构建未签名的 PSBT
- 将 PSBT 编码为 UR(
crypto-psbt),显示为动画二维码 - 设备扫描 → 返回已签名的 PSBT(
crypto-psbt) - 完成并广播
提示:保持 PSBT 大小在实际二维码限制内;根据需要拆分为多帧。
示例
安装 @keystonehq/bc-ur-registry 和 @ngraveio/bc-ur,并提供 Buffer 实现。请确认目标设备和固件支持 PSBT。
钱包应用负责提供未签名交易数据,以及设备导出的实际账户路径和主指纹。二维码渲染器应通过定时器调用 nextFrame(),并在签名对话框关闭时停止。以下片段只演示传输数据的编码与解码,不会提交交易。
编码未签名 PSBT → 动画二维码
import { CryptoPSBT } from '@keystonehq/bc-ur-registry';
const psbtBytes = Buffer.from(unsignedPsbtBase64, 'base64');
const req = new CryptoPSBT(psbtBytes);
const encoder = req.toUREncoder(200);
const nextFrame = () => encoder.nextPart(); // Call from your QR animation timer.解码动画二维码 → 已签名 PSBT
import { URDecoder } from '@ngraveio/bc-ur';
import { CryptoPSBT } from '@keystonehq/bc-ur-registry';
const dec = new URDecoder();
// For each scanned frame string from camera:
// dec.receivePart(frameString)
if (dec.isError()) throw new Error('QR decoding failed');
if (dec.isSuccess()) {
const ur = dec.resultUR();
if (ur.type !== 'crypto-psbt') throw new Error('Expected crypto-psbt');
const psbt = CryptoPSBT.fromCBOR(ur.cbor);
const signedPsbt = psbt.getPSBT(); // Buffer of PSBT (signed or partially signed)
// Check that the unsigned transaction, inputs, and outputs match the original PSBT.
// Verify signatures before finalizing and broadcasting with your backend.
}最后更新于