Esempio n. 1
0
    def channel_htlc_unlock(self, arguments):
        """

        :param arguments:
        :return:
        """
        channel_name = get_arg(arguments, 1)
        hashcode = get_arg(arguments, 2)
        rcode = get_arg(arguments, 3)
        payment = get_arg(arguments, 4)
        payment = TrinityNumber(payment).number

        console_log.console(
            "Force to unlock htlc payment for channel {}".format(channel_name))
        if channel_name:
            channel_event = ChannelHtlcUnlockEvent(channel_name, True)
            channel_event.register_args(
                EnumEventAction.EVENT_EXECUTE,
                invoker_uri=self.Wallet.url,
                channel_name=channel_name,
                hashcode=hashcode,
                rcode=rcode,
                invoker_key=self.Wallet._key.private_key_string,
                is_debug=True)
            ws_instance.register_event(channel_event)
        else:
            console_log.warn("No Channel Create")

        return
Esempio n. 2
0
    def channel_force_close(self, arguments):
        """

        :param arguments:
        :return:
        """
        channel_name = get_arg(arguments, 1)
        nonce = get_arg(arguments, 2, True)
        is_debug = False
        if 'debug' in arguments:
            is_debug = True

        console_log.console("Force to close channel {}".format(channel_name))
        if channel_name:
            channel_event = ChannelForceSettleEvent(channel_name, True)
            channel_event.register_args(
                EnumEventAction.EVENT_EXECUTE,
                invoker_uri=self.Wallet.url,
                channel_name=channel_name,
                nonce=nonce,
                invoker_key=self.Wallet._key.private_key_string,
                is_debug=is_debug)
            ws_instance.register_event(channel_event)
        else:
            console_log.warn("No Channel Create")

        return
Esempio n. 3
0
    def channel_payment(self, arguments):
        """

        :param arguments:
        :return:
        """
        asset_type = get_arg(arguments, 1)
        if not check_support_asset_type(asset_type):
            console_log.error(
                "No support asset, current just support {}".format(
                    SupportAssetType.SupportAssetType))
            return None
        value = TrinityNumber(get_arg(arguments, 2).strip()).number
        if not value:
            console_log.error("command not give the count")
            return None
        try:
            if 0 >= value or not check_float_decimals(value, asset_type):
                console_log.error("value should not be less than 0")
                return None
        except ValueError:
            console_log.error("value format error")
            return None
        comments = " ".join(arguments[3:])
        comments = comments if comments else "None"
        if len(comments) > 128:
            console_log.error("comments length should be less than 128")
            return None
        try:
            hash_r, rcode = Payment.create_hr()
            Channel.add_payment(None, hash_r, rcode, value)
            paycode = Payment.generate_payment_code(self.Wallet.url,
                                                    asset_type, value, hash_r,
                                                    comments, True)
        except Exception as e:
            LOG.error(e)
            console_log.error("Get payment link error, please check the log")
            return None
        if self.qrcode:
            qrcode_terminal.draw(paycode, version=4)
        console_log.console(paycode)
        return None
Esempio n. 4
0
    def channel_qrcode(self, arguments):
        """

        :param arguments:
        :return:
        """
        enable = get_arg(arguments, 1)
        if enable.upper() not in ["ON", "OFF"]:
            console_log.error("should be on or off")
        self.qrcode = True if enable.upper() == "ON" else False
        console_log.console(
            "Qrcode opened") if self.qrcode else console_log.info(
                "Qrcode closed")
        return None
Esempio n. 5
0
    def channel_show(self, arguments):
        """

        :param arguments:
        :return:
        """
        subcommand = get_arg(arguments, 1)
        if not subcommand:
            self.help()
            return None
        if subcommand.upper() == "URI":
            console_log.console(self.Wallet.url)
        elif subcommand.upper() == "TRANS_HISTORY":
            channel_name = get_arg(arguments, 2)
            if channel_name is None:
                console_log.error("No provide channel")
                return None
            tx_his = Channel.batch_query_trade(channel_name)
            for tx in tx_his:
                console_log.console(tx)
            return None
        else:
            self.help()
        return None
