本页目录
Solana
Solana 离线签名的 UR 注册表类型。
数据类型
- 请求 UR:
sol-sign-request(交易/消息字节 + BIP-44 路径) - 响应 UR:
sol-signature
流程
- 准备 Solana 交易或消息字节,推导路径
- 构建
sol-sign-request,显示为动画二维码 - 扫描设备响应 → 解码
sol-signature→ 附加到交易 - 提交到 Solana 网络
示例
安装 @keystonehq/bc-ur-registry-sol 和 @ngraveio/bc-ur,并提供 Buffer 实现。能够编码注册表对象不代表设备支持该操作;请确认目标固件接受的 UR 类型。
钱包应用负责提供未签名交易数据,以及设备导出的实际账户路径和主指纹。二维码渲染器应通过定时器调用 nextFrame(),并在签名对话框关闭时停止。以下片段只演示传输数据的编码与解码,不会提交交易。
构建 Solana 签名请求(UR)
import { SolSignRequest, SignType, CryptoKeypath, PathComponent } from '@keystonehq/bc-ur-registry-sol';
// BIP44: m/44'/501'/0'
const keypath = new CryptoKeypath([
new PathComponent({ index: 44, hardened: true }),
new PathComponent({ index: 501, hardened: true }),
new PathComponent({ index: 0, hardened: true }),
], Buffer.from(masterFingerprintHex, 'hex'));
const requestId = globalThis.crypto.randomUUID();
const requestIdBytes = Buffer.from(requestId.replace(/-/g, ''), 'hex');
const req = new SolSignRequest({
requestId: requestIdBytes,
signData: Buffer.from(rawTxBytes), // transaction or message bytes
derivationPath: keypath,
signType: SignType.Transaction, // or SignType.Message
});
const encoder = req.toUREncoder(200);
const nextFrame = () => encoder.nextPart(); // Call from your QR animation timer.解码 Solana 签名(UR)
import { URDecoder } from '@ngraveio/bc-ur';
import { SolSignature } from '@keystonehq/bc-ur-registry-sol';
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 !== 'sol-signature') throw new Error('Expected sol-signature');
const sig = SolSignature.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 payload before use.
// Attach transaction signatures to the original transaction; return message signatures to the caller.
}最后更新于