I was trying different things to verify the signature but couldn't succeed. I have got the following: message in string or decimal array (ASCII code for each char) public key in decimal array (which I can change it into HASH160(Publick Key) using RIPEMD160(SHA256(Public Key))) signature in decimal array Now I wanted to verify that the signature is valid or not using java8 (OpenJDK8), please help!
2 Answers
The code you are using to verify the signature is correct according to the bitcoinj-core 0.15.7 library. What may be wrong is the data you are verifying.
When spending a Transaction Output in a new transaction, the entire new transaction (with a few modifications) is signed to prevent malleability. For a simple transaction the "signing type", or SIGHASH
, is typically SIGHASH_ALL
and is represented by the number 0x01000000
(little-endian). This means that, for every transaction input you sign, the output's scriptSig
is placed where the signature would go for that input, all other input signature fields are set to empty, and the SIGHASH
is appended to the end of the transaction.
Details for verifying a standard transaction has already been answered, and includes step-by-step instructions: How to redeem a basic Tx?
public static boolean verifySignature(byte[] data, byte[] signature, byte[] pub) throws SignatureDecodeException, NoSuchAlgorithmException { boolean verified = false; reverse(data); verified = ECKey.verify(data, signature, pub); return verified; } Sometimes reversing the data alone helped getting as TRUE.
public static boolean verifySignature(byte[] data, byte[] signature, byte[] pub) throws SignatureDecodeException { boolean verified = false; verified = ECKey.verify(data, signature, pub); return verified; }