Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.
118 changes: 118 additions & 0 deletions docs/JSON-RPC-API-using-jmwalletd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
## JSON-RPC API for Joinmarket using jmwalletd.py

### Introduction - how to start the server

After installing Joinmarket as per the [INSTALL GUIDE](INSTALL.md), navigate to the `scripts/` directory as usual and start the server with:

```
(jmvenv) $python jmwalletd.py
```

which with defaults will start serving the RPC over HTTP on port 28183.

This HTTP server does *NOT* currently support multiple sessions; it is intended as a manager/daemon for all the Joinmarket services for a single user.

#### Rules about making requests

Currently authentication is done by providing a cookie on first request, which must then be reused to keep the same session. The cookie is sent in an HTTP header with name `b'JMCookie'`. This is fine for an early testing stage, but will be improved/reworked, and that will be documented here.

GET requests are used in case no content or parameters need to be provided with the request.

POST requests are used in case content or parameters are to be provided with the request, and they are provided as utf-8 encoded serialized JSON, in the *body* of the POST request.

Note that for some methods, it's particularly important to deal with the HTTP response asynchronously, since it can take some time for wallet synchronization, service startup etc. to occur.

### Methods

#### `createwallet`

Make a new wallet. The variable "wallettype" should be "sw" for native segwit wallets (now the Joinmarket default), otherwise a segwit legacy wallet (BIP49) will be created.

* HTTP Request type: POST
* Route: `/wallet/create`
* POST body contents: {"walletname": walletname, "password": password, "wallettype": wallettype}
* Returns: on success, {"walletname": walletname, "already_loaded": False}

(TODO some confusion over two different walletnames here, I need to check, but the wallet name sent by the caller will be used for the file name, I believe).

#### `unlockwallet`

Open an existing wallet using a password.

* HTTP Request type: POST
* Route: `/wallet/<string:walletname>/unlock`
* POST body contents: {"password": password}
* Returns: on success, {"walletname": walletname, "already_loaded": True}

(see previous on walletname, same applies here).

#### `lockwallet`

Stops the wallet service for the current wallet; meaning it cannot then be accessed without re-authentication.

* HTTP Request type: GET
* Route: `/wallet/<string:walletname>/lock`
* Returns: on success, {"walletname": walletname}

(see previous on walletname, same applies here).

#### `displaywallet`

Get JSON representation of wallet contents for wallet named `walletname`:

* HTTP Request type: GET
* Route: `/wallet/<string:walletname>/display`
* Returns: a JSON object which is the entire wallet contents, mixdepth by mixdepth.
- Example output from a signet wallet is given at the bottom of the document.

#### `maker/start`

Starts the yield generator/maker service for the given wallet, using the IRC and tor network connections
in the backend (inside the process started with jmwalletd).
See Joinmarket yield generator config defaults in `jmclient.configure` module for info on the data that must
be specified in the POST body contents.

* HTTP Request type: POST
* Route: `/wallet/<string:walletname>/maker/start`
* POST body contents: {"txfee", "cjfee_a", "cjfee_r", "ordertype", "minsize"]
* Returns: on success, {"walletname": walletname}

(see previous on walletname, same applies here).

#### `maker/stop`

Stops the yieldgenerator/maker service if currently running for the given wallet.

* HTTP Request type: GET
* Route: `/wallet/<string:walletname>/maker/start`
* Returns: on success, {"walletname": walletname}

(see previous on walletname, same applies here).

#### `snicker/start`

Starts the SNICKER service (see [here](SNICKER.md)) for the given wallet. Note that this requires
no configuration for now, though that is likely to change. Also note this is not yet supported for
mainnet.

* HTTP Request type: GET
* Route: `/wallet/<string:walletname>/snicker/start`
* Returns: on success, {"walletname": walletname}

(see previous on walletname, same applies here).

#### `snicker/stop`

Stops the snicker service if currently running for the given wallet.

* HTTP Request type: GET
* Route: `/wallet/<string:walletname>/snicker/start`
* Returns: on success, {"walletname": walletname}

(see previous on walletname, same applies here).

##### Example wallet display JSON output from signet wallet

