public static byte [ ] getTransactionHash ( BitcoinTransaction transaction ) throws IOException { // convert transaction to byte array ByteArrayOutputStream transactionBAOS = new ByteArrayOutputStream ( ) ; byte [ ] version = reverseByteArray ( convertIntToByteArray ( transaction . getVersion ( ) ) ) ; transactionBAOS . write ( version ) ; byte [ ] inCounter = transaction . getInCounter ( ) ; transactionBAOS . write ( inCounter ) ; for ( int i = 0 ; i < transaction . getListOfInputs ( ) . size ( ) ; i ++ ) { transactionBAOS . write ( transaction . getListOfInputs ( ) . get ( i ) . getPrevTransactionHash ( ) ) ; transactionBAOS . write ( reverseByteArray ( convertIntToByteArray ( ( int ) ( transaction . getListOfInputs ( ) . get ( i ) . getPreviousTxOutIndex ( ) ) ) ) ) ; transactionBAOS . write ( transaction . getListOfInputs ( ) . get ( i ) . getTxInScriptLength ( ) ) ; transactionBAOS . write ( transaction . getListOfInputs ( ) . get ( i ) . getTxInScript ( ) ) ; transactionBAOS . write ( reverseByteArray ( convertIntToByteArray ( ( int ) ( transaction . getListOfInputs ( ) . get ( i ) . getSeqNo ( ) ) ) ) ) ; } byte [ ] outCounter = transaction . getOutCounter ( ) ; transactionBAOS . write ( outCounter ) ; for ( int j = 0 ; j < transaction . getListOfOutputs ( ) . size ( ) ; j ++ ) { transactionBAOS . write ( convertBigIntegerToByteArray ( transaction . getListOfOutputs ( ) . get ( j ) . getValue ( ) , 8 ) ) ; transactionBAOS . write ( transaction . getListOfOutputs ( ) . get ( j ) . getTxOutScriptLength ( ) ) ; transactionBAOS . write ( transaction . getListOfOutputs ( ) . get ( j ) . getTxOutScript ( ) ) ; } byte [ ] lockTime = reverseByteArray ( convertIntToByteArray ( transaction . getLockTime ( ) ) ) ; transactionBAOS . write ( lockTime ) ; byte [ ] transactionByteArray = transactionBAOS . toByteArray ( ) ; byte [ ] firstRoundHash ; byte [ ] secondRoundHash ; try { MessageDigest digest = MessageDigest . getInstance ( "SHA-256" ) ; firstRoundHash = digest . digest ( transactionByteArray ) ; secondRoundHash = digest . digest ( firstRoundHash ) ; } catch ( NoSuchAlgorithmException nsae ) { LOG . error ( nsae ) ; return new byte [ 0 ] ; } return secondRoundHash ; }
Calculates the double SHA256 - Hash of a transaction in little endian format . This could be used for certain analysis scenario where one want to investigate the referenced transaction used as an input for a Transaction . Furthermore it can be used as a unique identifier of the transaction