Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamISZ committed Mar 27, 2021
1 parent ff6c9a6 commit 53d41eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
6 changes: 5 additions & 1 deletion jmclient/jmclient/wallet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,11 +1052,13 @@ def wallet_dumpprivkey(wallet, hdpath):
return wallet.get_wif_path(path) # will raise exception on invalid path


def wallet_signmessage(wallet, hdpath, message):
def wallet_signmessage(wallet, hdpath, message, out_str=True):
""" Given a wallet, a BIP32 HD path (as can be output
from the display method) and a message string, returns
a base64 encoded signature along with the corresponding
address and message.
If `out_str` is True, returns human readable representation,
otherwise returns tuple of (signature, message, address).
"""
if not get_network() == "mainnet":
return "Error: message signing is only supported on mainnet."
Expand All @@ -1071,6 +1073,8 @@ def wallet_signmessage(wallet, hdpath, message):
path = wallet.path_repr_to_path(hdpath)
sig = wallet.sign_message(msg, path)
addr = wallet.get_address_from_path(path)
if not out_str:
return (sig, message, addr)
return ("Signature: {}\nMessage: {}\nAddress: {}\n"
"To verify this in Electrum use Tools->Sign/verify "
"message.".format(sig, message, addr))
Expand Down
38 changes: 35 additions & 3 deletions jmclient/test/test_walletutils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
import pytest
from jmbitcoin import select_chain_params
from jmclient import (SegwitLegacyWallet, SegwitWallet, get_network,
jm_single, VolatileStorage, load_test_config)
from jmclient.wallet_utils import (bip32pathparse, WalletView,
WalletViewAccount, WalletViewBranch,
WalletViewEntry, wallet_signmessage)

from jmclient.wallet_utils import bip32pathparse, WalletView, \
WalletViewAccount, WalletViewBranch, WalletViewEntry

# The below signatures have all been verified against Electrum 4.0.9:
@pytest.mark.parametrize('seed, hdpath, walletcls, message, sig, addr', [
[b"\x01"*16, "m/84'/0'/0'/0/0", SegwitWallet, "hello",
"IOLk6ct/8aKtvTNnEAc+xojIWKv5FOwnzHGcnHkTJJwRBAyhrZ2ZyB0Re+dKS4SEav3qgjQeqMYRm+7mHi4sFKA=",
"bc1qq53d9372u8d50jfd5agq9zv7m7zdnzwducuqgz"],
[b"\x01"*16, "m/49'/0'/0'/0/0", SegwitLegacyWallet, "hello",
"HxVaQuXyBpl1UKutiusJjeLfKHwJYBzUiWuu6hEbmNFeSZGt/mbXKJ071ANR1gvdICbS/AnEa2RKDq9xMd/nU8s=",
"3AdTcqdoLHFGNq6znkahJDT41u65HAwiRv"],
[b"\x02"*16, "m/84'/0'/2'/1/0", SegwitWallet, "sign me",
"IA/V5DG7u108aNzCnpNPHqfrJAL8pF4GQ0sSqpf4Vlg5UWizauXzh2KskoD6Usl13hzqXBi4XDXl7Xxo5z6M298=",
"bc1q8mm69xs740sr0l2umrhmpl4ewhxfudxg2zvjw5"],
[b"\x02"*16, "m/49'/0'/2'/1/0", SegwitLegacyWallet, "sign me",
"H4cAtoE+zL+Mr+U8jm9DiYxZlym5xeZM3mcgymLz+TF4YYr4lgnM8qTZhFwlK4izcPaLuF27LFEoGJ/ltleIHUI=",
"3Qan1D4Vcy1yMGHfR9j7szDuC8QxSFVScA"],
])
def test_signmessage(seed, hdpath, walletcls, message, sig, addr):
load_test_config()
jm_single().config.set('BLOCKCHAIN', 'network', 'mainnet')
select_chain_params("bitcoin/mainnet")
storage = VolatileStorage()
walletcls.initialize(
storage, get_network(), entropy=seed, max_mixdepth=3)
wallet = walletcls(storage)
s, m, a = wallet_signmessage(wallet, hdpath, message,
out_str=False)
assert (s, m, a) == (sig, message, addr)
jm_single().config.set("BLOCKCHAIN", "network", "testnet")
select_chain_params("bitcoin/regtest")

def test_bip32_pathparse():
assert bip32pathparse("m/2/1/0017")
Expand Down

0 comments on commit 53d41eb

Please sign in to comment.