def handleLocalScript(self, fromAccount, output, deploy): gasLimit = Config.getIntValue('TRANSACTION_LOCAL_GAS_LIMIT') gasPrice = Config.getIntValue('TRANSACTION_LOCAL_GAS_PRICE') localTx = Transaction(gasLimit, gasPrice) localTx.gasRemaining = Config.getIntValue( 'TRANSACTION_LOCAL_GAS_REMAINING') _input = None if deploy: pass else: scriptData = [] scriptData.append(DataType.zeroFillArray(0, 32)) scriptData.append(DataType.zeroFillArray(0, 32)) scriptData.append(fromAccount.address) scriptData.extend(self.parameters) _input = Input(Config.getValue('SCRIPT_TRANSACTION_ID'), Config.getIntValue('SCRIPT_OUTPUT_INDEX')) _input.witness = scriptData if RVM.run(localTx, _input, output, True, deploy, True): if len(localTx.internalOutputs) > 0: internalOutput = localTx.internalOutputs[-1] if deploy: PersistentStorage.add(internalOutput.address, internalOutput, True) result = 'Script Deployed Locally' else: result = internalOutput.script try: result = DataType.toHex(result) result = DataType.asInt(result, 16) except ValueError: pass return result return None
def jumpi(self): destination = self.popAsInt() cond = self.pop() if cond != 0: self.pc = DataType.asInt(destination, 16) if self.JUMPDEST != self.code[self.pc]: self.pc = len(self.code) self.invalid = True
def do_POST(self): if self.hasAuthorization(): length = DataType.asInt(self.headers['Content-Length']) payload = self.rfile.read(length) payload = DataType.deserialize(payload) payload = json.loads(payload) self.handlePayload(payload) else: response = JSONRPC.createErrorObject( -32010, 'unauthorized', 'you are not authorized to access this resource', None) self.writeResponse(response, doHead=False)
def _sendData(host, data, hasPayload): try: if ':' in host: host = host.split(':') host[1] = DataType.asInt(host[1]) host = tuple(host) else: host = (host, getSocketPort()) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect(host) sock.settimeout(Config.getIntValue("CONNECTION_TIMEOUT")) data = DataType.serialize(data) dataLen = len(data) dataLen = DataType.intToBytes( dataLen, Config.getIntValue("SOCKET_HEADER_BUFFER_SIZE")) sock.sendall(dataLen) sock.sendall(data) if hasPayload: payload = getSocketPayload(sock) return RLP.decode(payload) except IOError: Log.error('Unable to send data to host: %s data: %s' % (host, data)) return None
def initialize(self): self.code = self.output.script[1:] parameters = [] if self.deploy: parameters = [] if self.output.extraData == None else self.output.extraData else: if self.output.hasExtScript(): parameters.extend(self.input.witness[3:]) else: self.stack.extend(self.input.witness) methodHash = False if self.deploy else True for parameter in parameters: if not isinstance(parameter, bytes): try: if methodHash: parameter = DataType.fromHex(parameter) parameter = DataType.serialize(parameter) methodHash = False else: parameter = DataType.asInt(parameter) except ValueError: try: parameter = DataType.fromHex(parameter) parameter = DataType.serialize(parameter) except ValueError: parameter = Address.toAddressBytes(parameter) if self.deploy: self.code += DataType.zeroFillArray(parameter, 32) else: if len(self.data) == 0: self.data += parameter else: self.data += DataType.zeroFillArray(parameter, 32) if len(self.data) > 0 and len(self.data) < 32: fillLen = 32 - len(self.data) self.data.extend(fillLen * b'\x00')
def handleCommand(command, parameters): id = 1 commands = [] if command == 'CreateAtomicSwapAccount': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'CreateMultiSigAccount': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'GetAccounts': commands.append(JSONRPC.createMethodObject(command, [], id)) elif command == 'GetNewAccount': commands.append(JSONRPC.createMethodObject(command, [], id)) elif command == 'DeleteAccounts': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'SendTransaction': fromAddress = parameters[0] toAddress = parameters[1] value = DataType.asFloat(parameters[2]) gasLimit = DataType.asInt(parameters[3]) gasPrice = DataType.asFloat(parameters[4]) params = { 'fromAddress': fromAddress, 'toAddress': toAddress, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'SendTransactionToMultiSig': fromAddress = parameters[0] threshold = DataType.asInt(parameters[1]) toMultiSigAddress = parameters[2] value = DataType.asFloat(parameters[3]) gasLimit = DataType.asInt(parameters[4]) gasPrice = DataType.asFloat(parameters[5]) params = { 'fromAddress': fromAddress, 'threshold': threshold, 'toAddress': toMultiSigAddress, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'SendTransactionFromMultiSig': fromAddress = parameters[0] publicKeys = [] signatures = [] idx = 0 for parameter in parameters[1:-5]: if idx % 2 == 0: publicKeys.append(parameter) else: signatures.append(parameter) idx += 1 threshold = DataType.asInt(parameters[-5]) toAddress = parameters[-4] value = DataType.asFloat(parameters[-3]) gasLimit = DataType.asInt(parameters[-2]) gasPrice = DataType.asFloat(parameters[-1]) params = { 'fromAddress': fromAddress, 'publicKeys': publicKeys, 'signatures': signatures, 'threshold': threshold, 'toAddress': toAddress, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'SignMultiSigOutput': multiSigAddress = parameters[0] threshold = DataType.asInt(parameters[1]) toAddress = parameters[2] value = DataType.asFloat(parameters[3]) gasLimit = DataType.asInt(parameters[4]) gasPrice = DataType.asFloat(parameters[5]) params = { 'fromAddress': multiSigAddress, 'threshold': threshold, 'toAddress': toAddress, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'GetMiningWorker': address = parameters[0] enabled = DataType.asBool(parameters[1]) params = {'address': address, 'enabled': enabled} commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'CreateAtomicSwapTx': fromAddress = parameters[0] threshold = DataType.asInt(parameters[1]) toAddress = parameters[2] value = DataType.asFloat(parameters[3]) gasLimit = DataType.asInt(parameters[4]) gasPrice = DataType.asFloat(parameters[5]) params = { 'fromAddress': fromAddress, 'threshold': threshold, 'toAddress': toAddress, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'SendAtomicSwapTx': fromAddress = parameters[0] toAddress = parameters[1] value = DataType.asFloat(parameters[2]) gasLimit = DataType.asInt(parameters[3]) gasPrice = DataType.asFloat(parameters[4]) params = { 'fromAddress': fromAddress, 'toAddress': toAddress, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'SignAtomicSwapTx': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'WatchContracts': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'DeleteContracts': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'GetContracts': commands.append(JSONRPC.createMethodObject(command, [], id)) elif command == 'GetScriptLogs': fromAddress = parameters[0] scriptAddress = parameters[1] value = DataType.asFloat(parameters[2]) gasLimit = DataType.asInt(parameters[3]) gasPrice = DataType.asFloat(parameters[4]) params = { 'fromAddress': fromAddress, 'script': scriptAddress, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'DeployScript' or command == 'DeployLocalScript': fromAddress = parameters[0] with open(parameters[1], 'r') as scriptFile: script = scriptFile.read() _parameters = parameters[2:-3] value = DataType.asFloat(parameters[-3]) gasLimit = DataType.asInt(parameters[-2]) gasPrice = DataType.asFloat(parameters[-1]) params = { 'fromAddress': fromAddress, 'script': script, 'parameters': _parameters, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'CallTxScript' or command == 'CallLocalScript': fromAddress = parameters[0] scriptAddress = parameters[1] _parameters = parameters[2:-3] if _parameters and len(_parameters) > 0: _parameters[0] = DataType.serialize(_parameters[0]) hashFunction = keccak.new(data=_parameters[0], digest_bits=256) methodHash = hashFunction.hexdigest() _parameters[0] = methodHash[0:8] value = DataType.asFloat(parameters[-3]) gasLimit = DataType.asInt(parameters[-2]) gasPrice = DataType.asFloat(parameters[-1]) params = { 'fromAddress': fromAddress, 'script': scriptAddress, 'parameters': _parameters, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'WatchSideChain': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'DeleteSideChains': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'GetSideChains': commands.append(JSONRPC.createMethodObject(command, [], id)) elif command == 'DeploySideChain': fromAddress = parameters[0] parameters = parameters[1] value = DataType.asFloat(parameters[2]) gasLimit = DataType.asInt(parameters[3]) gasPrice = DataType.asFloat(parameters[4]) params = { 'fromAddress': fromAddress, 'parameters': parameters, 'value': value, 'gasLimit': gasLimit, 'gasPrice': gasPrice } commands.append(JSONRPC.createMethodObject(command, params, id)) elif command == 'GetPeers': commands.append(JSONRPC.createMethodObject(command, [], id)) elif command == 'AddPeers': commands.append(JSONRPC.createMethodObject(command, parameters, id)) elif command == 'DeletePeers': commands.append(JSONRPC.createMethodObject(command, parameters, id)) else: raise ValueError('Unsupported command: ' + command) url = '%s:%s' % (Http.getNodeHostname(), Http.getHttpPort()) for cmd in commands: cmd = json.dumps(cmd) response = Http.send(url, cmd, doBasicAuth=True) if response == None or len(response) == 0: print('Empty response from ', url, 'request', cmd) continue response = json.loads(response) value = '\n' if 'result' in response: result = response['result'] if isinstance(result, list): for item in result: if isinstance(item, dict): for v in item.values(): value += ('%s\t' % v) value += '\n' else: value += item value += '\n' else: value += DataType.asString(result) value += '\n' elif 'error' in response: error = response['error'] code = error['code'] message = error['message'] data = error['data'] value += '%s %s %s' % (code, message, data) value += '\n' print(value)
def getIntValue(key, base=10): return DataType.asInt(Config.config[key], base)