Buffer
- class cometa.buffer.Buffer(ptr)[source]
Bases:
objectA dynamic, reference-counted byte buffer with configurable exponential growth.
This class wraps the C cardano_buffer_t type. It behaves similarly to a mutable Python bytearray, offering automatic resizing, slicing, and binary data manipulation.
- classmethod new(capacity)[source]
Creates a new dynamic buffer with the specified initial capacity.
- Parameters:
capacity (int) – The initial allocation size in bytes.
- Returns:
An empty buffer with the reserved capacity.
- Return type:
- classmethod from_bytes(data)[source]
Creates a new buffer initialized with a copy of the given data.
- Parameters:
data (bytes) – The raw bytes to copy into the buffer.
- Returns:
A new buffer containing the data.
- Return type:
- classmethod from_hex(hex_string)[source]
Creates a new buffer by decoding a given hex string.
- Parameters:
hex_string (str) – A hexadecimal string (e.g., “deadbeef”).
- Returns:
A new buffer containing the decoded bytes.
- Return type:
- property size: int
Returns the current number of used bytes in the buffer.
- property capacity: int
Returns the total allocated memory capacity of the buffer.
- compare(other)[source]
Compares two buffer objects lexicographically.
- Returns:
< 0 if self < other, 0 if equal, > 0 if self > other.
- Return type:
int
- Parameters:
other (Buffer)
- to_hex()[source]
Returns the hexadecimal string representation (e.g., ‘deadbeef’).
- Return type:
str
- to_str()[source]
Converts the buffer content to a UTF-8 string. Assumes the buffer contains valid UTF-8 encoded text.
- Return type:
str
- set_size(size)[source]
Sets the logical size of the buffer.
Warning: This does not allocate new memory; it only updates the internal usage marker. The new size must not exceed the current capacity.
- Parameters:
size (int) – The new size.
- Return type:
None
- memzero()[source]
Securely wipes the contents of the buffer from memory. Useful for clearing sensitive data like private keys.
- Return type:
None
- write(data)[source]
Appends raw bytes to the end of the buffer. The buffer will automatically resize if necessary.
- Parameters:
data (bytes)
- Return type:
None
- read(length)[source]
Reads a specified amount of data from the current cursor position.
- Parameters:
length (int) – Number of bytes to read.
- Returns:
The data read.
- Return type:
bytes
- seek(position)[source]
Repositions the internal cursor within the buffer.
- Parameters:
position (int) – The offset to seek to.
- Return type:
None
- write_uint16(value, order=ByteOrder.LITTLE_ENDIAN)[source]
Writes a 16-bit unsigned integer to the buffer.
- Parameters:
value (int)
order (ByteOrder)
- Return type:
None
- write_uint32(value, order=ByteOrder.LITTLE_ENDIAN)[source]
Writes a 32-bit unsigned integer to the buffer.
- Parameters:
value (int)
order (ByteOrder)
- Return type:
None
- write_uint64(value, order=ByteOrder.LITTLE_ENDIAN)[source]
Writes a 64-bit unsigned integer to the buffer.
- Parameters:
value (int)
order (ByteOrder)
- Return type:
None
- write_int16(value, order=ByteOrder.LITTLE_ENDIAN)[source]
Writes a 16-bit signed integer to the buffer.
- Parameters:
value (int)
order (ByteOrder)
- Return type:
None
- write_int32(value, order=ByteOrder.LITTLE_ENDIAN)[source]
Writes a 32-bit signed integer to the buffer.
- Parameters:
value (int)
order (ByteOrder)
- Return type:
None
- write_int64(value, order=ByteOrder.LITTLE_ENDIAN)[source]
Writes a 64-bit signed integer to the buffer.
- Parameters:
value (int)
order (ByteOrder)
- Return type:
None
- write_float(value, order=ByteOrder.LITTLE_ENDIAN)[source]
Writes a 32-bit floating point number to the buffer.
- Parameters:
value (float)
order (ByteOrder)
- Return type:
None
- write_double(value, order=ByteOrder.LITTLE_ENDIAN)[source]
Writes a 64-bit floating point number (double) to the buffer.
- Parameters:
value (float)
order (ByteOrder)
- Return type:
None
- read_uint16(order=ByteOrder.LITTLE_ENDIAN)[source]
Reads a 16-bit unsigned integer from the current position.
- Parameters:
order (ByteOrder)
- Return type:
int
- read_uint32(order=ByteOrder.LITTLE_ENDIAN)[source]
Reads a 32-bit unsigned integer from the current position.
- Parameters:
order (ByteOrder)
- Return type:
int
- read_uint64(order=ByteOrder.LITTLE_ENDIAN)[source]
Reads a 64-bit unsigned integer from the current position.
- Parameters:
order (ByteOrder)
- Return type:
int
- read_int16(order=ByteOrder.LITTLE_ENDIAN)[source]
Reads a 16-bit signed integer from the current position.
- Parameters:
order (ByteOrder)
- Return type:
int
- read_int32(order=ByteOrder.LITTLE_ENDIAN)[source]
Reads a 32-bit signed integer from the current position.
- Parameters:
order (ByteOrder)
- Return type:
int
- read_int64(order=ByteOrder.LITTLE_ENDIAN)[source]
Reads a 64-bit signed integer from the current position.
- Parameters:
order (ByteOrder)
- Return type:
int
- read_float(order=ByteOrder.LITTLE_ENDIAN)[source]
Reads a 32-bit floating point number from the current position.
- Parameters:
order (ByteOrder)
- Return type:
float
- read_double(order=ByteOrder.LITTLE_ENDIAN)[source]
Reads a 64-bit floating point number from the current position.
- Parameters:
order (ByteOrder)
- Return type:
float
- set_last_error(message)[source]
Records an error message in the buffer’s error register.
- Parameters:
message (str)
- Return type:
None
- get_last_error()[source]
Retrieves the last error message recorded for this buffer.
- Return type:
str
- __init__(ptr)[source]
Internal constructor.
Use factories like Buffer.new(), Buffer.from_bytes(), etc. instead.
- Return type:
None
- __bytes__()[source]
Converts the buffer content to a Python immutable bytes object.
- Return type:
bytes
- __getitem__(key: int) int[source]
- __getitem__(key: slice) Buffer
Retrieve a byte or a slice of the buffer.
- Parameters:
key (int | slice) – The index or slice range.
- Returns:
If key is an int, returns the byte value (0-255). Buffer: If key is a slice, returns a new Buffer containing the slice.
- Return type:
int
- __setitem__(key, value)[source]
Modifies a byte at the specified index.
- Parameters:
key (int) – The index to modify.
value (int) – The new byte value (0-255).
- Return type:
None
- __eq__(other)[source]
Checks if two buffers contain identical data.
- Parameters:
other (object)
- Return type:
bool
- __lt__(other)[source]
Returns True if this buffer is lexicographically less than the other.
- Parameters:
other (Buffer)
- Return type:
bool
- __le__(other)[source]
Returns True if this buffer is lexicographically less than or equal to the other.
- Parameters:
other (Buffer)
- Return type:
bool
- __gt__(other)[source]
Returns True if this buffer is lexicographically greater than the other.
- Parameters:
other (Buffer)
- Return type:
bool
- __hash__ = None