org.bitcoin.protocols.payments.Protos Java Examples

The following examples show how to use org.bitcoin.protocols.payments.Protos. 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: PaymentSessionTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
private Protos.PaymentRequest newExpiredPaymentRequest() {
    Protos.Output.Builder outputBuilder = Protos.Output.newBuilder()
            .setAmount(coin.value)
            .setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
    Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
            .setNetwork("test")
            .setTime(time - 10)
            .setExpires(time - 1)
            .setPaymentUrl(simplePaymentUrl)
            .addOutputs(outputBuilder)
            .setMemo(paymentRequestMemo)
            .setMerchantData(merchantData)
            .build();
    return Protos.PaymentRequest.newBuilder()
            .setPaymentDetailsVersion(1)
            .setPkiType("none")
            .setSerializedPaymentDetails(paymentDetails.toByteString())
            .build();
}
 
Example #2
Source File: PaymentSession.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
protected ListenableFuture<PaymentProtocol.Ack> sendPayment(final URL url, final Protos.Payment payment) {
    return executor.submit(new Callable<PaymentProtocol.Ack>() {
        @Override
        public PaymentProtocol.Ack call() throws Exception {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", PaymentProtocol.MIMETYPE_PAYMENT);
            connection.setRequestProperty("Accept", PaymentProtocol.MIMETYPE_PAYMENTACK);
            connection.setRequestProperty("Content-Length", Integer.toString(payment.getSerializedSize()));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request.
            DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
            payment.writeTo(outStream);
            outStream.flush();
            outStream.close();

            // Get response.
            Protos.PaymentACK paymentAck = Protos.PaymentACK.parseFrom(connection.getInputStream());
            return PaymentProtocol.parsePaymentAck(paymentAck);
        }
    });
}
 
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 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 #4
Source File: PaymentSession.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Generates a Payment message and sends the payment to the merchant who sent the PaymentRequest.
 * Provide transactions built by the wallet.
 * NOTE: This does not broadcast the transactions to the bitcoin network, it merely sends a Payment message to the
 * merchant confirming the payment.
 * Returns an object wrapping PaymentACK once received.
 * If the PaymentRequest did not specify a payment_url, returns null and does nothing.
 *
 * @param txns       list of transactions to be included with the Payment message.
 * @param refundAddr will be used by the merchant to send money back if there was a problem.
 * @param memo       is a message to include in the payment message sent to the merchant.
 */
