def Contract_Destroy(self, engine): hash = UInt160(data=engine.CurrentContext.ScriptHash()) contract = self.Snapshot.Contracts.TryGet(hash.ToBytes()) if contract is not None: self.Snapshot.Contracts.Delete(hash.ToBytes()) if contract.HasStorage: to_del = [] for k, v in self.Snapshot.Storages.Find(hash.ToArray()): storage_key = StorageKey(script_hash=hash, key=k[20:]) # Snapshot.Storages.Delete() modifies the underlying dictionary of the cache we'd be iterating # over using Storages.Find. We therefore need to postpone deletion to_del.append(storage_key) for storage_key in to_del: self.Snapshot.Storages.Delete(storage_key.ToArray()) self.events_to_dispatch.append( SmartContractEvent(SmartContractEvent.CONTRACT_DESTROY, ContractParameter( ContractParameterType.InteropInterface, contract), hash, GetBlockchain().Height + 1, engine.ScriptContainer.Hash if engine.ScriptContainer else None, test_mode=engine.testMode)) return True
def Storage_Delete(self, engine): context = engine.EvaluationStack.Pop().GetInterface() if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) keystr = key if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) dispatch_smart_contract_event(SmartContractEvent.STORAGE_DELETE, keystr, context.ScriptHash, Blockchain.Default().Height, engine.ScriptContainer.Hash, test_mode=engine.testMode) self._storages.Remove(storage_key.GetHashCodeBytes()) return True
def Storage_Delete(self, engine: ExecutionEngine): context = engine.CurrentContext.EvaluationStack.Pop().GetInterface() if not self.CheckStorageContext(context): return False key = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) keystr = key if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) self.events_to_dispatch.append( SmartContractEvent(SmartContractEvent.STORAGE_DELETE, ContractParameter(ContractParameterType.String, keystr), context.ScriptHash, Blockchain.Default().Height + 1, engine.ScriptContainer.Hash if engine.ScriptContainer else None, test_mode=engine.testMode)) self._storages.Remove(storage_key.ToArray()) return True
def Storage_Put(self, engine): context = None try: context = engine.EvaluationStack.Pop().GetInterface( 'neo.SmartContract.StorageContext.StorageContext') except Exception as e: self.__log.debug("Storage Context Not found on stack") return False if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() if len(key) > 1024: return False value = engine.EvaluationStack.Pop().GetByteArray() new_item = StorageItem(value=value) storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self._storages.GetAndChange(storage_key.GetHashCodeBytes(), new_instance=new_item) item.Value = value self.__log.debug("Put stored item %s %s " % (item, item.Value)) return True
def Storage_Delete(self, engine: ExecutionEngine): if self.Trigger != TriggerType.Application and self.Trigger != TriggerType.ApplicationR: return False context = engine.CurrentContext.EvaluationStack.Pop().GetInterface( neo.SmartContract.StorageContext.StorageContext) if not self.CheckStorageContext(context): return False if context.IsReadOnly: return False key = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) if type(engine) == ExecutionEngine: test_mode = False else: test_mode = engine.testMode self.events_to_dispatch.append( SmartContractEvent(SmartContractEvent.STORAGE_DELETE, ContractParameter(ContractParameterType.String, key), context.ScriptHash, GetBlockchain().Height + 1, engine.ScriptContainer.Hash if engine.ScriptContainer else None, test_mode=test_mode)) item = self.Snapshot.Storages.TryGet(storage_key.ToArray()) if item and item.IsConstant: return False self.Snapshot.Storages.Delete(storage_key.ToArray()) return True
def Storage_Get(self, engine): context = None try: item = engine.EvaluationStack.Pop() context = item.GetInterface() except Exception as e: logger.error("could not get storage context %s " % e) return False if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self.Storages.TryGet(storage_key.ToArray()) keystr = key valStr = bytearray(0) if item is not None: valStr = bytearray(item.Value) if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) try: valStr = int.from_bytes(valStr, 'little') except Exception as e: logger.error("Could not convert %s to number: %s " % (valStr, e)) if item is not None: engine.EvaluationStack.PushT(bytearray(item.Value)) else: engine.EvaluationStack.PushT(bytearray(0)) tx_hash = None if engine.ScriptContainer: tx_hash = engine.ScriptContainer.Hash self.events_to_dispatch.append( SmartContractEvent(SmartContractEvent.STORAGE_GET, ContractParameter(ContractParameterType.String, value='%s -> %s' % (keystr, valStr)), context.ScriptHash, Blockchain.Default().Height + 1, tx_hash, test_mode=engine.testMode)) return True
def Storage_Delete(self, engine): context = engine.EvaluationStack.Pop().GetInterface('neo.SmartContract.StorageContext.StorageContext') if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) self._storages.Delete(storage_key.GetHashCodeBytes()) return True
def Storage_Get(self, engine): context = None try: item = engine.EvaluationStack.Pop() context = item.GetInterface() shash = context.ScriptHash except Exception as e: logger.error("could not get storage context %s " % e) return False if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self._storages.TryGet(storage_key.GetHashCodeBytes()) keystr = key valStr = bytearray(0) if item is not None: valStr = bytearray(item.Value) if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) try: valStr = int.from_bytes(valStr, 'little') except Exception as e: pass if item is not None: engine.EvaluationStack.PushT(bytearray(item.Value)) else: engine.EvaluationStack.PushT(bytearray([0])) dispatch_smart_contract_event(SmartContractEvent.STORAGE_GET, '%s -> %s' % (keystr, valStr), context.ScriptHash, Blockchain.Default().Height, engine.ScriptContainer.Hash, test_mode=engine.testMode) return True
def Storage_Get(self, engine): context = engine.EvaluationStack.Pop().GetInterface( 'neo.SmartContract.StorageContext.StorageContext') contract = Blockchain.Default().GetContract( context.ScriptHash.ToBytes()) if contract is None or not contract.HasStorage: return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = Blockchain.Default().GetStorageItem(storage_key) if item is not None: engine.EvaluationStack.PushT(item.Value) else: engine.EvaluationStack.PushT(bytearray(0)) return True
def Storage_Get(self, engine): context = None try: item = engine.EvaluationStack.Pop() context = item.GetInterface( 'neo.SmartContract.StorageContext.StorageContext') shash = context.ScriptHash except Exception as e: print("could not get storage context %s " % e) return False if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self._storages.TryGet(storage_key.GetHashCodeBytes()) keystr = key valStr = bytearray(0) if item is not None: valStr = bytearray(item.Value) if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) try: valStr = int.from_bytes(valStr, 'little') except Exception as e: print("couldnt convert %s to number: %s " % (valStr, e)) if item is not None: print("[Neo.Storage.Get] [Script:%s] [%s] -> %s " % (context.ScriptHash, keystr, valStr)) engine.EvaluationStack.PushT(bytearray(item.Value)) else: print("[Neo.Storage.Get] [Script:%s] [%s] -> 0 " % (context.ScriptHash, keystr)) engine.EvaluationStack.PushT(bytearray(0)) return True
def Storage_Put(self, engine: ExecutionEngine): context = None try: context = engine.CurrentContext.EvaluationStack.Pop().GetInterface( ) except Exception as e: logger.error("Storage Context Not found on stack") return False if not self.CheckStorageContext(context): return False key = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() if len(key) > 1024: return False value = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() new_item = StorageItem(value=value) storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self._storages.ReplaceOrAdd(storage_key.ToArray(), new_item) keystr = key valStr = bytearray(item.Value) if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) try: valStr = int.from_bytes(valStr, 'little') except Exception as e: pass self.events_to_dispatch.append( SmartContractEvent( SmartContractEvent.STORAGE_PUT, ContractParameter(ContractParameterType.String, '%s -> %s' % (keystr, valStr)), context.ScriptHash, Blockchain.Default().Height + 1, engine.ScriptContainer.Hash if engine.ScriptContainer else None, test_mode=engine.testMode)) return True
def Storage_Get(self, engine: ExecutionEngine): context = None try: item = engine.CurrentContext.EvaluationStack.Pop() context = item.GetInterface( neo.SmartContract.StorageContext.StorageContext) except Exception as e: return False if not self.CheckStorageContext(context): return False key = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self.Snapshot.Storages.TryGet(storage_key.ToArray()) keystr = key valStr = bytearray(0) if item is not None: valStr = bytearray(item.Value) if item is not None: engine.CurrentContext.EvaluationStack.PushT(bytearray(item.Value)) else: engine.CurrentContext.EvaluationStack.PushT(bytearray(0)) tx_hash = None if engine.ScriptContainer: tx_hash = engine.ScriptContainer.Hash self.events_to_dispatch.append( SmartContractEvent(SmartContractEvent.STORAGE_GET, ContractParameter(ContractParameterType.String, value='%s -> %s' % (keystr, valStr)), context.ScriptHash, GetBlockchain().Height + 1, tx_hash, test_mode=engine.testMode)) return True
def Storage_Put(self, engine): context = None try: context = engine.EvaluationStack.Pop().GetInterface() except Exception as e: logger.error("Storage Context Not found on stack") return False if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() if len(key) > 1024: return False value = engine.EvaluationStack.Pop().GetByteArray() new_item = StorageItem(value=value) storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self._storages.GetOrAdd(storage_key.GetHashCodeBytes(), new_item) keystr = key valStr = bytearray(item.Value) if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) try: valStr = int.from_bytes(valStr, 'little') except Exception as e: pass dispatch_smart_contract_event(SmartContractEvent.STORAGE_PUT, '%s -> %s' % (keystr, valStr), context.ScriptHash, Blockchain.Default().Height, engine.ScriptContainer.Hash, test_mode=engine.testMode) return True
def Storage_Put(self, engine: ExecutionEngine): context = None try: context = engine.CurrentContext.EvaluationStack.Pop().GetInterface( neo.SmartContract.StorageContext.StorageContext) except Exception as e: return False if not self.CheckStorageContext(context): return False key = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() if len(key) > 1024: return False value = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self.Snapshot.Storages.GetAndChange(storage_key.ToArray(), lambda: StorageItem()) item.Value = value keystr = key valStr = bytearray(item.Value) if type(engine) == ExecutionEngine: test_mode = False else: test_mode = engine.testMode self.events_to_dispatch.append( SmartContractEvent( SmartContractEvent.STORAGE_PUT, ContractParameter(ContractParameterType.String, '%s -> %s' % (keystr, valStr)), context.ScriptHash, GetBlockchain().Height + 1, engine.ScriptContainer.Hash if engine.ScriptContainer else None, test_mode=test_mode)) return True
def Storage_Get(self, engine): context = None try: item = engine.EvaluationStack.Pop() context = item.GetInterface() shash = context.ScriptHash except Exception as e: logger.error("could not get storage context %s " % e) return False contract = Blockchain.Default().GetContract( context.ScriptHash.ToBytes()) if contract is not None: if not contract.HasStorage: return False else: return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = Blockchain.Default().GetStorageItem(storage_key) keystr = key valStr = bytearray(0) if item is not None: valStr = bytearray(item.Value) if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) try: valStr = int.from_bytes(valStr, 'little') except Exception as e: logger.error("Could not convert %s to number: %s " % (valStr, e)) if item is not None: engine.EvaluationStack.PushT(bytearray(item.Value)) else: engine.EvaluationStack.PushT(bytearray(0)) self.events_to_dispatch.append( SmartContractEvent(SmartContractEvent.STORAGE_GET, ['%s -> %s' % (keystr, valStr)], context.ScriptHash, Blockchain.Default().Height + 1, engine.ScriptContainer.Hash, test_mode=engine.testMode)) return True
def Storage_Put(self, engine): context = None try: context = engine.EvaluationStack.Pop().GetInterface( 'neo.SmartContract.StorageContext.StorageContext') except Exception as e: self.__log.debug("Storage Context Not found on stack") return False if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() if len(key) > 1024: return False value = engine.EvaluationStack.Pop().GetByteArray() new_item = StorageItem(value=value) storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self._storages.GetOrAdd(storage_key.GetHashCodeBytes(), new_item) keystr = key valStr = bytearray(item.Value) if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) try: valStr = int.from_bytes(valStr, 'little') except Exception as e: pass print("[Neo.Storage.Put] [Script: %s] [%s] -> %s" % (context.ScriptHash, keystr, valStr)) return True
def Storage_Get(self, engine): context = None try: context = engine.EvaluationStack.Pop().GetInterface( 'neo.SmartContract.StorageContext.StorageContext') except Exception as e: self.__log.debug("Storage Context not found") return False if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) item = self._storages.TryGet(storage_key.GetHashCodeBytes()) if item is not None: engine.EvaluationStack.PushT(item.Value) else: engine.EvaluationStack.PushT(bytearray(0)) return True
def Storage_Delete(self, engine): context = engine.EvaluationStack.Pop().GetInterface( 'neo.SmartContract.StorageContext.StorageContext') if not self.CheckStorageContext(context): return False key = engine.EvaluationStack.Pop().GetByteArray() storage_key = StorageKey(script_hash=context.ScriptHash, key=key) keystr = key if len(key) == 20: keystr = Crypto.ToAddress(UInt160(data=key)) print("[Neo.Storage.Delete] [Script %s] Delete %s " % (context.ScriptHash, keystr)) self._storages.Remove(storage_key.GetHashCodeBytes()) return True
def Contract_Migrate(self, engine): script = engine.EvaluationStack.Pop().GetByteArray() if len(script) > 1024 * 1024: return False param_list = engine.EvaluationStack.Pop().GetByteArray() if len(param_list) > 252: return False return_type = int(engine.EvaluationStack.Pop().GetBigInteger()) needs_storage = engine.EvaluationStack.Pop().GetBoolean() if len(engine.EvaluationStack.Peek().GetByteArray()) > 252: return False name = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') if len(engine.EvaluationStack.Peek().GetByteArray()) > 252: return False version = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') if len(engine.EvaluationStack.Peek().GetByteArray()) > 252: return False author = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') if len(engine.EvaluationStack.Peek().GetByteArray()) > 252: return False email = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') if len(engine.EvaluationStack.Peek().GetByteArray()) > 65536: return False description = engine.EvaluationStack.Pop().GetByteArray().decode( 'utf-8') hash = Crypto.ToScriptHash(script, unhex=False) contract = self._contracts.TryGet(hash.ToBytes()) if contract is None: code = FunctionCode(script=script, param_list=param_list, return_type=return_type) contract = ContractState(code=code, has_storage=needs_storage, name=name, version=version, author=author, email=email, description=description) self._contracts.Add(hash.ToBytes(), contract) self._contracts_created[hash.ToBytes()] = UInt160( data=engine.CurrentContext.ScriptHash) if needs_storage: for pair in self._storages.Find( engine.CurrentContext.ScriptHash()): key = StorageKey(script_hash=hash, key=pair.Key.Key) item = StorageItem(pair.Value.Value) self._storages.Add(key, item) engine.EvaluationStack.PushT(StackItem.FromInterface(contract)) # print("*****************************************************") # print("MIGRATED CONTRACT %s " % hash.ToBytes()) # print("*****************************************************") return True
def json_rpc_method_handler(self, method, params): if method == "getaccountstate": acct = Blockchain.Default().GetAccountState(params[0]) if acct is None: try: acct = AccountState( script_hash=Helper.AddrStrToScriptHash(params[0])) except Exception as e: raise JsonRpcError( -2146233033, "One of the identified items was in an invalid format." ) return acct.ToJson() elif method == "getassetstate": asset_id = UInt256.ParseString(params[0]) asset = Blockchain.Default().GetAssetState(asset_id.ToBytes()) if asset: return asset.ToJson() raise JsonRpcError(-100, "Unknown asset") elif method == "getbestblockhash": return '0x%s' % Blockchain.Default().CurrentHeaderHash.decode( 'utf-8') elif method == "getblock": # this should work for either str or int block = Blockchain.Default().GetBlock(params[0]) if not block: raise JsonRpcError(-100, "Unknown block") return self.get_block_output(block, params) elif method == "getblockcount": return Blockchain.Default().Height + 1 elif method == "getblockhash": height = params[0] if height >= 0 and height <= Blockchain.Default().Height: return '0x%s' % Blockchain.Default().GetBlockHash( height).decode('utf-8') else: raise JsonRpcError(-100, "Invalid Height") elif method == "getblocksysfee": height = params[0] if height >= 0 and height <= Blockchain.Default().Height: return Blockchain.Default().GetSysFeeAmountByHeight(height) else: raise JsonRpcError(-100, "Invalid Height") elif method == "getconnectioncount": return len(NodeLeader.Instance().Peers) elif method == "getcontractstate": script_hash = UInt160.ParseString(params[0]) contract = Blockchain.Default().GetContract(script_hash.ToBytes()) if contract is None: raise JsonRpcError(-100, "Unknown contract") return contract.ToJson() elif method == "getrawmempool": return list( map(lambda hash: "0x%s" % hash.decode('utf-8'), NodeLeader.Instance().MemPool.keys())) elif method == "getversion": return { "port": self.port, "nonce": NodeLeader.Instance().NodeId, "useragent": settings.VERSION_NAME } elif method == "getrawtransaction": tx_id = UInt256.ParseString(params[0]) tx, height = Blockchain.Default().GetTransaction(tx_id) if not tx: raise JsonRpcError(-100, "Unknown Transaction") return self.get_tx_output(tx, height, params) elif method == "getstorage": script_hash = UInt160.ParseString(params[0]) key = binascii.unhexlify(params[1].encode('utf-8')) storage_key = StorageKey(script_hash=script_hash, key=key) storage_item = Blockchain.Default().GetStorageItem(storage_key) if storage_item: return storage_item.Value.hex() return None elif method == "gettxout": hash = params[0].encode('utf-8') index = params[1] utxo = Blockchain.Default().GetUnspent(hash, index) if utxo: return utxo.ToJson(index) else: return None elif method == "invoke": shash = UInt160.ParseString(params[0]) contract_parameters = [ ContractParameter.FromJson(p) for p in params[1] ] sb = ScriptBuilder() sb.EmitAppCallWithJsonArgs(shash, contract_parameters) return self.get_invoke_result(sb.ToArray()) elif method == "invokefunction": contract_parameters = [] if len(params) > 2: contract_parameters = [ ContractParameter.FromJson(p).ToVM() for p in params[2] ] sb = ScriptBuilder() sb.EmitAppCallWithOperationAndArgs(UInt160.ParseString(params[0]), params[1], contract_parameters) return self.get_invoke_result(sb.ToArray()) elif method == "invokescript": script = params[0].encode('utf-8') return self.get_invoke_result(script) elif method == "sendrawtransaction": tx_script = binascii.unhexlify(params[0].encode('utf-8')) transaction = Transaction.DeserializeFromBufer(tx_script) result = NodeLeader.Instance().Relay(transaction) return result elif method == "validateaddress": return self.validateaddress(params) elif method == "getpeers": return self.get_peers() elif method == "getbalance": if self.wallet: return self.get_balance(params) else: raise JsonRpcError(-400, "Access denied.") elif method == "getwalletheight": if self.wallet: return self.wallet.WalletHeight else: raise JsonRpcError(-400, "Access denied.") elif method == "listaddress": if self.wallet: return self.list_address() else: raise JsonRpcError(-400, "Access denied.") elif method == "getnewaddress": if self.wallet: keys = self.wallet.CreateKey() account = Account.get( PublicKeyHash=keys.PublicKeyHash.ToBytes()) return account.contract_set[0].Address.ToString() else: raise JsonRpcError(-400, "Access denied.") raise JsonRpcError.methodNotFound()
def json_rpc_method_handler(self, method, params): if method == "getaccountstate": acct = Blockchain.Default().GetAccountState(params[0]) if acct is None: try: acct = AccountState(script_hash=Helper.AddrStrToScriptHash(params[0])) except Exception as e: raise JsonRpcError(-2146233033, "One of the identified items was in an invalid format.") return acct.ToJson() elif method == "getassetstate": asset_id = UInt256.ParseString(params[0]) asset = Blockchain.Default().GetAssetState(asset_id.ToBytes()) if asset: return asset.ToJson() raise JsonRpcError(-100, "Unknown asset") elif method == "getbestblockhash": return '0x%s' % Blockchain.Default().CurrentHeaderHash.decode('utf-8') elif method == "getblock": # this should work for either str or int block = Blockchain.Default().GetBlock(params[0]) if not block: raise JsonRpcError(-100, "Unknown block") return self.get_block_output(block, params) elif method == "getblockcount": return Blockchain.Default().Height + 1 elif method == "getblockhash": height = params[0] if height >= 0 and height <= Blockchain.Default().Height: return '0x%s' % Blockchain.Default().GetBlockHash(height).decode('utf-8') else: raise JsonRpcError(-100, "Invalid Height") elif method == "getblocksysfee": height = params[0] if height >= 0 and height <= Blockchain.Default().Height: return Blockchain.Default().GetSysFeeAmountByHeight(height) else: raise JsonRpcError(-100, "Invalid Height") elif method == "getconnectioncount": return len(NodeLeader.Instance().Peers) elif method == "getcontractstate": script_hash = UInt160.ParseString(params[0]) contract = Blockchain.Default().GetContract(script_hash.ToBytes()) if contract is None: raise JsonRpcError(-100, "Unknown contract") return contract.ToJson() elif method == "getrawmempool": return list(map(lambda hash: "0x%s" % hash.decode('utf-8'), NodeLeader.Instance().MemPool.keys())) elif method == "getversion": return { "port": self.port, "nonce": NodeLeader.Instance().NodeId, "useragent": settings.VERSION_NAME } elif method == "getrawtransaction": tx_id = UInt256.ParseString(params[0]) tx, height = Blockchain.Default().GetTransaction(tx_id) if not tx: raise JsonRpcError(-100, "Unknown Transaction") return self.get_tx_output(tx, height, params) elif method == "getstorage": script_hash = UInt160.ParseString(params[0]) key = binascii.unhexlify(params[1].encode('utf-8')) storage_key = StorageKey(script_hash=script_hash, key=key) storage_item = Blockchain.Default().GetStorageItem(storage_key) if storage_item: return storage_item.Value.hex() return None elif method == "gettxout": hash = params[0].encode('utf-8') index = params[1] utxo = Blockchain.Default().GetUnspent(hash, index) if utxo: return utxo.ToJson(index) else: return None elif method == "invoke": shash = UInt160.ParseString(params[0]) contract_parameters = [ContractParameter.FromJson(p) for p in params[1]] sb = ScriptBuilder() sb.EmitAppCallWithJsonArgs(shash, contract_parameters) return self.get_invoke_result(sb.ToArray()) elif method == "invokefunction": contract_parameters = [] if len(params) > 2: contract_parameters = [ContractParameter.FromJson(p).ToVM() for p in params[2]] sb = ScriptBuilder() sb.EmitAppCallWithOperationAndArgs(UInt160.ParseString(params[0]), params[1], contract_parameters) return self.get_invoke_result(sb.ToArray()) elif method == "invokescript": script = params[0].encode('utf-8') return self.get_invoke_result(script) elif method == "sendrawtransaction": tx_script = binascii.unhexlify(params[0].encode('utf-8')) transaction = Transaction.DeserializeFromBufer(tx_script) result = NodeLeader.Instance().Relay(transaction) return result elif method == "mw_construct_send_tx": return MyWishMethods.construct_send_tx(self.wallet, params) elif method == "mw_construct_deploy_tx": return MyWishMethods.construct_deploy_tx(self.wallet, params) elif method == "mw_construct_invoke_tx": return MyWishMethods.construct_invoke_tx(self.wallet, params) elif method == "getapplicationlog": assert(all([x in '1234567890abcdefABCDEFxX' for x in params[0]])) # prevent traversal try: with open('/home/neo/neo-python/notis/' + params[0]) as f: res = [json.loads(x) for x in f.read().split('\n') if x] return [{'name': x['notify_type'], 'contract': x['contract'], 'args': x['payload']} for x in res] except FileNotFoundError: return ([]) elif method == "submitblock": raise NotImplementedError() elif method == "validateaddress": return self.validateaddress(params) elif method == "getpeers": return self.get_peers() raise JsonRpcError.methodNotFound()
def Contract_Migrate(self, engine): script = engine.EvaluationStack.Pop().GetByteArray() if len(script) > 1024 * 1024: return False param_list = engine.EvaluationStack.Pop().GetByteArray() if len(param_list) > 252: return False return_type = int(engine.EvaluationStack.Pop().GetBigInteger()) contract_properties = engine.EvaluationStack.Pop().GetBigInteger() if len(engine.EvaluationStack.Peek().GetByteArray()) > 252: return False name = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') if len(engine.EvaluationStack.Peek().GetByteArray()) > 252: return False version = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') if len(engine.EvaluationStack.Peek().GetByteArray()) > 252: return False author = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') if len(engine.EvaluationStack.Peek().GetByteArray()) > 252: return False email = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') if len(engine.EvaluationStack.Peek().GetByteArray()) > 65536: return False description = engine.EvaluationStack.Pop().GetByteArray().decode( 'utf-8') hash = Crypto.ToScriptHash(script, unhex=False) contract = self._contracts.TryGet(hash.ToBytes()) if contract is None: code = FunctionCode(script=script, param_list=param_list, return_type=return_type) contract = ContractState(code=code, contract_properties=contract_properties, name=name, version=version, author=author, email=email, description=description) self._contracts.Add(hash.ToBytes(), contract) self._contracts_created[hash.ToBytes()] = UInt160( data=engine.CurrentContext.ScriptHash()) if contract.HasStorage: for pair in self._storages.Find( engine.CurrentContext.ScriptHash()): key = StorageKey(script_hash=hash, key=pair.Key.Key) item = StorageItem(pair.Value.Value) self._storages.Add(key, item) engine.EvaluationStack.PushT(StackItem.FromInterface(contract)) self.events_to_dispatch.append( SmartContractEvent(SmartContractEvent.CONTRACT_MIGRATED, [contract], hash, Blockchain.Default().Height + 1, engine.ScriptContainer.Hash if engine.ScriptContainer else None, test_mode=engine.testMode)) return self.Contract_Destroy(engine)
def json_rpc_method_handler(self, method, params): if method == "getaccountstate": acct = Blockchain.Default().GetAccountState(params[0]) if acct is None: try: acct = AccountState(script_hash=Helper.AddrStrToScriptHash(params[0])) except Exception as e: raise JsonRpcError(-2146233033, "One of the identified items was in an invalid format.") return acct.ToJson() elif method == "getassetstate": asset_id = UInt256.ParseString(params[0]) asset = Blockchain.Default().GetAssetState(asset_id.ToBytes()) if asset: return asset.ToJson() raise JsonRpcError(-100, "Unknown asset") elif method == "getbestblockhash": return '0x%s' % Blockchain.Default().CurrentHeaderHash.decode('utf-8') elif method == "getblock": # this should work for either str or int block = Blockchain.Default().GetBlock(params[0]) if not block: raise JsonRpcError(-100, "Unknown block") return self.get_block_output(block, params) elif method == "getblockcount": return Blockchain.Default().Height + 1 elif method == "getblockhash": height = params[0] if height >= 0 and height <= Blockchain.Default().Height: return '0x%s' % Blockchain.Default().GetBlockHash(height).decode('utf-8') else: raise JsonRpcError(-100, "Invalid Height") elif method == "getblocksysfee": height = params[0] if height >= 0 and height <= Blockchain.Default().Height: return Blockchain.Default().GetSysFeeAmountByHeight(height) else: raise JsonRpcError(-100, "Invalid Height") elif method == "getconnectioncount": return len(NodeLeader.Instance().Peers) elif method == "getcontractstate": script_hash = UInt160.ParseString(params[0]) contract = Blockchain.Default().GetContract(script_hash.ToBytes()) if contract is None: raise JsonRpcError(-100, "Unknown contract") return contract.ToJson() elif method == "getrawmempool": return list(map(lambda hash: "0x%s" % hash.decode('utf-8'), NodeLeader.Instance().MemPool.keys())) elif method == "getversion": return { "port": self.port, "nonce": NodeLeader.Instance().NodeId, "useragent": settings.VERSION_NAME } elif method == "getrawtransaction": tx_id = UInt256.ParseString(params[0]) tx, height = Blockchain.Default().GetTransaction(tx_id) if not tx: raise JsonRpcError(-100, "Unknown Transaction") return self.get_tx_output(tx, height, params) elif method == "getLastBlog": if len(params) > 0: height = params[0] else: height = Blockchain.Default().Height + 1 data = self.get_blog_content(height, "") return data; elif method == "getLastBlogByTag": if len(params) > 1: height = params[1] tag = params[0] elif len(params) > 0: tag = params[0] height = Blockchain.Default().Height + 1 else: raise JsonRpcError(-100, "no enough param") data = self.get_blog_content(height, tag) return data; elif method == "getLastBlogBySender": if len(params) > 1: height = params[1] sender = params[0] elif len(params) > 0: sender = params[0] height = Blockchain.Default().Height + 1 else: raise JsonRpcError(-100, "no enough param") data = self.get_blog_content(height, "", sender) return data; elif method == "getBlogContent": tx_id = UInt256.ParseString(params[0]) tx, height = Blockchain.Default().GetTransaction(tx_id) if not tx: raise JsonRpcError(-100, "Unknown Transaction") for attr in tx.Attributes: item = attr.ToJson; if item['usage'] == 240: item['content'] = binascii.a2b_hex(item['data']).decode("utf8") return item; raise JsonRpcError(-100, "no blog content") elif method == "getstorage": script_hash = UInt160.ParseString(params[0]) key = binascii.unhexlify(params[1].encode('utf-8')) storage_key = StorageKey(script_hash=script_hash, key=key) storage_item = Blockchain.Default().GetStorageItem(storage_key) if storage_item: return storage_item.Value.hex() return None elif method == "gettxout": hash = params[0].encode('utf-8') index = params[1] utxo = Blockchain.Default().GetUnspent(hash, index) if utxo: return utxo.ToJson(index) else: return None elif method == "gettxout": hash = params[0].encode('utf-8') index = params[1] utxo = Blockchain.Default().GetUnspent(hash, index) if utxo: return utxo.ToJson(index) else: return None elif method == "createAddress": private_key = bytes(Random.get_random_bytes(32)) key = KeyPair(priv_key=private_key) self.wallet._keys[key.PublicKeyHash.ToBytes()] = key self.wallet.OnCreateAccount(key) contract = WalletContract.CreateSignatureContract(key.PublicKey) self.wallet.AddContract(contract) if key : result = {'privateKey': key.Export(),'address':contract.Address} return result; else: return None elif method == "invoke": shash = UInt160.ParseString(params[0]) contract_parameters = [ContractParameter.FromJson(p) for p in params[1]] sb = ScriptBuilder() sb.EmitAppCallWithJsonArgs(shash, contract_parameters) return self.get_invoke_result(sb.ToArray()) # elif method == "sendBlog": # shash = UInt160.ParseString(params[0]) # contract_parameters = [ContractParameter.FromJson(p) for p in params[1]] # sb = ScriptBuilder() # sb.EmitAppCallWithJsonArgs(shash, contract_parameters) # script = sb.ToArray(); # tx = ContractTransaction() # tx.inputs = [] # tx.outputs = [] # attribute = TransactionAttribute(240, params[2].encode('utf-8')) # standard_contract = self.wallet.GetStandardAddress() # data = standard_contract.Data # tx.Attributes = [TransactionAttribute(usage=TransactionAttributeUsage.Script, # data=data)] # # tx.Attributes.append(attribute) # tx.Version = 1 # tx.scripts = [] # BC = GetBlockchain() # contract = BC.GetContract(params[0]) # output = TransactionOutput(AssetId=Blockchain.SystemShare().Hash, # Value=Fixed8.FromDecimal(float(1)), # script_hash=contract.Code.ScriptHash(), # ) # tx.outputs.append(output) # logger.info("output %s" % output.ToJson(0)) # tx.Script = binascii.unhexlify(script) # print("percentage %s %s" % (self.wallet.WalletHeight, Blockchain.Default().Height)) # scripthash_from = None # if not self.wallet.IsSynced: # raise JsonRpcError.invalidRequest("wallet not synced") # private_key = None # if len(params) > 3: # from_address = params[3] # if from_address is not None: # scripthash_from = lookup_addr_str(self.wallet, from_address) # if len(params) > 4: # private_key = params[4] # wallet_tx = SendBlog(self.wallet, tx, from_addr=scripthash_from, privatekey=private_key) # if wallet_tx != False: # return wallet_tx.ToJson(); # self.wallet.Rebuild() # raise JsonRpcError.invalidRequest("Field 'no enough asset") elif method == 'postblog': try: if len(params) > 3: from_address = params[3] assetId =Blockchain.SystemShare().Hash if len(params) > 4: private_key = params[4] if assetId is None: print("Asset id not found") return False contract_parameters = [ContractParameter.FromJson(p) for p in params[1]] sb = ScriptBuilder() sb.EmitAppCallWithJsonArgs(UInt160.ParseString(params[0]), contract_parameters) script = sb.ToArray(); scripthash_from = None if from_address is not None: scripthash_from = lookup_addr_str(self.wallet, from_address) # f8amount = Fixed8.TryParse(0, require_positive=True) # if f8amount is None: # print("invalid amount format") # return False # # if type(assetId) is UInt256 and f8amount.value % pow(10, 8 - Blockchain.Default().GetAssetState( # assetId.ToBytes()).Precision) != 0: # print("incorrect amount precision") # return False # fee = Fixed8.Zero() # # output = TransactionOutput(AssetId=assetId, Value=f8amount, script_hash=contract.Code.ScriptHash()) tx = InvocationTransaction() ttx = self.wallet.MakeTransaction(tx=tx, change_address=None, fee=fee, from_addr=scripthash_from) if ttx is None: print("insufficient funds") return False standard_contract = self.wallet.GetStandardAddress() if scripthash_from is not None: signer_contract = self.wallet.GetContract(scripthash_from) else: signer_contract = self.wallet.GetContract(standard_contract) if not signer_contract.IsMultiSigContract: data = scripthash_from.Data print(data) tx.Attributes = [TransactionAttribute(usage=TransactionAttributeUsage.Script, data=data),TransactionAttribute(usage=TransactionAttributeUsage.Remark1, data=from_address.encode('utf-8'))] # insert any additional user specified tx attributes tx.Attributes.append(TransactionAttribute(240, params[2].encode('utf-8'))) tx.Script = binascii.unhexlify(script) context = ContractParametersContext(tx, isMultiSig=signer_contract.IsMultiSigContract) if private_key is not None: prikey = KeyPair.PrivateKeyFromWIF(private_key) kp = KeyPair(prikey) self.wallet.Sign(context,kp) attributes = [attr.ToJson() for attr in tx.Attributes] print("attributes %s" %attributes) if context.Completed: tx.scripts = context.GetScripts() # print("will send tx: %s " % json.dumps(tx.ToJson(),indent=4)) relayed = NodeLeader.Instance().Relay(tx) if relayed: self.wallet.SaveTransaction(tx) print("Relayed Tx: %s " % tx.Hash.ToString()) return tx.ToJson() else: print("Could not relay tx %s " % tx.Hash.ToString()) else: print("Transaction initiated, but the signature is incomplete") print(json.dumps(context.ToJson(), separators=(',', ':'))) return False except Exception as e: print("could not send: %s " % e) return False elif method == "invokefunction": contract_parameters = [] if len(params) > 2: contract_parameters = [ContractParameter.FromJson(p).ToVM() for p in params[2]] sb = ScriptBuilder() sb.EmitAppCallWithOperationAndArgs(UInt160.ParseString(params[0]), params[1], contract_parameters) return self.get_invoke_result(sb.ToArray()) elif method == "invokescript": script = params[0].encode('utf-8') return self.get_invoke_result(script) elif method == "sendrawtransaction": 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() elif method == "validateaddress": return self.validateaddress(params) elif method == "getpeers": raise NotImplementedError() raise JsonRpcError.methodNotFound()
def Contract_Migrate(self, engine: ExecutionEngine): script = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() if len(script) > 1024 * 1024: return False param_list = engine.CurrentContext.EvaluationStack.Pop().GetByteArray() if len(param_list) > 252: return False return_type = int( engine.CurrentContext.EvaluationStack.Pop().GetBigInteger()) contract_properties = engine.CurrentContext.EvaluationStack.Pop( ).GetBigInteger() if len(engine.CurrentContext.EvaluationStack.Peek().GetByteArray() ) > 252: return False name = engine.CurrentContext.EvaluationStack.Pop().GetByteArray( ).decode('utf-8') if len(engine.CurrentContext.EvaluationStack.Peek().GetByteArray() ) > 252: return False version = engine.CurrentContext.EvaluationStack.Pop().GetByteArray( ).decode('utf-8') if len(engine.CurrentContext.EvaluationStack.Peek().GetByteArray() ) > 252: return False author = engine.CurrentContext.EvaluationStack.Pop().GetByteArray( ).decode('utf-8') if len(engine.CurrentContext.EvaluationStack.Peek().GetByteArray() ) > 252: return False email = engine.CurrentContext.EvaluationStack.Pop().GetByteArray( ).decode('utf-8') if len(engine.CurrentContext.EvaluationStack.Peek().GetByteArray() ) > 65536: return False description = engine.CurrentContext.EvaluationStack.Pop().GetByteArray( ).decode('utf-8') hash = Crypto.ToScriptHash(script, unhex=False) contract = self.Snapshot.Contracts.TryGet(hash.ToBytes()) if contract is None: code = FunctionCode(script=script, param_list=param_list, return_type=return_type) contract = ContractState(code=code, contract_properties=contract_properties, name=name, version=version, author=author, email=email, description=description) self.Snapshot.Contracts.Add(hash.ToBytes(), contract) cur_hash = UInt160(data=engine.CurrentContext.ScriptHash()) self._contracts_created[hash.ToBytes()] = cur_hash if contract.HasStorage: to_add = [] for key, item in self.Snapshot.Storages.Find( cur_hash.ToArray()): key = StorageKey(script_hash=hash, key=key[20:]) to_add.append((key.ToArray(), item)) for k, v in to_add: self.Snapshot.Storages.Add(k, v) engine.CurrentContext.EvaluationStack.PushT( StackItem.FromInterface(contract)) self.events_to_dispatch.append( SmartContractEvent(SmartContractEvent.CONTRACT_MIGRATED, ContractParameter( ContractParameterType.InteropInterface, contract), hash, GetBlockchain().Height + 1, engine.ScriptContainer.Hash if engine.ScriptContainer else None, test_mode=engine.testMode)) return self.Contract_Destroy(engine)
def json_rpc_method_handler(self, method, params): if method == "getaccountstate": acct = Blockchain.Default().GetAccountState(params[0]) if acct is None: try: acct = AccountState(script_hash=Helper.AddrStrToScriptHash(params[0])) except Exception as e: raise JsonRpcError(-2146233033, "One of the identified items was in an invalid format.") return acct.ToJson() elif method == "getassetstate": asset_str = params[0] if asset_str.lower() == 'neo': assetId = Blockchain.Default().SystemShare().Hash elif asset_str.lower() == 'gas': assetId = Blockchain.Default().SystemCoin().Hash else: assetId = UInt256.ParseString(params[0]) asset = Blockchain.Default().GetAssetState(assetId.ToBytes()) if asset: return asset.ToJson() raise JsonRpcError(-100, "Unknown asset") elif method == "getbestblockhash": return '0x%s' % Blockchain.Default().CurrentHeaderHash.decode('utf-8') elif method == "getblock": # this should work for either str or int block = Blockchain.Default().GetBlock(params[0]) if not block: raise JsonRpcError(-100, "Unknown block") return self.get_block_output(block, params) elif method == "getblockcount": return Blockchain.Default().Height + 1 elif method == "getblockhash": height = params[0] if height >= 0 and height <= Blockchain.Default().Height: return '0x%s' % Blockchain.Default().GetBlockHash(height).decode('utf-8') else: raise JsonRpcError(-100, "Invalid Height") elif method == "getblocksysfee": height = params[0] if height >= 0 and height <= Blockchain.Default().Height: return Blockchain.Default().GetSysFeeAmountByHeight(height) else: raise JsonRpcError(-100, "Invalid Height") elif method == "getconnectioncount": return len(self.nodemgr.nodes) elif method == "getcontractstate": script_hash = UInt160.ParseString(params[0]) contract = Blockchain.Default().GetContract(script_hash.ToBytes()) if contract is None: raise JsonRpcError(-100, "Unknown contract") return contract.ToJson() elif method == "getrawmempool": raise JsonRpcError(-100, "Not supported") # return list(map(lambda hash: f"{hash.To0xString()}", self.nodemgr.mempool.pool.keys())) elif method == "getversion": return { "port": self.port, "nonce": self.nodemgr.id, "useragent": settings.VERSION_NAME } elif method == "getrawtransaction": tx_id = UInt256.ParseString(params[0]) tx, height = Blockchain.Default().GetTransaction(tx_id) if not tx: raise JsonRpcError(-100, "Unknown Transaction") return self.get_tx_output(tx, height, params) elif method == "getstorage": script_hash = UInt160.ParseString(params[0]) key = binascii.unhexlify(params[1].encode('utf-8')) storage_key = StorageKey(script_hash=script_hash, key=key) storage_item = Blockchain.Default().GetStorageItem(storage_key) if storage_item: return storage_item.Value.hex() return None elif method == "gettransactionheight": try: hash = UInt256.ParseString(params[0]) except Exception: # throws exception, not anything more specific raise JsonRpcError(-100, "Unknown transaction") tx, height = Blockchain.Default().GetTransaction(hash) if tx: return height else: raise JsonRpcError(-100, "Unknown transaction") elif method == "gettxout": hash = params[0].encode('utf-8') index = params[1] utxo = Blockchain.Default().GetUnspent(hash, index) if utxo: return utxo.ToJson(index) else: return None elif method == "invoke": shash = UInt160.ParseString(params[0]) contract_parameters = [ContractParameter.FromJson(p) for p in params[1]] sb = ScriptBuilder() sb.EmitAppCallWithJsonArgs(shash, contract_parameters) return self.get_invoke_result(sb.ToArray()) elif method == "invokefunction": contract_parameters = [] if len(params) > 2: contract_parameters = [ContractParameter.FromJson(p).ToVM() for p in params[2]] sb = ScriptBuilder() sb.EmitAppCallWithOperationAndArgs(UInt160.ParseString(params[0]), params[1], contract_parameters) return self.get_invoke_result(sb.ToArray()) elif method == "invokescript": script = params[0].encode('utf-8') return self.get_invoke_result(script) elif method == "sendrawtransaction": tx_script = binascii.unhexlify(params[0].encode('utf-8')) transaction = Transaction.DeserializeFromBufer(tx_script) # TODO: relay blocks, change to await in the future result = self.nodemgr.relay(transaction) return result elif method == "validateaddress": return self.validateaddress(params) elif method == "getpeers": return self.get_peers() elif method == "getbalance": if self.wallet: return self.get_balance(params) else: raise JsonRpcError(-400, "Access denied.") elif method == "getwalletheight": if self.wallet: return self.wallet.WalletHeight else: raise JsonRpcError(-400, "Access denied.") elif method == "listaddress": if self.wallet: return self.list_address() else: raise JsonRpcError(-400, "Access denied.") elif method == "getnewaddress": if self.wallet: keys = self.wallet.CreateKey() account = Account.get( PublicKeyHash=keys.PublicKeyHash.ToBytes() ) return account.contract_set[0].Address.ToString() else: raise JsonRpcError(-400, "Access denied.") elif method == "sendtoaddress": if self.wallet: contract_tx, fee = self.parse_send_to_address_params(params) return self.process_transaction(contract_tx=contract_tx, fee=fee) else: raise JsonRpcError(-400, "Access denied.") elif method == "sendfrom": if self.wallet: contract_tx, address_from, fee, change_addr = self.parse_send_from_params(params) return self.process_transaction(contract_tx=contract_tx, fee=fee, address_from=address_from, change_addr=change_addr) else: raise JsonRpcError(-400, "Access denied.") elif method == "sendmany": if self.wallet: contract_tx, fee, change_addr = self.parse_send_many_params(params) return self.process_transaction(contract_tx=contract_tx, fee=fee, change_addr=change_addr) else: raise JsonRpcError(-400, "Access denied.") elif method == "getblockheader": # this should work for either str or int blockheader = Blockchain.Default().GetHeaderBy(params[0]) if not blockheader: raise JsonRpcError(-100, "Unknown block") return self.get_blockheader_output(blockheader, params) raise JsonRpcError.methodNotFound()