def parse_args(self):
        if len(self.senders) > 0:
            sender = self.senders[self.sender_combo.currentIndex()]
        else:
            sender = ''
        args = json.loads('[{}]'.format(self.args_e.text()))
        abi_index = self.abi_signatures[self.abi_combo.currentIndex()][0]
        if abi_index == -1:
            return None, [], sender
        abi = self.contract['interface'][abi_index]
        inputs = abi.get('inputs', [])
        if not len(args) == len(inputs):
            raise ParseArgsException(
                'invalid input count,expect {} got {}'.format(
                    len(inputs), len(args)))
        for index, _input in enumerate(inputs):
            _type = _input.get('type', '')
            if _type == 'address':
                addr = args[index]
                if is_address(addr):
                    __, hash160 = b58_address_to_hash160(addr)
                    addr = bh2u(hash160)
                if not is_hash160(addr):
                    raise ParseArgsException('invalid input:{}'.format(
                        args[index]))
                args[index] = addr.lower()
            elif 'int' in _type:
                if not isinstance(args[index], int):
                    raise ParseArgsException('inavlid input:{}'.format(
                        args[index]))
            elif _type == 'bytes':
                args[index] = bytes.fromhex(args[index])

        return abi, args, sender
 def parse_args(self):
     sender = None
     if len(self.senders) > 0:
         sender = self.senders[self.sender_combo.currentIndex()]
     if not sender:
         raise ParseArgsException('no sender selected')
     args = json.loads('[{}]'.format(self.args_e.text()))
     constructor = self.constructor
     inputs = constructor.get('inputs', [])
     if not len(args) == len(inputs):
         raise ParseArgsException(
             'invalid input count,expect {} got {}'.format(
                 len(inputs), len(args)))
     for index, _input in enumerate(inputs):
         _type = _input.get('type', '')
         if _type == 'address':
             addr = args[index]
             if is_address(addr):
                 __, hash160 = b58_address_to_hash160(addr)
                 addr = bh2u(hash160)
             if not is_hash160(addr):
                 raise ParseArgsException('invalid input:{}'.format(
                     args[index]))
             args[index] = addr.lower()
         elif 'int' in _type:
             if not isinstance(args[index], int):
                 raise ParseArgsException('invalid input:{}'.format(
                     args[index]))
         elif _type == 'bytes':
             args[index] = args[index].encode()
     return constructor, args, sender
    def do(self):
        try:
            staker = self.staker_e.text().strip()
            if not is_hash160(staker):
                try:
                    addr_type, staker = b58_address_to_hash160(staker)
                except BaseException:
                    raise Exception('invalid staker address')
                if addr_type != constants.net.ADDRTYPE_P2PKH:
                    raise Exception('invalid staker address')
                staker = staker.hex()

            fee = int(self.fee_e.text().strip())
            if fee < 0 or fee > 100:
                raise Exception('fee should between 0 and 100')

            addr = self.address_combo.currentText().strip()
            try:
                addr_type, self_h160 = b58_address_to_hash160(addr)
            except BaseException:
                raise Exception('invalid address')
            if len(
                    addr
            ) == 0 or addr not in self.addresses or addr_type != constants.net.ADDRTYPE_P2PKH:
                raise Exception('invalid address')
            if staker == self_h160.hex():
                raise Exception('cannot delegate to self')

            gas_limit, gas_price = self.parse_values()
            if gas_limit < 2250000:
                raise Exception('a minimum of 2,250,000 gas_limit is required')

            dele_exist = self.dele and self.dele.staker and self.dele.fee

            if self.mode == 'add' and dele_exist:
                self.dialog.parent().set_delegation(self.dele)
            elif self.mode in ['edit', 'add']:
                if dele_exist and self.staker_e.text().strip(
                ) == self.dele.staker and fee == self.dele.fee:
                    return
                if self.is_watching_only:
                    pod_str = self.pod_e.text().strip()
                    if not pod_str:
                        raise Exception('missing POD')
                    pod = base64.b64decode(pod_str)
                else:
                    pod = None
                self.dialog.parent().call_add_delegation(
                    addr, staker, fee, gas_limit, gas_price, self.dialog, pod)
            elif self.mode == 'undelegate':
                self.dialog.parent().call_remove_delegation(
                    addr, gas_limit, gas_price, self.dialog)

            self.dialog.reject()

        except (BaseException, ) as e:
            traceback.print_exc(file=sys.stderr)
            self.dialog.show_message(str(e))
 def save_input(self):
     try:
         contract_addr = self.contract_addr_e.text()
         bind_addr = self.addresses[self.address_combo.currentIndex()]
         if not is_hash160(contract_addr):
             raise Exception('invalid contrace address:{}'.format(contract_addr))
         self.callback(contract_addr, bind_addr)
         self.dialog.reject()
     except (BaseException,) as e:
         import traceback, sys
         traceback.print_exc(file=sys.stderr)
         self.dialog.show_message(str(e))
