Bitcoinjs-lib: Generate Litecoin testnet transaction from P2SH inputs

Created on 13 May 2018  路  6Comments  路  Source: bitcoinjs/bitcoinjs-lib

I want to create litecoin testnet transaction from P2SH inputs.

I read some example (ex: Create (and broadcast via 3PBP) a typical Transaction)

and, wrote the following code.

const bitcoin = require('bitcoinjs-lib')
// network_litecoin_testnet_tprv
const ltcnet = {
    messagePrefix: '\x19Litecoin Signed Message:\n',
    bip32: { // tpub, tprv
      public: 0x043587cf,
      private: 0x04358394
    },
    pubKeyHash: 0x6f,
    scriptHash: 0xc4, //  2 start address
    wif: 0xef
  }

const utxo = {
    txid : '6249981660b36827d2671fc4aa443b3d01c3654bfe99aacf6b56e2a699d35822', // has 100000000 litoshis
    vout : 1
}

var key = bitcoin.HDNode.fromSeedHex(seed, ltcnet)
                              .deriveHardened(49) // bip49
                              .deriveHardened(100002) // litecoin testnet
                              .deriveHardened(0).toBase58(); 
const node = bitcoin.HDNode.fromBase58(key, ltcnet)
                        .derive(0)
                        .derive(0); // address: QRsjxmgiTWWUN5zithCPttiTzJZ4QuSGsM
const keyPair = bitcoin.ECPair.fromWIF(node.keyPair.toWIF(), ltcnet);

const txb = new bitcoin.TransactionBuilder(ltcnet);
txb.addInput(utxo.txid, utxo.vout)
txb.addOutput('mzRFyFD6YvcEJjvx5Jm8sXsMYT3m2DymSs', 10000000)
txb.addOutput('QRsjxmgiTWWUN5zithCPttiTzJZ4QuSGsM', 89990000)

txb.sign(0, keyPair)
const rawTx = txb.build().toHex()
console.log('rawTx: ' + rawTx) 

shown rawTx is:

01000000012258d399a6e2566bcfaa99fe4b65c3013d3b44aac41f67d22768b36016984962010000006a47304402206f1ade6b0870b430857edf1d9f826ef321658575cccc8ac29fa9c58e5d093bb402201dd137f25229c2b1e259c51eca36b604685fabf3cd54a48ba02595cafb8f1ccd0121030ae89c930c477a5e2a3d1360700edd9e1a6e52357492d12ec230e5f89190b7b9ffffffff0280969800000000001976a914cf56a629660e236e88915ee72529f825cc9dfccc88ac70235d050000000017a91439d379972c41d0f503804fe95c7e6ff85df09c1a8700000000

then I broadcasted the transaction by litecoind sendrawtransaction , but I receive the following error.

MBP:  ../litecoin/src > ./litecoin-cli -testnet sendrawtransaction 0100.... 87000000000

error code: -26
error message:
16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element)

Is my network object is wrong?

How should I do to create valid transaction from P2SH inputs?

I'm using parameters defined in: ltcd/chaincfg/params.go

Any helps and comments are appreciated.

how to / question / docs

Most helpful comment

@dabura667 This example is worked for me perfectly.
I really appreciate your help. thank you!

All 6 comments

BIP49 P2WKH-in-P2SH transactions are a different example.

The Example which I used is wrong? I didn't notice.
Could you give me some example or advice to fix this error?
I want to develop litecoin testnet wallet with xpub format and P2SH address.

@dabura667 This example is worked for me perfectly.
I really appreciate your help. thank you!

Hey, this doesn't work anymore.
Specifically the line
var scriptPubKey = bitcoin.script.scriptHash.output.encode(redeemScriptHash)
gives a TypeError: Cannot read property 'output' of undefined. Can you give an updated example?
Thanks

check the updated file.

the file link above is anchored to the most recent commit at the time.

if you look at it now it's updated

Was this page helpful?
0 / 5 - 0 ratings

Related issues

namnv04 picture namnv04  路  3Comments

ishwarchandratiwari picture ishwarchandratiwari  路  3Comments

panpan2 picture panpan2  路  3Comments

dcousens picture dcousens  路  3Comments

Mr-Mondragon picture Mr-Mondragon  路  3Comments