Search in sources :

Example 1 with RPCBitcoinClient

use of it.unica.tcs.lib.client.impl.RPCBitcoinClient in project balzac by balzac-lang.

the class TrustedNodesPreferences method setBitcoinClientFactoryNodes.

public static void setBitcoinClientFactoryNodes(IPreferenceStore store) throws StorageException {
    ISecurePreferences secureStore = SecurePreferencesFactory.getDefault();
    String testnetHost = store.getString(PreferenceConstants.P_TESTNET_HOST);
    int testnetPort = store.getInt(PreferenceConstants.P_TESTNET_PORT);
    String testnetProtocol = store.getBoolean(PreferenceConstants.P_TESTNET_HTTPS) ? "https" : "http";
    String testnetUrl = store.getString(PreferenceConstants.P_TESTNET_URL);
    String testnetUsername = store.getString(PreferenceConstants.P_TESTNET_USERNAME);
    int testnetTimeout = store.getInt(PreferenceConstants.P_TESTNET_TIMEOUT);
    String testnetPassword = secureStore.node(SecureStorageUtils.SECURE_STORAGE__NODE__BITCOIN__TESTNET_NODE).get(SecureStorageUtils.SECURE_STORAGE__PROPERTY__TESTNET_PASSWORD, "");
    String mainnetHost = store.getString(PreferenceConstants.P_MAINNET_HOST);
    int mainnetPort = store.getInt(PreferenceConstants.P_MAINNET_PORT);
    String mainnetProtocol = store.getBoolean(PreferenceConstants.P_MAINNET_HTTPS) ? "https" : "http";
    String mainnetUrl = store.getString(PreferenceConstants.P_MAINNET_URL);
    String mainnetUsername = store.getString(PreferenceConstants.P_MAINNET_USERNAME);
    int mainnetTimeout = store.getInt(PreferenceConstants.P_MAINNET_TIMEOUT);
    String mainnetPassword = secureStore.node(SecureStorageUtils.SECURE_STORAGE__NODE__BITCOIN__MAINNET_NODE).get(SecureStorageUtils.SECURE_STORAGE__PROPERTY__MAINNET_PASSWORD, "");
    BitcoinClientI testnetClient = new RPCBitcoinClient(testnetHost, testnetPort, testnetProtocol, testnetUrl, testnetUsername, testnetPassword, testnetTimeout, TimeUnit.MILLISECONDS);
    BitcoinClientI mainnetClient = new RPCBitcoinClient(mainnetHost, mainnetPort, mainnetProtocol, mainnetUrl, mainnetUsername, mainnetPassword, mainnetTimeout, TimeUnit.MILLISECONDS);
    Injector injector = BitcointmActivator.getInstance().getInjector(BitcointmActivator.IT_UNICA_TCS_BITCOINTM);
    BitcoinClientFactory factory = injector.getInstance(BitcoinClientFactory.class);
    factory.setMainnetClient(mainnetClient);
    factory.setTestnetClient(testnetClient);
}
Also used : Injector(com.google.inject.Injector) BitcoinClientFactory(it.unica.tcs.utils.BitcoinClientFactory) BitcoinClientI(it.unica.tcs.lib.client.BitcoinClientI) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) RPCBitcoinClient(it.unica.tcs.lib.client.impl.RPCBitcoinClient)

Example 2 with RPCBitcoinClient

use of it.unica.tcs.lib.client.impl.RPCBitcoinClient in project balzac by balzac-lang.

the class TrustedNodesPreferences method createNetworkContents.

