How serialize, deserialize Transactions and how sign the deserialized Transaction on Bitcoinj?

How serialize, deserialize Transactions and how sign the deserialized Transaction on Bitcoinj?

Goog morning,

i'm trying to create an application with a watching wallet, connected to the blockchain, and another application with a wallet, with private key, not connected to it, in order to mantain the private key secret and offline. I choose bitcoinj as java library and i would like to create the transaction on the watching wallet's application, send it serialized in an hexadecimal string to the privat wallet's application, in order to be signed by it, here the code i use for the serialization of the transaction:

String hexTx = DatatypeConverter.printHexBinary(tx.bitcoinSerialize());

The problem is that i can't sign the transaction i deserialized from the string, here the code i use for deserilize the string and for sign the retured transaction:

byte[] serializedTx = hexStringToByteArray(hexTx);

Transaction deserializedTx = new Transaction(params,serializedTx);

log.debug(deserializedTx.toString());

SendRequest req = SendRequest.forTx(tx);

coldWallet.signTransaction(req);

Where the method hexStringToByteArray(hexTx) is defined like this:

private static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

For a better understanding of the problem here the log of the transaction before the serializzation:

2017-11-06 17:56:31,248 INFO i.i.b.TestRawTransaction [main]   5c3996212e48725e69eadcef02b4ea2826e0198e14ed9dd2002d8db0b3e708c7
     in   <no scriptSig> 
          outpoint:8bfcfba135101c5d15641602881acc4799069ea1885e93c6e6d9f07d2dd7802f:0 hash160:984201354d5781a017d3be3a006ae6828c553844
     out  DUP HASH160 PUSHDATA(20)[f0497fd691918563d96258eb7e7bdbf3f88a8452] EQUALVERIFY CHECKSIG 959.9763362 BTC
     out  RETURN PUSHDATA(5)[68656c6c6f] 0.0000273 BTC
     fee  0.00495049 BTC/kB, 0.0005 BTC for 101 bytes
     prps USER_PAYMENT

When i deserialize the hex string back to a Transaction object i obtain this log:

2017-11-06 17:56:31,252 INFO i.i.b.TestRawTransaction [main] Transazione deserializzata:
  5c3996212e48725e69eadcef02b4ea2826e0198e14ed9dd2002d8db0b3e708c7
     in   <no scriptSig>
          outpoint:8bfcfba135101c5d15641602881acc4799069ea1885e93c6e6d9f07d2dd7802f:0
     out  DUP HASH160 PUSHDATA(20)[f0497fd691918563d96258eb7e7bdbf3f88a8452] EQUALVERIFY CHECKSIG 959.9763362 BTC
     out  RETURN PUSHDATA(5)[68656c6c6f] 0.0000273 BTC
     prps UNKNOWN

I lost some data in the process but i can't say why, infact i compare the two byte[], the one after the serializzation with the one after the deserializzation, but they are identical.

When i try to sign the Transaction above with the method coldWallet.signTransaction(req), the input is not signed and the transaction broadcasted on the blockchain is discarded.

Why the wallet doesn't sign the transaction? Could it be caused by the lost of the data? I rather think so becouse when i try to broadcast the transaction, peerGroup.broadcastTransaction(req.tx), those data are recalculated.

Could be that i used the wrong method of serialization/deserialization of the Transaction? There's another way of implement it?

Thanks for the support.

http://ift.tt/2yEEevK

Comments

Popular posts from this blog

Antminer D3 Blissz Modified Firmware For Improved Performance

ETH To The Moon / Bank of England Cryptocurrency? / BTC Dominance / More (The Crypt0 Minute)