├── .gitignore ├── LICENSE ├── README.md ├── account.py ├── contract.py ├── hello.lua └── wallet.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Cocos-BCX 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Middleware-sample 2 | Python Middleware example 3 | -------------------------------------------------------------------------------- /account.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | from PythonMiddleware.graphene import Graphene 5 | from PythonMiddleware.instance import set_shared_graphene_instance 6 | from pprint import pprint 7 | from PythonMiddleware.account import Account 8 | 9 | nodeAddress = "ws://127.0.0.1:8020" 10 | gph = Graphene(node=nodeAddress, blocking=True) 11 | set_shared_graphene_instance(gph) 12 | 13 | #创建钱包 14 | #可以通过gph.wallet 直接使用钱包instance,操作钱包的接口。 15 | if gph.wallet.created() is False: 16 | gph.newWallet("123456") 17 | 18 | #钱包解锁 19 | if gph.wallet.locked() is True: 20 | gph.wallet.unlock("123456") 21 | 22 | #查看钱包导入的所有账户信息 23 | pprint(gph.wallet.getAccounts()) 24 | 25 | #init0 ["COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT", "5JHdMwsWkEXsMozFrQAQKnKwo44CaV77H45S9PsH7QVbFQngJfw"] 26 | pub="COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT" 27 | 28 | #获取公钥对应的私钥 29 | pprint(gph.wallet.getPrivateKeyForPublicKey(pub)) 30 | 31 | #通过公钥获取账号信息 32 | pprint(gph.wallet.getAccount(pub)) 33 | 34 | #创建账号 35 | try: 36 | pprint(gph.create_account(account_name="test10", password="123456", proxy_account="init14")) 37 | except Exception as e: 38 | print(repr(e)) 39 | gph.wallet.removeAccount(None) 40 | 41 | #转账1 42 | pprint(gph.transfer('test14', 100, "1.3.0", "test memo", 'init0')) 43 | ''' 44 | 执行结果: 45 | tx.buffer>>>: {'extensions': [], 'ref_block_num': 52379, 'operations': [[0, {'extensions': [], 'from': '1.2.4', 'memo': {'to': 'COCOS8GY2vkoK8gpLTuDxNfzD6JjwqYDmCRnpoUfZ78J4z8ChdcZi6h', 'from': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'nonce': 5901681697903210751, 'message': '8bb2f19b3a3b57cbbf04e539783f3d62'}, 'to': '1.2.17', 'fee': {'amount': 2089843, 'asset_id': '1.3.0'}, 'amount': {'amount': 10000000, 'asset_id': '1.3.0'}}]], 'signatures': ['1f7ebeba7dd6b0efdb9286260a05c426ab476a3ecb5fc8812ccc5dea826b56c5e76024a526ad510f77ff3fd1ac9f6439d4b40aa4e1099fe3676d322d5ffa1a31eb'], 'expiration': '2019-08-21T04:19:45', 'ref_block_prefix': 2924873733} 46 | tx======>>: {'extensions': [], 'ref_block_num': 52379, 'operations': [[0, {'extensions': [], 'from': '1.2.4', 'memo': {'to': 'COCOS8GY2vkoK8gpLTuDxNfzD6JjwqYDmCRnpoUfZ78J4z8ChdcZi6h', 'from': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'nonce': 5901681697903210751, 'message': '8bb2f19b3a3b57cbbf04e539783f3d62'}, 'to': '1.2.17', 'fee': {'amount': 2089843, 'asset_id': '1.3.0'}, 'amount': {'amount': 10000000, 'asset_id': '1.3.0'}}]], 'signatures': ['1f7ebeba7dd6b0efdb9286260a05c426ab476a3ecb5fc8812ccc5dea826b56c5e76024a526ad510f77ff3fd1ac9f6439d4b40aa4e1099fe3676d322d5ffa1a31eb'], 'expiration': '2019-08-21T04:19:45', 'ref_block_prefix': 2924873733} 47 | transaction>>>: {'extensions': [], 'ref_block_num': 52379, 'operations': [[0, {'extensions': [], 'from': '1.2.4', 'memo': {'to': 'COCOS8GY2vkoK8gpLTuDxNfzD6JjwqYDmCRnpoUfZ78J4z8ChdcZi6h', 'from': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'nonce': 5901681697903210751, 'message': '8bb2f19b3a3b57cbbf04e539783f3d62'}, 'to': '1.2.17', 'fee': {'amount': 2089843, 'asset_id': '1.3.0'}, 'amount': {'amount': 10000000, 'asset_id': '1.3.0'}}]], 'signatures': ['1f7ebeba7dd6b0efdb9286260a05c426ab476a3ecb5fc8812ccc5dea826b56c5e76024a526ad510f77ff3fd1ac9f6439d4b40aa4e1099fe3676d322d5ffa1a31eb'], 'expiration': '2019-08-21T04:19:45', 'ref_block_prefix': 2924873733} 48 | 49 | ['dee8d42d4e49458b54987cf563e82fa3c941e77580981fd4b8893bde9ad62a4d', 50 | {'block': 52380, 51 | 'expiration': '2019-08-21T04:19:45', 52 | 'extensions': [], 53 | 'operation_results': [[1, {'real_running_time': 198}]], 54 | 'operations': [[0, 55 | {'amount': {'amount': 10000000, 'asset_id': '1.3.0'}, 56 | 'extensions': [], 57 | 'fee': {'amount': 2089843, 'asset_id': '1.3.0'}, 58 | 'from': '1.2.4', 59 | 'memo': {'from': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 60 | 'message': '8bb2f19b3a3b57cbbf04e539783f3d62', 61 | 'nonce': '5901681697903210751', 62 | 'to': 'COCOS8GY2vkoK8gpLTuDxNfzD6JjwqYDmCRnpoUfZ78J4z8ChdcZi6h'}, 63 | 'to': '1.2.17'}]], 64 | 'ref_block_num': 52379, 65 | 'ref_block_prefix': 2924873733, 66 | 'signatures': ['1f7ebeba7dd6b0efdb9286260a05c426ab476a3ecb5fc8812ccc5dea826b56c5e76024a526ad510f77ff3fd1ac9f6439d4b40aa4e1099fe3676d322d5ffa1a31eb']}] 67 | ''' 68 | 69 | #转账2 70 | pprint(gph.transfer('test14', 100, "COCOS", "test memo", 'init0')) 71 | ''' 72 | 执行结果: 73 | tx.buffer>>>: {'expiration': '2019-08-21T06:14:50', 'ref_block_prefix': 310102910, 'operations': [[0, {'to': '1.2.17', 'memo': {'to': 'COCOS8GY2vkoK8gpLTuDxNfzD6JjwqYDmCRnpoUfZ78J4z8ChdcZi6h', 'message': '8ba4d671c0736eda127467110ef9043e', 'from': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'nonce': 6591532363490338807}, 'from': '1.2.4', 'extensions': [], 'amount': {'amount': 10000000, 'asset_id': '1.3.0'}, 'fee': {'amount': 2089843, 'asset_id': '1.3.0'}}]], 'signatures': ['1f16c3c089b96e8c32cdcf530b3542df434a55b3736ac34421fef5e461850c6adf66723dcdb03cd3ed84cc3a5eb8dc158b8aa4c57a368263109fc7e76677059706'], 'extensions': [], 'ref_block_num': 53342} 74 | tx======>>: {'expiration': '2019-08-21T06:14:50', 'ref_block_prefix': 310102910, 'operations': [[0, {'to': '1.2.17', 'memo': {'to': 'COCOS8GY2vkoK8gpLTuDxNfzD6JjwqYDmCRnpoUfZ78J4z8ChdcZi6h', 'message': '8ba4d671c0736eda127467110ef9043e', 'from': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'nonce': 6591532363490338807}, 'from': '1.2.4', 'extensions': [], 'amount': {'amount': 10000000, 'asset_id': '1.3.0'}, 'fee': {'amount': 2089843, 'asset_id': '1.3.0'}}]], 'signatures': ['1f16c3c089b96e8c32cdcf530b3542df434a55b3736ac34421fef5e461850c6adf66723dcdb03cd3ed84cc3a5eb8dc158b8aa4c57a368263109fc7e76677059706'], 'extensions': [], 'ref_block_num': 53342} 75 | transaction>>>: {'expiration': '2019-08-21T06:14:50', 'ref_block_prefix': 310102910, 'operations': [[0, {'to': '1.2.17', 'memo': {'to': 'COCOS8GY2vkoK8gpLTuDxNfzD6JjwqYDmCRnpoUfZ78J4z8ChdcZi6h', 'message': '8ba4d671c0736eda127467110ef9043e', 'from': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'nonce': 6591532363490338807}, 'from': '1.2.4', 'extensions': [], 'amount': {'amount': 10000000, 'asset_id': '1.3.0'}, 'fee': {'amount': 2089843, 'asset_id': '1.3.0'}}]], 'signatures': ['1f16c3c089b96e8c32cdcf530b3542df434a55b3736ac34421fef5e461850c6adf66723dcdb03cd3ed84cc3a5eb8dc158b8aa4c57a368263109fc7e76677059706'], 'extensions': [], 'ref_block_num': 53342} 76 | 77 | ['aaa792d7cd0788eb23fdffa8bc2536b37b9733cfb620a092842d04a33a47497f', 78 | {'block': 53343, 79 | 'expiration': '2019-08-21T06:14:50', 80 | 'extensions': [], 81 | 'operation_results': [[1, {'real_running_time': 201}]], 82 | 'operations': [[0, 83 | {'amount': {'amount': 10000000, 'asset_id': '1.3.0'}, 84 | 'extensions': [], 85 | 'fee': {'amount': 2089843, 'asset_id': '1.3.0'}, 86 | 'from': '1.2.4', 87 | 'memo': {'from': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 88 | 'message': '8ba4d671c0736eda127467110ef9043e', 89 | 'nonce': '6591532363490338807', 90 | 'to': 'COCOS8GY2vkoK8gpLTuDxNfzD6JjwqYDmCRnpoUfZ78J4z8ChdcZi6h'}, 91 | 'to': '1.2.17'}]], 92 | 'ref_block_num': 53342, 93 | 'ref_block_prefix': 310102910, 94 | 'signatures': ['1f16c3c089b96e8c32cdcf530b3542df434a55b3736ac34421fef5e461850c6adf66723dcdb03cd3ed84cc3a5eb8dc158b8aa4c57a368263109fc7e76677059706']}] 95 | ''' 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /contract.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | from PythonMiddleware.graphene import Graphene 5 | from PythonMiddleware.instance import set_shared_graphene_instance 6 | from pprint import pprint 7 | from PythonMiddleware.account import Account 8 | 9 | nodeAddress = "ws://127.0.0.1:8020" 10 | gph = Graphene(node=nodeAddress, blocking=True) 11 | set_shared_graphene_instance(gph) 12 | 13 | #创建钱包 14 | #可以通过gph.wallet 直接使用钱包instance,操作钱包的接口。 15 | if gph.wallet.created() is False: 16 | gph.newWallet("123456") 17 | 18 | #钱包解锁 19 | if gph.wallet.locked() is True: 20 | gph.wallet.unlock("123456") 21 | 22 | 23 | #init0 ["COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT", "5JHdMwsWkEXsMozFrQAQKnKwo44CaV77H45S9PsH7QVbFQngJfw"] 24 | pub="COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT" 25 | pprint(gph.wallet.getPrivateKeyForPublicKey(pub)) 26 | 27 | #合约创建1:单行合约 28 | data1="function hello() chainhelper:log('Hello World!') end" 29 | pprint(gph.create_contract("contract.pytest.hello", data=data1, con_authority=pub, account="init0")) 30 | ''' 31 | 执行结果: 32 | tx.buffer>>>: {'extensions': [], 'signatures': ['1f11b53efe5abb9fa5690644b437ce2fe7193b3deeb5bc0f13021241ad914c008019942a45a830253f49410e77537d8739a1f1c629f31938c613085faa8ad82aa9'], 'ref_block_prefix': 695707653, 'ref_block_num': 53573, 'operations': [[43, {'data': "function hello() chainhelper:log('Hello World!') end", 'extensions': [], 'fee': {'asset_id': '1.3.0', 'amount': 2051757}, 'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'owner': '1.2.4', 'name': 'contract.pytest.hello'}]], 'expiration': '2019-08-21T06:34:07'} 33 | tx======>>: {'extensions': [], 'signatures': ['1f11b53efe5abb9fa5690644b437ce2fe7193b3deeb5bc0f13021241ad914c008019942a45a830253f49410e77537d8739a1f1c629f31938c613085faa8ad82aa9'], 'ref_block_prefix': 695707653, 'ref_block_num': 53573, 'operations': [[43, {'data': "function hello() chainhelper:log('Hello World!') end", 'extensions': [], 'fee': {'asset_id': '1.3.0', 'amount': 2051757}, 'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'owner': '1.2.4', 'name': 'contract.pytest.hello'}]], 'expiration': '2019-08-21T06:34:07'} 34 | transaction>>>: {'extensions': [], 'signatures': ['1f11b53efe5abb9fa5690644b437ce2fe7193b3deeb5bc0f13021241ad914c008019942a45a830253f49410e77537d8739a1f1c629f31938c613085faa8ad82aa9'], 'ref_block_prefix': 695707653, 'ref_block_num': 53573, 'operations': [[43, {'data': "function hello() chainhelper:log('Hello World!') end", 'extensions': [], 'fee': {'asset_id': '1.3.0', 'amount': 2051757}, 'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'owner': '1.2.4', 'name': 'contract.pytest.hello'}]], 'expiration': '2019-08-21T06:34:07'} 35 | ['66d25d4d1a8fd68fe8d431f1af4b0c11d7d2760777261fc77488033a4c643177', 36 | {'block': 53574, 37 | 'expiration': '2019-08-21T06:34:07', 38 | 'extensions': [], 39 | 'operation_results': [[2, {'real_running_time': 822, 'result': '1.16.1'}]], 40 | 'operations': [[43, 41 | {'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 42 | 'data': "function hello() chainhelper:log('Hello World!') " 43 | 'end', 44 | 'extensions': [], 45 | 'fee': {'amount': 2051757, 'asset_id': '1.3.0'}, 46 | 'name': 'contract.pytest.hello', 47 | 'owner': '1.2.4'}]], 48 | 'ref_block_num': 53573, 49 | 'ref_block_prefix': 695707653, 50 | 'signatures': ['1f11b53efe5abb9fa5690644b437ce2fe7193b3deeb5bc0f13021241ad914c008019942a45a830253f49410e77537d8739a1f1c629f31938c613085faa8ad82aa9']}] 51 | ''' 52 | 53 | #合约创建2: 多行合约 54 | data2 = "function hello() \ 55 | chainhelper:log('Hello World!') \ 56 | chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) \ 57 | end " 58 | pprint(gph.create_contract("contract.pytest.hello2", data=data2, con_authority=pub, account="init0")) 59 | 60 | ''' 61 | 执行结果: 62 | tx.buffer>>>: {'extensions': [], 'ref_block_num': 53615, 'ref_block_prefix': 3922499816, 'signatures': ['1f5088ae364efb3b1bbcf59e6adcdd969548d94023564e740631a6b7cbd283a3376533d17c0da90f9f7f4abeb08615f046c056bee4a16d9f6850eb95a325f126be'], 'operations': [[43, {'extensions': [], 'name': 'contract.pytest.hello2', 'data': "function hello() chainhelper:log('Hello World!') chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) end ", 'owner': '1.2.4', 'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'fee': {'asset_id': '1.3.0', 'amount': 2122070}}]], 'expiration': '2019-08-21T06:37:37'} 63 | tx======>>: {'extensions': [], 'ref_block_num': 53615, 'ref_block_prefix': 3922499816, 'signatures': ['1f5088ae364efb3b1bbcf59e6adcdd969548d94023564e740631a6b7cbd283a3376533d17c0da90f9f7f4abeb08615f046c056bee4a16d9f6850eb95a325f126be'], 'operations': [[43, {'extensions': [], 'name': 'contract.pytest.hello2', 'data': "function hello() chainhelper:log('Hello World!') chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) end ", 'owner': '1.2.4', 'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'fee': {'asset_id': '1.3.0', 'amount': 2122070}}]], 'expiration': '2019-08-21T06:37:37'} 64 | transaction>>>: {'extensions': [], 'ref_block_num': 53615, 'ref_block_prefix': 3922499816, 'signatures': ['1f5088ae364efb3b1bbcf59e6adcdd969548d94023564e740631a6b7cbd283a3376533d17c0da90f9f7f4abeb08615f046c056bee4a16d9f6850eb95a325f126be'], 'operations': [[43, {'extensions': [], 'name': 'contract.pytest.hello2', 'data': "function hello() chainhelper:log('Hello World!') chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) end ", 'owner': '1.2.4', 'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'fee': {'asset_id': '1.3.0', 'amount': 2122070}}]], 'expiration': '2019-08-21T06:37:37'} 65 | ['dd79a4497506b0d24b0aff5e1896be5d7f2ff875adf08560d84382e128a3b87c', 66 | {'block': 53616, 67 | 'expiration': '2019-08-21T06:37:37', 68 | 'extensions': [], 69 | 'operation_results': [[2, {'real_running_time': 780, 'result': '1.16.2'}]], 70 | 'operations': [[43, 71 | {'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 72 | 'data': "function hello() chainhelper:log('Hello " 73 | "World!') " 74 | "chainhelper:log(date('%Y-%m-%dT%H:%M:%S', " 75 | 'chainhelper:time())) end ', 76 | 'extensions': [], 77 | 'fee': {'amount': 2122070, 'asset_id': '1.3.0'}, 78 | 'name': 'contract.pytest.hello2', 79 | 'owner': '1.2.4'}]], 80 | 'ref_block_num': 53615, 81 | 'ref_block_prefix': 3922499816, 82 | 'signatures': ['1f5088ae364efb3b1bbcf59e6adcdd969548d94023564e740631a6b7cbd283a3376533d17c0da90f9f7f4abeb08615f046c056bee4a16d9f6850eb95a325f126be']}] 83 | ''' 84 | 85 | #合约创建3:通过文件创建 86 | file = open("hello.lua", "r") #打开文件 87 | context = file.read() #读取文件 88 | print(context) 89 | pprint(gph.create_contract("contract.pytest.hello3", data=context, con_authority=pub, account="init0")) 90 | ''' 91 | 执行结果: 92 | function hello() 93 | chainhelper:log('Hello World!') 94 | chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) 95 | chainhelper:log('test end. 2019-08-21 14:35:08') 96 | end 97 | 98 | tx.buffer>>>: {'signatures': ['1f6a7c96c3c1664db6cd6fecd7e6764f3b1c61c1591a1b1ed54448b0055bae5ec508457fbf2f980fe35117982b2ef35f98fe43888d8a8c814bdc4c78ce84b61c07'], 'expiration': '2019-08-21T07:40:25', 'operations': [[43, {'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'owner': '1.2.4', 'name': 'contract.pytest.hello3', 'fee': {'amount': 2178710, 'asset_id': '1.3.0'}, 'extensions': [], 'data': "function hello() \n chainhelper:log('Hello World!') \n chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) \n chainhelper:log('test end. 2019-08-21 14:35:08') \nend\n"}]], 'ref_block_prefix': 2432441567, 'extensions': [], 'ref_block_num': 54369} 99 | tx======>>: {'signatures': ['1f6a7c96c3c1664db6cd6fecd7e6764f3b1c61c1591a1b1ed54448b0055bae5ec508457fbf2f980fe35117982b2ef35f98fe43888d8a8c814bdc4c78ce84b61c07'], 'expiration': '2019-08-21T07:40:25', 'operations': [[43, {'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'owner': '1.2.4', 'name': 'contract.pytest.hello3', 'fee': {'amount': 2178710, 'asset_id': '1.3.0'}, 'extensions': [], 'data': "function hello() \n chainhelper:log('Hello World!') \n chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) \n chainhelper:log('test end. 2019-08-21 14:35:08') \nend\n"}]], 'ref_block_prefix': 2432441567, 'extensions': [], 'ref_block_num': 54369} 100 | transaction>>>: {'signatures': ['1f6a7c96c3c1664db6cd6fecd7e6764f3b1c61c1591a1b1ed54448b0055bae5ec508457fbf2f980fe35117982b2ef35f98fe43888d8a8c814bdc4c78ce84b61c07'], 'expiration': '2019-08-21T07:40:25', 'operations': [[43, {'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 'owner': '1.2.4', 'name': 'contract.pytest.hello3', 'fee': {'amount': 2178710, 'asset_id': '1.3.0'}, 'extensions': [], 'data': "function hello() \n chainhelper:log('Hello World!') \n chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) \n chainhelper:log('test end. 2019-08-21 14:35:08') \nend\n"}]], 'ref_block_prefix': 2432441567, 'extensions': [], 'ref_block_num': 54369} 101 | ['53a1bcf027ddaf235f26b9987b41feeaa7087d2cf288f5efa76b727df6edfc67', 102 | {'block': 54370, 103 | 'expiration': '2019-08-21T07:40:25', 104 | 'extensions': [], 105 | 'operation_results': [[2, {'real_running_time': 936, 'result': '1.16.3'}]], 106 | 'operations': [[43, 107 | {'contract_authority': 'COCOS5vzuh6YRRmCjUMeeHLsjnVCdJwqm9WZoUBDDNVp7HTwFM1ZYQT', 108 | 'data': 'function hello() \n' 109 | " chainhelper:log('Hello World!') \n" 110 | " chainhelper:log(date('%Y-%m-%dT%H:%M:%S', " 111 | 'chainhelper:time())) \n' 112 | " chainhelper:log('test end. 2019-08-21 " 113 | "14:35:08') \n" 114 | 'end\n', 115 | 'extensions': [], 116 | 'fee': {'amount': 2178710, 'asset_id': '1.3.0'}, 117 | 'name': 'contract.pytest.hello3', 118 | 'owner': '1.2.4'}]], 119 | 'ref_block_num': 54369, 120 | 'ref_block_prefix': 2432441567, 121 | 'signatures': ['1f6a7c96c3c1664db6cd6fecd7e6764f3b1c61c1591a1b1ed54448b0055bae5ec508457fbf2f980fe35117982b2ef35f98fe43888d8a8c814bdc4c78ce84b61c07']}] 122 | ''' 123 | 124 | 125 | 126 | #调用合约: contract.pytest.hello2 127 | value_list=[] 128 | #调用1 129 | pprint(gph.call_contract_function("contract.pytest.hello2", "hello", value_list=value_list, account="test14")) 130 | 131 | #调用2 132 | pprint(gph.call_contract_function("1.16.2", "hello", value_list=value_list, account="test14")) 133 | ''' 134 | 执行结果: 135 | value_list:>>> [] 136 | value_list:>>> [] 137 | tx.buffer>>>: {'ref_block_num': 53792, 'expiration': '2019-08-21T06:52:24', 'operations': [[44, {'value_list': [], 'function_name': 'hello', 'caller': '1.2.17', 'fee': {'asset_id': '1.3.0', 'amount': 2007812}, 'contract_id': '1.16.2', 'extensions': []}]], 'extensions': [], 'ref_block_prefix': 1080919947, 'signatures': ['200e225780dc4381daeb9322a81733d2a085b68d41d5c5468794b106f2f21a3679228fcbc97035aa992c37570f1a548a6ed764a84d479b06cf5cf6adb70ce5d1a1', '203d5a926b2777f092c6659838c80c71521a37bf549edf221778b9aa7763fa97167d0f679123e4078b77371c58f15bccc01cc2ea4155ef322d8f0221af36e3126b']} 138 | tx======>>: {'ref_block_num': 53792, 'expiration': '2019-08-21T06:52:24', 'operations': [[44, {'value_list': [], 'function_name': 'hello', 'caller': '1.2.17', 'fee': {'asset_id': '1.3.0', 'amount': 2007812}, 'contract_id': '1.16.2', 'extensions': []}]], 'extensions': [], 'ref_block_prefix': 1080919947, 'signatures': ['200e225780dc4381daeb9322a81733d2a085b68d41d5c5468794b106f2f21a3679228fcbc97035aa992c37570f1a548a6ed764a84d479b06cf5cf6adb70ce5d1a1', '203d5a926b2777f092c6659838c80c71521a37bf549edf221778b9aa7763fa97167d0f679123e4078b77371c58f15bccc01cc2ea4155ef322d8f0221af36e3126b']} 139 | transaction>>>: {'ref_block_num': 53792, 'expiration': '2019-08-21T06:52:24', 'operations': [[44, {'value_list': [], 'function_name': 'hello', 'caller': '1.2.17', 'fee': {'asset_id': '1.3.0', 'amount': 2007812}, 'contract_id': '1.16.2', 'extensions': []}]], 'extensions': [], 'ref_block_prefix': 1080919947, 'signatures': ['200e225780dc4381daeb9322a81733d2a085b68d41d5c5468794b106f2f21a3679228fcbc97035aa992c37570f1a548a6ed764a84d479b06cf5cf6adb70ce5d1a1', '203d5a926b2777f092c6659838c80c71521a37bf549edf221778b9aa7763fa97167d0f679123e4078b77371c58f15bccc01cc2ea4155ef322d8f0221af36e3126b']} 140 | ['6a73a9d7dc3e56fd6087d17592ce55e5c8453f21c268c1f66ef0055759b943f3', 141 | {'block': 53794, 142 | 'expiration': '2019-08-21T06:52:24', 143 | 'extensions': [], 144 | 'operation_results': [[4, 145 | {'additional_cost': {'amount': 637109, 146 | 'asset_id': '1.3.0'}, 147 | 'contract_affecteds': [[3, 148 | {'affected_account': '1.2.17', 149 | 'message': 'Hello World!'}], 150 | [3, 151 | {'affected_account': '1.2.17', 152 | 'message': '2019-08-21T05:52:25'}]], 153 | 'contract_id': '1.16.2', 154 | 'existed_pv': False, 155 | 'process_value': '', 156 | 'real_running_time': 600}]], 157 | 'operations': [[44, 158 | {'caller': '1.2.17', 159 | 'contract_id': '1.16.2', 160 | 'extensions': [], 161 | 'fee': {'amount': 2007812, 'asset_id': '1.3.0'}, 162 | 'function_name': 'hello', 163 | 'value_list': []}]], 164 | 'ref_block_num': 53792, 165 | 'ref_block_prefix': 1080919947, 166 | 'signatures': ['200e225780dc4381daeb9322a81733d2a085b68d41d5c5468794b106f2f21a3679228fcbc97035aa992c37570f1a548a6ed764a84d479b06cf5cf6adb70ce5d1a1', 167 | '203d5a926b2777f092c6659838c80c71521a37bf549edf221778b9aa7763fa97167d0f679123e4078b77371c58f15bccc01cc2ea4155ef322d8f0221af36e3126b']}] 168 | ''' 169 | 170 | #合约调用:调用通过文件创建的合约:contract.pytest.hello3 171 | pprint(gph.call_contract_function("contract.pytest.hello3", "hello", value_list=value_list, account="test14")) 172 | ''' 173 | 合约执行: 174 | value_list:>>> [] 175 | value_list:>>> [] 176 | tx.buffer>>>: {'ref_block_num': 54396, 'ref_block_prefix': 398433404, 'extensions': [], 'operations': [[44, {'value_list': [], 'extensions': [], 'contract_id': '1.16.3', 'function_name': 'hello', 'caller': '1.2.17', 'fee': {'amount': 2007812, 'asset_id': '1.3.0'}}]], 'expiration': '2019-08-21T07:42:41', 'signatures': ['20312321ead5eefee489d55868fad2b3782f3b39fdf9fcf79a229e4967a1a49f0460e658e3d14fde1162a78f4707ac7ffd14a0c98f132723e72856bed6d75721c0', '203afda066ec1969e9eb5b37e015744011aff225ae0674692b3e0f505d21d64b352e9e51b8fffdf66cec6ec1bd9233cfefae8226b159b5673744304068172017a2']} 177 | tx======>>: {'ref_block_num': 54396, 'ref_block_prefix': 398433404, 'extensions': [], 'operations': [[44, {'value_list': [], 'extensions': [], 'contract_id': '1.16.3', 'function_name': 'hello', 'caller': '1.2.17', 'fee': {'amount': 2007812, 'asset_id': '1.3.0'}}]], 'expiration': '2019-08-21T07:42:41', 'signatures': ['20312321ead5eefee489d55868fad2b3782f3b39fdf9fcf79a229e4967a1a49f0460e658e3d14fde1162a78f4707ac7ffd14a0c98f132723e72856bed6d75721c0', '203afda066ec1969e9eb5b37e015744011aff225ae0674692b3e0f505d21d64b352e9e51b8fffdf66cec6ec1bd9233cfefae8226b159b5673744304068172017a2']} 178 | transaction>>>: {'ref_block_num': 54396, 'ref_block_prefix': 398433404, 'extensions': [], 'operations': [[44, {'value_list': [], 'extensions': [], 'contract_id': '1.16.3', 'function_name': 'hello', 'caller': '1.2.17', 'fee': {'amount': 2007812, 'asset_id': '1.3.0'}}]], 'expiration': '2019-08-21T07:42:41', 'signatures': ['20312321ead5eefee489d55868fad2b3782f3b39fdf9fcf79a229e4967a1a49f0460e658e3d14fde1162a78f4707ac7ffd14a0c98f132723e72856bed6d75721c0', '203afda066ec1969e9eb5b37e015744011aff225ae0674692b3e0f505d21d64b352e9e51b8fffdf66cec6ec1bd9233cfefae8226b159b5673744304068172017a2']} 179 | 180 | ['2bc05435b5d4eac3f69da12b5917eb9d29f2825db0dc152ee96415e03294f046', 181 | {'block': 54397, 182 | 'expiration': '2019-08-21T07:42:41', 183 | 'extensions': [], 184 | 'operation_results': [[4, 185 | {'additional_cost': {'amount': 701359, 186 | 'asset_id': '1.3.0'}, 187 | 'contract_affecteds': [[3, 188 | {'affected_account': '1.2.17', 189 | 'message': 'Hello World!'}], 190 | [3, 191 | {'affected_account': '1.2.17', 192 | 'message': '2019-08-21T06:42:40'}], 193 | [3, 194 | {'affected_account': '1.2.17', 195 | 'message': 'test end. ' 196 | '2019-08-21 ' 197 | '14:35:08'}]], 198 | 'contract_id': '1.16.3', 199 | 'existed_pv': False, 200 | 'process_value': '', 201 | 'real_running_time': 633}]], 202 | 'operations': [[44, 203 | {'caller': '1.2.17', 204 | 'contract_id': '1.16.3', 205 | 'extensions': [], 206 | 'fee': {'amount': 2007812, 'asset_id': '1.3.0'}, 207 | 'function_name': 'hello', 208 | 'value_list': []}]], 209 | 'ref_block_num': 54396, 210 | 'ref_block_prefix': 398433404, 211 | 'signatures': ['20312321ead5eefee489d55868fad2b3782f3b39fdf9fcf79a229e4967a1a49f0460e658e3d14fde1162a78f4707ac7ffd14a0c98f132723e72856bed6d75721c0', 212 | '203afda066ec1969e9eb5b37e015744011aff225ae0674692b3e0f505d21d64b352e9e51b8fffdf66cec6ec1bd9233cfefae8226b159b5673744304068172017a2']}] 213 | ''' 214 | -------------------------------------------------------------------------------- /hello.lua: -------------------------------------------------------------------------------- 1 | function hello() 2 | chainhelper:log('Hello World!') 3 | chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) 4 | chainhelper:log('test end. 2019-08-21 14:35:08') 5 | end 6 | -------------------------------------------------------------------------------- /wallet.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | from PythonMiddleware.graphene import Graphene 5 | from PythonMiddleware.instance import set_shared_graphene_instance 6 | from pprint import pprint 7 | from PythonMiddleware.account import Account 8 | 9 | nodeAddress = "ws://127.0.0.1:8020" 10 | gph = Graphene(node=nodeAddress, blocking=True) 11 | set_shared_graphene_instance(gph) 12 | 13 | #创建钱包1 14 | #可以通过gph.wallet 直接使用钱包instance,操作钱包的接口。 15 | if gph.wallet.created() is False: 16 | gph.newWallet("123456") 17 | 18 | #创建钱包2 19 | if gph.wallet.created() is False: 20 | gph.wallet.create("123456") 21 | 22 | #钱包解锁 23 | gph.wallet.unlock("123456") 24 | 25 | #钱包锁住 26 | gph.wallet.lock() 27 | 28 | #查看钱包锁定状态 29 | #返回: False 或 True 30 | pprint(gph.wallet.locked()) 31 | 32 | #更改钱包解锁密码 33 | #钱包需要unlock状态 34 | #gph.wallet.changePassphrase("654321") 35 | 36 | #查看钱包导入账户信息 37 | pprint(gph.wallet.getAccounts()) 38 | 39 | #钱包导入私钥 40 | privateKey="5JWKbGLfkZNtnSAb7fuk1pD4jsdPyMpJz4jyhwgu8RBk9RNzDYA" 41 | pub="COCOS78WwFk5YJVoCVa97NAKVALVZdhnYUdD2oHe2LCiX2KZaYNf4G8" 42 | gph.wallet.addPrivateKey(privateKey) 43 | 44 | #钱包获取导入的私钥 45 | pprint(gph.wallet.getPrivateKeyForPublicKey(pub)) 46 | 47 | #加密私钥 48 | encWif = gph.wallet.encrypt_wif(privateKey) 49 | pprint(encWif) 50 | 51 | #解密私钥 52 | pprint(gph.wallet.decrypt_wif(encWif) == privateKey) 53 | 54 | #钱包移除导入的私钥 55 | #gph.wallet.removePrivateKeyFromPublicKey(pub) 56 | 57 | #移除导入的账户None 58 | gph.wallet.removeAccount(None) 59 | gph.wallet.removeAccount('test13') 60 | 61 | #获取账号的owner private key 62 | pprint(gph.wallet.getOwnerKeyForAccount('test13')) 63 | pprint(gph.wallet.getMemoKeyForAccount('test13')) 64 | pprint(gph.wallet.getActiveKeyForAccount('test13')) 65 | 66 | #获取账户ID 67 | pprint(gph.wallet.getAccountFromPrivateKey(privateKey)) 68 | pprint(gph.wallet.getAccountFromPublicKey(pub)) 69 | 70 | #获取账户信息 71 | pprint(gph.wallet.getAccount(pub)) 72 | 73 | #获取钱包所有账户信息 74 | pprint(gph.wallet.getAccounts()) 75 | 76 | #获取钱包所有公钥 77 | pprint(gph.wallet.getPublicKeys()) 78 | 79 | #获取钱包公钥类型 80 | pprint(gph.wallet.getKeyType(Account('test13'), pub)) 81 | 82 | #清除钱包所有私钥 83 | # 慎用!!! 84 | #gph.wallet.wipe() 85 | 86 | --------------------------------------------------------------------------------