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

The following examples show how to use net.bither.bitherj.crypto.ECKey#sign() . 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: Address.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public List<byte[]> signHashes(List<byte[]> unsignedInHashes, CharSequence passphrase) throws
        PasswordException {
    ECKey key = PrivateKeyUtil.getECKeyFromSingleString(this.getFullEncryptPrivKey(), passphrase);
    if (key == null) {
        throw new PasswordException("do not decrypt eckey");
    }
    KeyParameter assKey = key.getKeyCrypter().deriveKey(passphrase);
    List<byte[]> result = new ArrayList<byte[]>();
    for (byte[] unsignedInHash : unsignedInHashes) {
        TransactionSignature signature = new TransactionSignature(key.sign(unsignedInHash,
                assKey), TransactionSignature.SigHash.ALL, false);
        result.add(ScriptBuilder.createInputScript(signature, key).getProgram());
    }
    key.clearPrivateKey();
    return result;
}
 
Example 2
Source File: Tx.java    From bitherj with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a new and fully signed input for the given parameters. Note that this method is
 * <b>not</b> thread safe
 * and requires external synchronization. Please refer to general documentation on Bitcoin
 * scripting and contracts
 * to understand the values of sigHash and anyoneCanPay: otherwise you can use the other form
 * of this method
 * that sets them to typical defaults.
 *
 * @throws net.bither.bitherj.exception.ScriptException if the scriptPubKey is not a pay to address or pay to pubkey script.
 */
public In addSignedInput(Out prevOut, Script scriptPubKey, ECKey sigKey,
                         TransactionSignature.SigHash sigHash, boolean anyoneCanPay) throws
        ScriptException {
    In input = new In(this, new byte[]{}, prevOut);
    addInput(input);
    byte[] hash = hashForSignature(ins.size() - 1, scriptPubKey, sigHash, anyoneCanPay);
    ECKey.ECDSASignature ecSig = sigKey.sign(hash);
    TransactionSignature txSig = new TransactionSignature(ecSig, sigHash, anyoneCanPay);
    if (scriptPubKey.isSentToRawPubKey()) {
        input.setInSignature(ScriptBuilder.createInputScript(txSig).getProgram());
    } else if (scriptPubKey.isSentToAddress()) {
        input.setInSignature(ScriptBuilder.createInputScript(txSig, sigKey).getProgram());
    } else {
        throw new ScriptException("Don't know how to sign for this kind of scriptPubKey: " +
                scriptPubKey);
    }
    return input;
}
 
Example 3
Source File: Tx.java    From bitherj with Apache License 2.0 3 votes vote down vote up
/**
 * Calculates a signature that is valid for being inserted into the input at the given
 * position. This is simply
 * a wrapper around calling {@link net.bither.bitherj.core.Tx#hashForSignature(int, byte[],
 * net.bither.bitherj.crypto.TransactionSignature.SigHash, boolean)}
 * followed by {@link net.bither.bitherj.crypto.ECKey#sign(byte[], org.spongycastle.crypto.params.KeyParameter)} and
 * then returning
 * a new {@link net.bither.bitherj.crypto.TransactionSignature}.
 *
 * @param inputIndex            Which input to calculate the signature for, as an index.
 * @param key                   The private key used to calculate the signature.
 * @param aesKey                If not null, this will be used to decrypt the key.
 * @param connectedPubKeyScript Byte-exact contents of the scriptPubKey that is being
 *                              satisified.
 * @param hashType              Signing mode, see the enum for documentation.
 * @param anyoneCanPay          Signing mode, see the SigHash enum for documentation.
 * @return A newly calculated signature object that wraps the r, s and sighash components.
 */
public synchronized TransactionSignature calculateSignature(int inputIndex, ECKey key,
                                                            @Nullable KeyParameter aesKey,
                                                            byte[] connectedPubKeyScript,
                                                            TransactionSignature.SigHash
                                                                    hashType,
                                                            boolean anyoneCanPay) {
    byte[] hash = hashForSignature(inputIndex, connectedPubKeyScript, hashType, anyoneCanPay);
    return new TransactionSignature(key.sign(hash, aesKey), hashType, anyoneCanPay);
}
 
Example 4
Source File: Tx.java    From bitherj with Apache License 2.0 3 votes vote down vote up
/**
 * Calculates a signature that is valid for being inserted into the input at the given
 * position. This is simply
 * a wrapper around calling {@link net.bither.bitherj.core.Tx#hashForSignature(int, byte[],
 * net.bither.bitherj.crypto.TransactionSignature.SigHash, boolean)}
 * followed by {@link net.bither.bitherj.crypto.ECKey#sign(byte[])} and then returning a new {@link net.bither.bitherj.crypto.TransactionSignature}.
 *
 * @param inputIndex            Which input to calculate the signature for, as an index.
 * @param key                   The private key used to calculate the signature.
 * @param connectedPubKeyScript The scriptPubKey that is being satisified.
 * @param hashType              Signing mode, see the enum for documentation.
 * @param anyoneCanPay          Signing mode, see the SigHash enum for documentation.
 * @return A newly calculated signature object that wraps the r, s and sighash components.
 */
public synchronized TransactionSignature calculateSignature(int inputIndex, ECKey key,
                                                            Script connectedPubKeyScript,
                                                            TransactionSignature.SigHash
                                                                    hashType,
                                                            boolean anyoneCanPay) {
    byte[] hash = hashForSignature(inputIndex, connectedPubKeyScript.getProgram(), hashType,
            anyoneCanPay);
    return new TransactionSignature(key.sign(hash), hashType, anyoneCanPay);
}