Java Code Examples for org.bitcoinj.core.ECKey#verify()

The following examples show how to use org.bitcoinj.core.ECKey#verify() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MeritConsensus.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
private static boolean isSignatureValid(byte[] signatureFromMerit, String pubKeyAsHex, String blindVoteTxId) {
    // We verify if signature of hash of blindVoteTxId is correct. EC key from first input for blind vote tx is
    // used for signature.
    if (pubKeyAsHex == null) {
        log.error("Error at isSignatureValid: pubKeyAsHex is null");
        return false;
    }

    boolean result = false;
    try {
        ECKey pubKey = ECKey.fromPublicOnly(Utilities.decodeFromHex(pubKeyAsHex));
        ECKey.ECDSASignature signature = ECKey.ECDSASignature.decodeFromDER(signatureFromMerit).toCanonicalised();
        Sha256Hash msg = Sha256Hash.wrap(blindVoteTxId);
        result = pubKey.verify(msg, signature);
    } catch (Throwable t) {
        log.error("Signature verification of issuance failed: " + t.toString());
    }
    if (!result) {
        log.error("Signature verification of issuance failed: blindVoteTxId={}, pubKeyAsHex={}",
                blindVoteTxId, pubKeyAsHex);
    }
    return result;
}
 
Example 2
Source File: MeritConsensus.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@VisibleForTesting
static boolean isSignatureValid(byte[] signatureFromMerit, String pubKeyAsHex, String blindVoteTxId) {
    // We verify if signature of hash of blindVoteTxId is correct. EC key from first input for blind vote tx is
    // used for signature.
    if (pubKeyAsHex == null) {
        log.error("Error at getMeritStake: pubKeyAsHex is null");
        return false;
    }

    // TODO Check if a sig key was used multiple times for different voters
    // At the moment we don't impl. that to not add too much complexity and as we consider that
    // risk very low.

    boolean result = false;
    try {
        ECKey pubKey = ECKey.fromPublicOnly(Utilities.decodeFromHex(pubKeyAsHex));
        ECKey.ECDSASignature signature = ECKey.ECDSASignature.decodeFromDER(signatureFromMerit).toCanonicalised();
        Sha256Hash msg = Sha256Hash.wrap(blindVoteTxId);
        result = pubKey.verify(msg, signature);
    } catch (Throwable t) {
        log.error("Signature verification of issuance failed: " + t.toString());
    }
    if (!result) {
        log.error("Signature verification of issuance failed: blindVoteTxId={}, pubKeyAsHex={}",
                blindVoteTxId, pubKeyAsHex);
    }
    return result;
}
 
Example 3
Source File: CryptoTools.java    From thundernetwork with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void verifySignature (ECKey pubkey, byte[] data, byte[] signature) throws NoSuchProviderException, NoSuchAlgorithmException {
    MessageDigest hashHandler = MessageDigest.getInstance("SHA256", "BC");
    hashHandler.update(data);
    byte[] hash = hashHandler.digest();

    if (!pubkey.verify(hash, signature)) {
        System.out.println("Signature does not match..");
        throw new RuntimeException("Signature does not match..");
    }
}
 
Example 4
Source File: Secp256k1Context.java    From sawtooth-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public final boolean verify(final String signature, final byte[] data, final PublicKey publicKey) {

  byte[] signatureBytes = Utils.HEX.decode(signature);

  byte[] rbytes = Arrays.copyOfRange(signatureBytes, 0, HALF_NUM_SIGNATURE_BYTES);
  byte[] sbytes = Arrays.copyOfRange(signatureBytes, HALF_NUM_SIGNATURE_BYTES, NUM_SIGNATURE_BYTES);

  BigInteger rSig = new BigInteger(1, rbytes);
  BigInteger sSig = new BigInteger(1, sbytes);

  ECKey.ECDSASignature ecdsaSignature = new ECKey.ECDSASignature(rSig, sSig);

  byte[] hash = Sha256Hash.of(data).getBytes();

  return ECKey.verify(hash, ecdsaSignature, publicKey.getBytes());
}
 
Example 5
Source File: AbstractScriptBuilderWithVar.java    From balzac with Apache License 2.0 4 votes vote down vote up
/**
 * Replace all the signatures placeholder with the actual signatures. Each
 * placeholder is already associated with the key and the modifiers.
 * 
 * @param tx         the transaction to be signed
 * @param inputIndex the index of the input that will contain this script
 * @param outScript  the redeemed output script
 * @return a <b>copy</b> of this builder
 * @throws KeyStoreException if an error occurs retrieving private keys
 */
@SuppressWarnings("unchecked")
public T setAllSignatures(ECKeyStore keystore, Transaction tx, int inputIndex, byte[] outScript, boolean isP2PKH)
    throws KeyStoreException {

    List<ScriptChunk> newChunks = new ArrayList<>();

    for (ScriptChunk chunk : getChunks()) {

        ScriptBuilderWithVar sb = new ScriptBuilderWithVar();
        if (isSignature(chunk)) {
            String mapKey = getMapKey(chunk);
            SignatureUtil sig = this.signatures.get(mapKey);

            // check if the private key is a variable
            String keyID = sig.keyID;
            if (keyID.startsWith(FREEVAR_PREFIX)) {
                // check that the variable is bound
                String varName = keyID.substring(FREEVAR_PREFIX.length());
                checkState(isBound(varName), "variable " + varName + " must be bound to retrieve the key");
                keyID = getValue(varName, String.class);
            }

            checkState(keystore != null, "keystore must be set to retrieve the private keys");
            checkState(keystore.containsKey(keyID), "key " + keyID + " not found on the specified keystore");

            PrivateKey privkey = keystore.getKey(keyID);
            ECKey key = ECKey.fromPrivate(privkey.getBytes());
            SigHash hashType = sig.hashType;
            boolean anyoneCanPay = sig.anyoneCanPay;

            // create the signature
            TransactionSignature txSig = tx.calculateSignature(inputIndex, key, outScript, hashType, anyoneCanPay);
            Sha256Hash hash = tx.hashForSignature(inputIndex, outScript, (byte) txSig.sighashFlags);
            boolean isValid = ECKey.verify(hash.getBytes(), txSig, privkey.toPublicKey().getBytes());
            checkState(isValid);
            checkState(txSig.isCanonical());
            sb.data(txSig.encodeToBitcoin());
            if (isP2PKH) {
                sb.data(privkey.toPublicKey().getBytes());
            }
        }
        else {
            sb.addChunk(chunk);
        }

        newChunks.addAll(sb.getChunks());
    }
    super.getChunks().clear();
    super.getChunks().addAll(newChunks);

    this.signatures.clear();
    return (T) this;
}
 
Example 6
Source File: CryptoTools.java    From thunder with GNU Affero General Public License v3.0 4 votes vote down vote up
public static boolean verifySignature (ECKey pubkey, byte[] data, byte[] signature) throws NoSuchProviderException, NoSuchAlgorithmException {
    MessageDigest hashHandler = MessageDigest.getInstance("SHA256", "BC");
    hashHandler.update(data);
    byte[] hash = hashHandler.digest();
    return pubkey.verify(hash, signature);
}