Example #1
0
 def __init__(self, ch_own):
     self.ch_own = ch_own
     self.ID_god = ch_own.ID_own
     self.type = G.ROOT
     self.hash_content = Hash.sha(G.ID_REAL_GOD)
     self.i_m = 0
     self.coin_rest = 0
     self.i_ch_m_pre = 0
     self.i_ch = 0
     self.hash_pre = Hash.sha(G.ID_REAL_GOD)
Example #2
0
    def rx_check(self, card_list):
        '''
        different card_types have different checks:
        check the valid of hash and sign of the card
        Note that in trade, there is no complete trade card to tranceive,
        so G.P_PUB_KEY to verify is ok to cover all trade card
        '''
        G.LOGGER.debug('')

        # valid the type of the card
        if card_list[0] != self.activity['card_type']:
            G.LOGGER.error('Card type is wrong.')
            return False

        if card_list[1] not in self.activity['card_subtype']:
            G.LOGGER.error('Card subtype is wrong.')
            return False

        # valid the card of the hash and sign
        hash_content = b' '.join(card_list[:-1])
        hash_value = card_list[-1]
        if Hash.sha256(hash_content) != hash_value:
            G.LOGGER.warn('Hash of the card is wrong.')
            return False

        sign_content = b' '.join(card_list[:-2])
        sign_value = card_list[-2]
        if not Ecc.verify(sign_content, G.P_PUB_KEY):
            G.LOGGER.error('Sign of the card is wrong.')
            return False

        return True
Example #3
0
    def earn(self, c_pay):
        T.LOGGER.debug('')

        # check the version
        if c_pay[G.P_VER] == G.VER:  # 0
            # check the sign
            if not Ecc.verify(c_pay, c_pay[G.P_ID_PAY]):  # -2
                T.LOGGER.error('Sign of the close pay card is wrong.')
                return False
            # check the mutual part, ignore others for quick trade, leave for watch
            # trade coin: 6 is leaved for ui to check
            if (c_pay[G.P_TYPE] == G.PAY and  # 2
                    c_pay[G.P_ID_PAY] == self.ID_pay and  # 3
                    c_pay[G.P_ID_EARN] == self.ID_earn and  # 4
                    c_pay[G.P_I_M] == self.i_m_earn):  # 5

                c_pay[G.P_TYPE] = G.EARN
                c = (
                    b' '.join(c_pay) + b' ' + self.coin_rest + b' ' +  # -6
                    self.i_ch_earn_pre + b' ' +  # -5
                    self.i_ch + b' ' +  # -4
                    self.hash_pre  # -3
                )

                sign_c = Ecc.sign(c, self.ch_own.pri_key_own)  # -2
                hash_c = Hash.sha(c + b' ' + sign_c)  # -1
                c_auth = c + b' ' + sign_c + b' ' + hash_c
                return c_auth, hash_c
Example #4
0
    async def charge(self):
        T.LOGGER.debug('')

        # charge coin: -7
        self.coin_charge = G.COIN_CREDIT

        if not await init(self, G.CHARGE):
            return False
        if self.i_m <= self.i_m_guide:
            return self.i_ch_fetch  # TODO: only consider this case now
        T.LOGGER.debug('init success')

        c = (b' '.join(self.c_rx_list[:G.P_HASH]) + b' ' +
             base58.b58encode_int(self.coin_charge) + b' ' +
             base58.b58encode_int(self.coin_rest) + b' ' +
             base58.b58encode_int(self.i_ch_m_pre) + b' ' +
             base58.b58encode_int(self.ch_own.i_ch_next) + b' ' +
             self.hash_pre)
        sign_c = Ecc.sign(c, self.ch_own.pri_key)
        hash_c = Hash.sha(c + b' ' + sign_c)
        c_auth = c + b' ' + sign_c + b' ' + hash_c  # to TX all card

        content_ack = (
            base58.b58encode_int(self.coin_charge) + b' ' +
            base58.b58encode_int(self.coin_rest) + b' ' +
            base58.b58encode_int(self.i_ch_m_pre) + b' ' +
            base58.b58encode_int(self.ch_own.i_ch_next) + b' ' +
            self.hash_pre + b' ' + sign_c + b' ' +
            hash_c  # to do, is it need to be TX
        )
        c_ack_auth = ack_god(self.ch_own, content_ack,
                             self.c_rx_list[G.P_HASH])  # to TX part card

        T.LOGGER.debug('c_auth success')
        return c_auth
