Java Code Examples for org.bitcoinj.core.Coin#getValue()

The following examples show how to use org.bitcoinj.core.Coin#getValue() . 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: FakeTxBuilder.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a fake TX for unit tests, for use with unit tests that need greater control. One outputs, 2 random inputs,
 * split randomly to create randomness.
 */
public static Transaction createFakeTxWithoutChangeAddress(NetworkParameters params, Coin value, Address to) {
    Transaction t = new Transaction(params);
    TransactionOutput outputToMe = new TransactionOutput(params, t, value, to);
    t.addOutput(outputToMe);

    // Make a random split in the output value so we get a distinct hash when we call this multiple times with same args
    long split = new Random().nextLong();
    if (split < 0) {
        split *= -1;
    }
    if (split == 0) {
        split = 15;
    }
    while (split > value.getValue()) {
        split /= 2;
    }

    // Make a previous tx simply to send us sufficient coins. This prev tx is not really valid but it doesn't
    // matter for our purposes.
    Transaction prevTx1 = new Transaction(params);
    TransactionOutput prevOut1 = new TransactionOutput(params, prevTx1, Coin.valueOf(split), to);
    prevTx1.addOutput(prevOut1);
    // Connect it.
    t.addInput(prevOut1).setScriptSig(ScriptBuilder.createInputScript(TransactionSignature.dummy()));
    // Fake signature.

    // Do it again
    Transaction prevTx2 = new Transaction(params);
    TransactionOutput prevOut2 = new TransactionOutput(params, prevTx2, Coin.valueOf(value.getValue() - split), to);
    prevTx2.addOutput(prevOut2);
    t.addInput(prevOut2).setScriptSig(ScriptBuilder.createInputScript(TransactionSignature.dummy()));

    // Serialize/deserialize to ensure internal state is stripped, as if it had been read from the wire.
    return roundTripTransaction(params, t);
}
 
Example 2
Source File: TxParser.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Parse and return the genesis transaction for bisq, if applicable.
 *
 * @param genesisTxId         The transaction id of the bisq genesis transaction.
 * @param genesisBlockHeight  The block height of the bisq genesis transaction.
 * @param genesisTotalSupply  The total supply of the genesis issuance for bisq.
 * @param rawTx               The candidate transaction.
 * @return The genesis transaction if applicable, or Optional.empty() otherwise.
 */
public static Optional<TempTx> findGenesisTx(String genesisTxId, int genesisBlockHeight, Coin genesisTotalSupply,
                                             RawTx rawTx) {
    boolean isGenesis = rawTx.getBlockHeight() == genesisBlockHeight &&
            rawTx.getId().equals(genesisTxId);
    if (!isGenesis)
        return Optional.empty();

    TempTx tempTx = TempTx.fromRawTx(rawTx);
    tempTx.setTxType(TxType.GENESIS);
    long remainingInputValue = genesisTotalSupply.getValue();
    for (int i = 0; i < tempTx.getTempTxOutputs().size(); ++i) {
        TempTxOutput txOutput = tempTx.getTempTxOutputs().get(i);
        long value = txOutput.getValue();
        boolean isValid = value <= remainingInputValue;
        if (!isValid)
            throw new InvalidGenesisTxException("Genesis tx is invalid; using more than available inputs. " +
                    "Remaining input value is " + remainingInputValue + " sat; tx info: " + tempTx.toString());

        remainingInputValue -= value;
        txOutput.setTxOutputType(TxOutputType.GENESIS_OUTPUT);
    }
    if (remainingInputValue > 0) {
        throw new InvalidGenesisTxException("Genesis tx is invalid; not using all available inputs. " +
                "Remaining input value is " + remainingInputValue + " sat, tx info: " + tempTx.toString());
    }
    return Optional.of(tempTx);
}
 
Example 3
Source File: ElementsTransactionOutput.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public ElementsTransactionOutput(final NetworkParameters params, final Transaction parent, final byte[] assetId, final Coin value, final ConfidentialAddress to) {
    this(params, parent, Coin.ZERO, to);

    blindingPubKey = to.getBlindingPubKey();
    unblindedValue = value.getValue();
    this.assetId = assetId;
    setAbfVbf(CryptoHelper.randomBytes(32), CryptoHelper.randomBytes(32), assetId);
    length += 2*33 - 8; // remove value (8), add tag/commitment (2*33)
}
 
Example 4
Source File: GenesisTxParser.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Parse and return the genesis transaction for bisq, if applicable.
 *
 * @param rawTx              The candidate transaction.
 * @param genesisTotalSupply The total supply of the genesis issuance for bisq.
 * @return The genesis transaction.
 */
@VisibleForTesting
static TempTx getGenesisTempTx(RawTx rawTx, Coin genesisTotalSupply) {
    TempTx tempTx = TempTx.fromRawTx(rawTx);
    tempTx.setTxType(TxType.GENESIS);
    long remainingInputValue = genesisTotalSupply.getValue();
    List<TempTxOutput> tempTxOutputs = tempTx.getTempTxOutputs();
    //noinspection ForLoopReplaceableByForEach
    for (int i = 0; i < tempTxOutputs.size(); ++i) {
        TempTxOutput txOutput = tempTxOutputs.get(i);
        long value = txOutput.getValue();
        boolean isValid = value <= remainingInputValue;
        if (!isValid)
            throw new InvalidGenesisTxException("Genesis tx is invalid; using more than available inputs. " +
                    "Remaining input value is " + remainingInputValue + " sat; tx info: " + tempTx.toString());

        remainingInputValue -= value;
        txOutput.setTxOutputType(TxOutputType.GENESIS_OUTPUT);
    }

    if (remainingInputValue > 0) {
        throw new InvalidGenesisTxException("Genesis tx is invalid; not using all available inputs. " +
                "Remaining input value is " + remainingInputValue + " sat, tx info: " + tempTx.toString());
    }

    return tempTx;
}