@Nullable
public ListenableFuture<PaymentProtocol.Ack> sendPayment(List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
        throws PaymentProtocolException, VerificationException, IOException {
    Protos.Payment payment = getPayment(txns, refundAddr, memo);
    if (payment == null)
        return null;
    if (isExpired())
        throw new PaymentProtocolException.Expired("PaymentRequest is expired");
    URL url;
    try {
        url = new URL(paymentDetails.getPaymentUrl());
    } catch (MalformedURLException e) {
        throw new PaymentProtocolException.InvalidPaymentURL(e);
    }
    return sendPayment(url, payment);
}
 
Example #5
Source File: PaymentSession.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
protected ListenableFuture<PaymentProtocol.Ack> sendPayment(final URL url, final Protos.Payment payment) {
    return executor.submit(new Callable<PaymentProtocol.Ack>() {
        @Override
        public PaymentProtocol.Ack call() throws Exception {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", PaymentProtocol.MIMETYPE_PAYMENT);
            connection.setRequestProperty("Accept", PaymentProtocol.MIMETYPE_PAYMENTACK);
            connection.setRequestProperty("Content-Length", Integer.toString(payment.getSerializedSize()));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request.
            DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
            payment.writeTo(outStream);
            outStream.flush();
            outStream.close();

            // Get response.
            Protos.PaymentACK paymentAck = Protos.PaymentACK.parseFrom(connection.getInputStream());
            return PaymentProtocol.parsePaymentAck(paymentAck);
        }
    });
}
 
Example #6
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 #7
Source File: PaymentProtocolTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testSignAndVerifyValid() throws Exception {
    Protos.PaymentRequest.Builder paymentRequest = minimalPaymentRequest().toBuilder();

    // Sign
    KeyStore keyStore = X509Utils
            .loadKeyStore("JKS", "password", getClass().getResourceAsStream("test-valid-cert"));
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("test-valid", "password".toCharArray());
    X509Certificate clientCert = (X509Certificate) keyStore.getCertificate("test-valid");
    PaymentProtocol.signPaymentRequest(paymentRequest, new X509Certificate[]{clientCert}, privateKey);

    // Verify
    PkiVerificationData verificationData = PaymentProtocol.verifyPaymentRequestPki(paymentRequest.build(), caStore);
    assertNotNull(verificationData);
    assertEquals(caCert, verificationData.rootAuthority.getTrustedCert());
}
 
Example #8
Source File: PaymentSessionTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private Protos.PaymentRequest newSimplePaymentRequest(String netID) {
    Protos.Output.Builder outputBuilder = Protos.Output.newBuilder()
            .setAmount(coin.value)
            .setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
    Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
            .setNetwork(netID)
            .setTime(time)
            .setPaymentUrl(simplePaymentUrl)
            .addOutputs(outputBuilder)
            .setMemo(paymentRequestMemo)
            .setMerchantData(merchantData)
            .build();
    return Protos.PaymentRequest.newBuilder()
            .setPaymentDetailsVersion(1)
            .setPkiType("none")
            .setSerializedPaymentDetails(paymentDetails.toByteString())
            .build();
}
 
Example #9
Source File: PaymentSessionTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private Protos.PaymentRequest newExpiredPaymentRequest() {
    Protos.Output.Builder outputBuilder = Protos.Output.newBuilder()
            .setAmount(coin.value)
            .setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
    Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
            .setNetwork("test")
            .setTime(time - 10)
            .setExpires(time - 1)
            .setPaymentUrl(simplePaymentUrl)
            .addOutputs(outputBuilder)
            .setMemo(paymentRequestMemo)
            .setMerchantData(merchantData)
            .build();
    return Protos.PaymentRequest.newBuilder()
            .setPaymentDetailsVersion(1)
            .setPkiType("none")
            .setSerializedPaymentDetails(paymentDetails.toByteString())
            .build();
}
 
Example #10
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 #11
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Generates a Payment message based on the information in the PaymentRequest.
 * Provide transactions built by the wallet.
 * If the PaymentRequest did not specify a payment_url, returns null.
 * @param txns list of transactions to be included with the Payment message.
 * @param refundAddr will be used by the merchant to send money back if there was a problem.
 * @param memo is a message to include in the payment message sent to the merchant.
 */
@Nullable
public Protos.Payment getPayment(List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
        throws IOException, PaymentProtocolException.InvalidNetwork {
    if (paymentDetails.hasPaymentUrl()) {
        for (Transaction tx : txns) {
            // BIP70 doesn't allow for regtest in its network type. If we mismatch,
            // treat regtest transactions for a testnet payment request as a match.
            if (!tx.getParams().equals(params) &&
                (!tx.getParams().equals(RegTestParams.get()) || !params.equals(TestNet3Params.get())))
                throw new PaymentProtocolException.InvalidNetwork(params.getPaymentProtocolId());
        }
        return PaymentProtocol.createPaymentMessage(txns, totalValue, refundAddr, memo, getMerchantData());
    } else {
        return null;
    }
}
 
Example #12
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
protected ListenableFuture<PaymentProtocol.Ack> sendPayment(final URL url, final Protos.Payment payment) {
    return executor.submit(new Callable<PaymentProtocol.Ack>() {
        @Override
        public PaymentProtocol.Ack call() throws Exception {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", PaymentProtocol.MIMETYPE_PAYMENT);
            connection.setRequestProperty("Accept", PaymentProtocol.MIMETYPE_PAYMENTACK);
            connection.setRequestProperty("Content-Length", Integer.toString(payment.getSerializedSize()));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request.
            DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
            payment.writeTo(outStream);
            outStream.flush();
            outStream.close();

            // Get response.
            Protos.PaymentACK paymentAck = Protos.PaymentACK.parseFrom(connection.getInputStream());
            return PaymentProtocol.parsePaymentAck(paymentAck);
        }
    });
}
 
Example #13
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 #14
Source File: PaymentProtocolTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testSignAndVerifyValid() throws Exception {
    Protos.PaymentRequest.Builder paymentRequest = minimalPaymentRequest().toBuilder();

    // Sign
    KeyStore keyStore = X509Utils
            .loadKeyStore("JKS", "password", getClass().getResourceAsStream("test-valid-cert"));
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("test-valid", "password".toCharArray());
    X509Certificate clientCert = (X509Certificate) keyStore.getCertificate("test-valid");
    PaymentProtocol.signPaymentRequest(paymentRequest, new X509Certificate[]{clientCert}, privateKey);

    // Verify
    PkiVerificationData verificationData = PaymentProtocol.verifyPaymentRequestPki(paymentRequest.build(), caStore);
    assertNotNull(verificationData);
    assertEquals(caCert, verificationData.rootAuthority.getTrustedCert());
}
 
Example #15
Source File: PaymentSessionTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private Protos.PaymentRequest newExpiredPaymentRequest() {
    Protos.Output.Builder outputBuilder = Protos.Output.newBuilder()
            .setAmount(coin.value)
            .setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
    Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
            .setNetwork("test")
            .setTime(time - 10)
            .setExpires(time - 1)
            .setPaymentUrl(simplePaymentUrl)
            .addOutputs(outputBuilder)
            .setMemo(paymentRequestMemo)
            .setMerchantData(merchantData)
            .build();
    return Protos.PaymentRequest.newBuilder()
            .setPaymentDetailsVersion(1)
            .setPkiType("none")
            .setSerializedPaymentDetails(paymentDetails.toByteString())
            .build();
}
 
Example #16
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Generates a Payment message and sends the payment to the merchant who sent the PaymentRequest.
 * Provide transactions built by the wallet.
 * NOTE: This does not broadcast the transactions to the bitcoin network, it merely sends a Payment message to the
 * merchant confirming the payment.
 * Returns an object wrapping PaymentACK once received.
 * If the PaymentRequest did not specify a payment_url, returns null and does nothing.
 * @param txns list of transactions to be included with the Payment message.
 * @param refundAddr will be used by the merchant to send money back if there was a problem.
 * @param memo is a message to include in the payment message sent to the merchant.
 */
@Nullable
public ListenableFuture<PaymentProtocol.Ack> sendPayment(List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
        throws PaymentProtocolException, VerificationException, IOException {
    Protos.Payment payment = getPayment(txns, refundAddr, memo);
    if (payment == null)
        return null;
    if (isExpired())
        throw new PaymentProtocolException.Expired("PaymentRequest is expired");
    URL url;
    try {
        url = new URL(paymentDetails.getPaymentUrl());
    } catch (MalformedURLException e) {
        throw new PaymentProtocolException.InvalidPaymentURL(e);
    }
    return sendPayment(url, payment);
}
 
Example #17
Source File: PaymentProtocolTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testSignAndVerifyValid() throws Exception {
    Protos.PaymentRequest.Builder paymentRequest = minimalPaymentRequest().toBuilder();

    // Sign
    KeyStore keyStore = X509Utils
            .loadKeyStore("JKS", "password", getClass().getResourceAsStream("test-valid-cert"));
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("test-valid", "password".toCharArray());
    X509Certificate clientCert = (X509Certificate) keyStore.getCertificate("test-valid");
    PaymentProtocol.signPaymentRequest(paymentRequest, new X509Certificate[]{clientCert}, privateKey);

    // Verify
    PkiVerificationData verificationData = PaymentProtocol.verifyPaymentRequestPki(paymentRequest.build(), caStore);
    assertNotNull(verificationData);
    assertEquals(caCert, verificationData.rootAuthority.getTrustedCert());
}
 
Example #18
Source File: PaymentSessionTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
private Protos.PaymentRequest newSimplePaymentRequest(String netID) {
    Protos.Output.Builder outputBuilder = Protos.Output.newBuilder()
            .setAmount(coin.value)
            .setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
    Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
            .setNetwork(netID)
            .setTime(time)
            .setPaymentUrl(simplePaymentUrl)
            .addOutputs(outputBuilder)
            .setMemo(paymentRequestMemo)
            .setMerchantData(merchantData)
            .build();
    return Protos.PaymentRequest.newBuilder()
            .setPaymentDetailsVersion(1)
            .setPkiType("none")
            .setSerializedPaymentDetails(paymentDetails.toByteString())
            .build();
}
 
Example #19
Source File: PaymentSessionTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testDefaults() throws Exception {
    Protos.Output.Builder outputBuilder = Protos.Output.newBuilder()
            .setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
    Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
            .setTime(time)
            .addOutputs(outputBuilder)
            .build();
    Protos.PaymentRequest paymentRequest = Protos.PaymentRequest.newBuilder()
            .setSerializedPaymentDetails(paymentDetails.toByteString())
            .build();
    MockPaymentSession paymentSession = new MockPaymentSession(paymentRequest);
    assertEquals(Coin.ZERO, paymentSession.getValue());
    assertNull(paymentSession.getPaymentUrl());
    assertNull(paymentSession.getMemo());
}
 
Example #20
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 #21
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 #22
Source File: PaymentSessionTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testPkiVerification() throws Exception {
    InputStream in = getClass().getResourceAsStream("pki_test.bitcoinpaymentrequest");
    Protos.PaymentRequest paymentRequest = Protos.PaymentRequest.newBuilder().mergeFrom(in).build();
    PaymentProtocol.PkiVerificationData pkiData = PaymentProtocol.verifyPaymentRequestPki(paymentRequest,
            new TrustStoreLoader.DefaultTrustStoreLoader().getKeyStore());
    assertEquals("www.bitcoincore.org", pkiData.displayName);
    assertEquals("The USERTRUST Network, Salt Lake City, US", pkiData.rootAuthorityName);
}
 
Example #23
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sign the provided payment request.
 * 
 * @param paymentRequest Payment request to sign, in its builder form.
 * @param certificateChain Certificate chain to send with the payment request, ordered from client certificate to root
 *            certificate. The root certificate itself may be omitted.
 * @param privateKey The key to sign with. Must match the public key from the first certificate of the certificate chain.
 */
public static void signPaymentRequest(Protos.PaymentRequest.Builder paymentRequest,
                                      X509Certificate[] certificateChain, PrivateKey privateKey) {
    try {
        final Protos.X509Certificates.Builder certificates = Protos.X509Certificates.newBuilder();
        for (final Certificate certificate : certificateChain)
            certificates.addCertificate(ByteString.copyFrom(certificate.getEncoded()));

        paymentRequest.setPkiType("x509+sha256");
        paymentRequest.setPkiData(certificates.build().toByteString());
        paymentRequest.setSignature(ByteString.EMPTY);
        final Protos.PaymentRequest paymentRequestToSign = paymentRequest.build();

        final String algorithm;
        if ("RSA".equalsIgnoreCase(privateKey.getAlgorithm()))
            algorithm = "SHA256withRSA";
        else
            throw new IllegalStateException(privateKey.getAlgorithm());

        final Signature signature = Signature.getInstance(algorithm);
        signature.initSign(privateKey);
        signature.update(paymentRequestToSign.toByteArray());

        paymentRequest.setSignature(ByteString.copyFrom(signature.sign()));
    } catch (final GeneralSecurityException x) {
        // Should never happen so don't make users have to think about it.
        throw new RuntimeException(x);
    }
}
 
Example #24
Source File: PaymentProtocol.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse transactions from payment message.
 * 
 * @param params network parameters (needed for transaction deserialization)
 * @param paymentMessage payment message to parse
 * @return list of transactions
 */
public static List<Transaction> parseTransactionsFromPaymentMessage(NetworkParameters params,
        Protos.Payment paymentMessage) {
    final List<Transaction> transactions = new ArrayList<>(paymentMessage.getTransactionsCount());
    for (final ByteString transaction : paymentMessage.getTransactionsList())
        transactions.add(params.getDefaultSerializer().makeTransaction(transaction.toByteArray()));
    return transactions;
}
 
Example #25
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 #26
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 #27
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 #28
Source File: PaymentProtocol.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a payment message with one standard pay to address output.
 *
 * @param transactions  one or more transactions that satisfy the requested outputs.
 * @param refundAmount  amount of coins to request as a refund, or null if no refund.
 * @param refundAddress address to refund coins to
 * @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 Coin refundAmount, @Nullable Address refundAddress, @Nullable String memo,
                                                  @Nullable byte[] merchantData) {
    if (refundAddress != null) {
        if (refundAmount == null)
            throw new IllegalArgumentException("Specify refund amount if refund address is specified.");
        return createPaymentMessage(transactions,
                ImmutableList.of(createPayToAddressOutput(refundAmount, refundAddress)), memo, merchantData);
    } else {
        return createPaymentMessage(transactions, null, memo, merchantData);
    }
}
 
Example #29
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 #30
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a payment message with one standard pay to address output.
 * 
 * @param transactions one or more transactions that satisfy the requested outputs.
 * @param refundAmount amount of coins to request as a refund, or null if no refund.
 * @param refundAddress address to refund coins to
 * @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 Coin refundAmount, @Nullable Address refundAddress, @Nullable String memo,
        @Nullable byte[] merchantData) {
    if (refundAddress != null) {
        if (refundAmount == null)
            throw new IllegalArgumentException("Specify refund amount if refund address is specified.");
        return createPaymentMessage(transactions,
                ImmutableList.of(createPayToAddressOutput(refundAmount, refundAddress)), memo, merchantData);
    } else {
        return createPaymentMessage(transactions, null, memo, merchantData);
    }
}