Example #5
0
    async def run(self):
        if self.c_list[G.P_TYPE] == G.DEMAND:
            ID_earn = self.c_list[G.P_ID_DEMAND]
            ID_pay = self.c_list[G.P_ID_DEMANDED]
            i_m_pay = self.c_list[G.P_I]  # for ch_pay
            ch_pay = Chain(ID_pay, T.FO_CH)
            # fetch demand card
            c_fetch = await ch_pay.fetch_m(ID_earn, i_m_pay)
            # c_fetch_list = c_fetch.split()
            c_ack = G.ACK + b' ' +\
                    base58.b58encode_int(int(time())) + b' ' +\
                    self.ID_ack + b' ' +\
                    self.c_list[G.P_HASH] + b' ' +\
                    c_fetch

        elif self.c_list[G.P_TYPE] == G.PAY:
            ID_pay = self.c_list[G.P_ID_PAY]
            ch_pay = Chain(ID_pay, T.FO_CH)
            # add pay card into chain and update guide
            await ch_pay.append(self.c_list)

            c_ack = G.ACK + b' ' +\
                   base58.b58encode_int(int(time())) + b' ' +\
                   self.ID_ack + b' ' +\
                   self.c_list[G.P_HASH]

        elif self.c_list[G.P_TYPE] == G.EARN:
            ID_earn = self.c_list[G.P_ID_EARN]
            ch_earn = Chain(ID_earn, T.FO_CH)
            # add earn card into chain and update guide
            await ch_earn.append(self.c_list)

            c_ack = G.ACK + b' ' +\
                   base58.b58encode_int(int(time())) + b' ' +\
                   self.ID_ack + b' ' +\
                   self.c_list[G.P_HASH]
        elif self.c_list[G.P_TYPE] == G.WATCH:
            pass
        elif self.c_list[G.P_TYPE] == G.POST:
            pass
        else:
            T.LOGGER.error(b'There should not be c_type: ' +
                           self.c_list[G.P_TYPE])
            return False

        c_ack_sign = Ecc.sign(c_ack)
        c_ack_hash = Hash.sha(c_ack + b' ' + c_ack_sign)
        c_ack_auth = c_ack + b' ' + c_ack_sign + b' ' + c_ack_hash

        return c_ack_auth
Example #6
0
    async def demand(self):
        T.LOGGER.debug('')

        await self.init()

        type_c = G.DEMAND
        c = G.VER + b' ' +\
                base58.b58encode_int(int(time())) + b' ' +\
                type_c + b' ' +\
                self.ID_earn + b' ' +\
                self.ID_pay + b' ' +\
                self.i_m_earn

        sign_c = Ecc.sign(c, self.ch_own.pri_key_own)
        hash_c = Hash.sha(c + b' ' + sign_c)
        c_auth = c + b' ' + sign_c + b' ' + hash_c
        return c_auth, hash_c
Example #7
0
def ack_god(ch_god, content, hash_src):
    T.LOGGER.debug('')

    type_c = G.ACK

    c = (
        G.VER + b' ' +  # 0
        base58.b58encode_int(int(time())) + b' ' +  # 1
        type_c + b' ' +  # 2
        ch_god.ID_own + b' ' +  # 3
        hash_src + b' ' +  # 4                
        content + b' '  # 5
    )

    sign_c = Ecc.sign(c, ch_god.pri_key)
    hash_c = Hash.sha(c + b' ' + sign_c)
    c_auth = c + b' ' + sign_c + b' ' + hash_c
    return c_auth
Example #8
0
    def act_group(self, c_sign_god):
        T.LOGGER.debug('')

        init_ch(self)
        c_node = (
            b' '.join(c_sign_god) + b' ' +  # 
            base58.b58encode_int(self.coin_rest) + b' ' +  # -6
            base58.b58encode_int(self.i_ch_m_pre) + b' ' +  # -5
            base58.b58encode_int(self.i_ch) + b' ' +  # -4
            self.hash_pre  # -3
        )
        T.LOGGER.debug('')
        sign_c_node = Ecc.sign(c_node, self.ch_own.pri_key)  # -2
        hash_c_node = Hash.sha(c_node + b' ' + sign_c_node)  # -1
        c_auth_node = c_node + b' ' + sign_c_node + b' ' + hash_c_node

        T.LOGGER.debug('complete')
        return c_auth_node
Example #9
0
    async def post(self):
        T.LOGGER.debug('')

        if not await init(self, G.POST):
            return False
        if self.i_m <= self.i_m_guide:
            return self.i_ch_fetch  # TODO: only consider this case now
        T.LOGGER.debug('init success')

        c = (b' '.join(self.c_rx_list[:G.P_HASH]) + b' ' +
             base58.b58encode_int(self.coin_rest) + b' ' +
             base58.b58encode_int(self.i_ch_m_pre) + b' ' +
             base58.b58encode_int(self.ch_own.i_ch_next) + b' ' +
             self.hash_pre)
        sign_c = Ecc.sign(c, self.ch_own.pri_key)
        hash_c = Hash.sha(c + b' ' + sign_c)
        c_auth = c + b' ' + sign_c + b' ' + hash_c  # to TX all card

        return c_auth
