From 6a9973e2c5c8720a3a839a8e8df3090e07bae255 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 21 Feb 2014 16:53:43 +0530 Subject: [PATCH] Add a tool to dump payment protocol requests to stdout. --- pom.xml | 2 +- tools/pom.xml | 14 +++ .../google/bitcoin/tools/PaymentProtocol.java | 85 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tools/src/main/java/com/google/bitcoin/tools/PaymentProtocol.java diff --git a/pom.xml b/pom.xml index 09b5819a..b56820d6 100644 --- a/pom.xml +++ b/pom.xml @@ -64,7 +64,7 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.2 + 3.1 1.6 1.6 diff --git a/tools/pom.xml b/tools/pom.xml index e9321877..dbc0dec8 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -31,7 +31,21 @@ A collection of useful tools that use the bitcoinj library to perform wallet operations + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + true + true + + + org.apache.maven.plugins maven-shade-plugin diff --git a/tools/src/main/java/com/google/bitcoin/tools/PaymentProtocol.java b/tools/src/main/java/com/google/bitcoin/tools/PaymentProtocol.java new file mode 100644 index 00000000..55b29a61 --- /dev/null +++ b/tools/src/main/java/com/google/bitcoin/tools/PaymentProtocol.java @@ -0,0 +1,85 @@ +/* + * Copyright 2014 The bitcoinj team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.bitcoin.tools; + +import com.google.bitcoin.protocols.payments.PaymentRequestException; +import com.google.bitcoin.protocols.payments.PaymentSession; +import com.google.bitcoin.uri.BitcoinURI; +import com.google.bitcoin.uri.BitcoinURIParseException; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Date; +import java.util.concurrent.ExecutionException; + +import static java.lang.String.format; + +/** Takes a URL or bitcoin URI and prints information about the payment request. */ +public class PaymentProtocol { + public static void main(String[] args) { + if (args.length < 1) { + System.err.println("Provide a bitcoin URI or URL as the argument."); + return; + } + dump(args[0]); + } + + private static void dump(String arg) { + try { + URI uri = new URI(arg); + PaymentSession session; + if (uri.getScheme().equals("http")) { + session = PaymentSession.createFromUrl(arg).get(); + } else if (uri.getScheme().equals("bitcoin")) { + BitcoinURI bcuri = new BitcoinURI(arg); + final String paymentRequestUrl = bcuri.getPaymentRequestUrl(); + if (paymentRequestUrl == null) { + System.err.println("No r= param in bitcoin URI"); + return; + } + session = PaymentSession.createFromBitcoinUri(bcuri).get(); + } else { + System.err.println("Unknown URI scheme: " + uri.getScheme()); + return; + } + final int version = session.getPaymentRequest().getPaymentDetailsVersion(); + StringBuilder output = new StringBuilder( + format("Bitcoin payment request, version %d%nDate: %s%n", version, session.getDate())); + PaymentSession.PkiVerificationData pki = session.verifyPki(); + if (pki != null) { + output.append(format("Signed by: %s%nIdentity verified by: %s%n", pki.name, pki.rootAuthorityName)); + } + if (session.getPaymentDetails().hasExpires()) { + output.append(format("Expires: %s%n", new Date(session.getPaymentDetails().getExpires() * 1000))); + } + if (session.getMemo() != null) { + output.append(format("Memo: %s%n", session.getMemo())); + } + output.append(format("%n%n%s%n%s", session.getPaymentRequest(), session.getPaymentDetails())); + System.out.println(output); + } catch (URISyntaxException | BitcoinURIParseException e) { + System.err.println("Could not parse URI: " + e.getMessage()); + } catch (PaymentRequestException e) { + System.err.println("Could not handle payment URL: " + e.getMessage()); + } catch (InterruptedException e) { + System.err.println("Interrupted whilst processing/downloading."); + } catch (ExecutionException e) { + System.err.println("Failed whilst retrieving payment URL: " + e.getMessage()); + e.printStackTrace(); + } + } +}