Search in sources :

Example 1 with PaymentProtocolException

use of org.bitcoinj.protocols.payments.PaymentProtocolException in project bitcoin-wallet by bitcoin-wallet.

the class InputParser method parsePaymentRequest.

public static PaymentIntent parsePaymentRequest(final byte[] serializedPaymentRequest) throws PaymentProtocolException {
    try {
        if (serializedPaymentRequest.length > 50000)
            throw new PaymentProtocolException("payment request too big: " + serializedPaymentRequest.length);
        final Protos.PaymentRequest paymentRequest = Protos.PaymentRequest.parseFrom(serializedPaymentRequest);
        final String pkiName;
        final String pkiCaName;
        if (!"none".equals(paymentRequest.getPkiType())) {
            final KeyStore keystore = new TrustStoreLoader.DefaultTrustStoreLoader().getKeyStore();
            final PkiVerificationData verificationData = PaymentProtocol.verifyPaymentRequestPki(paymentRequest, keystore);
            pkiName = verificationData.displayName;
            pkiCaName = verificationData.rootAuthorityName;
        } else {
            pkiName = null;
            pkiCaName = null;
        }
        final PaymentSession paymentSession = PaymentProtocol.parsePaymentRequest(paymentRequest);
        if (paymentSession.isExpired())
            throw new PaymentProtocolException.Expired("payment details expired: current time " + new Date() + " after expiry time " + paymentSession.getExpires());
        if (!paymentSession.getNetworkParameters().equals(Constants.NETWORK_PARAMETERS))
            throw new PaymentProtocolException.InvalidNetwork("cannot handle payment request network: " + paymentSession.getNetworkParameters());
        final ArrayList<PaymentIntent.Output> outputs = new ArrayList<PaymentIntent.Output>(1);
        for (final PaymentProtocol.Output output : paymentSession.getOutputs()) outputs.add(PaymentIntent.Output.valueOf(output));
        final String memo = paymentSession.getMemo();
        final String paymentUrl = paymentSession.getPaymentUrl();
        final byte[] merchantData = paymentSession.getMerchantData();
        final byte[] paymentRequestHash = Hashing.sha256().hashBytes(serializedPaymentRequest).asBytes();
        final PaymentIntent paymentIntent = new PaymentIntent(PaymentIntent.Standard.BIP70, pkiName, pkiCaName, outputs.toArray(new PaymentIntent.Output[0]), memo, paymentUrl, merchantData, null, paymentRequestHash);
        if (paymentIntent.hasPaymentUrl() && !paymentIntent.isSupportedPaymentUrl())
            throw new PaymentProtocolException.InvalidPaymentURL("cannot handle payment url: " + paymentIntent.paymentUrl);
        return paymentIntent;
    } catch (final InvalidProtocolBufferException x) {
        throw new PaymentProtocolException(x);
    } catch (final UninitializedMessageException x) {
        throw new PaymentProtocolException(x);
    } catch (final FileNotFoundException x) {
        throw new RuntimeException(x);
    } catch (final KeyStoreException x) {
        throw new RuntimeException(x);
    }
}
Also used : PaymentProtocol(org.bitcoinj.protocols.payments.PaymentProtocol) PkiVerificationData(org.bitcoinj.protocols.payments.PaymentProtocol.PkiVerificationData) TrustStoreLoader(org.bitcoinj.crypto.TrustStoreLoader) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) PaymentProtocolException(org.bitcoinj.protocols.payments.PaymentProtocolException) Protos(org.bitcoin.protocols.payments.Protos) UninitializedMessageException(com.google.protobuf.UninitializedMessageException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PaymentIntent(de.schildbach.wallet.data.PaymentIntent) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) PaymentSession(org.bitcoinj.protocols.payments.PaymentSession) Date(java.util.Date)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 UninitializedMessageException (com.google.protobuf.UninitializedMessageException)1 PaymentIntent (de.schildbach.wallet.data.PaymentIntent)1 FileNotFoundException (java.io.FileNotFoundException)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Protos (org.bitcoin.protocols.payments.Protos)1 TrustStoreLoader (org.bitcoinj.crypto.TrustStoreLoader)1 PaymentProtocol (org.bitcoinj.protocols.payments.PaymentProtocol)1 PkiVerificationData (org.bitcoinj.protocols.payments.PaymentProtocol.PkiVerificationData)1 PaymentProtocolException (org.bitcoinj.protocols.payments.PaymentProtocolException)1 PaymentSession (org.bitcoinj.protocols.payments.PaymentSession)1