```
{'wallet_name': 'JM wallet', 'total_balance': '0.15842426', 'accounts': [{'account': '0', 'account_balance': '0.00861458', 'branches': [{'branch': "external addresses\tm/84'/1'/0'/0\ttpubDFGxEsV7NvVc4h2XL4QEppZt3CrDiCFksP97H6YbFPmCTKM6KMP2xUxW57gAu7bzDfB3YTqnMeKQaQRS5GJM3xMcrhbi5AGsQUd7p4PLMDV", 'balance': '0.00000000', 'entries': [{'hd_path': "m/84'/1'/0'/0/4", 'address': 'tb1qzugshsm85x6luegyjc6mk5zces2zqr0j8m4zkd', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/0'/0/5", 'address': 'tb1qcwmdkg229ghmd8r3xgq4a9zxp459crws66n4ve', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/0'/0/6", 'address': 'tb1q7lv6dwex3mhwp32vhku0fvpar9faar2lu595su', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/0'/0/7", 'address': 'tb1qm42ltytvp22kj9efp995yu0r0r7x570d8j8crc', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/0'/0/8", 'address': 'tb1qwvux8g0khuvvkla3zaqdslj6xpgwtq7jlvwmgu', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/0'/0/9", 'address': 'tb1q3xr7l9nylsdlyqf9rkw0rg3f0yx6slguhtwpzp', 'amount': '0.00000000', 'labels': 'new'}]}, {'branch': "internal addresses\tm/84'/1'/0'/1\t", 'balance': '0.00861458', 'entries': [{'hd_path': "m/84'/1'/0'/1/7", 'address': 'tb1qjrzxkulgc5dnlyz0rjqj68zxgqjesqn839ue2w', 'amount': '0.00396839', 'labels': 'cj-out'}, {'hd_path': "m/84'/1'/0'/1/12", 'address': 'tb1qeqkk4te2t6gqt7jfgu8a9k4je2wwfw3d2m7gku', 'amount': '0.00464619', 'labels': 'non-cj-change'}]}]}, {'account': '1', 'account_balance': '0.09380968', 'branches': [{'branch': "external addresses\tm/84'/1'/1'/0\ttpubDE1TKa8tm3WWh4f9fV325BgYWX9i7WFMaQRd1C3tSFYU9RJEyE8w2Cw2KnhgXSKyjS4keeWAkc3iLEqp3pxUEG9T49RCtQiMpjuZM71FLpL", 'balance': '0.00000000', 'entries': [{'hd_path': "m/84'/1'/1'/0/0", 'address': 'tb1qd6qqg3uzk9sw88yhvpqpwt3tx5ls4hau3mwh3g', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/1'/0/1", 'address': 'tb1qhkrmqn9e4ldzlwna8w5w9l5vaw978zlrl54hmh', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/1'/0/2", 'address': 'tb1qp83afad8dl98w366vnvct0zc49qu33c2nfx386', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/1'/0/3", 'address': 'tb1qjv0elh4kn5yaywajedgcrf93ujzz3m3q7ld7k3', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/1'/0/4", 'address': 'tb1qk25u4ch7w0xylzh0krn4hefphe6xpyh0vc33sl', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/1'/0/5", 'address': 'tb1qs3ep9nlypwn43swv75zwv6lgl3wgsmha20g87p', 'amount': '0.00000000', 'labels': 'new'}]}, {'branch': "internal addresses\tm/84'/1'/1'/1\t", 'balance': '0.09380968', 'entries': [{'hd_path': "m/84'/1'/1'/1/44", 'address': 'tb1qgmgpk22ueq9xk8f722aqjnuwd6s3jv58nwwan2', 'amount': '0.00009631', 'labels': 'non-cj-change'}, {'hd_path': "m/84'/1'/1'/1/49", 'address': 'tb1qjq86y8nzvafv5dsde93zf0emv7yrsphvupv69e', 'amount': '0.00013383', 'labels': 'non-cj-change'}, {'hd_path': "m/84'/1'/1'/1/54", 'address': 'tb1q7lvxk407xs38t24hfzy7vprp9t7tfsemv4rfym', 'amount': '0.00371951', 'labels': 'non-cj-change'}, {'hd_path': "m/84'/1'/1'/1/56", 'address': 'tb1qn2azshrkcg0d7py5apgfr0jh29nt9w2fmx9fyy', 'amount': '0.08986003', 'labels': 'non-cj-change'}]}, {'branch': 'Imported keys\tm/0\t', 'balance': '0.00000000', 'entries': [{'hd_path': 'imported/1/0', 'address': 'tb1q8znprh8c85za3mpwzn3qf9m0vwqzjkfu4qdncy', 'amount': '0.00000000', 'labels': 'empty'}, {'hd_path': 'imported/1/1', 'address': 'tb1qu4ajg3enea90xxtjuwcurj3d6lkqrud8p7w0yu', 'amount': '0.00000000', 'labels': 'empty'}, {'hd_path': 'imported/1/2', 'address': 'tb1qg7saqx69yalcqshfr8mjndy0gpx2umxrwqs823', 'amount': '0.00000000', 'labels': 'empty'}]}]}, {'account': '2', 'account_balance': '0.05600000', 'branches': [{'branch': "external addresses\tm/84'/1'/2'/0\ttpubDF8K7wXCrRXX1CQLVZGwMvEg9YEWF2VRpM1tjCwpMZDRRqKjpJ5YaeaDaLkqN1D7YM4pkX32FcCnosbhLQz2BgRiPNNdybWuvSBKp72mJsJ", 'balance': '0.00000000', 'entries': [{'hd_path': "m/84'/1'/2'/0/0", 'address': 'tb1qw95x9m84t6hqcun560vqfk3yc6ptl4g9arsty0', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/2'/0/1", 'address': 'tb1qek4humez7rcwl53ly6uzr4mfwd0s2lu92e356q', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/2'/0/2", 'address': 'tb1qxne4hyyeq2vrh0dfzs56th29qsymp9eq5pljdc', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/2'/0/3", 'address': 'tb1qz3jk544j5vtwztznxfdwfgt8zcw77mjcut8vdz', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/2'/0/4", 'address': 'tb1qg902humlsuc5s6aua6ew3d893hlgcxr05ntpyd', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/2'/0/5", 'address': 'tb1qukz3l34ydy9snq8rkjaknk0ns04kfnlh34neqd', 'amount': '0.00000000', 'labels': 'new'}]}, {'branch': "internal addresses\tm/84'/1'/2'/1\t", 'balance': '0.05600000', 'entries': [{'hd_path': "m/84'/1'/2'/1/1", 'address': 'tb1qrtz5cwpneheg2v2v32wzc3h9yv0rzplxjtx9vc', 'amount': '0.00800000', 'labels': 'non-cj-change'}, {'hd_path': "m/84'/1'/2'/1/2", 'address': 'tb1qp4276g23y2w8g3367de25ustxkygjydmwk4fw2', 'amount': '0.00800000', 'labels': 'non-cj-change'}, {'hd_path': "m/84'/1'/2'/1/3", 'address': 'tb1qtqgvw445807tzcm8yhq6xgu3vmdfh66czx8jea', 'amount': '0.00800000', 'labels': 'non-cj-change'}, {'hd_path': "m/84'/1'/2'/1/4", 'address': 'tb1qxj7ulxdthe0dwxr5457p5d0w5u3jg7rwmc05pm', 'amount': '0.02400000', 'labels': 'non-cj-change'}, {'hd_path': "m/84'/1'/2'/1/5", 'address': 'tb1qv3kfe9ew42z0ldncgzmqcjznatsxz0vudvcjrv', 'amount': '0.00800000', 'labels': 'non-cj-change'}]}]}, {'account': '3', 'account_balance': '0.00000000', 'branches': [{'branch': "external addresses\tm/84'/1'/3'/0\ttpubDE9VN56aLW9BurCxHHGAWidSnVuU86ZsKPYQgxpTgkZxbogJYfj1vWJbtYip7WV5REcgmtjETb5eShXV8VUBzvCAMzuRm5Kv4ZGnnCiX6Jg", 'balance': '0.00000000', 'entries': [{'hd_path': "m/84'/1'/3'/0/0", 'address': 'tb1qp2w6ezmqn8nk9kc4gkpetgjj2mzqgp5x3hk86m', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/3'/0/1", 'address': 'tb1qd0tt93aulqs508mtap5p8gls5z57fqa4ggnfx7', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/3'/0/2", 'address': 'tb1qsp4hv46vgz4yjwt4p2wekh2gfmek7vgznrnd96', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/3'/0/3", 'address': 'tb1qvs322uyrwh7a74dsxel0xcrgucm27c6dzdmj9j', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/3'/0/4", 'address': 'tb1qnq9uk9azs9s7m5474ws7z7wxnwv3s3lxrtjter', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/3'/0/5", 'address': 'tb1q5tlq36q6ps0m9zu6h08gd3azsgkgvm73sjcmxw', 'amount': '0.00000000', 'labels': 'new'}]}, {'branch': "internal addresses\tm/84'/1'/3'/1\t", 'balance': '0.00000000', 'entries': []}]}, {'account': '4', 'account_balance': '0.00000000', 'branches': [{'branch': "external addresses\tm/84'/1'/4'/0\ttpubDE6QfTimeNgCFSYuxPPaLc1Cp3VokAuJAusYoiGwWtVHVtQDsepf5dRAFNLWMwpBCgKDYkXdWGs2JspxXPokrtooPh7db5fniqYbdKGqD4F", 'balance': '0.00000000', 'entries': [{'hd_path': "m/84'/1'/4'/0/3", 'address': 'tb1qr2llfup6cnh27n77nm7egcyf9r7c0ykucrcu8k', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/4'/0/4", 'address': 'tb1qahqjnd2y8j770l2m4kpf4fyfve9425c0zdumms', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/4'/0/5", 'address': 'tb1q0jm0cxwcm2g60489fvtmeeaf7mzg658t8f8fk4', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/4'/0/6", 'address': 'tb1qtpm5putpkzmrmecden0yytuuk4n9emhvxwqu8m', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/4'/0/7", 'address': 'tb1qn60fc04pmprn9wpzkt0dnt80awu0rpy99w376g', 'amount': '0.00000000', 'labels': 'new'}, {'hd_path': "m/84'/1'/4'/0/8", 'address': 'tb1qakvrpp2hd3a3303zx7w2shmvfc7tqk28pwa9sj', 'amount': '0.00000000', 'labels': 'new'}]}, {'branch': "internal addresses\tm/84'/1'/4'/1\t", 'balance': '0.00000000', 'entries': []}]}]}
```
7 changes: 7 additions & 0 deletions jmbase/jmbase/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class JMMsgSignatureVerify(JMCommand):
(b'fullmsg', Unicode()),
(b'hostid', Unicode())]

