bbc1.core.bbclib module

Copyright (c) 2017 beyond-blockchain.org.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

class bbc1.core.bbclib.BBcAsset(user_id=None, asset_file=None, asset_body=None, format_type=0, id_length=32)[source]

Bases: object

Asset part in a transaction

add(user_id=None, asset_file=None, asset_body=None)[source]

Add parts in this object

deserialize(data)[source]

Deserialize into this object

Parameters:data (bytes) – serialized binary data
Returns:True if successful
Return type:bool
deserialize_obj(obj)[source]

Deserialize bson/msgpack data into this object

Parameters:obj (bytes) – object data
Returns:True if successful
Return type:bool
digest()[source]

Calculate the digest

The digest corresponds to the asset_id of this object

Returns:asset_id (or digest)
Return type:bytes
get_asset_file()[source]

Get asset file content and its digest

Returns:digest of the file content bytes: the file content
Return type:bytes
get_dict(for_digest_calculation=False)[source]

Serialize this object

recover_asset_file(asset_file, id_length=32)[source]

Recover asset file info from the given raw content

serialize(for_digest_calculation=False)[source]

Serialize this object

Parameters:for_digest_calculation (bool) – True if digest calculation
Returns:serialized binary data
Return type:bytes
class bbc1.core.bbclib.BBcCrossRef(domain_id=None, transaction_id=None, deserialize=None, format_type=0)[source]

Bases: object

CrossRef part in a transaction

deserialize(data)[source]

Deserialize into this object

Parameters:data (bytes) – serialized binary data
Returns:True if successful
Return type:bool
deserialize_obj(obj)[source]

Deserialize bson/msgpack data into this object

Parameters:obj (bytes) – object data
Returns:True if successful
Return type:bool
get_dict()[source]

Serialize this object into bson format

serialize()[source]

Serialize this object

Returns:serialized binary data
Return type:bytes
class bbc1.core.bbclib.BBcEvent(asset_group_id=None, format_type=0, id_length=32)[source]

Bases: object

Event part in a transaction

add(asset_group_id=None, reference_index=None, mandatory_approver=None, option_approver_num_numerator=0, option_approver_num_denominator=0, option_approver=None, asset=None)[source]

Add parts

deserialize(data)[source]

Deserialize into this object

Parameters:data (bytes) – serialized binary data
Returns:True if successful
Return type:bool
deserialize_obj(obj)[source]

Deserialize bson/msgpack data into this object

Parameters:obj (bytes) – object data
Returns:True if successful
Return type:bool
get_dict()[source]

Serialize this object

serialize()[source]

Serialize this object

Returns:serialized binary data
Return type:bytes
class bbc1.core.bbclib.BBcFormat[source]

Bases: object

FORMAT_BINARY = 0
FORMAT_BSON = 1
FORMAT_BSON_COMPRESS_BZ2 = 2
FORMAT_BSON_COMPRESS_ZLIB = 3
FORMAT_MSGPACK = 4
FORMAT_MSGPACK_COMPRESS_BZ2 = 5
FORMAT_MSGPACK_COMPRESS_ZLIB = 6
class bbc1.core.bbclib.BBcPointer(transaction_id=None, asset_id=None, format_type=0, id_length=32)[source]

Bases: object

Pointer part in a transaction

add(transaction_id=None, asset_id=None)[source]

Add parts

deserialize(data)[source]

Deserialize into this object

Parameters:data (bytes) – serialized binary data
Returns:True if successful
Return type:bool
deserialize_obj(obj)[source]

Deserialize bson/msgpack data into this object

Parameters:obj (bytes) – object data
Returns:True if successful
Return type:bool
get_dict()[source]

Serialize this object

serialize()[source]

Serialize this object

Returns:serialized binary data
Return type:bytes
class bbc1.core.bbclib.BBcReference(asset_group_id, transaction, ref_transaction=None, event_index_in_ref=0, format_type=0, id_length=32)[source]

Bases: object

Reference part in a transaction

add_signature(user_id=None, signature=None)[source]

Add signature in the reserved space

Parameters:
  • user_id (bytes) – user_id of the signature owner
  • signature (BBcSignature) – signature
deserialize(data)[source]

Deserialize into this object

Parameters:data (bytes) – serialized binary data
Returns:True if successful
Return type:bool
deserialize_obj(obj)[source]

Deserialize bson/msgpack data into this object

Parameters:obj (bytes) – object data
Returns:True if successful
Return type:bool
get_destinations()[source]

