Java Code Examples for org.bitcoin.protocols.payments.Protos#Output

The following examples show how to use org.bitcoin.protocols.payments.Protos#Output . 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: PaymentProtocol.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a payment message. This wraps up transaction data along with anything else useful for making a payment.
 * 
 * @param transactions transactions to include with the payment message
 * @param refundOutputs list of outputs to refund coins to, or null
 * @param memo arbitrary, user readable memo, or null if none
 * @param merchantData arbitrary merchant data, or null if none
 * @return created payment message
 */
public static Protos.Payment createPaymentMessage(List<Transaction> transactions,
        @Nullable List<Protos.Output> refundOutputs, @Nullable String memo, @Nullable byte[] merchantData) {
    Protos.Payment.Builder builder = Protos.Payment.newBuilder();
    for (Transaction transaction : transactions) {
        transaction.verify();
        builder.addTransactions(ByteString.copyFrom(transaction.unsafeBitcoinSerialize()));
    }
    if (refundOutputs != null) {
        for (Protos.Output output : refundOutputs)
            builder.addRefundTo(output);
    }
    if (memo != null)
        builder.setMemo(memo);
    if (merchantData != null)
        builder.setMerchantData(ByteString.copyFrom(merchantData));
    return builder.build();
}
 
Example 2
Source File: PaymentProtocol.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a payment request. You may want to sign the request using {@link #signPaymentRequest}. Use
 * {@link Protos.PaymentRequest.Builder#build} to get the actual payment request.
 *
 * @param params       network parameters
 * @param outputs      list of outputs to request coins to
 * @param memo         arbitrary, user readable memo, or null if none
 * @param paymentUrl   URL to send payment message to, or null if none
 * @param merchantData arbitrary merchant data, or null if none
 * @return created payment request, in its builder form
 */
public static Protos.PaymentRequest.Builder createPaymentRequest(NetworkParameters params,
                                                                 List<Protos.Output> outputs, @Nullable String memo, @Nullable String paymentUrl,
                                                                 @Nullable byte[] merchantData) {
    final Protos.PaymentDetails.Builder paymentDetails = Protos.PaymentDetails.newBuilder();
    paymentDetails.setNetwork(params.getPaymentProtocolId());
    for (Protos.Output output : outputs)
        paymentDetails.addOutputs(output);
    if (memo != null)
        paymentDetails.setMemo(memo);
    if (paymentUrl != null)
        paymentDetails.setPaymentUrl(paymentUrl);
    if (merchantData != null)
        paymentDetails.setMerchantData(ByteString.copyFrom(merchantData));
    paymentDetails.setTime(Utils.currentTimeSeconds());

    final Protos.PaymentRequest.Builder paymentRequest = Protos.PaymentRequest.newBuilder();
    paymentRequest.setSerializedPaymentDetails(paymentDetails.build().toByteString());
    return paymentRequest;
}
 
Example 3
Source File: PaymentProtocol.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a payment message. This wraps up transaction data along with anything else useful for making a payment.
 *
 * @param transactions  transactions to include with the payment message
 * @param refundOutputs list of outputs to refund coins to, or null
 * @param memo          arbitrary, user readable memo, or null if none
 * @param merchantData  arbitrary merchant data, or null if none
 * @return created payment message
 */
public static Protos.Payment createPaymentMessage(List<Transaction> transactions,
                                                  @Nullable List<Protos.Output> refundOutputs, @Nullable String memo, @Nullable byte[] merchantData) {
    Protos.Payment.Builder builder = Protos.Payment.newBuilder();
    for (Transaction transaction : transactions) {
        transaction.verify();
        builder.addTransactions(ByteString.copyFrom(transaction.unsafeBitcoinSerialize()));
    }
    if (refundOutputs != null) {
        for (Protos.Output output : refundOutputs)
            builder.addRefundTo(output);
    }
    if (memo != null)
        builder.setMemo(memo);
    if (merchantData != null)
        builder.setMerchantData(ByteString.copyFrom(merchantData));
    return builder.build();
}
 
Example 4
Source File: PaymentProtocol.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a payment request. You may want to sign the request using {@link #signPaymentRequest}. Use
 * {@link Protos.PaymentRequest.Builder#build} to get the actual payment request.
 * 
 * @param params network parameters
 * @param outputs list of outputs to request coins to
 * @param memo arbitrary, user readable memo, or null if none
 * @param paymentUrl URL to send payment message to, or null if none
 * @param merchantData arbitrary merchant data, or null if none
 * @return created payment request, in its builder form
 */
public static Protos.PaymentRequest.Builder createPaymentRequest(NetworkParameters params,
        List<Protos.Output> outputs, @Nullable String memo, @Nullable String paymentUrl,
        @Nullable byte[] merchantData) {
    final Protos.PaymentDetails.Builder paymentDetails = Protos.PaymentDetails.newBuilder();
    paymentDetails.setNetwork(params.getPaymentProtocolId());
    for (Protos.Output output : outputs)
        paymentDetails.addOutputs(output);
    if (memo != null)
        paymentDetails.setMemo(memo);
    if (paymentUrl != null)
        paymentDetails.setPaymentUrl(paymentUrl);
    if (merchantData != null)
        paymentDetails.setMerchantData(ByteString.copyFrom(merchantData));
    paymentDetails.setTime(Utils.currentTimeSeconds());

    final Protos.PaymentRequest.Builder paymentRequest = Protos.PaymentRequest.newBuilder();
    paymentRequest.setSerializedPaymentDetails(paymentDetails.build().toByteString());
    return paymentRequest;
}
 
Example 5
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a payment request. You may want to sign the request using {@link #signPaymentRequest}. Use
 * {@link Protos.PaymentRequest.Builder#build} to get the actual payment request.
 * 
 * @param params network parameters
 * @param outputs list of outputs to request coins to
 * @param memo arbitrary, user readable memo, or null if none
 * @param paymentUrl URL to send payment message to, or null if none
 * @param merchantData arbitrary merchant data, or null if none
 * @return created payment request, in its builder form
 */
public static Protos.PaymentRequest.Builder createPaymentRequest(NetworkParameters params,
        List<Protos.Output> outputs, @Nullable String memo, @Nullable String paymentUrl,
        @Nullable byte[] merchantData) {
    final Protos.PaymentDetails.Builder paymentDetails = Protos.PaymentDetails.newBuilder();
    paymentDetails.setNetwork(params.getPaymentProtocolId());
    for (Protos.Output output : outputs)
        paymentDetails.addOutputs(output);
    if (memo != null)
        paymentDetails.setMemo(memo);
    if (paymentUrl != null)
        paymentDetails.setPaymentUrl(paymentUrl);
    if (merchantData != null)
        paymentDetails.setMerchantData(ByteString.copyFrom(merchantData));
    paymentDetails.setTime(Utils.currentTimeSeconds());

    final Protos.PaymentRequest.Builder paymentRequest = Protos.PaymentRequest.newBuilder();
    paymentRequest.setSerializedPaymentDetails(paymentDetails.build().toByteString());
    return paymentRequest;
}
 
Example 6
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a payment message. This wraps up transaction data along with anything else useful for making a payment.
 * 
 * @param transactions transactions to include with the payment message
 * @param refundOutputs list of outputs to refund coins to, or null
 * @param memo arbitrary, user readable memo, or null if none
 * @param merchantData arbitrary merchant data, or null if none
 * @return created payment message
 */
public static Protos.Payment createPaymentMessage(List<Transaction> transactions,
        @Nullable List<Protos.Output> refundOutputs, @Nullable String memo, @Nullable byte[] merchantData) {
    Protos.Payment.Builder builder = Protos.Payment.newBuilder();
    for (Transaction transaction : transactions) {
        transaction.verify();
        builder.addTransactions(ByteString.copyFrom(transaction.unsafeBitcoinSerialize()));
    }
    if (refundOutputs != null) {
        for (Protos.Output output : refundOutputs)
            builder.addRefundTo(output);
    }
    if (memo != null)
        builder.setMemo(memo);
    if (merchantData != null)
        builder.setMerchantData(ByteString.copyFrom(merchantData));
    return builder.build();
}
 
Example 7
Source File: PaymentProtocol.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a standard pay to address output for usage in {@link #createPaymentRequest} and
 * {@link #createPaymentMessage}.
 * 
 * @param amount amount to pay, or null
 * @param address address to pay to
 * @return output
 */
public static Protos.Output createPayToAddressOutput(@Nullable Coin amount, Address address) {
    Protos.Output.Builder output = Protos.Output.newBuilder();
    if (amount != null) {
        final NetworkParameters params = address.getParameters();
        if (params.hasMaxMoney() && amount.compareTo(params.getMaxMoney()) > 0)
            throw new IllegalArgumentException("Amount too big: " + amount);
        output.setAmount(amount.value);
    } else {
        output.setAmount(0);
    }
    output.setScript(ByteString.copyFrom(ScriptBuilder.createOutputScript(address).getProgram()));
    return output.build();
}
 
Example 8
Source File: PaymentSession.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the outputs of the payment request.
 */
public List<PaymentProtocol.Output> getOutputs() {
    List<PaymentProtocol.Output> outputs = new ArrayList<>(paymentDetails.getOutputsCount());
    for (Protos.Output output : paymentDetails.getOutputsList()) {
        Coin amount = output.hasAmount() ? Coin.valueOf(output.getAmount()) : null;
        outputs.add(new PaymentProtocol.Output(amount, output.getScript().toByteArray()));
    }
    return outputs;
}
 
Example 9
Source File: PaymentSession.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void parsePaymentRequest(Protos.PaymentRequest request) throws PaymentProtocolException {
    try {
        if (request == null)
            throw new PaymentProtocolException("request cannot be null");
        if (request.getPaymentDetailsVersion() != 1)
            throw new PaymentProtocolException.InvalidVersion("Version 1 required. Received version " + request.getPaymentDetailsVersion());
        paymentRequest = request;
        if (!request.hasSerializedPaymentDetails())
            throw new PaymentProtocolException("No PaymentDetails");
        paymentDetails = Protos.PaymentDetails.newBuilder().mergeFrom(request.getSerializedPaymentDetails()).build();
        if (paymentDetails == null)
            throw new PaymentProtocolException("Invalid PaymentDetails");
        if (!paymentDetails.hasNetwork())
            params = MainNetParams.get();
        else
            params = NetworkParameters.fromPmtProtocolID(paymentDetails.getNetwork());
        if (params == null)
            throw new PaymentProtocolException.InvalidNetwork("Invalid network " + paymentDetails.getNetwork());
        if (paymentDetails.getOutputsCount() < 1)
            throw new PaymentProtocolException.InvalidOutputs("No outputs");
        for (Protos.Output output : paymentDetails.getOutputsList()) {
            if (output.hasAmount())
                totalValue = totalValue.add(Coin.valueOf(output.getAmount()));
        }
        // This won't ever happen in practice. It would only happen if the user provided outputs
        // that are obviously invalid. Still, we don't want to silently overflow.
        if (params.hasMaxMoney() && totalValue.compareTo(params.getMaxMoney()) > 0)
            throw new PaymentProtocolException.InvalidOutputs("The outputs are way too big.");
    } catch (InvalidProtocolBufferException e) {
        throw new PaymentProtocolException(e);
    }
}
 
Example 10
Source File: PaymentSession.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a {@link SendRequest} suitable for broadcasting to the network.
 */
public SendRequest getSendRequest() {
    Transaction tx = new Transaction(params);
    for (Protos.Output output : paymentDetails.getOutputsList())
        tx.addOutput(new TransactionOutput(params, tx, Coin.valueOf(output.getAmount()), output.getScript().toByteArray()));
    return SendRequest.forTx(tx).fromPaymentDetails(paymentDetails);
}
 
Example 11
Source File: PaymentSession.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the outputs of the payment request.
 */
public List<PaymentProtocol.Output> getOutputs() {
    List<PaymentProtocol.Output> outputs = new ArrayList<>(paymentDetails.getOutputsCount());
    for (Protos.Output output : paymentDetails.getOutputsList()) {
        Coin amount = output.hasAmount() ? Coin.valueOf(output.getAmount()) : null;
        outputs.add(new PaymentProtocol.Output(amount, output.getScript().toByteArray()));
    }
    return outputs;
}
 
Example 12
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a standard pay to address output for usage in {@link #createPaymentRequest} and
 * {@link #createPaymentMessage}.
 * 
 * @param amount amount to pay, or null
 * @param address address to pay to
 * @return output
 */
public static Protos.Output createPayToAddressOutput(@Nullable Coin amount, Address address) {
    Protos.Output.Builder output = Protos.Output.newBuilder();
    if (amount != null) {
        final NetworkParameters params = address.getParameters();
        if (params.hasMaxMoney() && amount.compareTo(params.getMaxMoney()) > 0)
            throw new IllegalArgumentException("Amount too big: " + amount);
        output.setAmount(amount.value);
    } else {
        output.setAmount(0);
    }
    output.setScript(ByteString.copyFrom(ScriptBuilder.createOutputScript(address).getProgram()));
    return output.build();
}
 
Example 13
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void parsePaymentRequest(Protos.PaymentRequest request) throws PaymentProtocolException {
    try {
        if (request == null)
            throw new PaymentProtocolException("request cannot be null");
        if (request.getPaymentDetailsVersion() != 1)
            throw new PaymentProtocolException.InvalidVersion("Version 1 required. Received version " + request.getPaymentDetailsVersion());
        paymentRequest = request;
        if (!request.hasSerializedPaymentDetails())
            throw new PaymentProtocolException("No PaymentDetails");
        paymentDetails = Protos.PaymentDetails.newBuilder().mergeFrom(request.getSerializedPaymentDetails()).build();
        if (paymentDetails == null)
            throw new PaymentProtocolException("Invalid PaymentDetails");
        if (!paymentDetails.hasNetwork())
            params = MainNetParams.get();
        else
            params = NetworkParameters.fromPmtProtocolID(paymentDetails.getNetwork());
        if (params == null)
            throw new PaymentProtocolException.InvalidNetwork("Invalid network " + paymentDetails.getNetwork());
        if (paymentDetails.getOutputsCount() < 1)
            throw new PaymentProtocolException.InvalidOutputs("No outputs");
        for (Protos.Output output : paymentDetails.getOutputsList()) {
            if (output.hasAmount())
                totalValue = totalValue.add(Coin.valueOf(output.getAmount()));
        }
        // This won't ever happen in practice. It would only happen if the user provided outputs
        // that are obviously invalid. Still, we don't want to silently overflow.
        if (params.hasMaxMoney() && totalValue.compareTo(params.getMaxMoney()) > 0)
            throw new PaymentProtocolException.InvalidOutputs("The outputs are way too big.");
    } catch (InvalidProtocolBufferException e) {
        throw new PaymentProtocolException(e);
    }
}
 
Example 14
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a {@link SendRequest} suitable for broadcasting to the network.
 */
public SendRequest getSendRequest() {
    Transaction tx = new Transaction(params);
    for (Protos.Output output : paymentDetails.getOutputsList())
        tx.addOutput(new TransactionOutput(params, tx, Coin.valueOf(output.getAmount()), output.getScript().toByteArray()));
    return SendRequest.forTx(tx).fromPaymentDetails(paymentDetails);
}
 
Example 15
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the outputs of the payment request.
 */
public List<PaymentProtocol.Output> getOutputs() {
    List<PaymentProtocol.Output> outputs = new ArrayList<>(paymentDetails.getOutputsCount());
    for (Protos.Output output : paymentDetails.getOutputsList()) {
        Coin amount = output.hasAmount() ? Coin.valueOf(output.getAmount()) : null;
        outputs.add(new PaymentProtocol.Output(amount, output.getScript().toByteArray()));
    }
    return outputs;
}
 
Example 16
Source File: PaymentProtocol.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a standard pay to address output for usage in {@link #createPaymentRequest} and
 * {@link #createPaymentMessage}.
 *
 * @param amount  amount to pay, or null
 * @param address address to pay to
 * @return output
 */
public static Protos.Output createPayToAddressOutput(@Nullable Coin amount, Address address) {
    Protos.Output.Builder output = Protos.Output.newBuilder();
    if (amount != null) {
        final NetworkParameters params = address.getParameters();
        if (params.hasMaxMoney() && amount.compareTo(params.getMaxMoney()) > 0)
            throw new IllegalArgumentException("Amount too big: " + amount);
        output.setAmount(amount.value);
    } else {
        output.setAmount(0);
    }
    output.setScript(ByteString.copyFrom(ScriptBuilder.createOutputScript(address).getProgram()));
    return output.build();
}
 
Example 17
Source File: PaymentSession.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private void parsePaymentRequest(Protos.PaymentRequest request) throws PaymentProtocolException {
    try {
        if (request == null)
            throw new PaymentProtocolException("request cannot be null");
        if (request.getPaymentDetailsVersion() != 1)
            throw new PaymentProtocolException.InvalidVersion("Version 1 required. Received version " + request.getPaymentDetailsVersion());
        paymentRequest = request;
        if (!request.hasSerializedPaymentDetails())
            throw new PaymentProtocolException("No PaymentDetails");
        paymentDetails = Protos.PaymentDetails.newBuilder().mergeFrom(request.getSerializedPaymentDetails()).build();
        if (paymentDetails == null)
            throw new PaymentProtocolException("Invalid PaymentDetails");
        if (!paymentDetails.hasNetwork())
            params = MainNetParams.get();
        else
            params = NetworkParameters.fromPmtProtocolID(paymentDetails.getNetwork());
        if (params == null)
            throw new PaymentProtocolException.InvalidNetwork("Invalid network " + paymentDetails.getNetwork());
        if (paymentDetails.getOutputsCount() < 1)
            throw new PaymentProtocolException.InvalidOutputs("No outputs");
        for (Protos.Output output : paymentDetails.getOutputsList()) {
            if (output.hasAmount())
                totalValue = totalValue.add(Coin.valueOf(output.getAmount()));
        }
        // This won't ever happen in practice. It would only happen if the user provided outputs
        // that are obviously invalid. Still, we don't want to silently overflow.
        if (params.hasMaxMoney() && totalValue.compareTo(params.getMaxMoney()) > 0)
            throw new PaymentProtocolException.InvalidOutputs("The outputs are way too big.");
    } catch (InvalidProtocolBufferException e) {
        throw new PaymentProtocolException(e);
    }
}
 
Example 18
Source File: PaymentSession.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a {@link SendRequest} suitable for broadcasting to the network.
 */
public SendRequest getSendRequest() {
    Transaction tx = new Transaction(params);
    for (Protos.Output output : paymentDetails.getOutputsList())
        tx.addOutput(new TransactionOutput(params, tx, Coin.valueOf(output.getAmount()), output.getScript().toByteArray()));
    return SendRequest.forTx(tx).fromPaymentDetails(paymentDetails);
}