用于签署交易的 Litecore-lib 函数无法正常工作

Litecore-lib function to sign transactions does not work propertly

我正在尝试使用 litecore-lib 包在莱特币测试网上创建交易。 我附上代码片段。 我打印每一行来分析交易是如何建立的。 调用 'sign' 方法时没有变化。

const litecore = require('litecore-lib');

var wif = 'cRkP6k8dDab1PDg2SvS8mbKMSxCSJfDFg6hibkKGmrfaU4xaCi9R';
var privateKey = new litecore.PrivateKey(wif, 'testnet');
var address = privateKey.toAddress('testnet');

console.log("Private key:", privateKey.toString());
console.log("WIF Private key:", privateKey.toWIF());
console.log("Address: ", address.toString()); //mmQQ2Mz1UGWtos6fGmM6sTg69itJmvxW9h



var utxo = {
  "txId" : "d095e2e921ac6f0ed5f7f886529c14d662b82feb35e1afa0051a8328855ecdf8",
  "outputIndex" : 0,
  "address" : address.toString(),
  "script" : "76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac",
  "satoshis" : 100000,
  "network":'testnet'
};

var transaction = new litecore.Transaction()
console.log("\nTx Created:");
console.log(transaction.toString());

console.log("\nAdd input:");
transaction = transaction.from(utxo)
console.log(transaction.toString());

console.log("\nAdd output:");
transaction = transaction.to('mi1gqn4z9r1sm4XDjrTS8hegEaAMxYGWkW', 98000)
console.log(transaction.toString());

console.log("\nChange:");
transaction = transaction.change(address.toString())
console.log(transaction.toString());

console.log("\nFee:");
transaction = transaction.fee(1100)
console.log(transaction.toString());

console.log("Is fully signed:", transaction.isFullySigned());

console.log("\nSign:");
transaction = transaction.sign(privateKey)
console.log(transaction.toString());

console.log("Is fully signed:", transaction.isFullySigned());

下面附上输出。请对比签名前后的tx字符串:是一样的!

Private key: 7c54567e49114ffce7c54831f019e07eaf12ab666e2e075afde4699dc3947e96
WIF Private key: cRkP6k8dDab1PDg2SvS8mbKMSxCSJfDFg6hibkKGmrfaU4xaCi9R
Address:  mmQQ2Mz1UGWtos6fGmM6sTg69itJmvxW9h

Tx Created:
01000000000000000000

Add input:
0100000001f8cd5e8528831a05a0afe135eb2fb862d6149c5286f8f7d50e6fac21e9e295d00000000000ffffffff0000000000

Add output:
0100000001f8cd5e8528831a05a0afe135eb2fb862d6149c5286f8f7d50e6fac21e9e295d00000000000ffffffff01d07e0100000000001976a9141b5f6a37c731c1f2e84448c4298352144dadd18088ac00000000

Change:
0100000001f8cd5e8528831a05a0afe135eb2fb862d6149c5286f8f7d50e6fac21e9e295d00000000000ffffffff01d07e0100000000001976a9141b5f6a37c731c1f2e84448c4298352144dadd18088ac00000000

Fee:
0100000001f8cd5e8528831a05a0afe135eb2fb862d6149c5286f8f7d50e6fac21e9e295d00000000000ffffffff02d07e0100000000001976a9141b5f6a37c731c1f2e84448c4298352144dadd18088ac84030000000000001976a9144093522294655efcbfc2bdfdb448682bf3bef69a88ac00000000
Is fully signed: false

Sign:
0100000001f8cd5e8528831a05a0afe135eb2fb862d6149c5286f8f7d50e6fac21e9e295d00000000000ffffffff02d07e0100000000001976a9141b5f6a37c731c1f2e84448c4298352144dadd18088ac84030000000000001976a9144093522294655efcbfc2bdfdb448682bf3bef69a88ac00000000
Is fully signed: false

确保为您的 utxo 设置了正确的脚本。

var utxo = {
  "txId" : "d095e2e921ac6f0ed5f7f886529c14d662b82feb35e1afa0051a8328855ecdf8",
  "outputIndex" : 0,
  "address" : address.toString(),
  "script" : "76a9144093522294655efcbfc2bdfdb448682bf3bef69a88ac",
  // "script" : "76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac",
  "satoshis" : 100000,
  "network":'testnet'
};