Return the list of approvers in the referred transaction

get_dict()[source]

Serialize this object

get_referred_transaction()[source]

Return referred transaction in serialized format

prepare_reference(ref_transaction)[source]

Read the previous referencing transaction

serialize()[source]

Serialize this object

Returns:serialized binary data
Return type:bytes
class bbc1.core.bbclib.BBcRelation(asset_group_id=None, format_type=0, id_length=32)[source]

Bases: object

Relation part in a transaction

add(asset_group_id=None, asset=None, pointer=None)[source]

Add parts

deserialize(data)[source]

Deserialize bson data into this object

Parameters:data (dict) – bson data
Returns:True if successful
Return type:bool
deserialize_obj(obj)[source]

Deserialize bson/msgpack data into this object

Parameters:data (bytes) – object data
Returns:True if successful
Return type:bool
get_dict()[source]

Serialize this object

serialize()[source]

Serialize this object

Returns:serialized binary data
Return type:bytes
class bbc1.core.bbclib.BBcSignature(key_type=2, deserialize=None, format_type=0)[source]

Bases: object

Signature part in a transaction

add(signature=None, pubkey=None)[source]

Add signature and public key

deserialize(data)[source]

Deserialize into this object

Parameters:data (bytes) – serialized binary data
Returns:True if successful
Return type:bool
deserialize_obj(obj)[source]

Deserialize bson/msgpack data into this object

Parameters:obj (bytes) – object data
Returns:True if successful
Return type:bool
get_dict()[source]

Serialize this object

serialize()[source]

Serialize this object

verify(digest)[source]

Verify digest using pubkey in signature

Parameters:digest (bytes) – digest to verify
Returns:0:invalid, 1:valid
Return type:int
class bbc1.core.bbclib.BBcTransaction(version=1, deserialize=None, format_type=0, id_length=32)[source]

Bases: object

Transaction object

add(event=None, reference=None, relation=None, witness=None, cross_ref=None)[source]

Add parts

add_signature(user_id=None, signature=None)[source]

Add signature in the reserved space

Parameters:
  • user_id (bytes) – user_id of the signature owner
  • signature (BBcSignature) – signature
Returns:

True if successful

Return type:

bool

deserialize(data)[source]

Deserialize into this object

Parameters:data (bytes) – serialized binary data
Returns:True if successful
Return type:bool
deserialize_obj(data)[source]

Deserialize bson/msgpack data into this object

Parameters:data (bytes) – object data
Returns:True if successful
Return type:bool
digest()[source]

Calculate the digest

The digest corresponds to the transaction_id of this object

Returns:transaction_id (or digest)
Return type:bytes
get_sig_index(user_id)[source]

Reserve a space for signature for the specified user_id

Parameters:user_id (bytes) – user_id whose signature will be added to the signature part
Returns:position (index) in the signature part
Return type:int
serialize(for_id=False)[source]

Serialize the whole parts

serialize_obj(for_id=False, no_header=False)[source]

Serialize the whole parts

set_format_type(format_type)[source]
sign(key_type=2, private_key=None, public_key=None, keypair=None)[source]

Sign the transaction

Parameters:
  • key_type (int) – Type of encryption key’s curve
  • private_key (bytes) –
  • public_key (bytes) –
  • keypair (KeyPair) – keypair or set of private_key and public_key needs to be given
Returns:

Return type:

BBcSignature

class bbc1.core.bbclib.BBcWitness(format_type=0, id_length=32)[source]

Bases: object

Witness part in a transaction

add_signature(user_id=None, signature=None)[source]

Add signature in the reserved space for the user_id that was registered before

Parameters:
  • user_id (bytes) – user_id of the signature owner
  • signature (bytes) – signature
add_witness(user_id)[source]

Register user_id in the list

deserialize(data)[source]

Deserialize into this object

Parameters:data (bytes) – serialized binary data
Returns:True if successful
Return type:bool
deserialize_obj(obj)[source]

Deserialize bson/msgpack data into this object

Parameters:obj (bytes) – object data
Returns:True if successful
Return type:bool
get_dict()[source]

Serialize this object

serialize()[source]

Serialize this object

Returns:serialized binary data
Return type:bytes
class bbc1.core.bbclib.KeyPair(curvetype=2, compression=False, privkey=None, pubkey=None)[source]

Bases: object

POINT_CONVERSION_COMPRESSED = 2
POINT_CONVERSION_UNCOMPRESSED = 4

Key pair container

