def get(self, callback = None):
     accounts = []
     # Account Types
     standard = []
     multisig = []
     atomicswap = []
     for account in Accounts.getAccounts():
         confirmedBalance = Accounts.getConfirmedBalanceByAddress(account.address)
         confirmedBalance = Units.toValue(confirmedBalance)
         confirmedBalance = DataType.asFloat(confirmedBalance)
         address = Address.to0xAddress(account.address)
         accountInfo = {
             'address': address,
             'type': account.type,
             'balance': confirmedBalance
         }
         if account.type == AccountType.STANDARD:
             standard.append(accountInfo)
         elif account.type == AccountType.MULTISIGNATURE:
             multisig.append(accountInfo)
         elif account.type == AccountType.ATOMIC_SWAP:
             atomicswap.append(accountInfo)
     accounts.extend(standard)
     accounts.extend(multisig)
     accounts.extend(atomicswap)
     callback(
         JSONRPC.createResultObject(accounts, self.id)
     )
Beispiel #2
0
 def get(self, callback=None):
     contracts = []
     for contract in Contracts.getContracts():
         confirmedBalance = 0
         unspentTransactionScript = UXTO.getUnspentTransactionScript(
             contract.address)
         if unspentTransactionScript != None:
             txOut = unspentTransactionScript.output
             confirmedBalance = txOut.value
         confirmedBalance = Units.toValue(confirmedBalance)
         confirmedBalance = DataType.asFloat(confirmedBalance)
         address = Address.to0xAddress(contract.address)
         contracts.append({'address': address, 'balance': confirmedBalance})
     callback(JSONRPC.createResultObject(contracts, self.id))
Beispiel #3
0
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)