Esempio n. 6
0
    def do_channel(self, arguments):
        """

        :param arguments:
        :return:
        """
        command = get_arg(arguments)
        channel_command = [i.split()[1] for i in self.user_commands]
        if command not in channel_command:
            console_log.error("no support command, please check the help")
            self.help()
            return

        if command == 'create':
            self.do_channel_create(arguments)

        elif command == "enable":
            if not self.enable_channel():
                self._channel_noopen()

        elif command == "tx":
            self.channel_trans(arguments)

        elif command == "qrcode":
            self.channel_qrcode(arguments)

        elif command == "close":
            self.channel_close(arguments)

        elif command == "force-close":
            self.channel_force_close(arguments)

        elif command == "htlc-unlock":
            self.channel_htlc_unlock(arguments)

        elif command == "peer":
            self.channel_peer(arguments)

        elif command == "payment":
            self.channel_payment(arguments)

        elif command == "show":
            self.channel_show(arguments)

        elif command == "deposit_limit":
            self.channel_deposit_limit(arguments)
        else:
            return None
Esempio n. 7
0
    def channel_close(self, arguments):
        """

        :param arguments:
        :return:
        """
        channel_name = get_arg(arguments, 1)

        console_log.console("Closing channel {}".format(channel_name))
        if channel_name:
            Channel.quick_close(channel_name,
                                wallet=self.Wallet,
                                cli=True,
                                trigger=SettleMessage.create)
        else:
            console_log.warn("No Channel Create")
Esempio n. 8
0
    def channel_peer(self, arguments):
        """

        :param arguments:
        :return:
        """
        arg_dic = {"state": None, "peer": None, "channel": None}
        for i in range(1, 4):
            arg = get_arg(arguments, i)
            if arg is None:
                continue
            try:
                k_v = arg.strip().split("=")
                if k_v[0] in arg_dic.keys():
                    arg_dic[k_v[0]] = k_v[1]
            except IndexError:
                continue
            else:
                continue

        Channel.get_channel_list(self.Wallet.url, **arg_dic)
        return
Esempio n. 9
0
    def channel_trans(self, arguments):
        """

        :param arguments:
        :return:
        """

        if len(arguments) == 2:
            # payment code
            pay_code = get_arg(arguments, 1)
            result, info = Payment.decode_payment_code(pay_code)
            if result:
                receiver = info.get("uri")
                net_magic = info.get('net_magic')
                if not net_magic or net_magic != str(get_magic()):
                    console_log.error("No correct net magic")
                    return None
                hashcode = info.get("hashcode")
                asset_type = info.get("asset_type")
                # asset_type = get_asset_type_name(asset_type)
                count = info.get("payment")
                comments = info.get("comments")
                console_log.info("will pay {} {} to {} comments {}".format(
                    TrinityNumber.convert_to_number(count), asset_type,
                    receiver, comments))
            else:
                console_log.error("The payment code is not correct")
                return
        else:
            receiver = get_arg(arguments, 1)
            asset_type = get_arg(arguments, 2)
            count = TrinityNumber(get_arg(arguments, 3).strip()).number
            hashcode = get_arg(arguments, 4)
            if not receiver or not asset_type or not count:
                self.help()
                return None

            asset_type = asset_type.upper() if check_support_asset_type(
                asset_type) else None
            if not asset_type:
                console_log.error(
                    "No support asset, current just support {}".format(
                        str(SupportAssetType.SupportAssetType)))
                return None

            if 0 >= count:
                console_log.warn('Not support negative number or zero.')
                return None

        # query channels by address
        channel_set = Channel.get_channel(self.Wallet.url, receiver,
                                          EnumChannelState.OPENED)
        if channel_set and channel_set[0]:
            Channel.transfer(channel_set[0].channel,
                             self.Wallet,
                             receiver,
                             asset_type,
                             count,
                             cli=True,
                             comments=hashcode,
                             trigger=RsmcMessage.create)
        else:
            if not hashcode:
                console_log.error("No hashcode")
                return None
            try:
                message = {
                    "MessageType": "GetRouterInfo",
                    "Sender": self.Wallet.url,
                    "Receiver": receiver,
                    "AssetType": asset_type,
                    "NetMagic": get_magic(),
                    "MessageBody": {
                        "AssetType": asset_type,
                        "Value": count
                    }
                }
                result = gate_way.get_router_info(message)
                routerinfo = json.loads(result.get("result"))
            except Exception as error:
                LOG.error(
                    'Exception occurred during get route info. Exception: {}'.
                    format(error))
                console_log.warning('No router was found.')
                return
            else:
                router = routerinfo.get("RouterInfo")
                if not router:
                    LOG.error('Router between {} and {} was not found.'.format(
                        self.Wallet.url, receiver))
                    console_log.error('Router not found for HTLC transfer.')
                    return

            full_path = router.get("FullPath")
            LOG.info("Get Router {}".format(str(full_path)))

            next_jump = router.get("Next")
            LOG.info("Get Next {}".format(str(next_jump)))
            fee_router = [
                i for i in full_path if i[0] not in (self.Wallet.url, receiver)
            ]
            if fee_router:
                # fee = reduce(lambda x, y:x+y,[TrinityNumber(str(i[1]).strip()).number for i in fee_router])
                fee = reduce(lambda x, y: x + y,
                             [float(i[1]) for i in fee_router])
            else:
                fee = 0

            fee = TrinityNumber(str(fee)).number
            count = int(count) + fee
            fee = fee / pow(10, 8)
            receiver = full_path[1][0]
            channel_set = Channel.get_channel(self.Wallet.url, receiver,
                                              EnumChannelState.OPENED)
            if not (channel_set and channel_set[0]):
                print('No OPENED channel was found for HTLC trade.')
                return
            LOG.info("Get Fee {}".format(fee))
            answer = prompt(
                "You will pay extra fee {}. Do you wish continue this transaction? [Yes/No]>"
                .format(fee))
            if answer.upper() in ["YES", "Y"]:
                channel_name = channel_set[0].channel
                Channel.transfer(channel_name,
                                 self.Wallet,
                                 receiver,
                                 asset_type,
                                 count,
                                 hashcode,
                                 router=full_path,
                                 next_jump=full_path[2][0],
                                 cli=True,
                                 trigger=HtlcMessage.create)

            else:
                return
