예제 #1
0
def swap_between_users(name, amount1, amount2, currency_name, dic_currencies,
        dic_users):
    _, currency = get_selected(dic_currencies)
    _, user = get_selected(dic_users)
    counter_currency = dic_currencies[currency_name]
    counter_user = dic_users[name]

    idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    mint = token_lib.BBcMint(domain_id, currency.user_id, currency.user_id,
            idPubkeyMap)
    counter_mint = token_lib.BBcMint(domain_id,
            counter_currency.user_id, counter_currency.user_id, idPubkeyMap)

    currency_spec = mint.get_currency_spec()
    counter_currency_spec = counter_mint.get_currency_spec()
    value1 = int(amount1 * (10 ** currency_spec.decimal))
    value2 = int(amount2 * (10 ** counter_currency_spec.decimal))

    mint.swap(counter_mint, user.user_id, counter_user.user_id,
            value1, value2,
            keypair_this=user.keypair, keypair_that=counter_user.keypair,
            keypair_mint=currency.keypair,
            keypair_counter_mint=counter_currency.keypair)
    time.sleep(1) # this should be unnecessary but token_lib is incomplete now.

    value1_string = ("{0:.%df}" % (currency_spec.decimal)).format(
            value1 / (10 ** currency_spec.decimal))
    value2_string = ("{0:.%df}" % (counter_currency_spec.decimal)).format(
            value2 / (10 ** counter_currency_spec.decimal))

    print("%s%s is transferred to %s." % (value1_string,
            currency_spec.symbol, name))
    print("%s%s is transferred from %s." % (value2_string,
            counter_currency_spec.symbol, name))
예제 #2
0
def define_currency(name, symbol, file, dic_currencies):
    if name in dic_currencies:
        print("currency %s is already defined." % (name))
        return
    if name in dic_users:
        print("%s is already defined as a user name." % (name))
        return

    idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    mint_id, keypairs = idPubkeyMap.create_user_id(num_pubkeys=1)

    f = open(file, 'r')
    j_currency_spec = json.load(f)
    f.close()

    j_currency_spec['name'] = name
    j_currency_spec['symbol'] = symbol

    currency_spec = token_lib.CurrencySpec(j_currency_spec)

    mint = token_lib.BBcMint(domain_id, mint_id, mint_id, idPubkeyMap)
    mint.set_condition(0, keypair=keypairs[0])
    mint.set_currency_spec(currency_spec, keypair=keypairs[0])

    clear_selected(dic_currencies)
    dic_currencies[currency_spec.name] = User(mint_id, keypairs[0], True)

    write_dic(F_JSON_CURRENCIES, dic_currencies)

    print("currency %s/%s is defined." % (name, symbol))
예제 #3
0
def show_user(name, dic_currencies, dic_users):
    _, currency = get_selected(dic_currencies)

    idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    mint = token_lib.BBcMint(domain_id, currency.user_id, currency.user_id,
                             idPubkeyMap)

    currency_spec = mint.get_currency_spec()

    value = mint.get_balance_of(dic_users[name].user_id)

    print("balance = %f%s." %
          (value / (10**currency_spec.decimal), currency_spec.symbol))