class JMShutdown(JMCommand):
""" Requests shutdown of the current
message channel connections (to be used
when the client is shutting down).
"""
arguments = []

"""TAKER specific commands
"""

Expand Down
7 changes: 5 additions & 2 deletions jmclient/jmclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from .snicker_receiver import SNICKERError, SNICKERReceiver
from .client_protocol import (JMTakerClientProtocol, JMClientProtocolFactory,
start_reactor, SNICKERClientProtocolFactory,
BIP78ClientProtocolFactory)
BIP78ClientProtocolFactory,
get_daemon_serving_params)
from .podle import (set_commitment_file, get_commitment_file,
add_external_commitments,
PoDLE, generate_podle, get_podle_commitments,
Expand All @@ -57,7 +58,9 @@
wallet_change_passphrase)
from .wallet_service import WalletService
from .maker import Maker
from .yieldgenerator import YieldGenerator, YieldGeneratorBasic, ygmain
from .yieldgenerator import YieldGenerator, YieldGeneratorBasic, ygmain, \
YieldGeneratorService
from .snicker_receiver import SNICKERError, SNICKERReceiver, SNICKERReceiverService
from .payjoin import (parse_payjoin_setup, send_payjoin,
JMBIP78ReceiverManager)
# Set default logging handler to avoid "No handler found" warnings.
Expand Down
49 changes: 35 additions & 14 deletions jmclient/jmclient/client_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
SNICKERReceiver, process_shutdown)
import jmbitcoin as btc

