blockchainbitcoinlitecoin

Litecore-lib function to sign transactions does not work propertly


I'm trying to create a transaction on litecoin testnet using litecore-lib package. I attach the snippet of the code. I print every line to analyze how the transaction is being built. No changes when calling the 'sign' method.

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());

And below I attach the output. Please compare the tx string before and after signing: they are the same!

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

Solution

  • Make sure you set the correct script for your utxo.

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