예제 #4
0
파일: body.py 프로젝트: adjebbar/examples-1
def transfer_to_user(hex_mint_id=None):
    if hex_mint_id is None:
        abort_by_missing_param('mint_id')

    currency = from_hex_to_user(g, hex_mint_id, 'currency_table')

    hex_from_user_id = request.form.get('from_user_id')
    hex_to_user_id = request.form.get('to_user_id')
    amount = request.form.get('amount')
    s_label = request.form.get('label')

    if s_label is None or len(s_label) <= 0:
        s_label = ''
        label = None
    else:
        label_id = TransactionLabel.create_label_id(s_label, LABEL_SALT)
        label = TransactionLabel(label_group_id, label_id=label_id)

    if hex_from_user_id is None:
        abort_by_missing_param('from_user_id')
    if hex_to_user_id is None:
        abort_by_missing_param('to_user_id')
    if amount is None:
        abort_by_missing_param('amount')

    from_user = from_hex_to_user(g, hex_from_user_id, 'user_table')
    to_user = from_hex_to_user(g, hex_to_user_id, 'user_table')

    g.idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    g.mint = token_lib.BBcMint(domain_id, currency.user_id, currency.user_id,
                               g.idPubkeyMap)

    currency_spec = g.mint.get_currency_spec()
    value = int(float(amount) * (10**currency_spec.decimal))

    tx = g.mint.transfer(from_user.user_id,
                         to_user.user_id,
                         value,
                         keypair_from=from_user.keypair,
                         keypair_mint=currency.keypair)

    g.store.write_tx(tx.transaction_id, get_timestamp_in_seconds(tx),
                     currency.user_id, from_user.name, to_user.name, amount,
                     s_label)

    return jsonify({
        'amount': ('{0:.%df}' % (currency_spec.decimal)).format(
            value / (10**currency_spec.decimal)),
        'symbol':
        currency_spec.symbol
    })
예제 #5
0
def issue_to_user(name, amount, dic_currencies, dic_users):
    _, currency = get_selected(dic_currencies)

    idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    mint = token_lib.BBcMint(domain_id, currency.user_id, currency.user_id,
                             idPubkeyMap)

    currency_spec = mint.get_currency_spec()
    value = int(amount * (10**currency_spec.decimal))

    mint.issue(dic_users[name].user_id, value, keypair=currency.keypair)

    print("%f%s is issued to %s." %
          (value / (10**currency_spec.decimal), currency_spec.symbol, name))
예제 #6
0
def show_transactions(hex_mint_id=None):
    if hex_mint_id is None:
        abort_by_missing_param('mint_id')

    mint_id = bytes(binascii.a2b_hex(hex_mint_id))
    mint = token_lib.BBcMint(domain_id, mint_id, mint_id, g.idPubkeyMap)

    name = request.args.get('name')
    count = request.args.get('count')
    offset = request.args.get('offset')
    basetime = request.args.get('basetime')

    return jsonify(g.store.get_tx_list(mint, name=name, count=count,
            offset=offset, basetime=basetime))
예제 #7
0
def transfer_to_user(name, amount, dic_currencies, dic_users):
    _, currency = get_selected(dic_currencies)
    _, user = get_selected(dic_users)

    idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    mint = token_lib.BBcMint(domain_id, currency.user_id, currency.user_id,
            idPubkeyMap)

    currency_spec = mint.get_currency_spec()
    value = int(amount * (10 ** currency_spec.decimal))

    mint.transfer(user.user_id, dic_users[name].user_id, value,
            keypair_from=user.keypair, keypair_mint=currency.keypair)

    value_string = ("{0:.%df}" % (currency_spec.decimal)).format(
            value / (10 ** currency_spec.decimal))
    print("%s%s is transferred to %s." % (value_string,
            currency_spec.symbol, name))
예제 #8
0
def get_balances_of(user_id, currencies):
    g.idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)

    dics = []
    for currency in currencies:
        mint = token_lib.BBcMint(domain_id, currency.user_id, currency.user_id,
                g.idPubkeyMap)
        currency_spec = mint.get_currency_spec()
        value = mint.get_balance_of(user_id)
        mint.close()

        dics.append({
            'balance': ("{0:.%df}" % (currency_spec.decimal)).format(
                    value / (10 ** currency_spec.decimal)),
            'symbol': currency_spec.symbol,
            'mint_id': binascii.b2a_hex(currency.user_id).decode()
        })

    return dics
