Example #1
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 #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
    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 #4
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 #5
0
async def init():
    T.LOGGER.debug('')

    ID_own = None
    # load key
    try:
        c_root_auth = None
        async with aiosqlite.connect(T.PATH_CREDIT) as db:
            await db.execute('create table if not exists \
                                table_own(name nchar({0}) primary key, \
                                        id char({1}) unique, \
                                        pri_key char({2}), \
                                        pub_key_x char({2}), \
                                        pub_key_y char({2}))'. 
                                format(G.LEN_NAME, G.LEN_ID, G.LEN_KEY))
            async with db.execute('select * from table_own where name=?', (G.NAME_GOD,)) as cursor:
                async for row in cursor:
                    ID_own = row[T.P_OWN_ID]
                    T.LOGGER.debug(b'select: ' + ID_own)
                    pri_key = base58.b58decode_int(row[T.P_OWN_PRI])
                    pub_key_x = base58.b58decode_int(row[T.P_OWN_PUB_X])
                    pub_key_y = base58.b58decode_int(row[T.P_OWN_PUB_Y])
                    ch_own = Chain(T.FO_CH, ID_own, pri_key, pub_key_x, pub_key_y)
                    
            # no record, create root card
            if ID_own is None:          
                ID_own, pri_key, pub_key_x, pub_key_y = Ecc.generate()
                T.LOGGER.debug(b'create: ' + ID_own)
                pri_key_b58 = base58.b58encode_int(pri_key)
                pub_key_x_b58 = base58.b58encode_int(pub_key_x)
                pub_key_y_b58 = base58.b58encode_int(pub_key_y)
                
                await db.execute('insert into table_own values(?,?,?,?,?)',
                                    (G.NAME_GOD, ID_own, pri_key_b58, pub_key_x_b58, pub_key_y_b58))
                await db.commit()

                ch_own = Chain(T.FO_CH, ID_own, pri_key, pub_key_x, pub_key_y)
                c_root_auth = handle.root(ch_own)

        #  own chain
        if not await ch_own.init(c_root_auth):
            return False
        return ch_own
        
    except Exception as e:
        T.LOGGER.warning(e)
        return False
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 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 #8
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 #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
async def regist(cls_App, name, hash_ID_real, row_god):
    '''
    init the root card 
    row_god is in credit_DB.free_table: name, ID, credit
    '''
    T.LOGGER.debug('')

    ID_god = row_god[T.P_FREE_ID]
    ID, pri_key, pub_key_x, pub_key_y = Ecc.generate()
    pri_key_b58 = base58.b58encode_int(pri_key)
    pub_key_x_b58 = base58.b58encode_int(pub_key_x)
    pub_key_y_b58 = base58.b58encode_int(pub_key_y)

    # create init root card to chain
    ch_own = Chain(T.FO_CH, ID, pri_key, pub_key_x, pub_key_y)
    cls_App.ch_own = ch_own

    if not await charge(cls_App, ID_god, hash_ID_real, root=1):
        return False

    # add ID to table_own. to do: check the ID conflict
    try:
        async with aiosqlite.connect(T.PATH_CREDIT) as db:
            await db.execute('create table if not exists \
                                table_own(name nchar({0}) primary key, \
                                        id char({1}) unique, \
                                        pri_key char({2}), \
                                        pub_key_x char({2}), \
                                        pub_key_y char({2}))'.format(
                G.LEN_NAME, G.LEN_ID, G.LEN_KEY))

            await db.execute(
                'insert into table_own values(?,?,?,?,?)',
                (name, ID, pri_key_b58, pub_key_x_b58, pub_key_y_b58))
            await db.commit()
    except Exception as e:
        T.LOGGER.warning(e)
        return False

    return True
Example #14
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 #15
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 #16
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 #17
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 #18
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