Esempio n. 1
0
def define_user(name, dic_users):
    idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    (user_id, keypairs) = idPubkeyMap.create_user_id(num_pubkeys=1)

    clear_selected(dic_users)
    dic_users[name] = User(user_id, keypairs[0], True)

    write_dic(F_JSON_USERS, dic_users)

    print("user %s is defined." % (name))
Esempio n. 2
0
def test_default_map_creation(default_domain_id):

    NUM_KEYPAIRS = 5

    idPubkeyMap = id_lib.BBcIdPublickeyMap(default_domain_id)
    (user_id, keypairs) = idPubkeyMap.create_user_id(num_pubkeys=NUM_KEYPAIRS)

    assert len(keypairs) == NUM_KEYPAIRS

    for i in range(NUM_KEYPAIRS):
        assert idPubkeyMap.is_mapped(user_id, keypairs[i].public_key) == True
Esempio n. 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))
Esempio n. 4
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))
Esempio n. 5
0
def test_get_map(default_domain_id):

    NUM_KEYPAIRS = 3

    idPubkeyMap = id_lib.BBcIdPublickeyMap(default_domain_id)
    (user_id, keypairs) = idPubkeyMap.create_user_id(num_pubkeys=NUM_KEYPAIRS)

    assert len(keypairs) == NUM_KEYPAIRS

    public_keys = idPubkeyMap.get_mapped_public_keys(user_id)

    assert len(public_keys) == NUM_KEYPAIRS

    for i in range(NUM_KEYPAIRS):
        assert bytes(keypairs[i].public_key) == public_keys[i]
Esempio n. 6
0
def setup():
    global domain_id
    global mint_id
    global idPubkeyMap
    global keypairs

    domain_id = bbclib.get_new_id("test_token_lib", include_timestamp=False)

    tmpclient = bbc_app.BBcAppClient(port=DEFAULT_CORE_PORT, loglevel="all")
    tmpclient.domain_setup(domain_id, "simple_cluster")
    tmpclient.callback.synchronize()
    tmpclient.unregister_from_core()

    idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
    (mint_id, keypairs) = idPubkeyMap.create_user_id(num_pubkeys=1)
Esempio n. 7
0
def replace_keypair(name, dic, file_name):
    for name0, user in dic.items():
        if name0 == name:
            keypair_old = user.keypair
            keypair = bbclib.KeyPair()
            idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id)
            idPubkeyMap.update(user.user_id,
                               public_keys_to_replace=[keypair.public_key],
                               keypair=keypair_old)
            user.keypair = keypair
            break

    write_dic(file_name, dic)
    print("public key for %s is renewed:" % (name))
    print("old:", binascii.b2a_hex(keypair_old.public_key).decode())
    print("new:", binascii.b2a_hex(keypair.public_key).decode())
Esempio n. 8
0
def test_map_creation_with_pubkeys(default_domain_id):

    NUM_KEYPAIRS = 3

    public_keys = []
    for i in range(NUM_KEYPAIRS):
        keypair = bbclib.KeyPair()
        public_keys.append(keypair.public_key)

    idPubkeyMap = id_lib.BBcIdPublickeyMap(default_domain_id)
    (user_id, keypairs) = idPubkeyMap.create_user_id(public_keys=public_keys)

    assert len(keypairs) == 0

    for i in range(NUM_KEYPAIRS):
        assert idPubkeyMap.is_mapped(user_id, public_keys[i]) == True
Esempio n. 9
0
def test_map_update(default_domain_id):

    idPubkeyMap = id_lib.BBcIdPublickeyMap(default_domain_id)
    (user_id, keypairs) = idPubkeyMap.create_user_id(num_pubkeys=1)

    assert len(keypairs) == 1

    assert idPubkeyMap.is_mapped(user_id, keypairs[0].public_key) == True

    public_keys = []
    for i in range(3):
        keypair = bbclib.KeyPair()
        public_keys.append(keypair.public_key)

    tx = idPubkeyMap.update(user_id,
                            public_keys_to_replace=public_keys,
                            keypair=keypairs[0])

    assert idPubkeyMap.is_mapped(user_id, keypairs[0].public_key) == False
    for i in range(3):
        assert idPubkeyMap.is_mapped(user_id, public_keys[i]) == True
