Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Localhuman rpc4 #222

Merged
merged 5 commits into from
Feb 14, 2018
Merged
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
19 changes: 19 additions & 0 deletions neo/Implementations/Wallets/peewee/test_user_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from neocore.Fixed8 import Fixed8
from neocore.KeyPair import KeyPair
from neo.Wallets.NEP5Token import NEP5Token
from neo.SmartContract.ContractParameterContext import ContractParametersContext
from neo.Core.TX.Transaction import ContractTransaction, TransactionOutput
from neo.Network.NodeLeader import NodeLeader
import json
import binascii

Expand Down Expand Up @@ -171,3 +174,19 @@ def test_8_named_addr(self):
self.assertIsInstance(presult, bytearray)

self.assertEqual(presult, self.wallet_1_script_hash.Data)

def test_9_send_neo_tx(self):

wallet = self.GetWallet1()

tx = ContractTransaction()
tx.outputs = [TransactionOutput(Blockchain.SystemShare().Hash, Fixed8.FromDecimal(10.0), self.import_watch_addr)]

tx = wallet.MakeTransaction(tx)

cpc = ContractParametersContext(tx)
wallet.Sign(cpc)
tx.scripts = cpc.GetScripts()

result = NodeLeader.Instance().Relay(tx)
self.assertEqual(result, True)
43 changes: 20 additions & 23 deletions neo/api/JSONRPC/JsonRpcApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
from neo.Core.Blockchain import Blockchain
from neo.api.utils import json_response
from neo.Core.State.AccountState import AccountState
from neo.Core.State.ContractState import ContractState
from neo.Core.TX.Transaction import Transaction
from neocore.UInt160 import UInt160
from neocore.UInt256 import UInt256
from neo.Wallets import Wallet
from neo.Core.Helper import Helper
from neo.Network.NodeLeader import NodeLeader
import binascii
Expand Down Expand Up @@ -119,26 +118,6 @@ def home(self, request):
error = JsonRpcError.internalError(str(e))
return self.get_custom_error_payload(request_id, error.code, error.message)

def parse_uint_str(self, param):
if param[0:2] == '0x':
return param[2:]
return param

def validateaddress(self, params):
# check for [] parameter or [""]
if not params 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):

if method == "getaccountstate":
Expand Down Expand Up @@ -250,7 +229,10 @@ def json_rpc_method_handler(self, method, params):
return self.get_invoke_result(script)

elif method == "sendrawtransaction":
raise NotImplementedError()
tx_script = binascii.unhexlify(params[0].encode('utf-8'))
transaction = Transaction.DeserializeFromBufer(tx_script)
result = NodeLeader.Instance().Relay(transaction)
return result

elif method == "submitblock":
raise NotImplementedError()
Expand Down Expand Up @@ -309,3 +291,18 @@ def get_invoke_result(self, script):
"gas_consumed": appengine.GasConsumed().ToString(),
"stack": [ContractParameter.ToParameter(item).ToJson() for item in appengine.EvaluationStack.Items]
}

def validateaddress(self, params):
# check for [] parameter or [""]
if not params 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}
22 changes: 22 additions & 0 deletions neo/api/JSONRPC/test_json_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,25 @@ def test_gettxout(self):
expected_value = "2609.997813"
self.assertEqual(output_index, res["result"]["n"])
self.assertEqual(expected_value, res["result"]["value"])

def test_send_raw_tx(self):
raw_tx = '80000001b10ad9ec660bf343c0eb411f9e05b4fa4ad8abed31d4e4dc5bb6ae416af0c4de000002e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c60c8db571300000000af12a8687b14948bc4a008128a550a63695bc1a5e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c603808b44002000000eca8fcf94e7a2a7fc3fd54ae0ed3d34d52ec25900141404749ce868ed9588f604eeeb5c523db39fd57cd7f61d04393a1754c2d32f131d67e6b1ec561ac05012b7298eb5ff254487c76de0b2a0c4d097d17cec708c0a9802321025b5c8cdcb32f8e278e111a0bf58ebb463988024bb4e250aa4310b40252030b60ac'
req = self._gen_rpc_req("sendrawtransaction", params=[raw_tx])
mock_req = mock_request(json.dumps(req).encode("utf-8"))
res = json.loads(self.app.home(mock_req))
self.assertEqual(res['result'], True)

def test_send_raw_tx_bad(self):
raw_tx = '80000001b10ad9ec660bf343c0eb411f9e05b4fa4ad8abed31d4e4dc5bb6ae416af0c4de000002e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c60c8db571300000000af12a8687b14948bc4a008128a550a63695bc1a5e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c603808b44002000000eca8fcf94e7a2a7fc3fd54ae0ed3d34d52ec25900141404749ce868ed9588f604eeeb5c523db39fd57cd7f61d04393a1754c2d32f131d67e6b1ec561ac05012b7298eb5ff254487c76de0b2a0c4d097d17cec708c0a9802321025b5c8cdcb32f8e278e111a0bf58ebb463988024bb4e250aa4310b40252030b60ac'
req = self._gen_rpc_req("sendrawtransaction", params=[raw_tx])
mock_req = mock_request(json.dumps(req).encode("utf-8"))
res = json.loads(self.app.home(mock_req))
self.assertEqual(res['result'], False)

def test_send_raw_tx_bad_2(self):
raw_tx = '80000001b10ad9ec660bf343c0eb411f9e05b4fa4ad8abed31d4e4dc5bb6ae416af0c4de000002e72d286979ee6cbb7e65dfddfb2e384100b8d148e7758de42e4168b71792c60c8db571300000000af12a8687b14948bc4a008128a550a63695bc1a5e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c603808b44002000000eca8fcf94e7a2a7fc3fd54ae0ed3d34d52ec25900141404749ce868ed9588f604eeeb5c523db39fd57cd7f61d04393a1754c2d32f131d67e6b1ec561ac05012b7298eb5ff254487c76de0b2a0c4d097d17cec708c0a9802321025b5c8cdcb32f8e278e111a0bf58ebb463988024bb4e250aa4310b40252030b60ac'
req = self._gen_rpc_req("sendrawtransaction", params=[raw_tx])
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(res['error']['code'], -32603)