예제 #9
0
파일: body.py 프로젝트: adjebbar/examples-1
def define_currency():
    if request.headers['Content-Type'] != 'application/json':
        abort_by_bad_content_type(request.headers['Content-Type'])

    name = request.json.get('name')
    symbol = request.json.get('symbol')

    if name is None:
        abort_by_missing_param('name')
    if symbol is None:
        abort_by_missing_param('symbol')
    if g.store.user_exists(name, 'currency_table'):
        abort(
            409, {
                'code': 'Conflict',
                'message': 'currency {0} is already defined'.format(name)
            })
    if g.store.user_exists(name, 'user_table'):
        abort(
            409, {
                'code': 'Conflict',
                'message': '{0} is already defined as a user name'.format(name)
            })

    g.idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    mint_id, keypairs = g.idPubkeyMap.create_user_id(num_pubkeys=1)

    currency_spec = token_lib.CurrencySpec(request.json)

    g.mint = token_lib.BBcMint(domain_id, mint_id, mint_id, g.idPubkeyMap)
    g.mint.set_condition(0, keypair=keypairs[0])
    g.mint.set_currency_spec(currency_spec, keypair=keypairs[0])

    g.store.write_user(User(mint_id, name, keypairs[0]), 'currency_table')

    return jsonify({
        'name': name,
        'symbol': symbol,
        'mint_id': binascii.b2a_hex(mint_id).decode()
    })
예제 #10
0
파일: body.py 프로젝트: adjebbar/examples-1
def swap_between_users(hex_mint_id=None, hex_counter_mint_id=None):
    if hex_mint_id is None:
        abort_by_missing_param('mint_id')
    if hex_counter_mint_id is None:
        abort_by_missing_param('counter_mint_id')

    currency = from_hex_to_user(g, hex_mint_id, 'currency_table')
    counter_currency = from_hex_to_user(g, hex_counter_mint_id,
                                        'currency_table')

    hex_user_id = request.form.get('user_id')
    hex_counter_user_id = request.form.get('counter_user_id')
    amount = request.form.get('amount')
    counter_amount = request.form.get('counter_amount')
    s_label = request.form.get('label')

    if s_label is None or len(s_label) <= 0:
        s_label = ''
        label = None
    else:
        label_id = TransactionLabel.create_label_id(s_label, LABEL_SALT)
        label = TransactionLabel(label_group_id, label_id=label_id)

    if hex_user_id is None:
        abort_by_missing_param('user_id')
    if hex_counter_user_id is None:
        abort_by_missing_param('counter_user_id')
    if amount is None:
        abort_by_missing_param('amount')
    if counter_amount is None:
        abort_by_missing_param('counter_amount')

    user = from_hex_to_user(g, hex_user_id, 'user_table')
    counter_user = from_hex_to_user(g, hex_counter_user_id, 'user_table')

    g.idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    g.mint = token_lib.BBcMint(domain_id, currency.user_id, currency.user_id,
                               g.idPubkeyMap)
    counter_mint = token_lib.BBcMint(domain_id, counter_currency.user_id,
                                     counter_currency.user_id, g.idPubkeyMap)

    currency_spec = g.mint.get_currency_spec()
    counter_currency_spec = counter_mint.get_currency_spec()
    value = int(float(amount) * (10**currency_spec.decimal))
    counter_value = int(
        float(counter_amount) * (10**counter_currency_spec.decimal))

    tx = g.mint.swap(counter_mint,
                     user.user_id,
                     counter_user.user_id,
                     value,
                     counter_value,
                     keypair_this=user.keypair,
                     keypair_that=counter_user.keypair,
                     keypair_mint=currency.keypair,
                     keypair_counter_mint=counter_currency.keypair)
    counter_mint.close()

    g.store.write_tx(tx.transaction_id, get_timestamp_in_seconds(tx),
                     currency.user_id, user.user_id, counter_user.user_id,
                     amount, s_label)
    counter_txid = bytearray(tx.transaction_id)
    counter_txid.extend(b'00')
    g.store.write_tx(bytes(counter_tx_id), get_timestamp_in_seconds(tx),
                     counter_currency.user_id, counter_user.user_id,
                     user.user_id, counter_amount, s_label)

    return jsonify({
        'amount': ('{0:.%df}' % (currency_spec.decimal)).format(
            value / (10**currency_spec.decimal)),
        'symbol':
        currency_spec.symbol,
        'counter_amount':
        ('{0:.%df}' % (counter_currency_spec.decimal)).format(
            counter_value / (10**counter_currency_spec.decimal)),
        'counter_symbol':
        counter_currency_spec.symbol
    })