如何从XDR解码的交易信封中提取已签名公钥?
从Diamante Testnet交易信封提取已签名公钥
问题描述
我正在处理一个从XDR解码得到的交易信封,尝试从中提取已签名公钥。以下是该信封的截断版本:
{ "_networkPassphrase": "Diamante Testnet", "_tx": {...:...}, "_signatures": [ { "_attributes": { "hint": { "type": "Buffer", "data": [ 41, 45, 157, 51 ] }, "signature": { "type": "Buffer", "data": [ 101, 246, 73, 218, 181, 85, 247, 161, 150, 190, 226, 209, 120, 177, 38, 199, 4, 28, 46, 104, 141, 211, 45, 114, 239, 224, 135, 240, 178, 124, 255, 118, 79, 193, 249, 82, 114, 111, 168, 70, 62, 213, 115, 79, 215, 94, 60, 11, 114, 23, 227, 251, 17, 28, 219, 16, 169, 93, 51, 216, 35, 12, 148, 14 ] } } } ], ... }我曾尝试解码hint字段但未成功,恳请指导如何从该交易信封中提取已签名公钥。
解决方案
1. 明确hint字段的限制
hint只是公钥的最后4字节,仅用于快速匹配交易中的签名者,无法直接通过它还原完整公钥。必须结合交易本身的签名者信息或使用协议SDK解析。
2. 使用Diamante/Stellar SDK直接解析
Diamante基于Stellar协议,可借助Stellar SDK直接解析交易信封,提取签名对应的公钥:
const { TransactionEnvelope, Networks } = require('stellar-sdk'); // 方法1:从原始XDR字符串解析 const xdrEnvelope = "你的完整交易信封XDR字符串"; const envelope = TransactionEnvelope.fromXDR(xdrEnvelope, "Diamante Testnet"); // 遍历提取所有已签名公钥 envelope.signatures.forEach(sig => { console.log("已签名公钥:", sig.publicKey().toString()); }); // 方法2:从解码后的JSON构造解析 const { xdr } = require('stellar-sdk'); // 构造签名对象 const signatureHint = new xdr.SignatureHint(new Uint8Array([41,45,157,51])); const signatureValue = new xdr.DecoratedSignature({ hint: signatureHint, signature: new Uint8Array([101,246,73,218,...]) // 填入完整的signature data数组 }); // 从_tx字段的XDR构造交易对象 const tx = Transaction.fromXDR("你的交易XDR字符串", "Diamante Testnet"); const envelope = new TransactionEnvelope(tx, [signatureValue]); // 提取公钥 tx.signatures.forEach(sig => { console.log("已签名公钥:", sig.publicKey().toString()); });
3. 从交易签名者列表匹配
如果能获取_tx的完整内容,可通过以下步骤匹配:
- 从
_tx的签名者列表中提取所有公钥(通常在_attributes.signers路径下) - 将每个公钥转换为Buffer,截取最后4字节
- 和
hint的data数组对比,匹配项即为目标公钥
内容的提问来源于stack exchange,提问作者Akhil
相关产品推荐
相关产品推荐

