net.bither.bitherj.script.Script Java Examples

The following examples show how to use net.bither.bitherj.script.Script. 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: 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 #2
Source File: WalletUtils.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public static boolean isInternal(@Nonnull final Tx tx) {
    if (tx.isCoinBase()) {
        return false;
    }

    final List<Out> outputs = tx.getOuts();
    if (outputs.size() != 1) {
        return false;
    }

    try {
        final Out output = outputs.get(0);
        final Script scriptPubKey = output.getScriptPubKey();
        if (!scriptPubKey.isSentToRawPubKey()) {
            return false;
        }

        return true;
    } catch (final ScriptException x) {
        return false;
    }
}
 
Example #3
Source File: TxBuilder.java    From bitherj with Apache License 2.0 6 votes vote down vote up
static int estimationTxSize(int inCount, Script scriptPubKey, List<Out> outs, boolean isCompressed) {
    int size = 8 + 2;

    Script redeemScript = null;
    if (scriptPubKey.isMultiSigRedeem()) {
        redeemScript = scriptPubKey;
        scriptPubKey = ScriptBuilder.createP2SHOutputScript(redeemScript);
    }

    int sigScriptSize = scriptPubKey.getNumberOfBytesRequiredToSpend(isCompressed, redeemScript);
    size += inCount * (32 + 4 + 1 + sigScriptSize + 4);

    for (Out out : outs) {
        size += 8 + 1 + out.getOutScript().length;
    }
    return size;
}
 
Example #4
Source File: WalletUtils.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public static boolean isInternal(@Nonnull final Tx tx) {
    if (tx.isCoinBase()) {
        return false;
    }

    final List<Out> outputs = tx.getOuts();
    if (outputs.size() != 1) {
        return false;
    }

    try {
        final Out output = outputs.get(0);
        final Script scriptPubKey = output.getScriptPubKey();
        if (!scriptPubKey.isSentToRawPubKey()) {
            return false;
        }

        return true;
    } catch (final ScriptException x) {
        return false;
    }
}
 
Example #5
Source File: WalletUtils.java    From bither-android with Apache License 2.0 6 votes vote down vote up
public static boolean isInternal(@Nonnull final Tx tx) {
    if (tx.isCoinBase()) {
        return false;
    }

    final List<Out> outputs = tx.getOuts();
    if (outputs.size() != 1) {
        return false;
    }

    try {
        final Out output = outputs.get(0);
        final Script scriptPubKey = output.getScriptPubKey();
        if (!scriptPubKey.isSentToRawPubKey()) {
            return false;
        }

        return true;
    } catch (final ScriptException x) {
        return false;
    }
}
 
Example #6
Source File: In.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public String getFromAddress() {
    if (getConnectedOut() != null) {
        return getConnectedOut().getOutAddress();
    } else if (this.getInSignature() != null && !this.isCoinBase()) {
        Script script = new Script(this.getInSignature());
        return script.getFromAddress();
    }
    return null;
}
 
Example #7
Source File: Out.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public String getOutAddress() {
        if (outAddress == null) {
            try {
                Script pubKeyScript = new Script(this.getOutScript());
                outAddress = pubKeyScript.getToAddress();
            } catch (ScriptException e) {
//                if (this.getOutScript() != null) {
//                    log.warn("out script : " + Utils.bytesToHexString(this.getOutScript()));
//                }
            }
        }
        return outAddress;
    }
 
Example #8
Source File: Tx.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the count of regular SigOps in this transactions
 */
public int getSigOpCount() throws ScriptException {
    int sigOps = 0;
    for (In input : ins)
        sigOps += Script.getSigOpCount(input.getInSignature());
    for (Out output : outs)
        sigOps += Script.getSigOpCount(output.getOutScript());
    return sigOps;
}
 
Example #9
Source File: AddressManager.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HashMap<String, Address> getNeededPrivKeyAddresses(Tx tx) {
    HashMap<String, Address> result = new HashMap<String, Address>();
    for (In in : tx.getIns()) {
        Script pubKeyScript = new Script(in.getPrevOutScript());
        String address = pubKeyScript.getToAddress();
        for (Address privKey : this.getPrivKeyAddresses()) {
            if (Utils.compareString(address, privKey.address)) {
                result.put(address, privKey);
                break;
            }
        }
    }
    return result;
}
 
Example #10
Source File: In.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public List<byte[]> getP2SHPubKeys() {
    Script script = new Script(this.getInSignature());
    return script.getP2SHPubKeys(this.tx, this.inSn);
}
 
