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);
}
}
Aggregations