Example #10
0
    async def act_god(self, hash_content):
        T.LOGGER.debug('')

        await init(self, self.ID_god)
        c = (
            G.VER + b' ' +  # 0
            base58.b58encode_int(int(time())) + b' ' +  # 1
            self.type + b' ' +  # 2
            self.ID_god + b' ' +  # 3
            self.ID_post + b' ' +  # 4                
            base58.b58encode_int(self.i_m) + b' ' +  # 5
            hash_content  # 6
        )

        sign_c = Ecc.sign(c, self.ch_own.pri_key)  # -2
        hash_c = Hash.sha(c + b' ' + sign_c)  # -1

        c_auth = c + b' ' + sign_c + b' ' + hash_c
        T.LOGGER.debug(c_auth)
        return c_auth, hash_c
Example #11
0
    def root(self):
        T.LOGGER.debug('')

        c_root = (
            G.VER + b' ' +  # 0
            base58.b58encode_int(int(time())) + b' ' +  # 1
            self.type + b' ' +  # 2
            self.ID_god + b' ' +  # 3
            self.ID_god + b' ' +  # 4
            base58.b58encode_int(self.i_m) + b' ' +  # 5
            self.hash_content + b' ' +  # 6
            base58.b58encode_int(self.coin_rest) + b' ' +  # -6
            base58.b58encode_int(self.i_ch_m_pre) + b' ' +  # -5
            base58.b58encode_int(self.i_ch) + b' ' +  # -4
            self.hash_pre  # -3
        )
        sign_c_root = Ecc.sign(c_root, self.ch_own.pri_key)  # -2
        hash_c_root = Hash.sha(c_root + b' ' + sign_c_root)  # -1
        c_root_auth = c_root + b' ' + sign_c_root + b' ' + hash_c_root

        return c_root_auth
Example #12
0
    async def redeem(self):
        T.LOGGER.debug('')

        if not await self.init():
            return False

        c = (b' '.join(self.c_rx_list[:G.P_HASH]) + b' ' +
             base58.b58encode_int(self.coin_charge) + b' ' +
             base58.b58encode_int(self.coin_rest) + b' ' +
             base58.b58encode_int(self.i_ch_m_pre) + b' ' +
             base58.b58encode_int(self.ch_own.i_ch_next) + b' ' +
             self.hash_pre)

        sign_c = Ecc.sign(c, self.ch_own.pri_key)
        hash_c = Hash.sha(c + b' ' + sign_c)
        c_auth = c + b' ' + sign_c + b' ' + hash_c

        # add pay card into chain and update guide
        await self.ch_own.append(c_auth)

        return c_auth
Example #13
0
def check_rx(c_list):
    '''
    different c_types have different checks:
    check the valid of hash and sign of the card
    
    '''
    T.LOGGER.debug(sys._getframe().f_code.co_name)

    if c_list[G.P_VER] != G.VER:
        T.LOGGER.error('Card version is wrong.')
        return False

    # # valid the type of the card
    # if c_list[G.P_TYPE] != c_type:
    #     T.LOGGER.error('Card type is wrong.')
    #     return False

    # valid hash
    content_hash = b' '.join(c_list[:G.P_HASH])
    value_hash = c_list[G.P_HASH]
    if Hash.sha(content_hash) != value_hash:
        T.LOGGER.warn('Hash of the card is wrong.')
        return False

    # valid sign
    c_sign = content_hash
    if c_list[G.P_SUBTYPE] == G.EARN:
        ID = c_list[G.P_ID_EARN]
    elif c_list[G.P_SUBTYPE] == G.PAY:
        ID = c_list[G.P_ID_PAY]
    else:
        ID = c_list[G.P_ID]

    if not Ecc.verify(c_sign, ID):
        T.LOGGER.error('Sign of the card is wrong.')
        return False

    return True