# module level variable representing the port
# on which the daemon is running.
# note that this var is only set if we are running
# client+daemon in one process.
daemon_serving_port = -1
daemon_serving_host = ""

def get_daemon_serving_params():
return (daemon_serving_host, daemon_serving_port)

jlog = get_log()

Expand Down Expand Up @@ -367,6 +376,15 @@ def make_tx(self, nick_list, txhex):
txhex=txhex)
self.defaultCallbacks(d)

def request_mc_shutdown(self):
""" To ensure that lingering message channel
connections are shut down when the client itself
is shutting down.
"""
d = self.callRemote(commands.JMShutdown)
self.defaultCallbacks(d)
return {'accepted': True}

class JMMakerClientProtocol(JMClientProtocol):
def __init__(self, factory, maker, nick_priv=None):
self.factory = factory
Expand Down Expand Up @@ -787,9 +805,9 @@ def start_reactor(host, port, factory=None, snickerfactory=None,
#(Cannot start the reactor in tests)
#Not used in prod (twisted logging):
#startLogging(stdout)
usessl = True if jm_single().config.get("DAEMON",
"use_ssl") != 'false' else False

global daemon_serving_host
global daemon_serving_port
usessl = True if jm_single().config.get("DAEMON", "use_ssl") != 'false' else False
jmcport, snickerport, bip78port = [port]*3
if daemon:
try:
Expand Down Expand Up @@ -829,6 +847,9 @@ def start_daemon_on_port(p, f, name, port_offset):
p[0] += 1
return p[0]

daemon_serving_port = port
daemon_serving_host = host

if jm_coinjoin:
# TODO either re-apply this port incrementing logic
# to other protocols, or re-work how the ports work entirely.
Expand All @@ -848,17 +869,17 @@ def start_daemon_on_port(p, f, name, port_offset):

# Note the reactor.connect*** entries do not include BIP78 which
# starts in jmclient.payjoin:
if usessl:
if factory:
reactor.connectSSL(host, jmcport, factory, ClientContextFactory())
if snickerfactory:
reactor.connectSSL(host, snickerport, snickerfactory,
ClientContextFactory())
else:
if factory:
reactor.connectTCP(host, jmcport, factory)
if snickerfactory:
reactor.connectTCP(host, snickerport, snickerfactory)
if usessl:
Comment thread
AdamISZ marked this conversation as resolved.
Resolved
if factory:
reactor.connectSSL(host, jmcport, factory, ClientContextFactory())
if snickerfactory:
reactor.connectSSL(host, snickerport, snickerfactory,
ClientContextFactory())
else:
Comment thread
AdamISZ marked this conversation as resolved.
Resolved
if factory:
reactor.connectTCP(host, jmcport, factory)
if snickerfactory:
reactor.connectTCP(host, snickerport, snickerfactory)
if rs:
if not gui:
reactor.run(installSignalHandlers=ish)
Expand Down
48 changes: 46 additions & 2 deletions jmclient/jmclient/snicker_receiver.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
#! /usr/bin/env python

import os
from twisted.application.service import Service
from twisted.internet import task
import jmbitcoin as btc
from jmclient.configure import jm_single
from jmbase import (get_log, utxo_to_utxostr,
hextobin, bintohex)
from twisted.application.service import Service

jlog = get_log()

class SNICKERError(Exception):
pass

class SNICKERReceiver(Service):
class SNICKERReceiverService(Service):
def __init__(self, receiver):
assert isinstance(receiver, SNICKERReceiver)
self.receiver = receiver
# main monitor loop
self.monitor_loop = task.LoopingCall(self.receiver.poll_for_proposals)

def startService(self):
""" Encapsulates start up actions.
This service depends on the receiver's
wallet service to start, so wait for that.
"""
self.wait_for_wallet = task.LoopingCall(self.wait_for_wallet_sync)
self.wait_for_wallet.start(5.0)

def wait_for_wallet_sync(self):
if self.receiver.wallet_service.isRunning():
jlog.info("SNICKER service starting because wallet service is up.")
self.wait_for_wallet.stop()
self.monitor_loop.start(5.0)
super().startService()

def stopService(self, wallet=False):
""" Encapsulates shut down actions.
Optionally also shut down the underlying
wallet service (default False).
"""
if self.monitor_loop:
self.monitor_loop.stop()
if wallet:
self.receiver.wallet_service.stopService()
super().stopService()

def isRunning(self):
if self.running == 1:
return True
return False

class SNICKERReceiver(object):
supported_flags = []

def __init__(self, wallet_service, acceptance_callback=None,
Expand Down Expand Up @@ -66,6 +106,10 @@ def __init__(self, wallet_service, acceptance_callback=None,

def default_info_callback(self, msg):
jlog.info(msg)
if not os.path.exists(self.proposals_source):
with open(self.proposals_source, "wb") as f:
jlog.info("created proposals source file.")


def default_acceptance_callback(self, our_ins, their_ins,
our_outs, their_outs):
Expand Down
Loading