MsgReject.deserialize constructor

MsgReject.deserialize(
  1. Uint8List data
)

Deserializes a MsgReject from byte data.

Implementation

factory MsgReject.deserialize(Uint8List data) {
  final buffer = ByteData.sublistView(data);
  var offset = 0;

  // Command that was rejected (variable length string)
  final cmdLength = VarInt.read(buffer, offset);
  offset += VarInt.size(cmdLength);

  if (offset + cmdLength > data.length) {
    throw WireException('reject', 'Insufficient data for command string');
  }
  final cmdBytes = data.sublist(offset, offset + cmdLength);
  final cmd = utf8.decode(cmdBytes);
  offset += cmdLength;

  // Code indicating why the command was rejected
  if (offset >= data.length) {
    throw WireException('reject', 'Insufficient data for reject code');
  }
  final codeValue = data[offset];
  offset++;

  final code = RejectCode.fromValue(codeValue);
  if (code == null) {
    throw WireException('reject', 'Unknown reject code: $codeValue');
  }

  // Human readable reason string (variable length string)
  final reasonLength = VarInt.read(buffer, offset);
  offset += VarInt.size(reasonLength);

  if (offset + reasonLength > data.length) {
    throw WireException('reject', 'Insufficient data for reason string');
  }
  final reasonBytes = data.sublist(offset, offset + reasonLength);
  final reason = utf8.decode(reasonBytes);
  offset += reasonLength;

  // Hash (only for block/tx rejections)
  Hash? hash;
  if (offset < data.length) {
    if (data.length - offset < hashSize) {
      throw WireException('reject',
          'Insufficient data for hash: need $hashSize, got ${data.length - offset}');
    }
    final hashBytes = data.sublist(offset, offset + hashSize);
    hash = Hash.fromBytes(hashBytes);
  }

  return MsgReject(
    cmd: cmd,
    code: code,
    reason: reason,
    hash: hash,
  );
}