Electrum on Windows

Installing electrum by windows installer.

After installation you will have two shortcuts in “Start” menu “Electrum” and “Electrum Testnet”. As we are going to use testnet mode click on respective shortcut.

Since electrum doesn’t work with cmd in windows we should use GUI to restore a wallet from seed phrase and load it.

Next we should configure rpc. The file config for testnet is on the path:

C:\Users\UserName\AppData\Roaming\Electrum\testnet

Make sure that you changed rpcuser and rpcpassword and also added rpcport:

 …  
"rpcpassword": "t",
"rpcport": 7777,
"rpcuser": "user",
…

Now restart electrum testnet and watch whether the daemon is listening 7777 port. For that open cmd and type:

netstat -a

And if you get:

  Имя    Локальный адрес        Внешний адрес          Состояние
...
  TCP    127.0.0.1:7777         DESKTOP-88AQOQA:0      LISTENING 
...

That means that it is all right.

Getting some testnet coins

To get some testnet bitcoins go here https://coinfaucet.eu/en/btc-testnet/

Java program example

Now let’s try some code. Let’s try getbalance method of JSON-RPC. Copy from the electrum docs https://electrum.readthedocs.io/en/latest/protocol.html

curl --data-binary '{"jsonrpc":"2.0","id":"curltext","method":"getbalance","params":[]}' http://username:password@127.0.0.1:7777

to https://curlconverter.com/java/

And we get the code where we need replace the username and the password.

Then open your IDE and create new maven project:

Choose Create a simple project:

Set names for Group Id and Artifact Id:

If you have jdk 8 or higher you should add dependency to the pom

		<dependency>
    		<groupId>javax.xml.bind</groupId>
    		<artifactId>jaxb-api</artifactId>
    		<version>2.3.1</version>
		</dependency>

That’s it, now paste the code to main class and run it.

package electrum_api;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

class Main {

	public static void main(String[] args) throws IOException {
		URL url = new URL("http://127.0.0.1:7777");
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		httpConn.setRequestMethod("POST");

		httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

		//username:password
		byte[] message = ("user:t").getBytes("UTF-8"); 
		String basicAuth = DatatypeConverter.printBase64Binary(message);
		httpConn.setRequestProperty("Authorization", "Basic " + basicAuth);

		httpConn.setDoOutput(true);
		OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
		writer.write("{\"jsonrpc\":\"2.0\",\"id\":\"curltext\",\"method\":\"getbalance\",\"params\":[]}");
		writer.flush();
		writer.close();
		httpConn.getOutputStream().close();

		InputStream responseStream = httpConn.getResponseCode() / 100 == 2
				? httpConn.getInputStream()
				: httpConn.getErrorStream();
		Scanner s = new Scanner(responseStream).useDelimiter("\\A");
		String response = s.hasNext() ? s.next() : "";
		System.out.println(response);
	}
}

That is what you should get in the console:

{"id": "curltext", "jsonrpc": "2.0", "result": {"confirmed": "0.03508005"}}

If you got something like this:

{"id": "curltext", "jsonrpc": "2.0", "error": {"code": 1, "message": "wallet not loaded"}}

Make sure that you loaded your wallet

One more useful tip is: if you want to get help for electrum commands type in powershell:

set-alias electrum (get-item "C:\Program Files (x86)\Electrum\electrum-*-debug.exe")

and then

electrum help

4 responses to “Electrum on Windows”

  1. Denis says:

    “Make sure that you loaded your wallet”

    Hello, I’m getting “wallet not loaded”. What should I do?

  2. Anonymous says:

    Open your Electrum testnet GUI.
    In File menu click Open. And choose your wallet.

  3. spunky game says:

    Sprunki Incredibox really elevates the music-mixing fun with fresh beats and cool visuals. It’s a must-try if you love creative gameplay. Check out Spunky Game for more innovative mods!

Leave a Reply

Your email address will not be published. Required fields are marked *