def deploy(from_addr, bytecode, contract_params, return_type, details): response = requests.post('http://127.0.0.1:20332', json={ 'jsonrpc': '2.0', 'id': 1, 'method': 'mw_construct_deploy_tx', 'params': [{ 'from_addr': from_addr, 'bin': bytecode, 'contract_params': contract_params, 'return_type': return_type, 'details': details, }] }).json() if 'error' in response: print(response['error']['message']) return context = response['result']['context'] binary_tx = response['result']['tx'] contract_hash = response['result']['hash'] tx = ContractTransaction.DeserializeFromBufer( binascii.unhexlify(binary_tx)) scripts = requests.post('http://127.0.0.1:5000/neo_sign/', json={ 'binary_tx': binary_tx }).json() tx.scripts = [ Witness( x['invocation'].encode(), x['verification'].encode(), ) for x in scripts ] ms = StreamManager.GetStream() writer = BinaryWriter(ms) tx.Serialize(writer) ms.flush() signed_tx = ms.ToArray() response = requests.post('http://127.0.0.1:20332', json={ 'jsonrpc': '2.0', 'id': 1, 'method': 'sendrawtransaction', 'params': [ signed_tx.decode(), ] }).json() print('contract hash:', contract_hash) print(response)
def send(addr_from, addr_to, asset, amount): response = requests.post('http://127.0.0.1:20332', json={ 'jsonrpc': '2.0', 'id': 1, 'method': 'mw_construct_send_tx', 'params': { 'from': addr_from, 'to': addr_to, 'asset': asset, 'amount': amount, } }).json() print(response) context = response['result']['context'] binary_tx = response['result']['tx'] tx = ContractTransaction.DeserializeFromBufer( binascii.unhexlify(binary_tx)) scripts = requests.post('http://127.0.0.1:5000/neo_sign/', json={ 'binary_tx': binary_tx, 'address': addr_from }).json() print('scripts', scripts) tx.scripts = [ Witness( x['invocation'].encode(), x['verification'].encode(), ) for x in scripts ] print(scripts) ms = StreamManager.GetStream() writer = BinaryWriter(ms) tx.Serialize(writer) ms.flush() signed_tx = ms.ToArray() print(tx.ToJson()) # print('does not send: return') ; return response = requests.post('http://127.0.0.1:20332', json={ 'jsonrpc': '2.0', 'id': 1, 'method': 'sendrawtransaction', 'params': [ signed_tx.decode(), ] }).json() print(response)
def sign_context(binary_tx, private): wallet = Wallet(b'', b'0' * 32, True) wallet.CreateKey(binascii.unhexlify(private)) script_hash = WalletContract.CreateSignatureContract( list(wallet._keys.values())[0].PublicKey) wallet._contracts[script_hash.ScriptHash.ToBytes()] = script_hash tx = ContractTransaction.DeserializeFromBufer( binascii.unhexlify(binary_tx)) context = ContractParametersContext(tx, isMultiSig=False) context.ScriptHashes = [script_hash.ScriptHash] wallet.Sign(context) return [x.ToJson() for x in context.GetScripts()]
def deploy(self, contract_params='0710', return_type='05'): self.compile() from_addr = NETWORKS[self.contract.network.name]['address'] bytecode = self.neo_contract_crowdsale.bytecode neo_int = NeoInt(self.contract.network.name) print('from address', from_addr) details = { 'name': 'WISH', 'description': 'NEO smart contract', 'email': '*****@*****.**', 'version': '1', 'author': 'MyWish' } param_list = { 'from_addr': from_addr, 'bin': bytecode, 'needs_storage': True, 'needs_dynamic_invoke': False, 'contract_params': contract_params, 'return_type': return_type, 'details': details, } response = neo_int.mw_construct_deploy_tx(param_list) print('construct response', response, flush=True) binary_tx = response['tx'] contract_hash = response['hash'] tx = ContractTransaction.DeserializeFromBufer( binascii.unhexlify(binary_tx)) tx = sign_neo_transaction(tx, binary_tx, from_addr) print('after sign', tx.ToJson()['txid'], flush=True) ms = StreamManager.GetStream() writer = BinaryWriter(ms) tx.Serialize(writer) ms.flush() signed_tx = ms.ToArray() print('full tx:', flush=True) print(signed_tx, flush=True) result = neo_int.sendrawtransaction(signed_tx.decode()) print(result, flush=True) if not result: raise TxFail() print('contract hash:', contract_hash) print('result of send raw transaction: ', result) self.neo_contract_crowdsale.address = contract_hash self.neo_contract_crowdsale.tx_hash = tx.ToJson()['txid'] self.neo_contract_crowdsale.save()
def msg_deployed(self, message): neo_int = NeoInt(self.contract.network.name) from_addr = NETWORKS[self.contract.network.name]['address'] param_list = { 'from_addr': from_addr, 'contract_params': [{ 'type': str(ContractParameterType.String), 'value': 'init' }, { 'type': str(ContractParameterType.Array), 'value': [] }], 'addr': self.neo_contract_crowdsale.address, } response = neo_int.mw_construct_invoke_tx(param_list) binary_tx = response['tx'] tx = ContractTransaction.DeserializeFromBufer( binascii.unhexlify(binary_tx)) tx = sign_neo_transaction(tx, binary_tx, from_addr) print('after sign', tx.ToJson()['txid']) ms = StreamManager.GetStream() writer = BinaryWriter(ms) tx.Serialize(writer) ms.flush() signed_tx = ms.ToArray() print('signed_tx', signed_tx) result = neo_int.sendrawtransaction(signed_tx.decode()) print(result, flush=True) if not result: raise TxFail() print('result of send raw transaction: ', result) self.contract.save() self.neo_contract_crowdsale.tx_hash = tx.ToJson()['txid'] self.neo_contract_crowdsale.save() return