From 25473268dc9d07e9738feec17c8e24e63bddbbf5 Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Thu, 25 Jan 2018 22:02:33 +0100 Subject: [PATCH 1/3] add validate address rpc call + tests --- neo/api/JSONRPC/JsonRpcApi.py | 19 +++++++++++++++-- neo/api/JSONRPC/neo-cli-json-rpc-docs.md | 12 +++++++++++ neo/api/JSONRPC/test_json_rpc_api.py | 27 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/neo/api/JSONRPC/JsonRpcApi.py b/neo/api/JSONRPC/JsonRpcApi.py index 950dea6b0..e7778904a 100644 --- a/neo/api/JSONRPC/JsonRpcApi.py +++ b/neo/api/JSONRPC/JsonRpcApi.py @@ -7,6 +7,7 @@ * http://www.jsonrpc.org/specification """ import json +import base58 import random from json.decoder import JSONDecodeError @@ -128,6 +129,21 @@ def parse_uint_str(self, param): return param[2:] return param + def validateaddress(self, params): + # check for [] parameter or [""] + if params is None or params[0] == '': + raise JsonRpcError(-100, "Missing argument") + + isValid = False + try: + data = base58.b58decode_check(params[0]) + if len(data) == 21 and data[0] == settings.ADDRESS_VERSION: + isValid = True + except Exception as e: + pass + + return {"address": params[0], "isvalid": isValid} + def json_rpc_method_handler(self, method, params): # print("method", method, params) @@ -237,8 +253,7 @@ def json_rpc_method_handler(self, method, params): raise NotImplementedError() elif method == "validateaddress": - raise NotImplementedError() - + return self.validateaddress(params) elif method == "getpeers": raise NotImplementedError() diff --git a/neo/api/JSONRPC/neo-cli-json-rpc-docs.md b/neo/api/JSONRPC/neo-cli-json-rpc-docs.md index 030ecc533..559470a83 100644 --- a/neo/api/JSONRPC/neo-cli-json-rpc-docs.md +++ b/neo/api/JSONRPC/neo-cli-json-rpc-docs.md @@ -38,3 +38,15 @@ On MainNet there are actually entries, each of which has this format: `0xde3bc1d curl -X POST http://seed2.neo.org:20332 -H 'Content-Type: application/json' -d '{ "jsonrpc": "2.0", "id": 5, "method": "getversion", "params": [] }' { "jsonrpc": "2.0", "id": 5, "result": { "port": 20333, "nonce": 771199013, "useragent": "/NEO:2.6.0/" } } + +## `validateaddress` + curl -X POST http://seed2.neo.org:20332 -H 'Content-Type: application/json' -d '{ "jsonrpc": "2.0", "id": 5, "method": "validateaddress", "params": ["AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i"] }' + {"jsonrpc":"2.0","id":5,"result":{"address":"AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i","isvalid":true}} + +### with invalid address + curl -X POST http://seed2.neo.org:20332 -H 'Cication/json' -d '{ "jsonrpc": "2.0", "id": 5, "method": "validateaddress", "params": ["152f1muMCNa7goXYhYAQC61hxEgGacmncB"] }' + {"jsonrpc":"2.0","id":5,"result":{"address":"152f1muMCNa7goXYhYAQC61hxEgGacmncB","isvalid":false}} + +### with completely invalid argument + curl -X POST http://seed2.neo.org:20332 -H 'Content-Type: application/json' -d '{ "jsonrpc": "2.0", "id": 5, "method": "validateaddress", "params": [] }' + {"jsonrpc":"2.0","id":5,"error":{"code":-2146233086,"message":"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}} \ No newline at end of file diff --git a/neo/api/JSONRPC/test_json_rpc_api.py b/neo/api/JSONRPC/test_json_rpc_api.py index 5f87772e6..dce19ad41 100644 --- a/neo/api/JSONRPC/test_json_rpc_api.py +++ b/neo/api/JSONRPC/test_json_rpc_api.py @@ -263,3 +263,30 @@ def test_get_version(self): res = json.loads(self.app.home(mock_req)) self.assertEqual(res["result"]["port"], 20332) self.assertEqual(res["result"]["useragent"], "/NEO-PYTHON:%s/" % __version__) + + def test_validate_address(self): + # example from docs.neo.org + req = self._gen_rpc_req("validateaddress", params=["AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i"]) + mock_req = mock_request(json.dumps(req).encode("utf-8")) + res = json.loads(self.app.home(mock_req)) + self.assertTrue(res["result"]["isvalid"]) + + # example from docs.neo.org + req = self._gen_rpc_req("validateaddress", params=["152f1muMCNa7goXYhYAQC61hxEgGacmncB"]) + mock_req = mock_request(json.dumps(req).encode("utf-8")) + res = json.loads(self.app.home(mock_req)) + self.assertFalse(res["result"]["isvalid"]) + + # catch completely invalid argument + req = self._gen_rpc_req("validateaddress", params=[]) + mock_req = mock_request(json.dumps(req).encode("utf-8")) + res = json.loads(self.app.home(mock_req)) + self.assertTrue('error' in res) + self.assertEqual('Missing argument', res['error']['message']) + + # catch completely invalid argument + req = self._gen_rpc_req("validateaddress", params=[""]) + mock_req = mock_request(json.dumps(req).encode("utf-8")) + res = json.loads(self.app.home(mock_req)) + self.assertTrue('error' in res) + self.assertEqual('Missing argument', res['error']['message']) From b9b2b203878fcd2f77d3d9c8bda437ee688c9b47 Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Fri, 26 Jan 2018 16:05:47 +0100 Subject: [PATCH 2/3] processing review --- neo/api/JSONRPC/JsonRpcApi.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neo/api/JSONRPC/JsonRpcApi.py b/neo/api/JSONRPC/JsonRpcApi.py index e7778904a..5cdf0e7f0 100644 --- a/neo/api/JSONRPC/JsonRpcApi.py +++ b/neo/api/JSONRPC/JsonRpcApi.py @@ -131,7 +131,7 @@ def parse_uint_str(self, param): def validateaddress(self, params): # check for [] parameter or [""] - if params is None or params[0] == '': + if not params or params[0] == '': raise JsonRpcError(-100, "Missing argument") isValid = False @@ -254,6 +254,7 @@ def json_rpc_method_handler(self, method, params): elif method == "validateaddress": return self.validateaddress(params) + elif method == "getpeers": raise NotImplementedError() From 0380bd04c17f9051cd0729307a4191bd88a13092 Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Sat, 27 Jan 2018 19:31:34 +0100 Subject: [PATCH 3/3] =?UTF-8?q?blank=20line=20=C2=AF\=5F(=E3=83=84)=5F/?= =?UTF-8?q?=C2=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- neo/api/JSONRPC/test_json_rpc_api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/neo/api/JSONRPC/test_json_rpc_api.py b/neo/api/JSONRPC/test_json_rpc_api.py index d82c9d761..72093a470 100644 --- a/neo/api/JSONRPC/test_json_rpc_api.py +++ b/neo/api/JSONRPC/test_json_rpc_api.py @@ -374,4 +374,3 @@ def test_get_unspents(self): u = UInt256.ParseString('0ff23561c611ccda65470c9a4a5f1be31f2f4f61b98c75d051e1a72e85a302eb') unspents = GetBlockchain().GetAllUnspent(u) self.assertEqual(len(unspents), 1) -