Esempio n. 10
0
def define_currency(name, symbol, file, dic_currencies):
    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))
Esempio n. 11
0
def test_map_eval(default_domain_id):

    idPubkeyMap = id_lib.BBcIdPublickeyMap(default_domain_id)
    (user_id, keypairs0) = idPubkeyMap.create_user_id()

    time0 = int(time.time())
    print("\n2-second interval.")
    time.sleep(2)

    keypairs1 = []
    public_keys = []
    for i in range(3):
        keypairs1.append(bbclib.KeyPair())
        public_keys.append(keypairs1[i].public_key)

    tx = idPubkeyMap.update(user_id,
                            public_keys_to_add=public_keys,
                            keypair=keypairs0[0])

    time1 = int(time.time())
    print("2-second interval.")
    time.sleep(2)

    tx = idPubkeyMap.update(user_id,
                            public_keys_to_remove=public_keys,
                            keypair=keypairs0[0])

    time2 = int(time.time())
    print("2-second interval.")
    time.sleep(2)

    tx = idPubkeyMap.update(user_id,
                            public_keys_to_replace=public_keys,
                            keypair=keypairs0[0])

    time3 = int(time.time())

    public_keys = idPubkeyMap.get_mapped_public_keys(user_id, time3)

    assert len(public_keys) == 3

    for keypair in keypairs1:
        assert bytes(keypair.public_key) in public_keys

    assert idPubkeyMap.is_mapped(user_id, keypairs0[0].public_key,
                                 time3) == False
    assert idPubkeyMap.is_mapped(user_id, keypairs1[0].public_key,
                                 time3) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[1].public_key,
                                 time3) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[2].public_key,
                                 time3) == True

    public_keys = idPubkeyMap.get_mapped_public_keys(user_id, time2)

    assert len(public_keys) == 1

    for keypair in keypairs0:
        assert bytes(keypair.public_key) in public_keys

    assert idPubkeyMap.is_mapped(user_id, keypairs0[0].public_key,
                                 time2) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[0].public_key,
                                 time2) == False
    assert idPubkeyMap.is_mapped(user_id, keypairs1[1].public_key,
                                 time2) == False
    assert idPubkeyMap.is_mapped(user_id, keypairs1[2].public_key,
                                 time2) == False

    public_keys = idPubkeyMap.get_mapped_public_keys(user_id, time1)

    assert len(public_keys) == 4

    for keypair in keypairs0:
        assert bytes(keypair.public_key) in public_keys
    for keypair in keypairs1:
        assert bytes(keypair.public_key) in public_keys

    assert idPubkeyMap.is_mapped(user_id, keypairs0[0].public_key,
                                 time1) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[0].public_key,
                                 time1) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[1].public_key,
                                 time1) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[2].public_key,
                                 time1) == True

    public_keys = idPubkeyMap.get_mapped_public_keys(user_id, time0)

    assert len(public_keys) == 1

    for keypair in keypairs0:
        assert bytes(keypair.public_key) in public_keys

    assert idPubkeyMap.is_mapped(user_id, keypairs0[0].public_key,
                                 time0) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[0].public_key,
                                 time0) == False
    assert idPubkeyMap.is_mapped(user_id, keypairs1[1].public_key,
                                 time0) == False
    assert idPubkeyMap.is_mapped(user_id, keypairs1[2].public_key,
                                 time0) == False

    idPubkeyMap._BBcIdPublickeyMap__clear_local_database(user_id)
    print("cleared local database entries for the user for reconstruction.")

    public_keys = idPubkeyMap.get_mapped_public_keys(user_id)

    assert len(public_keys) == 3

    for keypair in keypairs1:
        assert bytes(keypair.public_key) in public_keys

    assert idPubkeyMap.is_mapped(user_id, keypairs0[0].public_key) == False
    assert idPubkeyMap.is_mapped(user_id, keypairs1[0].public_key) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[1].public_key) == True
    assert idPubkeyMap.is_mapped(user_id, keypairs1[2].public_key) == True