generate()[source]

Generate a new key pair

get_private_key_in_der()[source]

Return private key in DER format

get_private_key_in_pem()[source]

Return private key in PEM format

get_public_key_in_pem()[source]

Return public key in PEM format

import_publickey_cert_pem(cert_pemstring, privkey_pemstring=None)[source]

Verify and import X509 public key certificate in pem format

mk_keyobj_from_private_key()[source]

Make a keypair object from the binary data of private key

mk_keyobj_from_private_key_der(derdat)[source]

Make a keypair object from the private key in DER format

mk_keyobj_from_private_key_pem(pemdat_string)[source]

Make a keypair object from the private key in PEM format

sign(digest)[source]

Sign to the given value

Parameters:digest (bytes) – given value
Returns:signature
Return type:bytes
to_binary(dat)[source]
verify(digest, sig)[source]

Verify the digest and the signature using the rivate key in this object

class bbc1.core.bbclib.KeyType[source]

Bases: object

ECDSA_P256v1 = 2
ECDSA_SECP256k1 = 1
NOT_INITIALIZED = 0
class bbc1.core.bbclib.MsgType[source]

Bases: object

Message types for between core node and client

CANCEL_INSERT_NOTIFICATION = 16
DOMAIN_PING = 12
MESSAGE = 66
NOTIFY_CROSS_REF = 74
NOTIFY_DOMAIN_KEY_UPDATE = 19
NOTIFY_INSERTED = 73
REGISTER = 64
REQUEST_CLOSE_DOMAIN = 31
REQUEST_COUNT_TRANSACTIONS = 95
REQUEST_CROSS_REF_LIST = 92
REQUEST_CROSS_REF_VERIFY = 90
REQUEST_ECDH_KEY_EXCHANGE = 33
REQUEST_GATHER_SIGNATURE = 67
REQUEST_GET_CONFIG = 8
REQUEST_GET_DOMAINLIST = 13
REQUEST_GET_FORWARDING_LIST = 25
REQUEST_GET_NEIGHBORLIST = 21
REQUEST_GET_NODEID = 27
REQUEST_GET_NOTIFICATION_LIST = 29
REQUEST_GET_STATS = 17
REQUEST_GET_USERS = 23
REQUEST_INSERT = 71
REQUEST_INSERT_NOTIFICATION = 15
REQUEST_MANIP_LEDGER_SUBSYS = 10
REQUEST_REGISTER_HASH_IN_SUBSYS = 128
REQUEST_REPAIR = 94
REQUEST_SEARCH_TRANSACTION = 82
REQUEST_SEARCH_WITH_CONDITIONS = 86
REQUEST_SETUP_DOMAIN = 0
REQUEST_SET_STATIC_NODE = 4
REQUEST_SIGNATURE = 69
REQUEST_TRAVERSE_TRANSACTIONS = 88
REQUEST_VERIFY_HASH_IN_SUBSYS = 130
RESPONSE_CLOSE_DOMAIN = 32
RESPONSE_COUNT_TRANSACTIONS = 95
RESPONSE_CROSS_REF_LIST = 93
RESPONSE_CROSS_REF_VERIFY = 91
RESPONSE_ECDH_KEY_EXCHANGE = 34
RESPONSE_GATHER_SIGNATURE = 68
RESPONSE_GET_CONFIG = 9
RESPONSE_GET_DOMAINLIST = 14
RESPONSE_GET_FORWARDING_LIST = 26
RESPONSE_GET_NEIGHBORLIST = 22
RESPONSE_GET_NODEID = 28
RESPONSE_GET_NOTIFICATION_LIST = 30
RESPONSE_GET_STATS = 18
RESPONSE_GET_USERS = 24
RESPONSE_INSERT = 72
RESPONSE_MANIP_LEDGER_SUBSYS = 11
RESPONSE_REGISTER_HASH_IN_SUBSYS = 129
RESPONSE_SEARCH_TRANSACTION = 83
RESPONSE_SEARCH_WITH_CONDITIONS = 87
RESPONSE_SETUP_DOMAIN = 1
RESPONSE_SET_STATIC_NODE = 5
RESPONSE_SIGNATURE = 70
RESPONSE_TRAVERSE_TRANSACTIONS = 89
RESPONSE_VERIFY_HASH_IN_SUBSYS = 131
UNREGISTER = 65
bbc1.core.bbclib.add_event_asset(transaction, event_idx, asset_group_id, user_id, asset_body=None, asset_file=None)[source]

