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

The following examples show how to use org.bitcoin.protocols.payments.Protos#PaymentRequest . 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 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 2
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 3
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 4
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 5
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 6
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 7
Source File: PaymentProtocolTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private Protos.PaymentRequest minimalPaymentRequest() {
    Protos.PaymentDetails.Builder paymentDetails = Protos.PaymentDetails.newBuilder();
    paymentDetails.setTime(System.currentTimeMillis());
    Protos.PaymentRequest.Builder paymentRequest = Protos.PaymentRequest.newBuilder();
    paymentRequest.setSerializedPaymentDetails(paymentDetails.build().toByteString());
    return paymentRequest.build();
}
 
Example 8
Source File: PaymentProtocol.java    From bcm-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 9
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 10
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 11
Source File: PaymentProtocolTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private Protos.PaymentRequest minimalPaymentRequest() {
    Protos.PaymentDetails.Builder paymentDetails = Protos.PaymentDetails.newBuilder();
    paymentDetails.setTime(System.currentTimeMillis());
    Protos.PaymentRequest.Builder paymentRequest = Protos.PaymentRequest.newBuilder();
    paymentRequest.setSerializedPaymentDetails(paymentDetails.build().toByteString());
    return paymentRequest.build();
}
 
Example 12
Source File: PaymentSessionTest.java    From GreenBits 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 13
Source File: PaymentSessionTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public MockPaymentSession(Protos.PaymentRequest request) throws PaymentProtocolException {
    super(request);
}
 
Example 14
Source File: PaymentSession.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the protobuf that this object was instantiated with.
 */
public Protos.PaymentRequest getPaymentRequest() {
    return paymentRequest;
}
 
Example 15
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a PaymentSession from the provided {@link Protos.PaymentRequest}.
 * Verifies PKI by default.
 */
public PaymentSession(Protos.PaymentRequest request) throws PaymentProtocolException {
    this(request, true, null);
}
 
Example 16
Source File: PaymentSession.java    From bcm-android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a PaymentSession from the provided {@link Protos.PaymentRequest}.
 * Verifies PKI by default.
 */
public PaymentSession(Protos.PaymentRequest request) throws PaymentProtocolException {
    this(request, true, null);
}
 
Example 17
Source File: PaymentSession.java    From GreenBits with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a PaymentSession from the provided {@link Protos.PaymentRequest}.
 * Verifies PKI by default.
 */
public PaymentSession(Protos.PaymentRequest request) throws PaymentProtocolException {
    this(request, true, null);
}
 
Example 18
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Parse a payment request.
 * 
 * @param paymentRequest payment request to parse
 * @return instance of {@link PaymentSession}, used as a value object
 * @throws PaymentProtocolException
 */
public static PaymentSession parsePaymentRequest(Protos.PaymentRequest paymentRequest)
        throws PaymentProtocolException {
    return new PaymentSession(paymentRequest, false, null);
}
 
Example 19
Source File: PaymentSession.java    From GreenBits with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a PaymentSession from the provided {@link Protos.PaymentRequest}.
 * If verifyPki is true, also validates the signature and throws an exception if it fails.
 */
public PaymentSession(Protos.PaymentRequest request, boolean verifyPki) throws PaymentProtocolException {
    this(request, verifyPki, null);
}
 
Example 20
Source File: PaymentProtocol.java    From GreenBits with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Parse a payment request.
 * 
 * @param paymentRequest payment request to parse
 * @return instance of {@link PaymentSession}, used as a value object
 * @throws PaymentProtocolException
 */
public static PaymentSession parsePaymentRequest(Protos.PaymentRequest paymentRequest)
        throws PaymentProtocolException {
    return new PaymentSession(paymentRequest, false, null);
}