Java Code Examples for org.bitcoinj.script.ScriptBuilder#createCLTVPaymentChannelOutput()

The following examples show how to use org.bitcoinj.script.ScriptBuilder#createCLTVPaymentChannelOutput() . 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: SendRequest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static SendRequest toCLTVPaymentChannel(NetworkParameters params, BigInteger time, ECKey from, ECKey to, Coin value) {
    SendRequest req = new SendRequest();
    Script output = ScriptBuilder.createCLTVPaymentChannelOutput(time, from, to);
    req.tx = new Transaction(params);
    req.tx.addOutput(value, output);
    return req;
}
 
Example 2
Source File: SendRequest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public static SendRequest toCLTVPaymentChannel(NetworkParameters params, BigInteger time, ECKey from, ECKey to, Coin value) {
    SendRequest req = new SendRequest();
    Script output = ScriptBuilder.createCLTVPaymentChannelOutput(time, from, to);
    req.tx = new Transaction(params);
    req.tx.addOutput(value, output);
    return req;
}
 
Example 3
Source File: SendRequest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public static SendRequest toCLTVPaymentChannel(NetworkParameters params, BigInteger time, ECKey from, ECKey to, Coin value) {
    SendRequest req = new SendRequest();
    Script output = ScriptBuilder.createCLTVPaymentChannelOutput(time, from, to);
    req.tx = new Transaction(params);
    req.tx.addOutput(value, output);
    return req;
}
 
Example 4
Source File: PaymentChannelV2ClientState.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException {
    final NetworkParameters params = wallet.getParams();
    Transaction template = new Transaction(params);
    // There is also probably a change output, but we don't bother shuffling them as it's obvious from the
    // format which one is the change. If we start obfuscating the change output better in future this may
    // be worth revisiting.
    Script redeemScript =
            ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey);
    TransactionOutput transactionOutput = template.addOutput(totalValue,
            ScriptBuilder.createP2SHOutputScript(redeemScript));
    if (transactionOutput.isDust())
        throw new ValueOutOfRangeException("totalValue too small to use");
    SendRequest req = SendRequest.forTx(template);
    req.coinSelector = AllowUnconfirmedCoinSelector.get();
    req.shuffleOutputs = false;   // TODO: Fix things so shuffling is usable.
    req = clientChannelProperties.modifyContractSendRequest(req);
    if (userKey != null)
        req.aesKey = userKey;
    wallet.completeTx(req);
    Coin multisigFee = req.tx.getFee();
    contract = req.tx;

    // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc
    // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it because the
    // CheckLockTimeVerify opcode requires a lock time to be specified and the input to have a non-final sequence
    // number (so that the lock time is not disabled).
    refundTx = new Transaction(params);
    // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing.
    refundTx.addInput(contract.getOutput(0)).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L);
    refundTx.setLockTime(expiryTime);
    if (Context.get().isEnsureMinRequiredFee()) {
        // Must pay min fee.
        final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
        if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0)
            throw new ValueOutOfRangeException("totalValue too small to use");
        refundTx.addOutput(valueAfterFee, LegacyAddress.fromKey(params, myKey));
        refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
    } else {
        refundTx.addOutput(totalValue, LegacyAddress.fromKey(params, myKey));
        refundFees = multisigFee;
    }

    TransactionSignature refundSignature =
            refundTx.calculateSignature(0, myKey.maybeDecrypt(userKey),
                    getSignedScript(), Transaction.SigHash.ALL, false);
    refundTx.getInput(0).setScriptSig(ScriptBuilder.createCLTVPaymentChannelP2SHRefund(refundSignature, redeemScript));

    refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF);
    log.info("initiated channel with contract {}", contract.getHashAsString());
    stateMachine.transition(State.SAVE_STATE_IN_WALLET);
    // Client should now call getIncompleteRefundTransaction() and send it to the server.
}
 
Example 5
Source File: PaymentChannelV2ClientState.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Script getSignedScript() {
    return ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey);
}
 
Example 6
Source File: PaymentChannelV2ServerState.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
private Script createP2SHRedeemScript() {
    return ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(getExpiryTime()), clientKey, serverKey);
}
 
