4

I've started creating my own wallet in bitcoinj and I have serious problem. I want to create raw transaction without wallet object. So I wrote this piece of code:

    //String to a private key
    DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, 
            privKey);
    ECKey key = dumpedPrivateKey.getKey();

    //String to an address
    Address address2 = Address.fromBase58(params, address);

    Transaction tx = new Transaction(params);
    //value is a sum of all inputs, fee is 4013
    tx.addOutput(Coin.valueOf(amount-4013), address2);

    //utxos is an array of inputs from my wallet
    for(UTXO utxo : utxos)
    {
        TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash());
        tx.addSignedInput(outPoint, utxo.getScript(), key);
    }

    tx.getConfidence().setSource(TransactionConfidence.Source.SELF);
    tx.setPurpose(Transaction.Purpose.USER_PAYMENT);

    System.out.println(tx.getHashAsString());
    b_peerGroup.GetPeerGroup().broadcastTransaction(tx);

But this ends with:

mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack e

What is wrong with my code?

EDIT: I found a solution. I've changed only this line: tx.addSignedInput(outPoint, utxo.getScript(), key, Transaction.SigHash.ALL, true);

5
  • bitcoin.stackexchange.com/questions/47928/… This might help?
    – hartmut
    Sep 7 '16 at 9:14
  • Unfortunately I've already used this code :(
    – Dragomirus
    Sep 7 '16 at 10:06
  • I believe this means that you provided the right public key, but the signature does not match the transaction. Is the transaction being modified between when it is signed and when it is broadcast?
    – Nick ODell
    Sep 7 '16 at 16:21
  • Already found the solution :) All is under edit
    – Dragomirus
    Sep 8 '16 at 8:26
  • Could you please describe your solution in an answer post rather instead of putting it in the question? Thanks.
    – Murch
    Sep 21 '16 at 14:05
2

Sure :)

My solution was pretty simple:

//String to a private key
DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, 
        privKey);
ECKey key = dumpedPrivateKey.getKey();

//String to an address
Address address2 = Address.fromBase58(params, address);

Transaction tx = new Transaction(params);
//value is a sum of all inputs, fee is 4013
tx.addOutput(Coin.valueOf(amount-4013), address2);

//utxos is an array of inputs from my wallet
for(UTXO utxo : utxos)
{
    TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash());
    //YOU HAVE TO CHANGE THIS
    tx.addSignedInput(outPoint, utxo.getScript(), key, Transaction.SigHash.ALL, true);
}

tx.getConfidence().setSource(TransactionConfidence.Source.SELF);
tx.setPurpose(Transaction.Purpose.USER_PAYMENT);

System.out.println(tx.getHashAsString());
b_peerGroup.GetPeerGroup().broadcastTransaction(tx);
2
  • How you create UTXO? Aug 18 '20 at 12:50
  • Pawan, you need to fetch it before by listunspent api request Nov 22 '20 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.