Example #14
0
def check_rx(c_list, hash_src=None):
    '''
    check the valid of hash and sign of the card
    '''
    T.LOGGER.debug('')

    if c_list[G.P_VER] != G.VER:
        T.LOGGER.error('Card version is wrong.')
        return False

    # valid hash
    content_hash = b' '.join(c_list[:G.P_HASH])
    value_hash = c_list[G.P_HASH]
    if Hash.sha(content_hash) != value_hash:
        T.LOGGER.warn('Hash of the card is wrong.')
        return False

    # valid sign
    content_sign = b' '.join(c_list[:G.P_SIGN])
    value_sign = c_list[G.P_SIGN]
    if c_list[G.P_TYPE] == G.EARN:
        ID = c_list[G.P_ID_EARN]
    elif c_list[G.P_TYPE] == G.PAY:
        ID = c_list[G.P_ID_PAY]
    elif c_list[G.P_TYPE] == G.CHARGE or c_list[G.P_TYPE] == G.POST:
        ID = c_list[G.P_ID_GOD]
    elif c_list[G.P_TYPE] == G.ACK:
        ID = c_list[G.P_ID_ACK]
        # check if it is the source card ack
        if c_list[G.P_HASH_SRC] != hash_src:
            T.LOGGER.error('Not the corresponding acker.')
            return False

    if not Ecc.verify(content_sign, value_sign, ID):
        T.LOGGER.error('Sign of the card is wrong.')
        return False

    return True
Example #15
0
def check_rx(c_list):
    '''
    different c_types have different checks:
    check the valid of hash and sign of the card
    
    '''
    T.LOGGER.debug('')

    if c_list[G.P_VER] != G.VER:
        T.LOGGER.error('Card version is wrong.')
        return False

    # # valid the type of the card
    # if c_list[G.P_TYPE] != c_type:
    #     T.LOGGER.error('Card type is wrong.')
    #     return False

    # valid hash
    content_hash = b' '.join(c_list[:G.P_HASH])
    value_hash = c_list[G.P_HASH]
    T.LOGGER.debug(content_hash)
    T.LOGGER.debug(value_hash)
    if Hash.sha(content_hash) != value_hash:
        T.LOGGER.warn('Hash of the card is wrong.')
        return False
    T.LOGGER.debug('hash success')

    # valid sign
    content_sign = b' '.join(c_list[:G.P_SIGN])
    value_sign = c_list[G.P_SIGN]
    ID = c_list[G.P_ID_NODE]

    if not Ecc.verify(content_sign, value_sign, ID):
        T.LOGGER.error('Sign of the card is wrong.')
        return False

    T.LOGGER.debug('check_rx success')
    return True
Example #16
0
    async def pay(self, coin_pay):
        T.LOGGER.debug('')

        await self.init()
        c = (
            G.VER + b' ' +  # 0
            base58.b58encode_int(int(time())) + b' ' +  # 1
            self.type + b' ' +  # 2
            self.ID_pay + b' ' +  # 3
            self.ID_earn + b' ' +  # 4
            self.i_m_pay + b' ' +  # 5
            coin_pay + b' ' +  # 6
            self.coin_rest + b' ' +  # -6
            self.i_ch_pay_pre + b' ' +  # -5
            self.i_ch + b' ' +  # -4
            self.hash_pre  # -3
        )

        sign_c = Ecc.sign(c, self.ch_own.pri_key_own)  # -2
        hash_c = Hash.sha(c + b' ' + sign_c)  # -1

        c_auth = c + b' ' + sign_c + b' ' + hash_c
        return c_auth, hash_c
Example #17
0
    async def ack(self):
        T.LOGGER.debug('')

        if not await self.init():
            return False

        ID_earn = self.c_rx_list[G.P_ID_DEMAND]
        ID_pay = self.c_rx_list[G.P_ID_DEMANDED]
        i_m_pay = self.c_rx_list[G.P_I]  # for ch_pay
        ch_pay = Chain(ID_pay, T.FO_CH)
        # fetch demand card
        c_fetch = await ch_pay.fetch_m(ID_earn, i_m_pay)
        # c_fetch_list = c_fetch.split()
        c = (G.ACK + b' ' +\
                base58.b58encode_int(int(time())) + b' ' +
                self.ID_ack + b' ' +
                self.c_rx_list[G.P_HASH] + b' ' +
                c_fetch)
        sign_c = Ecc.sign(c, self.ch_own.pri_key)
        hash_c = Hash.sha(c + b' ' + sign_c)
        c_auth = c + b' ' + sign_c + b' ' + hash_c

        return c_auth
Example #18
0
    async def regist(self):
        T.LOGGER.debug('')

        name_str = self.regist_name_entry.get()
        ID_real_str = self.regist_ID_real_entry.get()
        if (not name_str) or (not ID_real_str):
            self.regist_status.set('input name/ID, retry')
            return

        row_god = await Credit.search(T.TA_FREE, G.NAME_GOD)
        if not row_god:
            self.regist_status.set('god is not added')
            return

        name = name_str.encode()
        hash_ID_real = Hash.sha(ID_real_str.encode())

        if not await handle.regist(self, name, hash_ID_real, row_god):
            self.regist_status.set('regist fail, retry')
            return

        self.regist_button.configure(state='disable')
        self.login_button.configure(state='active')
        self.regist_status.set('regist success')