我正在使用Nano Ledger S生成一个Stellar密钥对,然后使用私钥对随机字符串进行签名(例如“测试”),并希望使用Stellar SDK检索公共密钥并验证签名
我正在使用nano分类帐(https://github.com/LedgerHQ/ledgerjs/tree/master/packages/hw-app-str)提供的Nodejs SDK生成密钥对,并使用signTransaction
函数对字符串进行签名。
但是最后,我尝试使用公钥来验证签名,它只是不断返回false:(
const Transport = require("@ledgerhq/hw-transport-node-hid");
const Str = require("@ledgerhq/hw-app-str");
const StellarSdk = require("stellar-sdk");
// retrieve the public key from the nano ledger
const getStrPublicKey = async(path) => {
try {
const transport = await Transport.default.open();
const str = new Str.default(transport);
const result = await str.getPublicKey(path);
transport.close()
return result.publicKey;
} catch (error) {
console.log(error)
}
}
// sign the raw string using the keypair we created(using path to specify which address to sign)
const signStrTransaction = async (path, raw) => {
try{
console.log("signing message: "+raw)
const transport = await Transport.default.open();
const str = new Str.default(transport);
let buf = new Buffer(raw);
const result = await str.signTransaction(path, buf);
transport.close()
return result
} catch (error){
console.log(error)
}
}
// entry point
async function main(){
let publicKey;
let raw = "test" // raw message waited to be signed
const path = "44'/148'/1'" // the BIP 32 path of the stellar address
getStrPublicKey(path).then(pk => {
console.log(pk);
publicKey = pk;
signStrTransaction(path, raw).then(result =>{
// load the public key retrieved from nano ledger to the keypair
const keypair = StellarSdk.Keypair.fromPublicKey(publicKey)
let buf = new Buffer(raw);
// verify the signature is correct
const valid = keypair.verify(buf, result.signature)
//expected to be true, but got false
console.log(valid)
});
});
}
由于我使用正确的签名和公钥进行验证,因此预期输出应为true,但它只会显示false。 它是否与缓冲区编码有关?(创建缓冲区时我未指定任何编码)
非常感谢您看到我的问题,欢迎提出任何建议和意见!