package org.bitcoinj.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Base64;

import java.security.SignatureException;
import java.util.Arrays;

import static org.bitcoinj.core.Utils.HEX;

/**
 * Created by HashEngineering on 6/25/2018.
 */
public class HashSigner {
    private static final Logger log = LoggerFactory.getLogger(HashSigner.class);

    public static MasternodeSignature signHash(Sha256Hash hash, ECKey key) {
        ECKey.ECDSASignature signature = key.sign(hash);
        return new MasternodeSignature(signature.encodeToDER());
    }

    /// Verify the hash signature, returns true if successful
    public static boolean verifyHash(Sha256Hash hash, PublicKey pubkey, MasternodeSignature vchSig, StringBuilder strErrorRet) {
        return verifyHash(hash, pubkey.getId(), vchSig, strErrorRet);
    }
    public static boolean verifyHash(Sha256Hash hash, byte [] pubkeyId, MasternodeSignature vchSig, StringBuilder strErrorRet) {
        ECKey pubkeyFromSig;

        try {
            pubkeyFromSig = ECKey.signedMessageToKey(hash, vchSig.getBytes());
            if (pubkeyFromSig == null) {
                strErrorRet.append("Error recovering public key.");
                return false;
            }
            if (!Arrays.equals(pubkeyFromSig.getPubKeyHash(), pubkeyId)) {
                strErrorRet.append(String.format("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s",
                        HEX.encode(pubkeyId), HEX.encode(pubkeyFromSig.getPubKeyHash()),
                        hash.toString(), Base64.toBase64String(vchSig.getBytes())));
                return false;
            }

            return true;
        } catch (SignatureException x) {
            strErrorRet.append("exception:  " + x.getMessage());
            return false;
        }
    }


}

@@ -477,65 +477,8 @@
 ) %7B%0A
-        ECKey.ECDSASignature signature = key.sign(hash);%0A
     
@@ -516,30 +516,25 @@
 ure(
-signature.encodeToDER(
+key.signHash(hash
 ));%0A

Correct the signature format returned when signing