Search in sources :

Example 1 with BitcoindApi

use of com.sulacosoft.bitcoindconnector4j.BitcoindApi 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)

Example 2 with BitcoindApi

use of com.sulacosoft.bitcoindconnector4j.BitcoindApi in project balzac by balzac-lang.

the class RPCBitcoinClientTest method main.

public static void main(String[] args) {
    RPCBitcoinClient client = new RPCBitcoinClient("co2.unica.it", 80, "http", "/bitcoin-testnet", "bitcoin", "L4mbWnzC35BNrmTJ", 3, TimeUnit.SECONDS);
    BitcoindApi api = ((RPCBitcoinClient) client).getApi();
    System.out.println("Best block count: " + client.getBlockCount());
    System.out.println("Get raw transaction: " + client.getRawTransaction("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845"));
    System.out.println("Get raw transaction: " + api.getrawtransaction("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845"));
    System.out.println("Get raw transaction: " + api.getrawtransaction("ee40379cdf5439983d7603a88cafdd6de1c20d3b164850ab1055ed276ed95468"));
    System.out.println("Get raw transaction: " + api.getrawtransaction("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", true));
    System.out.println("Get raw transaction: " + api.getrawtransaction("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", false));
    System.out.println("Get UTXO: " + api.gettxout("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", 0));
    System.out.println("Get UTXO: " + api.gettxout("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", 2));
    System.out.println("Get isUTXO: " + client.isUTXO("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845"));
    System.out.println("Get isUTXO: " + client.isUTXO("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", 1));
    System.out.println("Is mined: " + client.isMined("82a560381ac769d778ad42d72c0355123c0df55282fe12630638740a18cc7b66"));
    System.out.println("Is mined (reliability low): " + client.isMined("82a560381ac769d778ad42d72c0355123c0df55282fe12630638740a18cc7b66", Confidentiality.LOW));
}
Also used : BitcoindApi(com.sulacosoft.bitcoindconnector4j.BitcoindApi)

Example 3 with BitcoindApi

use of com.sulacosoft.bitcoindconnector4j.BitcoindApi in project balzac by balzac-lang.

the class RPCBitcoinClientTest method test.

@Test
public void test() {
    BitcoindApi api = ((RPCBitcoinClient) client).getApi();
    System.out.println("Best block count: " + client.getBlockCount());
    System.out.println("Get raw transaction: " + client.getRawTransaction("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845"));
    System.out.println("Get raw transaction: " + api.getrawtransaction("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845"));
    System.out.println("Get raw transaction: " + api.getrawtransaction("ee40379cdf5439983d7603a88cafdd6de1c20d3b164850ab1055ed276ed95468"));
    System.out.println("Get raw transaction: " + api.getrawtransaction("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", true));
    System.out.println("Get raw transaction: " + api.getrawtransaction("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", false));
    System.out.println("Get UTXO: " + api.gettxout("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", 0));
    System.out.println("Get UTXO: " + api.gettxout("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", 2));
    System.out.println("Get isUTXO: " + client.isUTXO("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845"));
    System.out.println("Get isUTXO: " + client.isUTXO("17a2d3aeea1d742c9e42629bbf9ca04c0a19061497142f1f8b390ea43b1d5845", 1));
    System.out.println("Is mined: " + client.isMined("82a560381ac769d778ad42d72c0355123c0df55282fe12630638740a18cc7b66"));
    System.out.println("Is mined (reliability low): " + client.isMined("82a560381ac769d778ad42d72c0355123c0df55282fe12630638740a18cc7b66", Confidentiality.LOW));
}
Also used : BitcoindApi(com.sulacosoft.bitcoindconnector4j.BitcoindApi) Test(org.junit.Test)

Aggregations

BitcoindApi (com.sulacosoft.bitcoindconnector4j.BitcoindApi)3 RPCBitcoinClient (it.unica.tcs.lib.client.impl.RPCBitcoinClient)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 Label (org.eclipse.swt.widgets.Label)1 Spinner (org.eclipse.swt.widgets.Spinner)1 Text (org.eclipse.swt.widgets.Text)1 Test (org.junit.Test)1