Пример #1
0
    def test_06_transaction_with_relation_and_witness(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        transaction1 = bbclib.make_transaction(relation_num=1, witness=True)
        bbclib.add_relation_asset(transaction1,
                                  relation_idx=0,
                                  asset_group_id=asset_group_id,
                                  user_id=user_id,
                                  asset_body=b'ccccc')
        bbclib.add_relation_pointer(transaction1,
                                    0,
                                    ref_transaction_id=transaction2.digest())
        transaction1.witness.add_witness(user_id)
        transaction1.witness.add_witness(user_id2)

        sig = transaction1.sign(private_key=keypair2.private_key,
                                public_key=keypair2.public_key)
        if sig is None:
            print(bbclib.error_text)
            assert sig
        transaction1.witness.add_signature(user_id=user_id2, signature=sig)

        sig = transaction1.sign(private_key=keypair1.private_key,
                                public_key=keypair1.public_key)
        if sig is None:
            print(bbclib.error_text)
            assert sig
        transaction1.witness.add_signature(user_id=user_id, signature=sig)

        print(transaction1)
Пример #2
0
    def test_03_transaction_insert(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        global transaction
        print("-- insert transaction only at core_node_1 --")
        user1 = clients[1]['user_id']
        transaction = bbclib.make_transaction(event_num=2, witness=True)
        transaction.events[0].add(mandatory_approver=clients[1]['user_id'])
        bbclib.add_event_asset(transaction,
                               event_idx=0,
                               asset_group_id=asset_group_id,
                               user_id=user1,
                               asset_body=b'123456')
        bbclib.add_event_asset(transaction,
                               event_idx=1,
                               asset_group_id=asset_group_id,
                               user_id=user1,
                               asset_file=b'abcdefg')
        transaction.witness.add_witness(user_id=user1)

        sig = transaction.sign(keypair=clients[1]['keypair'])
        transaction.witness.add_signature(user_id=user1, signature=sig)
        transaction.digest()
        print(transaction)
        print("register transaction=",
              binascii.b2a_hex(transaction.transaction_id))
        asset_file = dict()
        asset_file[transaction.events[1].asset.
                   asset_id] = transaction.events[1].asset.asset_file
        ret = cores[1].insert_transaction(domain_id, transaction.serialize(),
                                          asset_file)
        assert ret[KeyType.transaction_id] == transaction.transaction_id
Пример #3
0
    def test_13_gather_signature(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        prev_tx = transactions[0]
        user = clients[1]['user_id']
        transactions[1] = bbclib.make_transaction(event_num=1)
        bbclib.add_event_asset(transactions[1],
                               event_idx=0,
                               asset_group_id=asset_group_id,
                               user_id=user,
                               asset_body=b'123456')

        reference = bbclib.add_reference_to_transaction(
            transactions[1], asset_group_id, prev_tx, 0)
        clients[1]['app'].gather_signatures(transactions[1],
                                            reference_obj=reference)
        dat = msg_processor[1].synchronize()
        assert dat[KeyType.status] == ESUCCESS
        result = dat[KeyType.result]
        transactions[1].references[result[0]].add_signature(
            user_id=result[1], signature=result[2])

        print(transactions[1])
        transactions[1].digest()
        print("register transaction=",
              binascii.b2a_hex(transactions[1].transaction_id))
        clients[1]['app'].insert_transaction(transactions[1])
        dat = msg_processor[1].synchronize()
        assert KeyType.transaction_id in dat
        assert dat[KeyType.transaction_id] == transactions[1].transaction_id
    def test_23_make_transaction(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        for i, cl in enumerate(clients):
            print("---- start transaction at node %d---" % i)
            user = cl['user_id']
            other_user = (i + 1) % client_num
            transactions[i] = bbclib.make_transaction(event_num=1,
                                                      witness=True)
            transactions[i].events[0].add(
                mandatory_approver=clients[other_user]['user_id'])
            bbclib.add_event_asset(transactions[i],
                                   event_idx=0,
                                   asset_group_id=asset_group_id,
                                   user_id=user,
                                   asset_body="data=%d" % i)

            transactions[i].witness.add_witness(user_id=cl['user_id'])
            sig = transactions[i].sign(keypair=cl['keypair'])
            transactions[i].witness.add_signature(user_id=cl['user_id'],
                                                  signature=sig)
            transactions[i].digest()
            print("insert_transaction=",
                  binascii.b2a_hex(transactions[i].transaction_id))
            cl['app'].insert_transaction(transactions[i])
            print("  ----> wait for notification")
            for j in range(client_num):
                check_insert_response(j, transactions[i].transaction_id)
                if i == j:
                    check_insert_response(j, transactions[i].transaction_id)
Пример #5
0
    def test_05_1__search_transaction_by_txid_other_node_not_found(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")

        print("-- insert transaction only at core_node_2 --")
        global transaction
        user1 = clients[2]['user_id']
        transaction = bbclib.make_transaction(event_num=2, witness=True)
        bbclib.add_event_asset(transaction, event_idx=0, asset_group_id=asset_group_id,
                               user_id=user1, asset_body=b'aaddbbdd')
        bbclib.add_event_asset(transaction, event_idx=1, asset_group_id=asset_group_id,
                               user_id=user1, asset_file=b'112423')
        for i, user in enumerate(clients):
            transaction.witness.add_witness(user_id=clients[i]['user_id'])
        for i, user in enumerate(clients):
            sig = transaction.sign(keypair=clients[i]['keypair'])
            transaction.witness.add_signature(user_id=clients[i]['user_id'], signature=sig)
        transaction.digest()
        print(transaction)
        print("register transaction=", binascii.b2a_hex(transaction.transaction_id))
        asset_file = dict()
        asset_file[transaction.events[1].asset.asset_id] = transaction.events[1].asset.asset_file
        ret = cores[2].insert_transaction(domain_id, bbclib.serialize(transaction), asset_file)
        assert KeyType.transaction_id in ret
        assert ret[KeyType.transaction_id] == transaction.transaction_id

        # -- search the transaction at core_node_0
        ret = cores[0]._search_transaction_by_txid(domain_id, transaction.transaction_id)
        print(ret)
        assert ret is None
Пример #6
0
def create_transaction_object_and_send_sign_req(idx, receiver_user_id, ref_txids=None, file_data=None):
    if ref_txids is None or ref_txids[0] is None:
        ref_txids = []

    txobj = bbclib.make_transaction(relation_num=1, witness=True)
    bbclib.add_relation_asset(txobj, relation_idx=0, asset_group_id=asset_group_id,
                                   user_id=receiver_user_id, asset_body="transferred", asset_file=file_data)
    txobj.witness.add_witness(user_ids[idx])
    txobj.witness.add_witness(receiver_user_id)

    for i, ref_txid in enumerate(ref_txids):
        clients[idx].search_transaction(ref_txid)
        response_data = clients[idx].callback.synchronize()
        if response_data[KeyType.status] < ESUCCESS:
            print("ERROR: ", response_data[KeyType.reason].decode())
            sys.exit(0)
        prev_tx, fmt_type = bbclib.deserialize(response_data[KeyType.transaction_data])
        bbclib.add_relation_pointer(txobj, 0, ref_transaction_id=prev_tx.digest())

    sig_mine = txobj.sign(private_key=keypairs[idx].private_key, public_key=keypairs[idx].public_key)
    txobj.witness.add_signature_object(user_id=user_ids[idx], signature=sig_mine)

    asset_id = txobj.relations[0].asset.asset_id
    asset_files = {asset_id: file_data}
    ret = clients[idx].gather_signatures(txobj, destinations=[receiver_user_id], asset_files=asset_files)
    assert ret
    return txobj
Пример #7
0
    def update(self,
               user_id,
               public_keys_to_add=None,
               public_keys_to_remove=None,
               public_keys_to_replace=None,
               keypair=None):
        reftx = self.__update_local_database(user_id)

        dat = bytearray(b'')
        if public_keys_to_add is not None:
            dat.extend(
                Directive(Directive.CMD_ADD, public_keys_to_add).serialize())
        if public_keys_to_remove is not None:
            dat.extend(
                Directive(Directive.CMD_REMOVE,
                          public_keys_to_remove).serialize())
        if public_keys_to_replace is not None:
            dat.extend(
                Directive(Directive.CMD_REPLACE,
                          public_keys_to_replace).serialize())

        tx = bbclib.make_transaction(event_num=1)
        tx.events[0].asset_group_id = self.namespace_id
        tx.events[0].asset.add(user_id=user_id, asset_body=dat)
        tx.events[0].add(mandatory_approver=user_id)
        bbclib.add_reference_to_transaction(tx, self.namespace_id, reftx, 0)

        if keypair is None:
            return tx

        return self.sign_and_insert(tx, user_id, keypair)
Пример #8
0
    def test_12_make_transaction(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        for i, cl in enumerate(clients):
            print("---- start transaction ---")
            user = cl['user_id']
            other_user = (i + 1) % client_num

            transactions[i] = bbclib.make_transaction(event_num=1,
                                                      witness=True,
                                                      format_type=fmt)
            transactions[i].events[0].add(
                mandatory_approver=clients[other_user]['user_id'])
            bbclib.add_event_asset(transactions[i],
                                   event_idx=0,
                                   asset_group_id=asset_group_id,
                                   user_id=user,
                                   asset_body=b"data=%d" % i)

            transactions[i].witness.add_witness(user_id=cl['user_id'])
            sig = transactions[i].sign(keypair=cl['keypair'])
            transactions[i].add_signature(user_id=cl['user_id'], signature=sig)

            transactions[i].digest()
            print("register transaction=",
                  binascii.b2a_hex(transactions[i].transaction_id))
            cl['app'].insert_transaction(transactions[i])
            print("  ----> wait insert")
            dat = msg_processor[i].synchronize()
            assert KeyType.transaction_id in dat
            assert dat[
                KeyType.transaction_id] == transactions[i].transaction_id
            print("    ==> got insert")
        time.sleep(2)
Пример #9
0
    def test_13_make_transaction(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        for i, cl in enumerate(clients):
            user = cl['user_id']
            other_user = (i + 1) % client_num

            txobj = bbclib.make_transaction(event_num=1,
                                            witness=True,
                                            format_type=fmt)
            txobj.events[0].add(
                reference_index=0,
                mandatory_approver=clients[other_user]['user_id'])
            bbclib.add_event_asset(txobj,
                                   event_idx=0,
                                   asset_group_id=asset_group_id,
                                   user_id=user,
                                   asset_body=b"data=%d" % i)
            reference = bbclib.add_reference_to_transaction(
                txobj, asset_group_id, transactions[i], 0)
            ret = cl['app'].gather_signatures(txobj, reference_obj=reference)
            assert ret
            dat = msg_processor[i].synchronize()
            assert dat[KeyType.status] == ESUCCESS
            result = dat[KeyType.result]
            txobj.references[result[0]].add_signature(user_id=result[1],
                                                      signature=result[2])

            txobj.digest()
            cl['app'].insert_transaction(txobj)
            dat = msg_processor[i].synchronize()
            assert KeyType.transaction_id in dat
            assert dat[KeyType.transaction_id] == txobj.transaction_id
            transactions[i] = txobj
Пример #10
0
    def test_10_store_file(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        # -- first file store by user_0
        store_transaction = bbclib.make_transaction(relation_num=1,
                                                    witness=True)
        bbclib.add_relation_asset(store_transaction,
                                  relation_idx=0,
                                  asset_group_id=asset_group_id,
                                  user_id=user_ids[0],
                                  asset_body="Owner is 0",
                                  asset_file=large_data)

        store_transaction.witness.add_witness(user_ids[0])
        sig = store_transaction.sign(private_key=keypairs[0].private_key,
                                     public_key=keypairs[0].public_key)
        store_transaction.get_sig_index(user_ids[0])
        store_transaction.add_signature(user_id=user_ids[0], signature=sig)
        store_transaction.digest()
        print(store_transaction)

        global transaction_id, asset_id
        transaction_id = store_transaction.transaction_id
        asset_id = store_transaction.relations[0].asset.asset_id
        clients[0].insert_transaction(store_transaction)
        response_data = clients[0].callback.synchronize()
        if response_data[KeyType.status] < ESUCCESS:
            print("ERROR: ", response_data[KeyType.reason].decode())
            assert False
        time.sleep(1)
Пример #11
0
def sendback_exception_asset(approver_id, asset_group, asid):
    asset = json.loads(get_data(asset_group, asid).decode("UTF-8"))
    asset["owner"] = approver_id
    asset["date"] = datetime.now().strftime('%s')
    data = json.dumps(asset)

    bbc_app_client = setup_bbc_client()

    transaction = bbclib.make_transaction(event_num=1)
    transaction.events[0].add(mandatory_approver=binascii.unhexlify(approver_id))
    bbclib.add_event_asset(transaction, event_idx=0, asset_group_id=asset_group,
                           user_id=user_id, asset_body=data)

    ref_tx = get_txid_from_asid(asset_group, asid)
    transaction = add_ref_tx(asset_group, transaction, ref_tx, 0)

    transaction.digest()
    print(transaction)

    ret = bbc_app_client.insert_transaction(transaction)
    assert ret
    response_data = bbc_app_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("ERROR: ", response_data[KeyType.reason].decode())
        sys.exit(0)
    print("TxID: %s", binascii.b2a_hex(response_data[KeyType.transaction_id]))
    print("AsID: %s", binascii.b2a_hex(transaction.events[0].asset.asset_id))

    bbc_app.store_id_mappings(data, asset_group,
                              transaction_id=response_data[KeyType.transaction_id],
                              asset_ids=transaction.events[0].asset.asset_id)

    txinfo = [transaction.transaction_id, transaction.events[0].asset.asset_id]
    bbc_app_client.send_message(txinfo, binascii.unhexlify(new_owner), asset_group)
    print("Transfer is done.....")
Пример #12
0
    def test_13_insert_first_transaction(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        user = clients[0]['user_id']
        transactions[0] = bbclib.make_transaction(event_num=2, witness=True)
        transactions[0].events[0].add(reference_index=0, mandatory_approver=user)
        bbclib.add_event_asset(transactions[0], event_idx=0, asset_group_id=asset_group_id,
                               user_id=user, asset_body=b'123456')
        bbclib.add_event_asset(transactions[0], event_idx=1, asset_group_id=asset_group_id,
                               user_id=user, asset_body=b'abcdefg')

        transactions[0].witness.add_witness(user)
        sig = transactions[0].sign(keypair=clients[0]['keypair'])
        assert sig is not None
        if sig is None:
            print(bbclib.error_text)
            import os
            os._exit(1)
        transactions[0].add_signature(user_id=user, signature=sig)
        print(transactions[0])
        transactions[0].digest()
        global transaction_dat
        transaction_dat = bbclib.serialize(transactions[0])
        print("register transaction=", binascii.b2a_hex(transactions[0].transaction_id))
        clients[0]['app'].insert_transaction(transactions[0])
        dat = msg_processor[0].synchronize()
        assert KeyType.transaction_id in dat
        assert dat[KeyType.transaction_id] == transactions[0].transaction_id
        time.sleep(2)
Пример #13
0
    def test_20_make_transaction(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        user = clients[0]['user_id']
        kp = clients[0]['keypair']

        global start_from, transactions1

        transactions1[0] = bbclib.make_transaction(relation_num=1,
                                                   witness=True)
        bbclib.add_relation_asset(transactions1[0],
                                  relation_idx=0,
                                  asset_group_id=asset_group_id,
                                  user_id=user,
                                  asset_body=b'transaction1_0')
        transactions1[0].witness.add_witness(user)
        sig = transactions1[0].sign(keypair=kp)
        transactions1[0].witness.add_signature(user, sig)
        start_from = transactions1[0].timestamp
        print("timestamp: %d" % transactions1[0].timestamp)

        for i in range(1, 20):
            time.sleep(0.5)
            k = i - 1
            transactions1[i] = bbclib.make_transaction(relation_num=1,
                                                       witness=True)
            bbclib.add_relation_asset(transactions1[i],
                                      0,
                                      asset_group_id=asset_group_id,
                                      user_id=user,
                                      asset_body=b'transaction1_%d' % i)
            bbclib.add_relation_pointer(
                transactions1[i],
                0,
                ref_transaction_id=transactions1[k].transaction_id,
                ref_asset_id=transactions1[k].relations[0].asset.asset_id)
            transactions1[i].witness.add_witness(user)
            sig = transactions1[i].sign(keypair=kp)
            transactions1[i].witness.add_signature(user, sig)
            print("timestamp: %d" % transactions1[i].timestamp)

            clients[0]['app'].insert_transaction(transactions1[i])
            dat = clients[0]['app'].callback.synchronize()
            assert KeyType.transaction_id in dat
            assert dat[
                KeyType.transaction_id] == transactions1[i].transaction_id
Пример #14
0
    def update(self,
               user_id,
               public_keys_to_add=None,
               public_keys_to_remove=None,
               public_keys_to_replace=None,
               key_types_to_add=None,
               key_types_to_remove=None,
               key_types_to_replace=None,
               keypair=None,
               label=None):
        """Updates the mapping between the user ID and public keys.

        Args:
            user_id (bytes): The user ID.
            public_keys_to_add (list): Adding keys. None by default.
            public_keys_to_remove (list): Removing keys. None by default.
            public_keys_to_replace (list): Replacing keys. None by default.
            key_types_to_add (list): Adding key types. None by default.
            key_types_to_remove (list): Removing key types. None by default.
            key_types_to_replace (list): Replacing key types. None by default.
            keypair (BBcKeypair): The keypair to sign the transaction with.
            label (TransactionLabel): Label of transaction. None by default.

        """
        reftx = self.__update_local_database(user_id)

        dat = bytearray(b'')
        if public_keys_to_add is not None:
            dat.extend(
                Directive(Directive.CMD_ADD, public_keys_to_add,
                          key_types_to_add).serialize(self.default_key_type))
        if public_keys_to_remove is not None:
            dat.extend(
                Directive(Directive.CMD_REMOVE, public_keys_to_remove,
                          key_types_to_remove).serialize(
                              self.default_key_type))
        if public_keys_to_replace is not None:
            dat.extend(
                Directive(Directive.CMD_REPLACE, public_keys_to_replace,
                          key_types_to_replace).serialize(
                              self.default_key_type))

        tx = bbclib.make_transaction(event_num=1)
        tx.events[0].asset_group_id = self.namespace_id
        tx.events[0].asset.add(user_id=user_id, asset_body=dat)
        tx.events[0].add(mandatory_approver=user_id)

        if label is not None:
            tx.add(event=label.get_event())

        bbclib.add_reference_to_transaction(tx, self.namespace_id, reftx, 0)

        if keypair is None:
            return tx

        return self.sign_and_insert(tx, user_id, keypair)
Пример #15
0
def store_proc(asset_body,
               asset_file,
               asset_group_id,
               domain_id,
               key_pair,
               user_id,
               txid=None):

    bbc_app_client = setup_bbc_client(domain_id, user_id)

    store_transaction = bbclib.make_transaction(relation_num=1, witness=True)
    bbclib.add_relation_asset(store_transaction,
                              relation_idx=0,
                              asset_group_id=asset_group_id,
                              user_id=user_id,
                              asset_body=asset_body,
                              asset_file=asset_file)
    store_transaction.witness.add_witness(user_id)

    if txid:
        bbc_app_client.search_transaction(txid)
        response_data = bbc_app_client.callback.synchronize()
        if response_data[KeyType.status] < ESUCCESS:
            return None, None, "ERROR: %s" % response_data[
                KeyType.reason].decode()

        prev_tx, fmt_type = bbclib.deserialize(
            response_data[KeyType.transaction_data])

        keypair = bbclib.KeyPair(privkey=key_pair.private_key,
                                 pubkey=key_pair.public_key)
        if not keypair.verify(prev_tx.transaction_id,
                              prev_tx.signatures[0].signature):
            return None, None, "ERROR: Signature or keypair is invalid."

        bbclib.add_relation_pointer(transaction=store_transaction,
                                    relation_idx=0,
                                    ref_transaction_id=prev_tx.transaction_id)

    sig = store_transaction.sign(private_key=key_pair.private_key,
                                 public_key=key_pair.public_key)
    store_transaction.get_sig_index(user_id)
    store_transaction.add_signature_object(user_id=user_id, signature=sig)
    store_transaction.digest()
    print(store_transaction)

    ret = bbc_app_client.insert_transaction(store_transaction)
    assert ret
    response_data = bbc_app_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        return None, None, "ERROR: %s" % response_data[KeyType.reason].decode()

    transaction_id = response_data[KeyType.transaction_id]
    asset_ids = store_transaction.relations[0].asset.asset_id

    return asset_ids, transaction_id, None
    def test_02_make_transaction(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        user = clients[0]['user_id']
        kp = clients[0]['keypair']

        transactions1[0] = bbclib.make_transaction(relation_num=1, witness=True, format_type=fmt)
        bbclib.add_relation_asset(transactions1[0], relation_idx=0, asset_group_id=asset_group_id,
                                  user_id=user, asset_body=b'transaction1_0')
        transactions1[0].witness.add_witness(user)
        sig = transactions1[0].sign(keypair=kp)
        transactions1[0].witness.add_signature(user, sig)

        transactions2[0] = bbclib.make_transaction(event_num=1, witness=True, format_type=fmt)
        transactions2[0].events[0].add(mandatory_approver=user)
        bbclib.add_event_asset(transactions2[0], event_idx=0, asset_group_id=asset_group_id,
                               user_id=user, asset_body=b'transaction2_0')
        transactions2[0].witness.add_witness(user)
        sig = transactions2[0].sign(keypair=kp)
        transactions2[0].witness.add_signature(user_id=user, signature=sig)

        for i in range(1, 20):
            k = i - 1
            transactions1[i] = bbclib.make_transaction(relation_num=1, witness=True, format_type=fmt)
            bbclib.add_relation_asset(transactions1[i], 0, asset_group_id=asset_group_id, user_id=user,
                                      asset_body=b'transaction1_%d' % i)
            bbclib.add_relation_pointer(transactions1[i], 0, ref_transaction_id=transactions1[k].transaction_id,
                                        ref_asset_id=transactions1[k].relations[0].asset.asset_id)
            transactions1[i].witness.add_witness(user)
            sig = transactions1[i].sign(keypair=kp)
            transactions1[i].witness.add_signature(user, sig)

            transactions2[i] = bbclib.make_transaction(event_num=1, witness=True, format_type=fmt)
            transactions2[i].events[0].add(mandatory_approver=user)
            bbclib.add_event_asset(transactions2[i], event_idx=0, asset_group_id=asset_group_id,
                                   user_id=user, asset_body=b'transaction2_%d' % i)
            transactions2[i].witness.add_witness(user)
            bbclib.add_reference_to_transaction(transactions2[i], asset_group_id, transactions2[k], 0)
            sig = transactions2[i].sign(keypair=kp)
            transactions2[i].witness.add_signature(user_id=user, signature=sig)
            if i == 9:
                bbclib.add_reference_to_transaction(transactions2[i], asset_group_id, transactions2[5], 0)
            sig = transactions2[i].sign(keypair=kp)
            transactions2[i].references[0].add_signature(user_id=user, signature=sig)
Пример #17
0
    def test_02_make_transaction(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        user = clients[0]['user_id']
        global transactions
        transactions[0] = bbclib.make_transaction(event_num=2, witness=True)
        transactions[0].events[0].add(reference_index=0, mandatory_approver=user)
        bbclib.add_event_asset(transactions[0], event_idx=0, asset_group_id=asset_group_id,
                               user_id=user, asset_body=b'123456')
        bbclib.add_event_asset(transactions[0], event_idx=1, asset_group_id=asset_group_id,
                               user_id=user, asset_body=b'abcdefg')

        transactions[0].witness.add_witness(user)
Пример #18
0
def make_transaction(user_id, keypair):
    txobj = bbclib.make_transaction(relation_num=1, witness=True)
    bbclib.add_relation_asset(txobj,
                              relation_idx=0,
                              asset_group_id=asset_group_id,
                              user_id=user_id,
                              asset_body="data=%d" % random.randint(1, 10000),
                              asset_file=asset_file_content)
    txobj.witness.add_witness(user_id)
    txobj.add_signature(user_id, keypair=keypair)
    txobj.digest()
    return txobj
Пример #19
0
    def create_user_id(self,
                       num_pubkeys=1,
                       public_keys=None,
                       key_types=None,
                       label=None):
        """Creates a user ID (and key pairs) and map public keys to it.

        Args: 
            num_pubkeys (int): The number of new public keys to map to the ID.
            public_keys (list): The public keys to map. None by default.
            key_types (list): Types of the public keys. None by default.
            label (TransactionLabel): Label of transaction. None by default.
        
        Returns:
            user_id (bytes): The created user ID.
            initial_keypairs (list): The list of created key pairs.

        """

        keypair = bbclib.KeyPair(curvetype=self.default_key_type)
        keypair.generate()
        user_id = hashlib.sha256(bytes(keypair.public_key)).digest()
        # FIXME: detect collision

        initial_keypairs = []
        if public_keys is None:
            public_keys = []
            for i in range(num_pubkeys):
                new_keypair = bbclib.KeyPair(curvetype=self.default_key_type)
                new_keypair.generate()
                initial_keypairs.append(new_keypair)
                public_keys.append(new_keypair.public_key)

        directive = Directive(Directive.CMD_REPLACE, public_keys, key_types)

        tx = bbclib.make_transaction(event_num=1, witness=True)
        tx.events[0].asset_group_id = self.namespace_id
        tx.events[0].asset.add(user_id=user_id,
                               asset_body=directive.serialize(
                                   self.default_key_type))
        tx.events[0].add(mandatory_approver=user_id)

        if label is not None:
            tx.add(event=label.get_event())

        tx.witness.add_witness(user_id)
        self.sign_and_insert(tx, user_id, keypair)
        return user_id, initial_keypairs
Пример #20
0
def generate_transaction(user_id, previous_txid):
    """
    Generate a transaction object
    :param user_id:
    :param previous_txid:
    :return:
    """
    if previous_txid is not None:
        print(previous_txid.hex())

    # create a frame of transaction
    txobj = bbclib.make_transaction(relation_num=1, witness=True)

    # build asset body, which is the main data body you want to register in the bbc_core
    asset_body_dict = {
        "item_a": 1000,
        "item_b": "xxxx",
        "item_c": binascii.a2b_hex("0123456789")
    }
    asset_body_data = msgpack.dumps(
        asset_body_dict
    )  # in this example, messagepack is used to transform the body data into binary format

    # create relation object (BBcRelation) with asset and put the object into the transaction
    bbclib.add_relation_asset(txobj,
                              relation_idx=0,
                              asset_group_id=asset_group_id,
                              user_id=user_id,
                              asset_body=asset_body_data)

    # if previous_txid is not None, set BBcPointer to the relation object
    if previous_txid is not None:
        bbclib.add_relation_pointer(txobj, 0, ref_transaction_id=previous_txid)

    # add witness to include signature of the user
    txobj.witness.add_witness(user_id)

    # sign to the transaction
    keypair = read_keypair_file()
    signature_obj = txobj.sign(private_key=keypair.private_key,
                               public_key=keypair.public_key)
    txobj.witness.add_signature(user_id=user_id, signature=signature_obj)
    txobj.digest()
    return txobj
Пример #21
0
def store_proc(file, txid=None):
    with open(file, "rb") as fin:
        data = fin.read()
    bbc_app_client = setup_bbc_client()

    store_transaction = bbclib.make_transaction(relation_num=1, witness=True)
    user_info = "Owner is %s" % user_name
    bbclib.add_relation_asset(store_transaction,
                              relation_idx=0,
                              asset_group_id=asset_group_id,
                              user_id=user_id,
                              asset_body=user_info,
                              asset_file=data)
    store_transaction.witness.add_witness(user_id)

    if txid:
        bbc_app_client.search_transaction(txid)
        response_data = bbc_app_client.callback.synchronize()
        if response_data[KeyType.status] < ESUCCESS:
            print("ERROR: ", response_data[KeyType.reason].decode())
            sys.exit(0)
        prev_tx, fmt_type = bbclib.deserialize(
            response_data[KeyType.transaction_data])
        bbclib.add_relation_pointer(transaction=store_transaction,
                                    relation_idx=0,
                                    ref_transaction_id=prev_tx.transaction_id)
    sig = store_transaction.sign(private_key=key_pair.private_key,
                                 public_key=key_pair.public_key)
    store_transaction.get_sig_index(user_id)
    store_transaction.add_signature_object(user_id=user_id, signature=sig)
    store_transaction.digest()
    print(store_transaction)

    ret = bbc_app_client.insert_transaction(store_transaction)
    assert ret
    response_data = bbc_app_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("ERROR: ", response_data[KeyType.reason].decode())
        sys.exit(0)

    store_id_mappings(os.path.basename(file),
                      asset_group_id,
                      transaction_id=response_data[KeyType.transaction_id],
                      asset_ids=store_transaction.relations[0].asset.asset_id)
Пример #22
0
def store_proc(data, approver_id, txid=None):
    bbc_app_client = setup_bbc_client()
    transaction = bbclib.make_transaction(event_num=1)
    transaction.events[0].add(mandatory_approver=approver_id)
    bbclib.add_event_asset(transaction,
                           event_idx=0,
                           asset_group_id=asset_group_id,
                           user_id=user_id,
                           asset_body=data)
    if txid:
        bbc_app_client.search_transaction(txid)
        response_data = bbc_app_client.callback.synchronize()
        if response_data[KeyType.status] < ESUCCESS:
            print("ERROR: ", response_data[KeyType.reason].decode())
            sys.exit(0)
        prev_tx = bbclib.BBcTransaction(
            deserialize=response_data[KeyType.transaction_data])
        reference = bbclib.add_reference_to_transaction(
            transaction, asset_group_id, prev_tx, 0)
        sig = transaction.sign(key_type=bbclib.KeyType.ECDSA_SECP256k1,
                               private_key=key_pair.private_key,
                               public_key=key_pair.public_key)
        transaction.references[0].add_signature(user_id=user_id, signature=sig)
    else:
        sig = transaction.sign(key_type=bbclib.KeyType.ECDSA_SECP256k1,
                               private_key=key_pair.private_key,
                               public_key=key_pair.public_key)
        transaction.add_signature(signature=sig)
    transaction.digest()
    print(transaction)

    ret = bbc_app_client.insert_transaction(transaction)
    assert ret
    response_data = bbc_app_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("ERROR: ", response_data[KeyType.reason].decode())
        sys.exit(0)
    print("TxID: %s", binascii.b2a_hex(response_data[KeyType.transaction_id]))
    print("AsID: %s", binascii.b2a_hex(transaction.events[0].asset.asset_id))

    txinfo = [transaction.transaction_id, transaction.events[0].asset.asset_id]
    return txinfo
Пример #23
0
def create_transaction_object_for_filedata(receiver_name, receiver_user_id, ref_txids=None, file_data=None,
                                           bbc_app_client=None):
    if ref_txids is None or ref_txids[0] is None:
        ref_txids = []
    transaction = bbclib.make_transaction(relation_num=1, witness=True)

    user_info_msg = "Ownership is transfered from %s to %s" % (user_name, receiver_name)
    bbclib.add_relation_asset(transaction, relation_idx=0, asset_group_id=asset_group_id,
                              user_id=receiver_user_id, asset_body=user_info_msg, asset_file=file_data)
    transaction.witness.add_witness(user_id)
    transaction.witness.add_witness(receiver_user_id)

    for i, ref_txid in enumerate(ref_txids):
        bbc_app_client.search_transaction(ref_txid)
        response_data = bbc_app_client.callback.synchronize()
        if response_data[KeyType.status] < ESUCCESS:
            print("ERROR: ", response_data[KeyType.reason].decode())
            sys.exit(0)
        prev_tx = bbclib.BBcTransaction(deserialize=response_data[KeyType.transaction_data])
        bbclib.add_relation_pointer(transaction, 0, ref_transaction_id=prev_tx.digest())

    asset_id = transaction.relations[0].asset.asset_id
    asset_files = {asset_id: file_data}
    ret = bbc_app_client.gather_signatures(transaction, destinations=[receiver_user_id], asset_files=asset_files)
    if not ret:
        print("Failed to send sign request")
        sys.exit(0)
    response_data = bbc_app_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("Rejected because ", response_data[KeyType.reason].decode(), "")
        sys.exit(0)
    result = response_data[KeyType.result]

    transaction.witness.add_signature(user_id=result[1], signature=result[2])

    sig_mine = transaction.sign(key_type=bbclib.KeyType.ECDSA_SECP256k1, private_key=key_pair.private_key,
                                public_key=key_pair.public_key)
    transaction.witness.add_signature(user_id=user_id, signature=sig_mine)
    transaction.digest()
    return transaction
Пример #24
0
def send_signreq(receiver_name,
                 receiver_user_id,
                 ref_txids=None,
                 file_data=None,
                 bbc_app_client=None):
    transaction = bbclib.make_transaction(relation_num=1, witness=True)

    user_info_msg = "Ownership is transfered from %s to %s" % (user_name,
                                                               receiver_name)
    bbclib.add_relation_asset(transaction,
                              relation_idx=0,
                              asset_group_id=asset_group_id,
                              user_id=receiver_user_id,
                              asset_body=user_info_msg,
                              asset_file=file_data)
    transaction.witness.add_witness(user_id)
    transaction.witness.add_witness(receiver_user_id)

    for i, ref_txid in enumerate(ref_txids):
        bbc_app_client.search_transaction(ref_txid)
        response_data = bbc_app_client.callback.synchronize()
        if response_data[KeyType.status] < ESUCCESS:
            print("ERROR: ", response_data[KeyType.reason].decode())
            sys.exit(0)
        prev_tx, fmt_type = bbclib.deserialize(
            response_data[KeyType.transaction_data])
        bbclib.add_relation_pointer(transaction,
                                    0,
                                    ref_transaction_id=prev_tx.digest())

    asset_id = transaction.relations[0].asset.asset_id
    asset_files = {asset_id: file_data}
    ret = bbc_app_client.gather_signatures(transaction,
                                           destinations=[receiver_user_id],
                                           asset_files=asset_files)
    if not ret:
        print("Failed to send sign request")
        sys.exit(0)
    return transaction
Пример #25
0
    def create_user_id(self, num_pubkeys=1, public_keys=None):

        keypair = bbclib.KeyPair()
        user_id = hashlib.sha256(bytes(keypair.public_key)).digest()
        # FIXME: detect collision

        initial_keypairs = []
        if public_keys is None:
            public_keys = []
            for i in range(num_pubkeys):
                new_keypair = bbclib.KeyPair()
                initial_keypairs.append(new_keypair)
                public_keys.append(new_keypair.public_key)

        directive = Directive(Directive.CMD_REPLACE, public_keys)

        tx = bbclib.make_transaction(event_num=1, witness=True)
        tx.events[0].asset_group_id = self.namespace_id
        tx.events[0].asset.add(user_id=user_id,
                               asset_body=directive.serialize())
        tx.events[0].add(mandatory_approver=user_id)
        tx.witness.add_witness(user_id)
        self.sign_and_insert(tx, user_id, keypair)
        return (user_id, initial_keypairs)
Пример #26
0
def execute_escrow():
    coinasset = json.loads(get_data(coin_asset_group, coinasid).decode("UTF-8"))
    coinasset["owner"] = escrow["owner"]
    coinasset["date"] = datetime.now().strftime('%s')
    coinasset = json.dumps(coinasset)
    cointx_id = get_txid_from_asid(coin_asset_group, coinasid)

    landasset = json.loads(get_data(land_asset_group, landasid).decode("UTF-8"))
    landasset["owner"] = escrow["newowner"]
    landasset["date"] = datetime.now().strftime('%s')
    landasset = json.dumps(landasset)
    landtx_id = get_txid_from_asid(land_asset_group, landasid)


    # Make TX
    land_client = setup_bbc_client(land_domain_id)
    transaction = bbclib.make_transaction(event_num=2)

    # Add event and asset
    print("Add event and asset")
    bbclib.add_event_asset(transaction, event_idx=0, asset_group_id=land_asset_group,
                           user_id=user_id, asset_body=landasset)
    transaction.events[0].add(mandatory_approver=binascii.unhexlify(escrow["newowner"]))
    LAB_id = bbclib.get_new_id("LegalAffairsBureau", include_timestamp=False)
    transaction.events[0].add(option_approver=LAB_id)

    coin_client = setup_bbc_client(coin_domain_id)
    bbclib.add_event_asset(transaction, event_idx=1, asset_group_id=coin_asset_group,
                           user_id=user_id, asset_body=coinasset)
    transaction.events[1].add(mandatory_approver=binascii.unhexlify(escrow["owner"]))

    # Add reference
    print("Add reference")
    land_client.search_transaction(landtx_id)
    response_data = land_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("ERROR: ", response_data[KeyType.reason].decode())
        sys.exit(0)
    prev_tx = bbclib.BBcTransaction(deserialize=response_data[KeyType.transaction_data])
    reference = bbclib.add_reference_to_transaction(transaction, land_asset_group, prev_tx, 0)

    coin_client.search_transaction(cointx_id)
    response_data = coin_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("ERROR: ", response_data[KeyType.reason].decode())
        sys.exit(0)
    prev_tx = bbclib.BBcTransaction(deserialize=response_data[KeyType.transaction_data])
    reference = bbclib.add_reference_to_transaction(transaction, coin_asset_group, prev_tx,0)

    # Add signature
    print("Add signature to escrow TX")
    sig = transaction.sign(key_type=bbclib.KeyType.ECDSA_SECP256k1,
                                 private_key=key_pair.private_key,
                                 public_key=key_pair.public_key)
    transaction.references[0].add_signature(user_id=user_id, signature=sig)

    print("Get signature from LegalAffairsBureau")
    land_client.gather_signatures(transaction, destinations=[LAB_id])
    response_data = land_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("Rejected because ", response_data[KeyType.reason].decode(), "")
        sys.exit(0)
    result = response_data[KeyType.result]
    transaction.get_sig_index(result[1])
    transaction.add_signature(user_id=result[1], signature=result[2])

    transaction.digest()
    print(transaction)

    print("insert coin asset group")
    ret = coin_client.insert_transaction(transaction)
    assert ret
    response_data = coin_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("ERROR: ", response_data[KeyType.reason].decode())
        sys.exit(0)
    print("TxID: %s", binascii.b2a_hex(response_data[KeyType.transaction_id]))
    print("coinAsID: %s", binascii.b2a_hex(transaction.events[0].asset.asset_id))
    print("landAsID: %s", binascii.b2a_hex(transaction.events[1].asset.asset_id))

    cointxinfo = [transaction.transaction_id, transaction.events[0].asset.asset_id]
    landtxinfo = [transaction.transaction_id, transaction.events[1].asset.asset_id]

    print("insert land asset group")
    ret = land_client.insert_transaction(transaction)
    assert ret
    response_data = land_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("ERROR: ", response_data[KeyType.reason].decode())
        sys.exit(0)
    print("TxID: %s", binascii.b2a_hex(response_data[KeyType.transaction_id]))
    print("coinAsID: %s", binascii.b2a_hex(transaction.events[0].asset.asset_id))
    print("landAsID: %s", binascii.b2a_hex(transaction.events[1].asset.asset_id))

    txinfo = [transaction.transaction_id, transaction.events[0].asset.asset_id]
    coin_client.send_message(txinfo, binascii.unhexlify(escrow["owner"]), coin_asset_group)
    txinfo = [transaction.transaction_id, transaction.events[1].asset.asset_id]
    land_client.send_message(txinfo, binascii.unhexlify(escrow["newowner"]), land_asset_group)
    print("Transfer is done.....")
Пример #27
0
def store_proc(data, approver_id, txid=None):
    bbc_app_client = setup_bbc_client()
    store_transaction = bbclib.make_transaction(event_num=1)
    store_transaction.events[0].add(mandatory_approver=approver_id)
    bbclib.add_event_asset(store_transaction,
                           event_idx=0,
                           asset_group_id=asset_group_id,
                           user_id=user_id,
                           asset_body=data)

    LAB_id = bbclib.get_new_id("LegalAffairsBureau", include_timestamp=False)
    store_transaction.events[0].add(option_approver=LAB_id)

    if txid:
        bbc_app_client.search_transaction(txid)
        response_data = bbc_app_client.callback.synchronize()
        if response_data[KeyType.status] < ESUCCESS:
            print("ERROR: ", response_data[KeyType.reason].decode())
            sys.exit(0)
        prev_tx = bbclib.BBcTransaction(
            deserialize=response_data[KeyType.transaction_data])
        reference = bbclib.add_reference_to_transaction(
            store_transaction, asset_group_id, prev_tx, 0)
        sig = store_transaction.sign(key_type=bbclib.KeyType.ECDSA_SECP256k1,
                                     private_key=key_pair.private_key,
                                     public_key=key_pair.public_key)
        store_transaction.references[0].add_signature(user_id=user_id,
                                                      signature=sig)
    else:
        sig = store_transaction.sign(key_type=bbclib.KeyType.ECDSA_SECP256k1,
                                     private_key=key_pair.private_key,
                                     public_key=key_pair.public_key)
        store_transaction.add_signature(signature=sig)
    store_transaction.add_signature(user_id=user_id, signature=sig)

    # Get signature from LegalAffairsBureau
    bbc_app_client.gather_signatures(store_transaction, destinations=[LAB_id])
    response_data = bbc_app_client.callback.synchronize()

    if response_data[KeyType.status] < ESUCCESS:
        print("Rejected because ", response_data[KeyType.reason].decode(), "")
        sys.exit(0)
    result = response_data[KeyType.result]
    store_transaction.get_sig_index(result[1])
    store_transaction.add_signature(user_id=result[1], signature=result[2])

    store_transaction.digest()
    print(store_transaction)

    ret = bbc_app_client.insert_transaction(store_transaction)
    assert ret
    response_data = bbc_app_client.callback.synchronize()
    if response_data[KeyType.status] < ESUCCESS:
        print("ERROR: ", response_data[KeyType.reason].decode())
        sys.exit(0)
    print("TxID: %s", binascii.b2a_hex(response_data[KeyType.transaction_id]))
    print("AsID: %s",
          binascii.b2a_hex(store_transaction.events[0].asset.asset_id))

    txinfo = [
        store_transaction.transaction_id,
        store_transaction.events[0].asset.asset_id
    ]
    return txinfo