Пример #5
0
 def get_inputs(self):
     try:
         gas_limit, gas_price, amount = self.parse_values()
     except (BaseException, ) as e:
         raise e
     if self.token.balance < amount:
         raise Exception(_('token not enough'))
     address_to = self.address_to_e.text().rstrip().lstrip()
     if is_b58_address(address_to):
         addr_type, hash160 = b58_address_to_hash160(address_to)
         if addr_type == constants.net.ADDRTYPE_P2PKH:
             hash160 = bh2u(hash160)
         else:
             raise Exception(_('invalid address to send to'))
     elif is_hash160(address_to):
         hash160 = address_to.lower()
     else:
         raise Exception(_('invalid address to send to'))
     return hash160, amount, gas_limit, gas_price
    def parse_args(self):
        if len(self.senders) > 0:
            sender = self.sender_combo.currentText().strip()
            if sender not in self.senders:
                raise ParseArgsException('invalid sender address')
        else:
            sender = ''
        args_str = f'[{self.args_e.text()}]'.replace("\n", "")
        try:
            args = json.loads(args_str)
        except BaseException as e:
            raise ParseArgsException(f"json decode error {e} for {args_str}")
        abi_index = self.abi_signatures[self.abi_combo.currentIndex()][0]
        if abi_index == -1:
            return None, [], sender
        abi = self.contract['interface'][abi_index]
        inputs = abi.get('inputs', [])
        if not len(args) == len(inputs):
            raise ParseArgsException(
                'invalid input count,expect {} got {}'.format(
                    len(inputs), len(args)))
        for index, _input in enumerate(inputs):
            _type = _input.get('type', '')
            if _type == 'address':
                addr = args[index]
                if is_address(addr):
                    __, hash160 = b58_address_to_hash160(addr)
                    addr = hash160.hex()
                if addr.startswith("0x"):
                    addr = addr[2:]
                if not is_hash160(addr):
                    raise ParseArgsException('invalid input:{}'.format(
                        args[index]))
                args[index] = addr.lower()
            elif 'int' in _type:
                if not isinstance(args[index], int):
                    raise ParseArgsException('inavlid input:{}'.format(
                        args[index]))
            elif _type == 'bytes':
                args[index] = bytes.fromhex(args[index])

        return abi, args, sender
Пример #7
0
 def do_send(self):
     from electrum.bitcoin import is_p2pkh, is_hash160, b58_address_to_hash160, bh2u, TYPE_SCRIPT
     from electrum.transaction import opcodes, contract_script, PartialTxOutput
     address = str(self.to_addr)
     if not address:
         self.app.show_error(
             _('Recipient not specified.') + ' ' +
             _('Please scan a Bitcoin address or a payment request'))
         return
     if is_p2pkh(address):
         addr_type, hash160 = b58_address_to_hash160(address)
         hash160 = bh2u(hash160)
     elif is_hash160(address):
         hash160 = address.lower()
     else:
         self.app.show_error(_('Invalid Bitcoin Address') + ':\n' + address)
         return
     if address == self.bind_addr:
         self.app.show_error(_('You can not send to bind address!'))
         return
     try:
         amount = self.app.get_token_amount(self.amount, self.symbol,
                                            self.decimals)
     except:
         self.app.show_error(
             _('Invalid amount') + ':\n' + self.screen.amount)
         return
     if self.balance < amount:
         self.app.show_error(_('token not enough'))
         return
     datahex = 'a9059cbb{}{:064x}'.format(hash160.zfill(64), amount)
     tx_desc = _('Pay out {} {}').format(amount / (10**self.decimals),
                                         self.symbol)
     gas_limit = int(self.gas_limit)
     gas_price = int(float(self.gas_price) * (10**8))
     script = contract_script(gas_limit, gas_price, datahex,
                              self.contract_addr, opcodes.OP_CALL)
     outputs = [PartialTxOutput(scriptpubkey=script, value=0)]
     amount = sum(map(lambda x: x[2], outputs))
     self._do_send(amount, tx_desc, outputs, gas_limit * gas_price)
Пример #8
0
    def do(self):
        try:
            staker = self.staker_e.text()
            if not is_hash160(staker):
                addr_type, staker = b58_address_to_hash160(staker)
                if addr_type != constants.net.ADDRTYPE_P2PKH:
                    raise Exception('wrong staker address')
                staker = bh2u(staker)

            fee = int(self.fee_e.text())
            if fee < 0 or fee > 100:
                raise Exception('fee should between 0 and 100')

            addr = self.addresses[self.address_combo.currentIndex()]
            if not addr:
                raise Exception('please select a address')

            gas_limit, gas_price = self.parse_values()
            if gas_limit < 2250000:
                raise Exception('a minimum of 2,250,000 gas_limit is required')

            dele_exist = self.dele and self.dele.staker and self.dele.fee

            if self.mode == 'add' and dele_exist:
                self.dialog.parent().set_delegation(self.dele)
            elif self.mode in ['edit', 'add']:
                if dele_exist and self.staker_e.text(
                ) == self.dele.staker and fee == self.dele.fee:
                    return
                self.dialog.parent().call_add_delegation(
                    addr, staker, fee, gas_limit, gas_price, self.dialog)
            elif self.mode == 'undelegate':
                self.dialog.parent().call_remove_delegation(
                    addr, gas_limit, gas_price, self.dialog)

            self.dialog.reject()

        except (BaseException, ) as e:
            traceback.print_exc(file=sys.stderr)
            self.dialog.show_message(str(e))
 def save_input(self):
     interface_text = self.interface_e.text()
     try:
         interface = json.loads(interface_text)
     except json.JSONDecodeError as e:
         self.dialog.show_message(_('invalid interface') + ' {}'.format(e))
         return
     address = self.address_e.text()
     address = address.rstrip().lstrip()
     if not is_hash160(address):
         self.dialog.show_message(_('invalid contract address'))
         return
     name = self.name_e.text()
     name = name.rstrip().lstrip()
     if len(name) > 32:
         self.dialog.show_message(_('name too long'))
         return
     if not name:
         self.dialog.show_message(_('empty name not allowed'))
         return
     self.contract['interface'] = interface
     self.contract['address'] = address
     self.contract['name'] = name
     self.callback(self.contract)