Esempio n. 10
0
    def do_channel_create(self, arguments):
        """

        :return:
        """

        partner = get_arg(arguments, 1)
        if not is_correct_uri(partner):
            console_log.error("No correct uri format")
            return None

        asset_type = get_arg(arguments, 2)
        asset_type = asset_type.upper() if check_support_asset_type(
            asset_type) else None
        if not asset_type:
            console_log.error(
                "No support asset, current just support {}".format(
                    str(SupportAssetType.SupportAssetType)))
            return None

        try:
            deposit_limit = DepositAuth.deposit_limit()
            deposit_cmp = TrinityNumber(str(deposit_limit)).number
            deposit = TrinityNumber(get_arg(arguments, 3).strip()).number
            #when partner_deposit not given ,partner_deposit is equal to deposit
            if get_arg(arguments, 4) == None:
                partner_deposit = deposit
            elif TrinityNumber(get_arg(arguments, 4)).number == None:
                console_log.error("partner_deposit is not valid")
                return
            else:
                partner_deposit = TrinityNumber(get_arg(arguments, 4)).number

            if deposit_cmp > deposit:
                console_log.error(
                    "Founder's Deposit MUST be larger than {}".format(
                        deposit_limit))
                return None
            elif deposit_cmp > partner_deposit:
                console_log.error(
                    "Partner's Deposit should be larger than {}".format(
                        deposit_limit))
                return None
            elif partner_deposit > deposit:
                console_log.error(
                    "Founder's Deposit should not be less than Partner's")
                return None
        except ValueError:
            console_log.error(
                "Founder's Deposit should not be less than Partner'")
            return None

        if not check_partner(self.Wallet, partner):
            console_log.error(
                "Partner URI is not correct, Please check the partner uri")
            return None

        if not check_onchain_balance(self.Wallet.address, asset_type, deposit):
            console_log.error(
                "Now the balance on-chain is less than the deposit.")
            return None

        if not check_onchain_balance(partner.strip().split("@")[0], asset_type,
                                     partner_deposit):
            console_log.error(
                "Partner balance on chain is less than the deposit")
            return None

        Channel.create(self.Wallet,
                       self.Wallet.url,
                       partner,
                       asset_type,
                       deposit,
                       partner_deposit,
                       trigger=FounderMessage.create)