Search in sources :

Example 1 with WalletAppKit

use of com.google.bitcoin.kits.WalletAppKit in project PayFile by mikehearn.

the class Main method init.

private void init(Stage mainWindow) throws IOException {
    this.mainWindow = mainWindow;
    // commented out for now as Modena looks better, but might want to bring this back.
    /* if (System.getProperty("os.name").toLowerCase().contains("mac")) {
            AquaFx.style();
        } */
    // Load the GUI. The Controller class will be automagically created and wired up.
    URL location = getClass().getResource("main.fxml");
    FXMLLoader loader = new FXMLLoader(location);
    mainUI = loader.load();
    controller = loader.getController();
    // Configure the window with a StackPane so we can overlay things on top of the main UI.
    uiStack = new StackPane(mainUI);
    mainWindow.setTitle(APP_NAME);
    final Scene scene = new Scene(uiStack);
    // Add CSS that we need.
    TextFieldValidator.configureScene(scene);
    mainWindow.setScene(scene);
    // Make log output concise.
    BriefLogFormatter.init();
    // Tell bitcoinj to run event handlers on the JavaFX UI thread. This keeps things simple and means
    // we cannot forget to switch threads when adding event handlers. Unfortunately, the DownloadListener
    // we give to the app kit is currently an exception and runs on a library thread. It'll get fixed in
    // a future version. Also note that this doesn't affect the default executor for ListenableFutures.
    // That must be specified each time.
    Threading.USER_THREAD = Platform::runLater;
    // Create the app kit. It won't do any heavyweight initialization until after we start it.
    bitcoin = new WalletAppKit(params, new File("."), filePrefix + APP_NAME) {

        @Override
        protected void addWalletExtensions() throws Exception {
            super.addWalletExtensions();
            wallet().addExtension(new StoredPaymentChannelClientStates(wallet(), peerGroup()));
        }
    };
    if (params == RegTestParams.get()) {
        // You should run a regtest mode bitcoind locally.
        bitcoin.connectToLocalHost();
    } else if (params == MainNetParams.get()) {
        // Checkpoints are block headers that ship inside our app: for a new user, we pick the last header
        // in the checkpoints file and then download the rest from the network. It makes things much faster.
        // Checkpoint files are made using the BuildCheckpoints tool and usually we have to download the
        // last months worth or more (takes a few seconds).
        bitcoin.setCheckpoints(getClass().getResourceAsStream("checkpoints"));
    }
    // Now configure and start the appkit. It won't block for very long.
    bitcoin.setDownloadListener(controller.progressBarUpdater()).setBlockingStartup(false).setUserAgent("PayFile Client", "1.0").startAndWait();
    // Don't make the user wait for confirmations for now, as the intention is they're sending it their own money!
    bitcoin.wallet().allowSpendingUnconfirmedTransactions();
    System.out.println(bitcoin.wallet());
    controller.onBitcoinSetup();
    overlayUI("connect_server.fxml");
    mainUI.setVisible(false);
    mainWindow.show();
}
Also used : Platform(javafx.application.Platform) StoredPaymentChannelClientStates(com.google.bitcoin.protocols.channels.StoredPaymentChannelClientStates) WalletAppKit(com.google.bitcoin.kits.WalletAppKit) Scene(javafx.scene.Scene) FXMLLoader(javafx.fxml.FXMLLoader) File(java.io.File) URL(java.net.URL) StackPane(javafx.scene.layout.StackPane) OptionException(joptsimple.OptionException) IOException(java.io.IOException) BlockStoreException(com.google.bitcoin.store.BlockStoreException)

Example 2 with WalletAppKit

use of com.google.bitcoin.kits.WalletAppKit in project PayFile by mikehearn.

the class Server method main.

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();
    // Usage: --file-directory=<file-directory> [--network=[mainnet|testnet|regtest]] [--port=<port>]"
    OptionParser parser = new OptionParser();
    OptionSpec<File> fileDir = parser.accepts("file-directory").withRequiredArg().required().ofType(File.class);
    parser.accepts("network").withRequiredArg().withValuesConvertedBy(regex("(mainnet)|(testnet)|(regtest)")).defaultsTo("mainnet");
    parser.accepts("port").withRequiredArg().ofType(Integer.class).defaultsTo(PORT);
    parser.accepts("help").forHelp();
    parser.formatHelpWith(new BuiltinHelpFormatter(120, 10));
    OptionSet options;
    try {
        options = parser.parse(args);
    } catch (OptionException e) {
        System.err.println(e.getMessage());
        System.err.println("");
        parser.printHelpOn(System.err);
        return;
    }
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        return;
    }
    directoryToServe = options.valueOf(fileDir);
    if (!buildFileList())
        return;
    if (options.valueOf("network").equals(("testnet"))) {
        params = TestNet3Params.get();
        filePrefix = "testnet-";
    } else if (options.valueOf("network").equals(("mainnet"))) {
        params = MainNetParams.get();
        filePrefix = "";
    } else if (options.valueOf("network").equals(("regtest"))) {
        params = RegTestParams.get();
        filePrefix = "regtest-";
    }
    final int port = Integer.parseInt(options.valueOf("port").toString());
    WalletAppKit appkit = new WalletAppKit(params, new File("."), filePrefix + "payfile-server-" + port) {

        @Override
        protected void addWalletExtensions() throws Exception {
            super.addWalletExtensions();
            wallet().addExtension(new StoredPaymentChannelServerStates(wallet(), peerGroup()));
        }
    };
    if (params == RegTestParams.get()) {
        appkit.connectToLocalHost();
    }
    appkit.setUserAgent("PayFile Server", "1.0").startAndWait();
    System.out.println(appkit.wallet().toString(false, true, true, appkit.chain()));
    ServerSocket socket = new ServerSocket(port);
    Socket clientSocket;
    do {
        clientSocket = socket.accept();
        final Server server = new Server(appkit.wallet(), appkit.peerGroup(), clientSocket);
        Thread clientThread = new Thread(server, clientSocket.toString());
        clientThread.start();
    } while (true);
}
Also used : PaymentChannelServer(com.google.bitcoin.protocols.channels.PaymentChannelServer) ServerSocket(java.net.ServerSocket) StoredPaymentChannelServerStates(com.google.bitcoin.protocols.channels.StoredPaymentChannelServerStates) BigInteger(java.math.BigInteger) WalletAppKit(com.google.bitcoin.kits.WalletAppKit) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Aggregations

WalletAppKit (com.google.bitcoin.kits.WalletAppKit)2 PaymentChannelServer (com.google.bitcoin.protocols.channels.PaymentChannelServer)1 StoredPaymentChannelClientStates (com.google.bitcoin.protocols.channels.StoredPaymentChannelClientStates)1 StoredPaymentChannelServerStates (com.google.bitcoin.protocols.channels.StoredPaymentChannelServerStates)1 BlockStoreException (com.google.bitcoin.store.BlockStoreException)1 File (java.io.File)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 URL (java.net.URL)1 Platform (javafx.application.Platform)1 FXMLLoader (javafx.fxml.FXMLLoader)1 Scene (javafx.scene.Scene)1 StackPane (javafx.scene.layout.StackPane)1 OptionException (joptsimple.OptionException)1