Beispiel #1
0
 private static void send(PaymentSession session) {
   try {
     System.out.println("Payment Request");
     System.out.println("Amount: " + session.getValue().doubleValue() / 100000 + "mDOGE");
     System.out.println("Date: " + session.getDate());
     System.out.println("Memo: " + session.getMemo());
     if (session.pkiVerificationData != null) {
       System.out.println("Pki-Verified Name: " + session.pkiVerificationData.name);
       if (session.pkiVerificationData.orgName != null)
         System.out.println("Pki-Verified Org: " + session.pkiVerificationData.orgName);
       System.out.println(
           "PKI data verified by: " + session.pkiVerificationData.rootAuthorityName);
     }
     final Wallet.SendRequest req = session.getSendRequest();
     if (password != null) {
       if (!wallet.checkPassword(password)) {
         System.err.println("Password is incorrect.");
         return;
       }
       req.aesKey = wallet.getKeyCrypter().deriveKey(password);
     }
     wallet.completeTx(req); // may throw InsufficientMoneyException.
     if (options.has("offline")) {
       wallet.commitTx(req.tx);
       return;
     }
     setup();
     // No refund address specified, no user-specified memo field.
     ListenableFuture<PaymentSession.Ack> future =
         session.sendPayment(ImmutableList.of(req.tx), null, null);
     if (future == null) {
       // No payment_url for submission so, broadcast and wait.
       peers.startAndWait();
       peers.broadcastTransaction(req.tx).get();
     } else {
       PaymentSession.Ack ack = future.get();
       wallet.commitTx(req.tx);
       System.out.println("Memo from server: " + ack.getMemo());
     }
   } catch (PaymentRequestException e) {
     System.err.println("Failed to send payment " + e.getMessage());
     System.exit(1);
   } catch (VerificationException e) {
     System.err.println("Failed to send payment " + e.getMessage());
     System.exit(1);
   } catch (ExecutionException e) {
     System.err.println("Failed to send payment " + e.getMessage());
     System.exit(1);
   } catch (IOException e) {
     System.err.println("Invalid payment " + e.getMessage());
     System.exit(1);
   } catch (InterruptedException e1) {
     // Ignore.
   } catch (InsufficientMoneyException e) {
     System.err.println(
         "Insufficient funds: have " + Utils.bitcoinValueToFriendlyString(wallet.getBalance()));
   } catch (BlockStoreException e) {
     throw new RuntimeException(e);
   }
 }
Beispiel #2
0
 private static void sendPaymentRequest(String location, boolean verifyPki) {
   if (location.startsWith("http") || location.startsWith("defcoin")) {
     try {
       ListenableFuture<PaymentSession> future;
       if (location.startsWith("http")) {
         future = PaymentSession.createFromUrl(location, verifyPki);
       } else {
         BitcoinURI paymentRequestURI = new BitcoinURI(location);
         future = PaymentSession.createFromBitcoinUri(paymentRequestURI, verifyPki);
       }
       PaymentSession session = future.get();
       if (session != null) {
         send(session);
       } else {
         System.err.println("Server returned null session");
         System.exit(1);
       }
     } catch (PaymentRequestException e) {
       System.err.println("Error creating payment session " + e.getMessage());
       System.exit(1);
     } catch (BitcoinURIParseException e) {
       System.err.println("Invalid defcoin uri: " + e.getMessage());
       System.exit(1);
     } catch (InterruptedException e) {
       // Ignore.
     } catch (ExecutionException e) {
       throw new RuntimeException(e);
     }
   } else {
     // Try to open the payment request as a file.
     FileInputStream stream = null;
     try {
       File paymentRequestFile = new File(location);
       stream = new FileInputStream(paymentRequestFile);
     } catch (Exception e) {
       System.err.println("Failed to open file: " + e.getMessage());
       System.exit(1);
     }
     try {
       paymentRequest =
           org.bitcoin.protocols.payments.Protos.PaymentRequest.newBuilder()
               .mergeFrom(stream)
               .build();
     } catch (IOException e) {
       System.err.println("Failed to parse payment request from file " + e.getMessage());
       System.exit(1);
     }
     PaymentSession session = null;
     try {
       session = new PaymentSession(paymentRequest, verifyPki);
     } catch (PaymentRequestException e) {
       System.err.println("Error creating payment session " + e.getMessage());
       System.exit(1);
     }
     send(session);
   }
 }