private void createNetworkContents(Composite parent, boolean testnet) {
    Group group = new Group(parent, SWT.NONE);
    group.setText(testnet ? " Testnet node " : " Mainnet node ");
    GridLayout gl_grpTestnet = new GridLayout(2, false);
    gl_grpTestnet.marginTop = 5;
    gl_grpTestnet.marginRight = 5;
    gl_grpTestnet.marginLeft = 5;
    gl_grpTestnet.marginBottom = 5;
    group.setLayout(gl_grpTestnet);
    Label hostLabel = new Label(group, SWT.NONE);
    hostLabel.setText("Host");
    Text hostText = new Text(group, SWT.BORDER);
    hostText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    Label lblPort = new Label(group, SWT.NONE);
    lblPort.setText("Port");
    Spinner portSpinner = new Spinner(group, SWT.BORDER);
    portSpinner.setMaximum(65535);
    portSpinner.setMinimum(1);
    GridData gd_portSpinner = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_portSpinner.heightHint = 19;
    gd_portSpinner.widthHint = 163;
    portSpinner.setLayoutData(gd_portSpinner);
    Label lblHttpsButton = new Label(group, SWT.NONE);
    lblHttpsButton.setText("Https");
    Button httpsCheckbox = new Button(group, SWT.CHECK);
    httpsCheckbox.setToolTipText("Recommended if the server accepts SSL connections");
    Label usernameLabel = new Label(group, SWT.NONE);
    usernameLabel.setText("Username");
    Text usernameText = new Text(group, SWT.BORDER);
    usernameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    Label urlLabel = new Label(group, SWT.NONE);
    urlLabel.setText("URL");
    Text urlText = new Text(group, SWT.BORDER);
    urlText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    urlText.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            String text = urlText.getText();
            if (!text.startsWith("/"))
                urlText.setText("/" + text);
        }

        @Override
        public void focusGained(FocusEvent e) {
        }
    });
    Label testnetPasswordLabel = new Label(group, SWT.NONE);
    testnetPasswordLabel.setText("Password");
    Text passwordText = new Text(group, SWT.BORDER | SWT.PASSWORD);
    passwordText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    Label timeoutLabel = new Label(group, SWT.NONE);
    timeoutLabel.setText("Timeout");
    Spinner timeoutSpinner = new Spinner(group, SWT.BORDER);
    timeoutSpinner.setMaximum(Integer.MAX_VALUE);
    timeoutSpinner.setMinimum(-1);
    timeoutSpinner.setToolTipText("Set -1 for undefined waiting time (not recommended)");
    GridData gd_timeoutSpinner = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_timeoutSpinner.heightHint = 19;
    gd_timeoutSpinner.widthHint = 163;
    timeoutSpinner.setLayoutData(gd_timeoutSpinner);
    // test button with feedbacks
    Composite compositeBtnTest = new Composite(group, SWT.NONE);
    compositeBtnTest.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
    GridLayout gl_composite = new GridLayout(2, false);
    gl_composite.horizontalSpacing = 10;
    gl_composite.marginWidth = 0;
    compositeBtnTest.setLayout(gl_composite);
    Button btnTestConnection = new Button(compositeBtnTest, SWT.NONE);
    btnTestConnection.setText("Test");
    Composite compositeFeedbacks = new Composite(compositeBtnTest, SWT.NONE);
    Canvas networkFeedbackOK = new Canvas(compositeFeedbacks, SWT.NONE);
    networkFeedbackOK.setBounds(0, 0, 16, 16);
    networkFeedbackOK.setBackgroundImage(imageSuccess);
    networkFeedbackOK.setVisible(false);
    Canvas networkFeedbackERR = new Canvas(compositeFeedbacks, SWT.NONE);
    networkFeedbackERR.setBounds(0, 0, 16, 16);
    networkFeedbackERR.setBackgroundImage(imageError);
    networkFeedbackERR.setVisible(false);
    // error details
    Composite errorDetailsComposite = new Composite(group, SWT.BORDER);
    StyledText errorDetailsTextArea = new StyledText(errorDetailsComposite, SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL);
    GridData gd_errorDetailsTextArea = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
    gd_errorDetailsTextArea.grabExcessVerticalSpace = true;
    errorDetailsComposite.setLayoutData(gd_errorDetailsTextArea);
    errorDetailsComposite.setBackground(errorDetailsTextArea.getBackground());
    errorDetailsTextArea.setAlwaysShowScrollBars(false);
    errorDetailsComposite.addControlListener(new ControlListener() {

        private int TOP_MARGIN = 4;

        private int LEFT_MARGIN = 8;

        @Override
        public void controlResized(ControlEvent e) {
            Composite parent = errorDetailsTextArea.getParent();
            errorDetailsTextArea.setBounds(LEFT_MARGIN, TOP_MARGIN, parent.getSize().x - 2 * parent.getBorderWidth() - LEFT_MARGIN, parent.getSize().y - 2 * parent.getBorderWidth() - TOP_MARGIN);
        }

        @Override
        public void controlMoved(ControlEvent e) {
        }
    });
    btnTestConnection.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDown(MouseEvent e) {
            errorDetailsTextArea.setText("");
            networkFeedbackOK.setVisible(false);
            networkFeedbackERR.setVisible(false);
        }

        @Override
        public void mouseUp(MouseEvent e) {
            String address = hostText.getText();
            Integer port = portSpinner.getSelection();
            String protocol = httpsCheckbox.getSelection() ? "https" : "http";
            String url = urlText.getText();
            String user = usernameText.getText();
            String password = passwordText.getText();
            Integer timeout = timeoutSpinner.getSelection();
            if (password.isEmpty()) {
                try {
                    if (testnet)
                        password = secureStorage.node(SecureStorageUtils.SECURE_STORAGE__NODE__BITCOIN__TESTNET_NODE).get(SecureStorageUtils.SECURE_STORAGE__PROPERTY__TESTNET_PASSWORD, "");
                    else
                        password = secureStorage.node(SecureStorageUtils.SECURE_STORAGE__NODE__BITCOIN__MAINNET_NODE).get(SecureStorageUtils.SECURE_STORAGE__PROPERTY__MAINNET_PASSWORD, "");
                } catch (StorageException e1) {
                    e1.printStackTrace();
                    errorDetailsTextArea.append("Error fetching the password from the secure store.\n\n");
                    errorDetailsTextArea.append(Throwables.getStackTraceAsString(e1));
                    networkFeedbackERR.setVisible(true);
                    return;
                }
            }
            RPCBitcoinClient bitcoinClient = new RPCBitcoinClient(address, port, protocol, url, user, password, timeout, TimeUnit.MILLISECONDS);
            try {
                BitcoindApi api = bitcoinClient.getApi();
                boolean isTestnet = api.getblockchaininfo().getChain().equals("test");
                if (isTestnet != testnet) {
                    String expected = testnet ? "testnet" : "mainnet";
                    String actual = isTestnet ? "testnet" : "mainnet";
                    errorDetailsTextArea.append("Wrong network type: expected " + expected + ", found " + actual + ".");
                    networkFeedbackERR.setVisible(true);
                    return;
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                errorDetailsTextArea.append("Cannot connect to the node.\n\n");
                errorDetailsTextArea.append(Throwables.getStackTraceAsString(e1));
                networkFeedbackERR.setVisible(true);
                return;
            }
            errorDetailsTextArea.append("ok");
            networkFeedbackOK.setVisible(true);
        }
    });
    if (testnet)
        testnetHostText = hostText;
    else
        mainnetHostText = hostText;
    if (testnet)
        testnetPortSpinner = portSpinner;
    else
        mainnetPortSpinner = portSpinner;
    if (testnet)
        testnetHttpsCheckbox = httpsCheckbox;
    else
        mainnetHttpsCheckbox = httpsCheckbox;
    if (testnet)
        testnetUrlText = urlText;
    else
        mainnetUrlText = urlText;
    if (testnet)
        testnetUsernameText = usernameText;
    else
        mainnetUsernameText = usernameText;
    if (testnet)
        testnetPasswordText = passwordText;
    else
        mainnetPasswordText = passwordText;
    if (testnet)
        testnetTimeoutSpinner = timeoutSpinner;
    else
        mainnetTimeoutSpinner = timeoutSpinner;
}
Also used : Group(org.eclipse.swt.widgets.Group) StyledText(org.eclipse.swt.custom.StyledText) MouseEvent(org.eclipse.swt.events.MouseEvent) Composite(org.eclipse.swt.widgets.Composite) Spinner(org.eclipse.swt.widgets.Spinner) Canvas(org.eclipse.swt.widgets.Canvas) Label(org.eclipse.swt.widgets.Label) MouseAdapter(org.eclipse.swt.events.MouseAdapter) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) RPCBitcoinClient(it.unica.tcs.lib.client.impl.RPCBitcoinClient) FocusEvent(org.eclipse.swt.events.FocusEvent) StorageException(org.eclipse.equinox.security.storage.StorageException) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) ControlListener(org.eclipse.swt.events.ControlListener) ControlEvent(org.eclipse.swt.events.ControlEvent) FocusListener(org.eclipse.swt.events.FocusListener) StorageException(org.eclipse.equinox.security.storage.StorageException) BitcoindApi(com.sulacosoft.bitcoindconnector4j.BitcoindApi)