Example 7
Source File: PaymentChannelV2ClientState.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException {
    final NetworkParameters params = wallet.getParams();
    Transaction template = new Transaction(params);
    // There is also probably a change output, but we don't bother shuffling them as it's obvious from the
    // format which one is the change. If we start obfuscating the change output better in future this may
    // be worth revisiting.
    Script redeemScript =
            ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey);
    TransactionOutput transactionOutput = template.addOutput(totalValue,
            ScriptBuilder.createP2SHOutputScript(redeemScript));
    if (transactionOutput.isDust())
        throw new ValueOutOfRangeException("totalValue too small to use");
    SendRequest req = SendRequest.forTx(template);
    req.coinSelector = AllowUnconfirmedCoinSelector.get();
    req.shuffleOutputs = false;   // TODO: Fix things so shuffling is usable.
    req = clientChannelProperties.modifyContractSendRequest(req);
    if (userKey != null) req.aesKey = userKey;
    wallet.completeTx(req);
    Coin multisigFee = req.tx.getFee();
    contract = req.tx;

    // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc
    // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it because the
    // CheckLockTimeVerify opcode requires a lock time to be specified and the input to have a non-final sequence
    // number (so that the lock time is not disabled).
    refundTx = new Transaction(params);
    // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing.
    refundTx.addInput(contract.getOutput(0)).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L);
    refundTx.setLockTime(expiryTime);
    if (Context.get().isEnsureMinRequiredFee()) {
        // Must pay min fee.
        final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
        if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0)
            throw new ValueOutOfRangeException("totalValue too small to use");
        refundTx.addOutput(valueAfterFee, myKey.toAddress(params));
        refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
    } else {
        refundTx.addOutput(totalValue, myKey.toAddress(params));
        refundFees = multisigFee;
    }

    TransactionSignature refundSignature =
            refundTx.calculateSignature(0, myKey.maybeDecrypt(userKey),
                    getSignedScript(), Transaction.SigHash.ALL, false);
    refundTx.getInput(0).setScriptSig(ScriptBuilder.createCLTVPaymentChannelP2SHRefund(refundSignature, redeemScript));

    refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF);
    log.info("initiated channel with contract {}", contract.getHashAsString());
    stateMachine.transition(State.SAVE_STATE_IN_WALLET);
    // Client should now call getIncompleteRefundTransaction() and send it to the server.
}
 
Example 8
Source File: PaymentChannelV2ClientState.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Script getSignedScript() {
    return ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey);
}
 
Example 9
Source File: PaymentChannelV2ServerState.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
private Script createP2SHRedeemScript() {
    return ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(getExpiryTime()), clientKey, serverKey);
}
 
Example 10
Source File: PaymentChannelV2ClientState.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public synchronized void initiate(@Nullable KeyParameter userKey, ClientChannelProperties clientChannelProperties) throws ValueOutOfRangeException, InsufficientMoneyException {
    final NetworkParameters params = wallet.getParams();
    Transaction template = new Transaction(params);
    // There is also probably a change output, but we don't bother shuffling them as it's obvious from the
    // format which one is the change. If we start obfuscating the change output better in future this may
    // be worth revisiting.
    Script redeemScript =
            ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey);
    TransactionOutput transactionOutput = template.addOutput(totalValue,
            ScriptBuilder.createP2SHOutputScript(redeemScript));
    if (transactionOutput.isDust())
        throw new ValueOutOfRangeException("totalValue too small to use");
    SendRequest req = SendRequest.forTx(template);
    req.coinSelector = AllowUnconfirmedCoinSelector.get();
    req.shuffleOutputs = false;   // TODO: Fix things so shuffling is usable.
    req = clientChannelProperties.modifyContractSendRequest(req);
    if (userKey != null) req.aesKey = userKey;
    wallet.completeTx(req);
    Coin multisigFee = req.tx.getFee();
    contract = req.tx;

    // Build a refund transaction that protects us in the case of a bad server that's just trying to cause havoc
    // by locking up peoples money (perhaps as a precursor to a ransom attempt). We time lock it because the
    // CheckLockTimeVerify opcode requires a lock time to be specified and the input to have a non-final sequence
    // number (so that the lock time is not disabled).
    refundTx = new Transaction(params);
    // by using this sequence value, we avoid extra full replace-by-fee and relative lock time processing.
    refundTx.addInput(contract.getOutput(0)).setSequenceNumber(TransactionInput.NO_SEQUENCE - 1L);
    refundTx.setLockTime(expiryTime);
    if (Context.get().isEnsureMinRequiredFee()) {
        // Must pay min fee.
        final Coin valueAfterFee = totalValue.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
        if (Transaction.MIN_NONDUST_OUTPUT.compareTo(valueAfterFee) > 0)
            throw new ValueOutOfRangeException("totalValue too small to use");
        refundTx.addOutput(valueAfterFee, myKey.toAddress(params));
        refundFees = multisigFee.add(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
    } else {
        refundTx.addOutput(totalValue, myKey.toAddress(params));
        refundFees = multisigFee;
    }

    TransactionSignature refundSignature =
            refundTx.calculateSignature(0, myKey.maybeDecrypt(userKey),
                    getSignedScript(), Transaction.SigHash.ALL, false);
    refundTx.getInput(0).setScriptSig(ScriptBuilder.createCLTVPaymentChannelP2SHRefund(refundSignature, redeemScript));

    refundTx.getConfidence().setSource(TransactionConfidence.Source.SELF);
    log.info("initiated channel with contract {}", contract.getHashAsString());
    stateMachine.transition(State.SAVE_STATE_IN_WALLET);
    // Client should now call getIncompleteRefundTransaction() and send it to the server.
}
 
Example 11
Source File: PaymentChannelV2ClientState.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Script getSignedScript() {
    return ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(expiryTime), myKey, serverKey);
}
 
Example 12
Source File: PaymentChannelV2ServerState.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
private Script createP2SHRedeemScript() {
    return ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.valueOf(getExpiryTime()), clientKey, serverKey);
}