Java Code Examples for net.bither.bitherj.crypto.ECKey#verify()

The following examples show how to use net.bither.bitherj.crypto.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: Script.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private static void executeCheckSig(Tx txContainingThis, int index, Script script, LinkedList<byte[]> stack,
                                    int lastCodeSepLocation, int opcode) throws ScriptException {
    if (stack.size() < 2)
        throw new ScriptException("Attempted OP_CHECKSIG(VERIFY) on a stack with size < 2");
    byte[] pubKey = stack.pollLast();
    byte[] sigBytes = stack.pollLast();

    byte[] prog = script.getProgram();
    byte[] connectedScript = Arrays.copyOfRange(prog, lastCodeSepLocation, prog.length);

    UnsafeByteArrayOutputStream outStream = new UnsafeByteArrayOutputStream(sigBytes.length + 1);
    try {
        writeBytes(outStream, sigBytes);
    } catch (IOException e) {
        throw new RuntimeException(e); // Cannot happen
    }
    connectedScript = removeAllInstancesOf(connectedScript, outStream.toByteArray());

    // TODO: Use int for indexes everywhere, we can't have that many inputs/outputs
    boolean sigValid = false;
    try {
        TransactionSignature sig = TransactionSignature.decodeFromBitcoin(sigBytes, false);
        byte[] hash = txContainingThis.hashForSignature(index, connectedScript, (byte) sig.sighashFlags);
        sigValid = ECKey.verify(hash, sig, pubKey);
    } catch (Exception e1) {
        // There is (at least) one exception that could be hit here (EOFException, if the sig is too short)
        // Because I can't verify there aren't more, we use a very generic Exception catch
        log.warn(e1.toString());
    }

    if (opcode == OP_CHECKSIG)
        stack.add(sigValid ? new byte[]{1} : new byte[]{0});
    else if (opcode == OP_CHECKSIGVERIFY)
        if (!sigValid)
            throw new ScriptException("Script failed OP_CHECKSIGVERIFY");
}
 
Example 2
Source File: EnterpriseHDMTxSignaturePool.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private byte[] recoverPub(byte[] signature, byte[] hash) {
    for (int i = 0; i < pubs.size(); i++) {
        byte[] pubByte = pubs.get(i);
        if (ECKey.verify(hash, signature, pubByte)) {
            return pubByte;
        }
    }
    return null;
}
 
Example 3
Source File: Script.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public List<byte[]> getP2SHPubKeys(Tx tx, int index) {
    if (!this.isSendFromMultiSig()) {
        throw new ScriptException("[Script Error] is not send from multisig");
    }

    Script scriptPubKey = new Script(this.chunks.get(this.chunks.size() - 1).data);
    int sigCount = scriptPubKey.chunks.get(0).opcode - 80;
    int pubKeyCount = scriptPubKey.chunks.get(scriptPubKey.chunks.size() - 2).opcode - 80;
    if (pubKeyCount < 0 || pubKeyCount > 20) {
        throw new ScriptException("[Script Error] OP_CHECKMULTISIG(VERIFY) with pubkey count out of range");
    }

    List<byte[]> pubKeys = new ArrayList<byte[]>();

    for (int i = 0; i < pubKeyCount; i++) {
        pubKeys.add(scriptPubKey.chunks.get(i + 1).data);
    }

    if (sigCount < 0 || sigCount > pubKeyCount) {
        throw new ScriptException("[Script Error] OP_CHECKMULTISIG(VERIFY) with sig count out of range");
    }

    List<byte[]> sigs = new ArrayList<byte[]>();
    for (int i = 1; i < sigCount + 1; i++) {
        sigs.add(this.chunks.get(i).data);
    }

    List<byte[]> result = new ArrayList<byte[]>();
    while (sigs.size() > 0) {
        byte[] pubKey = pubKeys.get(pubKeys.size() - 1);
        pubKeys.remove(pubKeys.size() - 1);

        byte[] sigBytes = sigs.get(sigs.size() - 1);

        if (sigBytes.length > 0) {
            TransactionSignature sig = TransactionSignature.decodeFromBitcoin(sigBytes, false);
            byte[] hash = tx.hashForSignature(index, scriptPubKey.getProgram(), (byte) sig.sighashFlags);
            boolean sigValid = ECKey.verify(hash, sig, pubKey);

            if (sigValid) {
                result.add(pubKey);
                sigs.remove(sigs.size() - 1);
            }
        }
        if (sigs.size() > pubKeys.size()) {
            break;
        }
    }

    return result;
}
 
Example 4
Source File: AlertMessage.java    From bitherj with Apache License 2.0 2 votes vote down vote up
/**
 * Returns true if the digital signature attached to the message verifies. Don't do anything with the alert if it
 * doesn't verify, because that would allow arbitrary attackers to spam your users.
 */
public boolean isSignatureValid() {
    return ECKey.verify(Utils.doubleDigest(content), signature, BitherjSettings.SATOSHI_KEY);
}