decompile method

List? decompile(
  1. dynamic _buffer
)

Decompile script

_buffer should be Uint8List or List

Implementation

List<dynamic>? decompile(dynamic _buffer) {
  if (_buffer is List && _buffer is! Uint8List) {
    return _buffer;
  } else if (_buffer is! Uint8List) {
    throw ArgumentError('Buffer must be an Uint8List or List<Uint8List>');
  }

  final List<dynamic> chunks = [];
  int i = 0;

  while (i < _buffer.lengthInBytes) {
    final opcode = _buffer[i];

    // data chunk
    if (opcode > OPS['OP_0']! && opcode <= OPS['OP_PUSHDATA4']!) {
      final d = pushdata.decode(_buffer, i);

      // did reading a pushDataInt fail?
      if (d == null) return null;
      i += d.size;

      // attempt to read too much data?
      if (i + d.number > _buffer.lengthInBytes) return null;

      final data = _buffer.sublist(i, i + d.number);
      i += d.number;

      // decompile minimally
      final op = _asMinimalOP(data);
      if (op != null) {
        chunks.add(op);
      } else {
        chunks.add(data);
      }

      // opcode
    } else {
      chunks.add(opcode);

      i += 1;
    }
  }

  return chunks;
}