Exemplo n.º 1
0
    def _do_trans(self, vfrom, value, vto, state):
        msg = 'transfer "{n}"->"{t}" by {v}'.format(n=vfrom, t=vto, v=value)
        LOGGER.debug(msg)

        if vfrom not in state or vto not in state:
            raise InvalidTransaction(
                'Verb is "trans" but vallet "{}" or vallet "{}" not in state'.
                format(vfrom, vto))

        curr = state[vfrom]
        token = BgtTokenInfo()
        token.ParseFromString(curr)
        to = state[vto]
        token1 = BgtTokenInfo()
        token1.ParseFromString(to)
        LOGGER.debug('_do_tans token[%s]=%s', token.group_code, value)
        decd = token.decimals - value
        if decd < MIN_VALUE:
            raise InvalidTransaction(
                'Verb is "trans", but result would be less than {}'.format(
                    MIN_VALUE))
        incd = token1.decimals + value
        if incd > MAX_VALUE:
            raise InvalidTransaction(
                'Verb is "inc", but result would be greater than {}'.format(
                    MAX_VALUE))

        updated = {k: v for k, v in state.items()}
        token.decimals = decd
        updated[vfrom] = token.SerializeToString()
        token1.decimals = incd
        updated[vto] = token1.SerializeToString()

        return updated
Exemplo n.º 2
0
    def _do_dec(self, name, value, to, state):
        msg = 'Decrementing "{n}" by {v}'.format(n=name, v=value)
        LOGGER.debug(msg)

        if name not in state:
            raise InvalidTransaction(
                'Verb is "dec" but name "{}" not in state'.format(name))

        curr = state[name]
        token = BgtTokenInfo()
        token.ParseFromString(curr)
        LOGGER.debug('_do_dec token[%s]=%s', token.group_code, token.decimals,
                     value)
        decd = token.decimals - value

        if decd < MIN_VALUE:
            raise InvalidTransaction(
                'Verb is "dec", but result would be less than {}'.format(
                    MIN_VALUE))

        updated = {k: v for k, v in state.items()}
        token.decimals = decd
        updated[name] = token.SerializeToString()

        return updated
Exemplo n.º 3
0
def do_show(args):
    name = args.name
    client = _get_client(args)
    value = client.show(name)
    token = BgtTokenInfo()
    token.ParseFromString(value)
    print('{}: {}={}'.format(name,token.group_code,token.decimals))
Exemplo n.º 4
0
def do_list(args):
    client = _get_client(args)
    results = client.list()
    token = BgtTokenInfo()
    for pair in results:
        for name, value in pair.items():
            token.ParseFromString(value)
            print('{}: {}={}'.format(name,token.group_code,token.decimals))
Exemplo n.º 5
0
def loads_bgt_token(data, name):
    value = cbor.loads(base64.b64decode(data))[name]
    token = BgtTokenInfo()
    token.ParseFromString(value)
    LOGGER.debug("BGT:%s %s=%s", name, token.group_code, token.decimals)
    return {
        'bgt': name,
        'group': token.group_code,
        'value': token.decimals,
        'sign': token.sign
    }
Exemplo n.º 6
0
    def _do_set(self,name, value, state):
        msg = 'Setting "{n}" to {v}'.format(n=name, v=value)
        LOGGER.debug(msg)
        

        if name in state:
            raise InvalidTransaction('Verb is "set", but already exists: Name: {n}, Value {v}'.format(n=name,v=state[name]))

        updated = {k: v for k, v in state.items()}
        #owner_key = self._context.sign('BGT_token'.encode(),self._private_key)
        token = BgtTokenInfo(group_code = 'BGT_token',
                             owner_key = self._signer.sign('BGT_token'.encode()), #owner_key,
                             sign = self._public_key.as_hex(),
                             decimals = int(value)
                )
        updated[name] = token.SerializeToString()
        LOGGER.debug('_do_set updated=%s',updated)
        return updated
Exemplo n.º 7
0
def _token_info(val):
    token = BgtTokenInfo()
    token.ParseFromString(val)
    return token