@@ -69,7 +69,7 @@ public byte[] createHash(Transaction unsignedTxn, int sigHashType, int inputInde
69
69
sigHashType = (newForkValue << 8 ) | (sigHashType & 0xff );
70
70
}
71
71
72
- if (((sigHashType & SigHashType .FORKID .byteValue () ) != 0 ) && (flags & ENABLE_SIGHASH_FORKID ) != 0 ) {
72
+ if (((sigHashType & SigHashType .FORKID .value ) != 0 ) && (flags & ENABLE_SIGHASH_FORKID ) != 0 ) {
73
73
return sigHashForForkid (txnCopy , sigHashType , inputIndex , subscriptCopy , amount );
74
74
}
75
75
@@ -91,8 +91,8 @@ public byte[] createHash(Transaction unsignedTxn, int sigHashType, int inputInde
91
91
92
92
//txnCopy.serialize(false); //FIXME: why are we serializing ? what side-effect is triggered here on internal state ?
93
93
94
- if ((sigHashType & 31 ) == SigHashType .NONE .byteValue () ||
95
- (sigHashType & 31 ) == SigHashType .SINGLE .byteValue () ) {
94
+ if ((sigHashType & 31 ) == SigHashType .NONE .value ||
95
+ (sigHashType & 31 ) == SigHashType .SINGLE .value ) {
96
96
// clear all sequenceNumbers
97
97
int ndx = 0 ;
98
98
for (TransactionInput input : txnCopy .getInputs ()){
@@ -103,9 +103,9 @@ public byte[] createHash(Transaction unsignedTxn, int sigHashType, int inputInde
103
103
};
104
104
}
105
105
106
- if ((sigHashType & 31 ) == SigHashType .NONE .byteValue () ) {
106
+ if ((sigHashType & 31 ) == SigHashType .NONE .value ) {
107
107
txnCopy .clearOutputs ();
108
- } else if ((sigHashType & 31 ) == SigHashType .SINGLE .byteValue () ) {
108
+ } else if ((sigHashType & 31 ) == SigHashType .SINGLE .value ) {
109
109
// The SIGHASH_SINGLE bug.
110
110
// https://bitcointalk.org/index.php?topic=260595.0
111
111
if (inputIndex >= txnCopy .getOutputs ().size ()) {
@@ -139,7 +139,7 @@ public byte[] createHash(Transaction unsignedTxn, int sigHashType, int inputInde
139
139
txnCopy .replaceOutput (inputIndex , txout ); //FIXME : ??? Is this the correct way ?
140
140
}
141
141
142
- if ((this ._sigHashType & SigHashType .ANYONECANPAY .byteValue () ) > 0 ) {
142
+ if ((this ._sigHashType & SigHashType .ANYONECANPAY .value ) > 0 ) {
143
143
TransactionInput keepInput = txnCopy .getInputs ().get (inputIndex );
144
144
txnCopy .clearInputs ();
145
145
txnCopy .addInput (keepInput );
@@ -153,8 +153,8 @@ private byte[] getPrevoutHash(Transaction tx) throws IOException {
153
153
WriteUtils writer = new WriteUtils ();
154
154
155
155
for (TransactionInput input : tx .getInputs ()){
156
- byte [] prevTxId = input .getPrevTxnId ();
157
- writer .writeBytes (prevTxId , prevTxId .length ); //FIXME: This was reversed. LE ?
156
+ byte [] prevTxId = Utils . reverseBytes ( input .getPrevTxnId () );
157
+ writer .writeBytes (prevTxId , prevTxId .length );
158
158
writer .writeUint32LE (input .getPrevTxnOutputIndex ());
159
159
}
160
160
@@ -205,19 +205,19 @@ private byte[] sigHashForForkid(Transaction txnCopy, int sigHashType, int inputI
205
205
byte [] hashSequence = new byte [32 ];
206
206
byte [] hashOutputs = new byte [32 ];
207
207
208
- if (!((sigHashType & SigHashType .ANYONECANPAY .byteValue () ) > 0 )) {
208
+ if (!((sigHashType & SigHashType .ANYONECANPAY .value ) > 0 )) {
209
209
hashPrevouts = getPrevoutHash (txnCopy );
210
210
}
211
211
212
- if (!((sigHashType & SigHashType .ANYONECANPAY .byteValue () ) > 0 ) &&
213
- ((sigHashType & 31 ) != SigHashType .SINGLE .byteValue () ) &&
214
- ((sigHashType & 31 ) != SigHashType .NONE .byteValue () )) {
212
+ if (!((sigHashType & SigHashType .ANYONECANPAY .value ) > 0 ) &&
213
+ ((sigHashType & 31 ) != SigHashType .SINGLE .value ) &&
214
+ ((sigHashType & 31 ) != SigHashType .NONE .value )) {
215
215
hashSequence = getSequenceHash (txnCopy );
216
216
}
217
217
218
- if (((sigHashType & 31 ) != SigHashType .SINGLE .byteValue ()) && ((sigHashType & 31 ) != SigHashType .NONE .byteValue () )) {
218
+ if (((sigHashType & 31 ) != SigHashType .SINGLE .value ) && ((sigHashType & 31 ) != SigHashType .NONE .value )) {
219
219
hashOutputs = getOutputsHash (txnCopy , null );
220
- } else if (((sigHashType & 31 ) == SigHashType .SINGLE .byteValue () ) && inputIndex < txnCopy .getOutputs ().size ()) {
220
+ } else if (((sigHashType & 31 ) == SigHashType .SINGLE .value ) && inputIndex < txnCopy .getOutputs ().size ()) {
221
221
hashOutputs = getOutputsHash (txnCopy , inputIndex );
222
222
}
223
223
@@ -231,7 +231,7 @@ private byte[] sigHashForForkid(Transaction txnCopy, int sigHashType, int inputI
231
231
writer .writeBytes (hashSequence , hashSequence .length );
232
232
233
233
// outpoint (32-byte hash + 4-byte little endian)
234
- writer .writeBytes (input .getPrevTxnId (), input .getPrevTxnId ().length );
234
+ writer .writeBytes (Utils . reverseBytes ( input .getPrevTxnId () ), input .getPrevTxnId ().length );
235
235
writer .writeUint32LE (input .getPrevTxnOutputIndex ());
236
236
237
237
// scriptCode of the input (serialized as scripts inside CTxOuts)
@@ -256,7 +256,9 @@ private byte[] sigHashForForkid(Transaction txnCopy, int sigHashType, int inputI
256
256
writer .writeUint32LE (sigHashType >> 0 );
257
257
258
258
byte [] buf = writer .getBytes ();
259
- return Sha256Hash .hashTwice (buf );
259
+ byte [] hash = Sha256Hash .hashTwice (buf );
260
+
261
+ return hash ;
260
262
}
261
263
262
264
@@ -267,7 +269,10 @@ private byte[] getHash(Transaction txn) throws IOException {
267
269
writer .writeBytes (txnBytes , txnBytes .length );
268
270
writer .writeUint32LE (this ._sigHashType );
269
271
270
- return Sha256Hash .hashTwice (writer .getBytes ());
272
+ byte [] preImage = writer .getBytes ();
273
+ String preImageHex = Utils .HEX .encode (preImage );
274
+
275
+ return Sha256Hash .hashTwice (preImage );
271
276
}
272
277
273
278
0 commit comments