Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed address validation in spectrum #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cryptoadvance/spectrum/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from functools import wraps

from embit import bip32
from embit.base import EmbitError
from embit.descriptor import Descriptor as EmbitDescriptor
from embit.descriptor.checksum import add_checksum
from embit.finalizer import finalize_psbt
Expand Down Expand Up @@ -983,7 +984,10 @@ def listsinceblock(

@walletrpc
def getreceivedbyaddress(self, wallet, address, minconf=1):
sc = EmbitScript.from_address(address)
try:
sc = EmbitScript.from_address(address)
except EmbitError as e:
raise RPCError(f"Invalid Bitcoin address: {address}: {e}")
script = Script.query.filter_by(script=sc.data.hex()).first()
if not script:
return 0
Expand Down
59 changes: 59 additions & 0 deletions tests/test_spectrum_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,62 @@ def test_rescanblockchain(caplog, client):

assert result.status_code == 200
print(json.loads(result.data))


def test_createwallet(caplog, client):
caplog.set_level(logging.DEBUG)
caplog.set_level(logging.DEBUG, logger="cryptoadvance.spectrum")

result = client.post(
"/wallet/some_shiny_new_wallet",
json={
"method": "createwallet",
"params": ["some_shiny_new_wallet"],
"jsonrpc": "2.0",
"id": 0,
},
)
print(json.loads(result.data))
assert result.status_code == 200


def test_getreceivedbyaddress(caplog, client):
caplog.set_level(logging.DEBUG)
caplog.set_level(logging.DEBUG, logger="cryptoadvance.spectrum")

result = client.post(
"/wallet/some_nonexisting_wallet",
json={
"method": "getreceivedbyaddress",
"params": ["bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", 6],
"jsonrpc": "2.0",
"id": 0,
},
)
assert result.status_code == 200
assert (
json.loads(result.data)["error"]["message"]
== "Requested wallet some_nonexisting_wallet does not exist or is not loaded"
)
result = client.post(
"/",
json={
"method": "createwallet",
"params": ["some_new_wallet_to_test_getreceivedbyaddress"],
"jsonrpc": "2.0",
"id": 0,
},
)

result = client.post(
"/wallet/some_new_wallet_to_test_getreceivedbyaddress",
json={
"method": "getreceivedbyaddress",
"params": ["bc1qtr5cwxum3uwvyxgc8sgqm8gr658eksruyv3sty", 6],
"jsonrpc": "2.0",
"id": 0,
},
)
assert result.status_code == 200
print(json.loads(result.data))
assert False