Aggregations

RPCBitcoinClient (it.unica.tcs.lib.client.impl.RPCBitcoinClient)2 Injector (com.google.inject.Injector)1 BitcoindApi (com.sulacosoft.bitcoindconnector4j.BitcoindApi)1 BitcoinClientI (it.unica.tcs.lib.client.BitcoinClientI)1 BitcoinClientFactory (it.unica.tcs.utils.BitcoinClientFactory)1 ISecurePreferences (org.eclipse.equinox.security.storage.ISecurePreferences)1 StorageException (org.eclipse.equinox.security.storage.StorageException)1 StyledText (org.eclipse.swt.custom.StyledText)1 ControlEvent (org.eclipse.swt.events.ControlEvent)1 ControlListener (org.eclipse.swt.events.ControlListener)1 FocusEvent (org.eclipse.swt.events.FocusEvent)1 FocusListener (org.eclipse.swt.events.FocusListener)1 MouseAdapter (org.eclipse.swt.events.MouseAdapter)1 MouseEvent (org.eclipse.swt.events.MouseEvent)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Button (org.eclipse.swt.widgets.Button)1 Canvas (org.eclipse.swt.widgets.Canvas)1 Composite (org.eclipse.swt.widgets.Composite)1 Group (org.eclipse.swt.widgets.Group)1