def make_transactions(id_len_conf=None, idlen=None, no_pubkey=False): keypair1 = KeyPair() keypair1.generate() keypair2 = KeyPair() keypair2.generate() global pem1, pem2 pem1 = keypair1.get_private_key_in_pem() pem2 = keypair2.get_private_key_in_pem() transactions = list() if id_len_conf is not None: bbclib.configure_id_length(id_len_conf) elif idlen is not None: bbclib.configure_id_length_all(idlen) txobj = bbclib.make_transaction(event_num=1, relation_num=1, witness=True) transactions.append(txobj) txobj.relations[0].set_asset_group(asset_group_id1).create_asset(user_id=user_id1, asset_body=b'relation:asset_0-0') txobj.events[0].set_asset_group(asset_group_id1).create_asset(user_id=user_id1, asset_body=b'event:asset_0-0').add_mandatory_approver(user_id1) transactions[0].add_witness(user_id1) transactions[0].add_signature(user_id=user_id1, keypair=keypair1) for i in range(1, 20): k = i - 1 txobj = bbclib.make_transaction(event_num=1, relation_num=4, witness=True) txobj.relations[0].set_asset_group(asset_group_id1)\ .create_asset(user_id=user_id1, asset_body=b'relation:asset_1-%d' % i) \ .create_pointer(transaction_id=transactions[k].transaction_id, asset_id=transactions[k].relations[0].asset.asset_id) txobj.relations[1].set_asset_group(asset_group_id2) \ .create_asset(user_id=user_id2, asset_body=b'relation:asset_2-%d' % i) \ .create_pointer(transaction_id=transactions[k].transaction_id, asset_id=transactions[k].relations[0].asset.asset_id) \ .create_pointer(transaction_id=transactions[0].transaction_id, asset_id=transactions[0].relations[0].asset.asset_id) txobj.events[0].set_asset_group(asset_group_id1)\ .create_asset(user_id=user_id2, asset_body=b'event:asset_3-%d' % i).add_mandatory_approver(user_id1) ash = [bbclib.get_new_id("assethash%d" % i)[:bbclib.id_length_conf["asset_id"]] for i in range(5)] txobj.relations[2].set_asset_group(asset_group_id1)\ .create_asset_raw(asset_id=ash[0], asset_body=b'relation:asset_4-%d' % i) \ .create_pointer(transaction_id=transactions[0].transaction_id, asset_id=transactions[0].relations[0].asset.asset_id) \ .create_pointer(transaction_id=transactions[0].transaction_id, asset_id=None) txobj.relations[3].set_asset_group(asset_group_id2)\ .create_asset_hash(asset_ids=ash[1:])\ .create_pointer(transaction_id=transactions[0].transaction_id, asset_id=transactions[0].relations[0].asset.asset_id) txobj.create_reference(asset_group_id1, ref_transaction=transactions[i-1], event_index_in_ref=0) txobj.create_cross_ref(transactions[0].transaction_id, domain_id=domain_id) txobj.add_witness(user_id1) txobj.add_witness(user_id2) txobj.add_signature(user_id1, keypair=keypair1, no_pubkey=no_pubkey) txobj.add_signature(user_id2, keypair=keypair2, no_pubkey=no_pubkey) transactions.append(txobj) return transactions
def make_transactions(id_len_conf=None, idlen=None, no_pubkey=False): transactions = list() if id_len_conf is not None: bbclib.configure_id_length(id_len_conf) elif idlen is not None: bbclib.configure_id_length_all(idlen) transactions.append(bbclib.make_transaction(relation_num=1, event_num=1, witness=True, version=2)) bbclib.add_relation_asset(transactions[0], relation_idx=0, asset_group_id=asset_group_id1, user_id=user_id1, asset_body=b'relation:asset_0-0') bbclib.add_event_asset(transactions[0], event_idx=0, asset_group_id=asset_group_id1, user_id=user_id1, asset_body=b'event:asset_0-0') transactions[0].events[0].add(mandatory_approver=user_id1) transactions[0].witness.add_witness(user_id1) sig = transactions[0].sign(keypair=keypair1) transactions[0].witness.add_signature(user_id1, sig) for i in range(1, 20): k = i - 1 transactions.append(bbclib.make_transaction(relation_num=2, event_num=1, witness=True, version=2)) bbclib.add_relation_asset(transactions[i], 0, asset_group_id=asset_group_id1, user_id=user_id1, asset_body=b'relation:asset_1-%d' % i) bbclib.add_relation_pointer(transactions[i], 0, ref_transaction_id=transactions[k].transaction_id, ref_asset_id=transactions[k].relations[0].asset.asset_id) bbclib.add_relation_asset(transactions[i], 1, asset_group_id=asset_group_id2, user_id=user_id2, asset_body=b'relation:asset_2-%d' % i) bbclib.add_relation_pointer(transactions[i], 1, ref_transaction_id=transactions[k].transaction_id, ref_asset_id=transactions[k].relations[0].asset.asset_id) bbclib.add_relation_pointer(transactions[i], 1, ref_transaction_id=transactions[0].transaction_id, ref_asset_id=transactions[0].relations[0].asset.asset_id) bbclib.add_event_asset(transactions[i], event_idx=0, asset_group_id=asset_group_id1, user_id=user_id2, asset_body=b'event:asset_3-%d' % i) ash = [bbclib.get_new_id("assethash%d" % i)[:bbclib.id_length_conf["asset_id"]] for i in range(5)] rtn2 = bbclib.make_relation_with_asset_raw(asset_group_id1, asset_id=ash[0], asset_body=b'relation:asset_4-%d' % i) rtn3 = bbclib.make_relation_with_asset_hash(asset_group_id2, asset_ids=ash[1:]) transactions[i].add(relation=[rtn2, rtn3]) bbclib.add_relation_pointer(transactions[i], 2, ref_transaction_id=transactions[0].transaction_id, ref_asset_id=transactions[0].relations[0].asset.asset_id) bbclib.add_relation_pointer(transactions[i], 2, ref_transaction_id=transactions[0].transaction_id, ref_asset_id=None) bbclib.add_relation_pointer(transactions[i], 3, ref_transaction_id=transactions[0].transaction_id, ref_asset_id=transactions[0].relations[0].asset.asset_id) transactions[i].events[0].add(mandatory_approver=user_id1) bbclib.add_reference_to_transaction(transactions[i], asset_group_id1, transactions[i-1], 0) transactions[i].add(cross_ref=bbclib.BBcCrossRef(domain_id=domain_id, transaction_id=transactions[0].transaction_id)) transactions[i].witness.add_witness(user_id1) transactions[i].witness.add_witness(user_id2) transactions[i].digest() sig1 = transactions[i].sign(keypair=keypair1, no_pubkey=no_pubkey) sig2 = transactions[i].sign(keypair=keypair2, no_pubkey=no_pubkey) transactions[i].witness.add_signature(user_id1, sig1) transactions[i].witness.add_signature(user_id2, sig2) return transactions
def test_01_transaction_with_relation(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=users[0][0], asset_body=b'ccccc') for u in users: transaction1.witness.add_witness(user_id=u[0]) packed_data = transaction1.pack() unpacked_txobj = bbclib.BBcTransaction() unpacked_txobj.unpack(packed_data) print(unpacked_txobj) for u in users: sig = unpacked_txobj.sign(keypair=u[1]) unpacked_txobj.add_signature_object(user_id=u[0], signature=sig) transaction1.add_signature_object(user_id=u[0], signature=sig) original_packed_tx = transaction1.pack() recovered_packed_tx = unpacked_txobj.pack() assert original_packed_tx == recovered_packed_tx print(transaction1)
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)
def test_02_transaction_with_reference_relation(self): print("\n-----", sys._getframe().f_code.co_name, "-----") print("**** prepare original (previous) transaction to refer to") transaction1 = bbclib.make_transaction(event_num=1, witness=True) transaction1.events[0].add(mandatory_approver=users[0][0]) transaction1.events[0].add(mandatory_approver=users[1][0]) bbclib.add_event_asset(transaction1, event_idx=0, asset_group_id=asset_group_id, user_id=users[0][0], asset_body=b"data1") transaction1.witness.add_witness(user_id=users[0][0]) sig = transaction1.sign(keypair=users[0][1]) transaction1.add_signature_object(user_id=users[0][0], signature=sig) transaction1.digest() print(transaction1) transaction2 = bbclib.make_transaction(relation_num=1, witness=True) bbclib.add_relation_asset(transaction2, relation_idx=0, asset_group_id=asset_group_id, user_id=users[0][0], asset_body=b'ccccc') transaction2.witness.add_witness(user_id=users[2][0]) bbclib.add_reference_to_transaction(transaction2, asset_group_id, transaction1, 0) transaction2.witness.add_witness(user_id=users[3][0]) print(transaction2) packed_data = transaction2.pack() unpacked_txobj = bbclib.BBcTransaction() unpacked_txobj.unpack(packed_data) unpacked_txobj.references[0].prepare_reference(transaction1) #print(unpacked_txobj) for i in range(len(users) - 2, -1, -1): sig = unpacked_txobj.sign(keypair=users[i][1]) unpacked_txobj.add_signature_object(user_id=users[i][0], signature=sig) transaction2.add_signature_object(user_id=users[i][0], signature=sig) print(unpacked_txobj) original_packed_tx = transaction2.pack() recovered_packed_tx = unpacked_txobj.pack() assert original_packed_tx == recovered_packed_tx
def make_transactions(id_len_conf={}, idlen=None): global transactions if idlen is None: configure_id_length(id_len_conf) else: bbclib.configure_id_length_all(idlen) transactions[0] = bbclib.make_transaction(relation_num=1, witness=True) bbclib.add_relation_asset(transactions[0], relation_idx=0, asset_group_id=asset_group_id, user_id=user_id, asset_body=b'transaction_0') transactions[0].witness.add_witness(user_id) sig = transactions[0].sign(keypair=keypair1) transactions[0].witness.add_signature(user_id, sig) txdata[0] = bbclib.serialize(transactions[0]) for i in range(1, 20): k = i - 1 transactions[i] = bbclib.make_transaction(relation_num=1, witness=True) bbclib.add_relation_asset(transactions[i], 0, asset_group_id=asset_group_id, user_id=user_id, asset_body=b'transaction_%d' % i) bbclib.add_relation_pointer( transactions[i], 0, ref_transaction_id=transactions[k].transaction_id, ref_asset_id=transactions[k].relations[0].asset.asset_id) transactions[i].witness.add_witness(user_id) sig = transactions[i].sign(keypair=keypair1) transactions[i].witness.add_signature(user_id, sig) txdata[i] = bbclib.serialize(transactions[i]) bbclib.configure_id_length_all(32) for i in range(0, 20): txobj, fmt_type = bbclib.deserialize(txdata[i]) txdata_deserialized[i] = txobj print(txobj.digest().hex())
def test_01_transaction_with_relation_and_witness_and_proof(self): print("\n-----", sys._getframe().f_code.co_name, "-----") transaction1 = bbclib.make_transaction(relation_num=1, witness=True) transaction1.version = 2 bbclib.add_relation_asset(transaction1, relation_idx=0, asset_group_id=asset_group_id, user_id=user_id, asset_body=b'ccccc') rtn1 = BBcRelation(asset_group_id=asset_group_id) rtn2 = BBcRelation(asset_group_id=asset_group_id) transaction1.add(relation=[rtn1, rtn2]) asid = bbclib.get_new_id("assetraw1")[:ID_LEN] asset_raw = BBcAssetRaw(asset_id=asid, asset_body=b'1234567890abcdefg') rtn1.add(asset_raw=asset_raw) ash = [ bbclib.get_new_id("assethash%d" % i)[:ID_LEN] for i in range(1, 4) ] asset_hash = BBcAssetHash(asset_ids=ash) rtn2.add(asset_hash=asset_hash) bbclib.add_relation_pointer( transaction1, relation_idx=0, ref_transaction_id=bbclib.get_new_id("dummy1")) bbclib.add_relation_pointer( transaction1, relation_idx=1, ref_transaction_id=bbclib.get_new_id("dummy2")) bbclib.add_relation_pointer( transaction1, relation_idx=2, ref_transaction_id=bbclib.get_new_id("dummy3")) transaction1.witness.add_witness(user_id=user_id) transaction1.witness.add_witness(user_id=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) digest = transaction1.digest() dat = transaction1.pack() print("Digest:", binascii.b2a_hex(digest)) print("Serialized data:", binascii.b2a_hex(dat)) print(transaction1) transaction_tmp = BBcTransaction() transaction_tmp.unpack(dat) transaction1 = transaction_tmp print(transaction1) assert transaction1.relations[1].asset_raw is not None assert transaction1.relations[1].asset_raw.asset_id == asid assert transaction1.relations[2].asset_hash is not None for i, h in enumerate(transaction1.relations[2].asset_hash.asset_ids): assert ash[i] == h digest = transaction1.digest() ret = transaction1.signatures[0].verify(digest) print("Proof result:", ret) if not ret: print(bbclib.error_text) assert ret
def test_02_create_transaction_by_utility(self): print("\n-----", sys._getframe().f_code.co_name, "-----") transaction1 = bbclib.make_transaction(relation_num=3, witness=True, version=3) ash = [bbclib.get_new_id("assethash%d" % i)[:ID_LEN] for i in range(7)] 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_asset_raw(transaction1, relation_idx=1, asset_group_id=asset_group_id, asset_id=ash[0]) bbclib.add_relation_asset_hash(transaction1, relation_idx=2, asset_group_id=asset_group_id, asset_ids=ash[1:3]) rtn3 = bbclib.make_relation_with_asset(asset_group_id, user_id, asset_body=b'xxxxxx') rtn4 = bbclib.make_relation_with_asset_raw(asset_group_id, asset_id=ash[4], asset_body=b'yyyyyy') rtn5 = bbclib.make_relation_with_asset_hash(asset_group_id, asset_ids=ash[5:]) transaction1.add(relation=[rtn3, rtn4, rtn5]) bbclib.add_relation_pointer( transaction1, relation_idx=0, ref_transaction_id=bbclib.get_new_id("dummy1")) bbclib.add_relation_pointer( transaction1, relation_idx=1, ref_transaction_id=bbclib.get_new_id("dummy2")) bbclib.add_relation_pointer( transaction1, relation_idx=2, ref_transaction_id=bbclib.get_new_id("dummy3")) transaction1.witness.add_witness(user_id=user_id) transaction1.witness.add_witness(user_id=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) digest = transaction1.digest() dat = transaction1.pack() print("Digest:", binascii.b2a_hex(digest)) print("Serialized data:", binascii.b2a_hex(dat)) print(transaction1) transaction_tmp = BBcTransaction() transaction_tmp.unpack(dat) transaction1 = transaction_tmp print(transaction1) assert transaction1.relations[1].asset_raw is not None assert transaction1.relations[1].asset_raw.asset_id == ash[0] assert transaction1.relations[2].asset_hash is not None for i, h in enumerate(transaction1.relations[2].asset_hash.asset_ids): assert ash[i + 1] == h assert transaction1.relations[3].asset.asset_body == b'xxxxxx' assert transaction1.relations[4].asset_raw.asset_id == ash[4] assert len(transaction1.relations[5].asset_hash.asset_ids) == 2 digest = transaction1.digest() ret = transaction1.signatures[0].verify(digest) print("Proof result:", ret) if not ret: print(bbclib.error_text) assert ret