Example #11
Source File: EnterpriseHDMAddress.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public Script getMultiSigScript() {
    return ScriptBuilder.createMultiSigOutputScript(threshold, pubs);
}
 
Example #12
Source File: HDMAddress.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public Script getMultiSigScript() {
    assert isCompleted();
    return ScriptBuilder.createMultiSigOutputScript(2, Arrays.asList(hot, cold, remote));
}
 
Example #13
Source File: Tx.java    From bitherj with Apache License 2.0 4 votes vote down vote up
/**
 * Same as {@link #addSignedInput(net.bither.bitherj.core.Out, net.bither.bitherj.script.Script, net.bither.bitherj.crypto.ECKey, net.bither.bitherj.crypto.TransactionSignature.SigHash, boolean)}
 * but defaults to {@link net.bither.bitherj.crypto.TransactionSignature.SigHash#ALL} and
 * "false" for the anyoneCanPay flag. This is normally what you want.
 */
public In addSignedInput(Out prevOut, Script scriptPubKey,
                         ECKey sigKey) throws ScriptException {
    return addSignedInput(prevOut, scriptPubKey, sigKey, TransactionSignature.SigHash.ALL,
            false);
}
 
Example #14
Source File: MultisigTest.java    From bitherj with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddress() {
    String pubHot = "026e3f39cd82606a3aa0d9c8194cf516b98ee51c1107e6c7f334cde22b5059e928";
    String pubCold = "034d490441de1cc4a8f1e7192083583c16e513b3b550c8410500db7853fd1fa5fe";
    String pubRemote = "0255b72bc52dfa0ffc40742b1a3eb01858714341c1f72bc1f8fdc731098323e96e";
    String address = "3K2Cbzxfoxey8cbq1w2YutLzhvxByxvNxy";

    ECKey keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    ECKey keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    ECKey keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    List<byte[]> pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    Script script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    String multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);

    pubHot = "033dc6dcf7d90cb8f4ee3adbc87bf55c700d6c32a74800af6de6e1af57f46bfc41";
    pubCold = "025ed1f76ae3fc0cb84782131594020e885a060daf9f55c199fdb299e7169779b9";
    pubRemote = "0378b509c95fd7aa30dc82c4bbe8b84dcb8bb7d7224d891cce7ccf454c79527b5d";
    address = "3ELN8yYSGoz4fTy8HSbfgLRoDWBU6p9zev";

    keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);

    pubHot = "03d29143a6b76d393075d620df9cf80bbb5eaceb2e2b57e5cc4704a6eb3c125a8d";
    pubCold = "03f7d2d484d903fa498d6069009e77ed9ad0842947a7a58441f9406a4728ae2240";
    pubRemote = "02a5fc2584b879fa5a7b04e67d7ab8abb3b08d7981f9f24b03e9353355162c2e04";
    address = "34RgHSRfg3P7FSk3YBbcWnHaMWxapMtrWf";

    keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);
}
 
Example #15
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);
}
 
Example #16
Source File: Tx.java    From bitherj with Apache License 2.0 3 votes vote down vote up
/**
 * <p>Calculates a signature hash, that is, a hash of a simplified form of the transaction.
 * How exactly the transaction
 * is simplified is specified by the type and anyoneCanPay parameters.</p>
 * <p/>
 * <p>You don't normally ever need to call this yourself. It will become more useful in
 * future as the contracts
 * features of Bitcoin are developed.</p>
 *
 * @param inputIndex      input the signature is being calculated for. Tx signatures are
 *                        always relative to an input.
 * @param connectedScript the script that should be in the given input during signing.
 * @param type            Should be SigHash.ALL
 * @param anyoneCanPay    should be false.
 */
public synchronized byte[] hashForSignature(int inputIndex, Script connectedScript,
                                            TransactionSignature.SigHash type,
                                            boolean anyoneCanPay) {
    int sigHash = TransactionSignature.calcSigHashValue(type, anyoneCanPay);
    return hashForSignature(inputIndex, connectedScript.getProgram(), (byte) sigHash);
}
 
Example #17
Source File: Tx.java    From bitherj with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an output that pays to the given script. The address and key forms are
 * specialisations of this method,
 * you won't normally need to use it unless you're doing unusual things.
 */
public Out addOutput(long value, Script script) {
    return addOutput(new Out(this, value, script.getProgram()));
}