bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.7k stars 2.11k forks source link

i still have problem, can you give a example. #1341

Closed tangmingkao closed 5 years ago

tangmingkao commented 5 years ago

i still have problem, can you give a example.

when i use :

function bitcoinSign(privateKey, amount, utxo, sendFee, toAddress, changeAddress) {
    if(!privateKey || !amount || !utxo || !sendFee || !toAddress || !changeAddress ) {
        console.log("one of privateKey, amount, utxo, sendFee, toAddress and changeAddress is null, please give a valid param");
    } else {
        console.log("param is valid, start sign transaction");
        var network = bitcoin.networks.bitcoin;
        var set = bitcoin.ECPair.fromWIF(privateKey,network);

        const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: set.publicKey, network: network });
        const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network: network });
        var redeemScript = p2sh.redeem.output;

        var txb = new bitcoin.TransactionBuilder(network);

        var sendAmount = parseFloat(amount);
        var fee = parseFloat(sendFee);
        sendAmount += fee;
        console.log("Send Transaction total amount is: " + sendAmount)
        txb.setVersion(1);
        var totalMoney = 0;
        for(var i=0; i<utxo.length; i++){
            txb.addInput(utxo[i].tx_hash_big_endian, utxo[i].tx_output_n);
            totalMoney += utxo[i].value;
        }
        console.log("this address total money is: " + totalMoney)
        txb.addOutput(toAddress, sendAmount - fee);
        txb.addOutput(changeAddress,totalMoney-sendAmount);
        for(var i=0;i<utxo.length;i++){
            txb.sign(0,set,redeemScript,null,totalMoney);
        }
    }
    return txb.buildIncomplete().toHex();
}

The first time you can succeed, you can’t succeed again.will get this error: Uncaught (in promise) Error: Signature already exists

Originally posted by @tangmingkao in https://github.com/bitcoinjs/bitcoinjs-lib/issues/1340#issuecomment-467867235

junderw commented 5 years ago

sign i, not 0

also, little endian, not big endian

tangmingkao commented 5 years ago

Thank you, I solved this problem.