Utility to add BBcEvent object with BBcAsset in the transaction

bbc1.core.bbclib.add_pointer_in_relation(relation, ref_transaction_id=None, ref_asset_id=None)[source]

Utility to add BBcRelation object with BBcPointer in the BBcRelation object

bbc1.core.bbclib.add_reference_to_transaction(transaction, asset_group_id, ref_transaction_obj, event_index_in_ref)[source]

Utility to add BBcReference object in the transaction

Returns:
Return type:BBcReference
bbc1.core.bbclib.add_relation_asset(transaction, relation_idx, asset_group_id, user_id, asset_body=None, asset_file=None)[source]

Utility to add BBcRelation object with BBcAsset in the transaction

bbc1.core.bbclib.add_relation_pointer(transaction, relation_idx, ref_transaction_id=None, ref_asset_id=None)[source]

Utility to add BBcRelation object with BBcPointer in the transaction

bbc1.core.bbclib.bin2str_base64(dat)[source]
bbc1.core.bbclib.convert_id_to_string(data, bytelen=32)[source]

Convert binary data to hex string

bbc1.core.bbclib.convert_idstring_to_bytes(datastr, bytelen=32)[source]

Convert hex string to binary data

bbc1.core.bbclib.deep_copy_with_key_stringify(u, d=None)[source]

Utility for updating nested dictionary

bbc1.core.bbclib.get_bigint(ptr, dat)[source]
bbc1.core.bbclib.get_n_byte_int(ptr, n, dat)[source]
bbc1.core.bbclib.get_n_bytes(ptr, n, dat)[source]
bbc1.core.bbclib.get_new_id(seed_str=None, include_timestamp=True)[source]

Return 256-bit binary data

Parameters:
  • seed_str (str) – seed string that is hashed by SHA256
  • include_timestamp (bool) – if True, timestamp (current time) is appended to the seed string
Returns:

256-bit binary

Return type:

bytes

bbc1.core.bbclib.get_random_id()[source]

Return 256-bit binary data

Returns:256-bit random binary
Return type:bytes
bbc1.core.bbclib.get_random_value(length=32)[source]

Return 1-byte random value

bbc1.core.bbclib.make_relation_with_asset(asset_group_id, user_id, asset_body=None, asset_file=None, format_type=0, id_length=32)[source]

Utility to make BBcRelation object

bbc1.core.bbclib.make_transaction(event_num=0, relation_num=0, witness=False, format_type=0, id_length=32)[source]

Utility to make transaction object

Parameters:
  • event_num (int) – the number of BBcEvent object to include in the transaction
  • relation_num (int) – the number of BBcRelation object to include in the transaction
  • witness (bool) – If true, BBcWitness object is included in the transaction
  • format_type (int) – Data format defined in BBcFormat class
  • id_length (int) – If <32, IDs will be truncated
Returns:

Return type:

BBcTransaction

bbc1.core.bbclib.recover_signature_object(data, format_type=0)[source]

Deserialize signature data

bbc1.core.bbclib.reset_error()[source]
bbc1.core.bbclib.set_error(code=-1, txt='')[source]
bbc1.core.bbclib.str_binary(dat)[source]
bbc1.core.bbclib.to_1byte(val)[source]
bbc1.core.bbclib.to_2byte(val)[source]
bbc1.core.bbclib.to_4byte(val)[source]
bbc1.core.bbclib.to_8byte(val)[source]
bbc1.core.bbclib.to_bigint(val, size=32)[source]
bbc1.core.bbclib.validate_transaction_object(txobj, asset_files=None)[source]

Validate transaction and its asset

Parameters:
  • txobj (BBcTransaction) – target transaction object
  • asset_files (dict) – dictionary containing the asset file contents
Returns:

True if valid tuple: list of valid assets tuple: list of invalid assets

Return type:

bool

bbc1.core.bbclib.verify_using_cross_ref(domain_id, transaction_id, transaction_base_digest, cross_ref_data, sigdata, format_type=0)[source]

Confirm the existence of the transaction using cross_ref

Parameters:
  • domain_id (bytes) – target domain_id
  • transaction_id (bytes) – target transaction_id of which existence you want to confirm
  • transaction_base_digest (bytes) – digest obtained from the outer domain
  • cross_ref_data (bytes) – serialized BBcCrossRef object
  • sigdata (bytes) – serialized signature
  • format_type (int) – Data format type when calculating the digest (transaction_id)
Returns:

True if valid

Return type:

bool