Transaction signing


(manoj) #1

Please find the below code and generated exception while signing the transaction using bitcore api:

Code:

            var bitcore=require('bitcore-lib'); 
            var Insight=require('bitcore-explorers');
            bitcore.Networks.defaultNetwork = 'testnet';
            var privateKey=new bitcore.PrivateKey();
            
            // unspent bitcoins
            var                utxo={"address":"n3RWEaDzXU8fg4p8TQR7S7CHnLbqCiMTdG","txid":"6ba120f57a41eb270fc511d3240fa255a94fc194f2de80df3a7a3daee9560240","vout":1,"ts":1455168824867,"scriptPubKey":"76a914f04aef02c90c5d0c78344fd084ad41246686404288ac","amount":0.15,"confirmations":6132};
            
             var tx = new bitcore.Transaction()
            .from(utxo)
            .to('n2FWZXavp797Ha5pGQcHfmJJsQRvF8DfzK', 50000)
            .change('n3RWEaDzXU8fg4p8TQR7S7CHnLbqCiMTdG')
            .fee(10000)
            .sign(privateKey);

             console.log("transaction = "+tx.verify() );
            console.log("Signature = "+tx.isFullySigned()); 
             console.log("Transaction = "+JSON.stringify(tx)); 
                             
                            
            var txSerialized = tx.serialize();
            insight.broadcast(txSerialized, function(err, txid) {
               if (err) {
                            console.log('Error in transaction'+err);
               } else {
                            console.log('Transaction should be broadcast'+txid);
               }
            });

Output:

transaction = true
Signature = false
Transaction = {“hash”:“6427c7841d1d2b470d58c7f21d795143ee85a687d7b971689a72632d27018b7b”,“version”:1,“inputs”:[{“prevTxId”:“6ba120f57a41eb270fc511d3240fa255a94fc194f2de80df3a7a3daee9560240”,“outputIndex”:1,“sequenceNumber”:4294967295,“script”:"",“scriptString”:"",“output”:{“satoshis”:15000000,“script”:“76a914f04aef02c90c5d0c78344fd084ad41246686404288ac”}}],“outputs”:[{“satoshis”:50000,“script”:“76a914e36eebc8f1aec06e2612b2ad5b959042bb7552ba88ac”},{“satoshis”:14940000,“script”:“76a914f04aef02c90c5d0c78344fd084ad41246686404288ac”}],“nLockTime”:0,“changeScript”:“OP_DUP OP_HASH160 20 0xf04aef02c90c5d0c78344fd084ad412466864042 OP_EQUALVERIFY OP_CHECKSIG”,“changeIndex”:1,“fee”:10000}
bitcore-lib.js:9334 Uncaught bitcore.ErrorTransactionMissingSignatures: Some inputs have not been fully signed Use Transaction#uncheckedSerialize if you want to skip security checks. See http://bitcore.io/guide/transaction.html#Serialization for more info.Transaction.checkedSerialize @ bitcore-lib.js:9334Transaction.serialize @ bitcore-lib.js:9314getTransaction @ (index):229jQuery.event.dispatch @ jquery.js:5227elemData.handle @ jquery.js:4879

Please help…

Regards,
Manoj


#2

You’re creating a completely new private key and attempting to sign the transaction with it.

You need to sign the transaction with the private key corresponding to the address in the UTXO. (n3RWEaDzXU8fg4p8TQR7S7CHnLbqCiMTdG). Otherwise you could spend anyones btc.


(manoj) #3

Thanks, It resolved my problem…