Skip to content

Commit

Permalink
Enable importing joinmarket wallet as watch-only
Browse files Browse the repository at this point in the history
  • Loading branch information
Wukong committed Jun 7, 2022
1 parent bbd5019 commit 18be935
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion jmclient/jmclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
StoragePasswordError, VolatileStorage)
from .cryptoengine import (BTCEngine, BTC_P2PKH, BTC_P2SH_P2WPKH, BTC_P2WPKH, EngineError,
TYPE_P2PKH, TYPE_P2SH_P2WPKH, TYPE_P2WPKH, detect_script_type,
is_extended_public_key)
is_extended_public_key, convert_xpub_if_needed)
from .configure import (load_test_config, process_shutdown,
load_program_config, jm_single, get_network, update_persist_config,
validate_address, is_burn_destination, get_mchannels,
Expand Down
8 changes: 8 additions & 0 deletions jmclient/jmclient/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ def jm_single():
# 2. You cannot change the type of a pre-existing wallet.
native = true
# Display zpub for native segwit wallet, and ypub for non-native segwit wallet.
# If set to false, the extended public key will always display in the xpub
# format.
display_ypub_zpub = true
# for dust sweeping, try merge_algorithm = gradual
# for more rapid dust sweeping, try merge_algorithm = greedy
# for most rapid dust sweeping, try merge_algorithm = greediest
Expand Down Expand Up @@ -986,6 +991,9 @@ def is_native_segwit_mode():
return False
return jm_single().config.get('POLICY', 'native') != 'false'

def display_ypub_zpub():
return jm_single().config.get('POLICY', 'display_ypub_zpub') != 'false'

def process_shutdown(mode="command-line"):
if mode=="command-line":
from twisted.internet import reactor
Expand Down
22 changes: 21 additions & 1 deletion jmclient/jmclient/cryptoengine.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

from collections import OrderedDict
from bitcointx import base58
import struct

import jmbitcoin as btc
from jmbase import bintohex
from .configure import get_network, jm_single
from .configure import get_network, jm_single, is_segwit_mode, is_native_segwit_mode, display_ypub_zpub


#NOTE: before fidelity bonds and watchonly wallet, each of these types corresponded
Expand All @@ -25,7 +26,9 @@

BIP32_PUB_PREFIX = "xpub"
BIP49_PUB_PREFIX = "ypub"
BIP49_PUB_PREFIX_BYTES = b"\x04\x9d\x7c\xb2"
BIP84_PUB_PREFIX = "zpub"
BIP84_PUB_PREFIX_BYTES = b"\x04\xb2\x47\x46"
TESTNET_PUB_PREFIX = "tpub"

def detect_script_type(script_str):
Expand Down Expand Up @@ -61,6 +64,23 @@ def is_extended_public_key(key_str):
BIP32_PUB_PREFIX, BIP49_PUB_PREFIX, BIP84_PUB_PREFIX, TESTNET_PUB_PREFIX]])


def convert_xpub_if_needed(xpub):
if not xpub.startswith(BIP32_PUB_PREFIX):
return xpub
if not display_ypub_zpub():
return xpub

# Convert xpub to ypub or zpub for display
if is_segwit_mode():
if is_native_segwit_mode():
# BIP84 format
xpub = base58.encode(BIP84_PUB_PREFIX_BYTES + base58.decode(xpub)[4:])
else:
# BIP49 format
xpub = base58.encode(BIP49_PUB_PREFIX_BYTES + base58.decode(xpub)[4:])
return xpub


class classproperty(object):
"""
from https://stackoverflow.com/a/5192374
Expand Down
6 changes: 3 additions & 3 deletions scripts/joinmarket-qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
parse_payjoin_setup, send_payjoin, JMBIP78ReceiverManager, \
detect_script_type, general_custom_change_warning, \
nonwallet_custom_change_warning, sweep_custom_change_warning, EngineError,\
TYPE_P2WPKH, check_and_start_tor, is_extended_public_key
TYPE_P2WPKH, check_and_start_tor, is_extended_public_key, convert_xpub_if_needed
from jmclient.wallet import BaseWallet

from qtsupport import ScheduleWizard, TumbleRestartWizard, config_tips,\
Expand Down Expand Up @@ -1573,7 +1573,7 @@ def updateWalletInfo(self, walletinfo=None):
continue

if address_type == BaseWallet.ADDRESS_TYPE_EXTERNAL:
heading = "EXTERNAL " + xpubs[mixdepth][address_type]
heading = "EXTERNAL " + convert_xpub_if_needed(xpubs[mixdepth][address_type])
elif address_type == BaseWallet.ADDRESS_TYPE_INTERNAL:
heading = "INTERNAL"
elif address_type == FidelityBondMixin.BIP32_TIMELOCK_ID:
Expand Down Expand Up @@ -2339,7 +2339,7 @@ def get_wallet_printout(wallet_service):
account_xpub = acct.xpub
if account_xpub.startswith(FBONDS_PUBKEY_PREFIX):
account_xpub = account_xpub[len(FBONDS_PUBKEY_PREFIX):]
xpubs[j].append(account_xpub)
xpubs[j].append(convert_xpub_if_needed(account_xpub))
# in case the wallet is not yet synced, don't return an incorrect
# 0 balance, but signal incompleteness:
total_bal = walletview.get_fmt_balance() if wallet_service.synced else None
Expand Down

0 comments on commit 18be935

Please sign in to comment.