def data(self): """ Retruns the binary format of the data on the condition """ result = bytearray() result.extend(binary.encode(self._min_nr_sig)) result.extend(binary.encode(len(self._unlockhashes))) for unlockhash in self._unlockhashes: result.extend(binary.encode(UnlockHash.from_string(unlockhash))) return result
def test_encode_binary(): """ Test encode binary values """ value = b'hello world' output = binary.encode(value) assert output == value, "Failed to encode binary value to binary"
def test_encode_hex(): """ Test encode hex values """ value = b'hello world' output = binary.encode(value.hex(), type_='hex') assert output == value, "Failed to encode hex value to binary"
def binary(self): """ Encodes the public key into (sia) binary format """ key_value = bytearray() key_value.extend(self._algorithm.binary_specifier) key_value.extend(rbinary.encode(self._pub_key, type_='slice')) return key_value
def binary(self): """ Returns a binary encoded unlockhash """ result = bytearray() result.extend(self._unlock_type) result.extend(binary.encode(self._hash)) return result
def test_encode_list(): """ Test encode list values """ value = [15, True] output = binary.encode(value) expected_output = bytearray(b'\x0f\x00\x00\x00\x00\x00\x00\x00\x01') assert output == expected_output, "Failed to encode list value to binary"
def test_encode_bool(): """ Test encode bool values """ value = True output = binary.encode(value) expected_output = bytearray(b'\x01') assert output == expected_output, "Failed to encode boolean value to binary"
def test_encode_int(): """ Test encode int values """ value = 15 output = binary.encode(value) expected_output = b'\x0f\x00\x00\x00\x00\x00\x00\x00' assert output == expected_output, "Failed to encode integer value to binary"
def test_encode_None(): """ Test encode None value """ # test encoding None values result = binary.encode(None) assert len( result) == 0, "Wrong length of bytearray after encoding None value" assert type(result) == bytearray, "Wrong type after encoding None value"
def test_encode_currency(): """ Test encode currency values """ value = 15000000000000000 expected_output = bytearray( b'\x07\x00\x00\x00\x00\x00\x00\x005Jk\xa7\xa1\x80\x00') output = binary.encode(value, type_='currency') assert output == expected_output, "Failed to encode currency value to binary"
def binary(self): """ Encodes the public key into binary format """ key_value = bytearray() s = bytearray(SPECIFIER_SIZE) s[:len(self._algorithm)] = bytearray(self._algorithm, encoding='utf-8') key_value.extend(s) key_value.extend(binary.encode(self._pub_key, type_='slice')) return key_value
def test_encode_slice(): """ Test encode slice values """ value = [15, True] expected_output = bytearray( b'\x02\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x01' ) output = binary.encode(value, type_='slice') assert output == expected_output, "Failed to encode slice value to binary"
def _binary(self, encoder): result = bytearray() result.extend(self._type) condition_binary = bytearray() condition_binary.extend(encoder.encode(self._min_nr_sig)) condition_binary.extend(encoder.encode(len(self._unlockhashes))) for unlockhash in self._unlockhashes: condition_binary.extend( encoder.encode(UnlockHash.from_string(unlockhash))) result.extend(binary.encode(condition_binary, type_='slice')) return result
def hash(data, encoding_type=None): """ Hashes the input binary data using the blake2b algorithm @param data: Input data to be hashed @param encoding_type: Type of the data to guide the binary encoding before hashing @returns: Hashed value of the input data """ binary_data = binary.encode(data, type_=encoding_type) h = blake2b(binary_data, digest_size=HASH_SIZE).digest() return h
def test_encode_object(): """ Test encode custom object that implement the binary property """ expected_output = b'custom object here' class CustomObj: @property def binary(self): return expected_output output = binary.encode(CustomObj()) assert output == expected_output, "Failed to encode custom value to binary"
def test_encode_unknown_type(): """ Test encode unknown data type """ with pytest.raises(ValueError): binary.encode('hello', type_='not supported type')