fromRaw static method

Script fromRaw({
  1. required String hexData,
  2. bool hasSegwit = false,
})

Implementation

static Script fromRaw({required String hexData, bool hasSegwit = false}) {
  List<String> commands = [];
  int index = 0;
  final scriptraw = hexToBytes(hexData);
  while (index < scriptraw.length) {
    int byte = scriptraw[index];
    if (CODE_OPS.containsKey(byte)) {
      commands.add(CODE_OPS[byte]!);
      index = index + 1;
    } else if (!hasSegwit && byte == 0x4c) {
      int bytesToRead = scriptraw[index + 1];
      index = index + 1;
      commands.add(scriptraw
          .sublist(index, index + bytesToRead)
          .map((b) => b.toRadixString(16).padLeft(2, '0'))
          .join());
      index = index + bytesToRead;
    } else if (!hasSegwit && byte == 0x4d) {
      int bytesToRead = ByteData.sublistView(scriptraw, index + 1, index + 3)
          .getUint16(0, Endian.little);
      index = index + 3;
      commands.add(scriptraw
          .sublist(index, index + bytesToRead)
          .map((b) => b.toRadixString(16).padLeft(2, '0'))
          .join());
      index = index + bytesToRead;
    } else if (!hasSegwit && byte == 0x4e) {
      int bytesToRead = ByteData.sublistView(scriptraw, index + 1, index + 5)
          .getUint32(0, Endian.little);
      index = index + 5;
      commands.add(scriptraw
          .sublist(index, index + bytesToRead)
          .map((b) => b.toRadixString(16).padLeft(2, '0'))
          .join());
      index = index + bytesToRead;
    } else {
      final viAndSize = viToInt(scriptraw.sublist(index, index + 9));
      int dataSize = viAndSize.item1;
      int size = viAndSize.item2;
      final lastIndex = (index + size + dataSize) > scriptraw.length
          ? scriptraw.length
          : (index + size + dataSize);
      commands.add(bytesToHex(scriptraw.sublist(index + size, lastIndex)));
      index = index + dataSize + size;
    }
  }
  return Script(script: commands);
}