def pack(self): """Pack this object""" if self.not_initialized: dat = bytearray(bbclib_binary.to_4byte(KeyType.NOT_INITIALIZED)) return bytes(dat) dat = bytearray(bbclib_binary.to_4byte(self.key_type)) if self.pubkey is None: dat.extend(bbclib_binary.to_4byte(0)) else: pubkey_len_bit = len(self.pubkey) * 8 dat.extend(bbclib_binary.to_4byte(pubkey_len_bit)) dat.extend(self.pubkey) sig_len_bit = len(self.signature) * 8 dat.extend(bbclib_binary.to_4byte(sig_len_bit)) dat.extend(self.signature) return bytes(dat)
def pack(self, for_digest_calculation=False): """Pack this object Args: for_digest_calculation (bool): True if digest calculation Returns: bytes: packed binary data """ dat = bytearray() if not for_digest_calculation: dat.extend( bbclib_binary.to_bigint(self.asset_id, size=self.idlen_conf["asset_id"])) dat.extend( bbclib_binary.to_bigint(self.user_id, size=self.idlen_conf["user_id"])) dat.extend(bbclib_binary.to_2byte(len(self.nonce))) dat.extend(self.nonce) dat.extend(bbclib_binary.to_4byte(self.asset_file_size)) if self.asset_file_size > 0: dat.extend(bbclib_binary.to_bigint(self.asset_file_digest)) if isinstance(self.asset_body, dict): dat.extend(bbclib_binary.to_2byte(1)) astbdy = msgpack.dumps(self.asset_body) dat.extend(bbclib_binary.to_2byte(len(astbdy))) dat.extend(astbdy) else: dat.extend(bbclib_binary.to_2byte(0)) dat.extend(bbclib_binary.to_2byte(self.asset_body_size)) if self.asset_body_size > 0: dat.extend(self.asset_body) return bytes(dat)
def pack(self, for_id=False): """Pack the whole parts""" dat = bytearray(bbclib_binary.to_4byte(self.version)) dat.extend(bbclib_binary.to_8byte(self.timestamp)) if self.version != 0: dat.extend( bbclib_binary.to_2byte(self.idlen_conf["transaction_id"])) dat.extend(bbclib_binary.to_2byte(len(self.events))) for i in range(len(self.events)): evt = self.events[i].pack() dat.extend(bbclib_binary.to_4byte(len(evt))) dat.extend(evt) dat.extend(bbclib_binary.to_2byte(len(self.references))) for i in range(len(self.references)): refe = self.references[i].pack() dat.extend(bbclib_binary.to_4byte(len(refe))) dat.extend(refe) dat.extend(bbclib_binary.to_2byte(len(self.relations))) for i in range(len(self.relations)): rtn = self.relations[i].pack() dat.extend(bbclib_binary.to_4byte(len(rtn))) dat.extend(rtn) if self.witness is not None: dat.extend(bbclib_binary.to_2byte(1)) witness = self.witness.pack() dat.extend(bbclib_binary.to_4byte(len(witness))) dat.extend(witness) else: dat.extend(bbclib_binary.to_2byte(0)) self.transaction_base_digest = hashlib.sha256(dat).digest() dat_cross = bytearray() if self.cross_ref is not None: cross = self.cross_ref.pack() dat_cross.extend(bbclib_binary.to_2byte(1)) dat_cross.extend(bbclib_binary.to_4byte(len(cross))) dat_cross.extend(cross) else: dat_cross.extend(bbclib_binary.to_2byte(0)) if for_id: dat_for_id = bytearray(self.transaction_base_digest) dat_for_id.extend(dat_cross) return bytes(dat_for_id) dat.extend(dat_cross) dat.extend(bbclib_binary.to_2byte(len(self.signatures))) for signature in self.signatures: sig = signature.pack() dat.extend(bbclib_binary.to_4byte(len(sig))) dat.extend(sig) self.transaction_data = bytes(dat) return self.transaction_data
def verify_using_cross_ref(domain_id, transaction_id, transaction_base_digest, cross_ref_data, sigdata): """Confirm the existence of the transaction using cross_ref Args: 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): packed BBcCrossRef object sigdata (bytes): packed signature Returns: bool: True if valid """ cross = BBcCrossRef(unpack=cross_ref_data) if cross.domain_id != domain_id or cross.transaction_id != transaction_id: return False dat = bytearray(transaction_base_digest) dat.extend(bbclib_binary.to_2byte(1)) dat.extend(bbclib_binary.to_4byte(len(cross_ref_data))) dat.extend(cross.pack()) digest = hashlib.sha256(bytes(dat)).digest() sig = BBcSignature(unpack=sigdata) return sig.verify(digest) == 1
def pack(self): """Pack this object Returns: bytes: packed binary data """ if self.asset_group_id is None: raise Exception("need asset_group_id in BBcEvent") dat = bytearray(bbclib_binary.to_bigint(self.asset_group_id, size=self.idlen_conf["asset_group_id"])) dat.extend(bbclib_binary.to_2byte(len(self.reference_indices))) for i in range(len(self.reference_indices)): dat.extend(bbclib_binary.to_2byte(self.reference_indices[i])) dat.extend(bbclib_binary.to_2byte(len(self.mandatory_approvers))) for i in range(len(self.mandatory_approvers)): dat.extend(bbclib_binary.to_bigint(self.mandatory_approvers[i], size=self.idlen_conf["user_id"])) dat.extend(bbclib_binary.to_2byte(self.option_approver_num_numerator)) dat.extend(bbclib_binary.to_2byte(self.option_approver_num_denominator)) for i in range(self.option_approver_num_denominator): dat.extend(bbclib_binary.to_bigint(self.option_approvers[i], size=self.idlen_conf["user_id"])) ast = self.asset.pack() dat.extend(bbclib_binary.to_4byte(len(ast))) dat.extend(ast) return bytes(dat)
def pack(self): """Pack this object Returns: bytes: packed binary data """ if self.asset_group_id is None: raise Exception("need asset_group_id in BBcRelation") dat = bytearray( bbclib_binary.to_bigint(self.asset_group_id, size=self.idlen_conf["asset_group_id"])) dat.extend(bbclib_binary.to_2byte(len(self.pointers))) for i in range(len(self.pointers)): pt = self.pointers[i].pack() dat.extend(bbclib_binary.to_2byte(len(pt))) dat.extend(pt) if self.asset is not None: ast = self.asset.pack() dat.extend(bbclib_binary.to_4byte(len(ast))) dat.extend(ast) else: dat.extend(bbclib_binary.to_4byte(0)) if self.version >= 2: if self.asset_raw is not None: ast = self.asset_raw.pack() dat.extend(bbclib_binary.to_4byte(len(ast))) dat.extend(ast) else: dat.extend(bbclib_binary.to_4byte(0)) if self.asset_hash is not None: ast = self.asset_hash.pack() dat.extend(bbclib_binary.to_4byte(len(ast))) dat.extend(ast) else: dat.extend(bbclib_binary.to_4byte(0)) return bytes(dat)