toBytes method

Uint8List toBytes()

returns a serialized byte version of the script

Implementation

Uint8List toBytes() {
  DynamicByteTracker scriptBytes = DynamicByteTracker();
  try {
    for (var token in script) {
      if (OP_CODES.containsKey(token)) {
        scriptBytes.add(OP_CODES[token]!);
      } else if (token is int && token >= 0 && token <= 16) {
        scriptBytes.add(OP_CODES['OP_$token']!);
      } else {
        if (token is int) {
          scriptBytes.add(pushInteger(token));
        } else {
          scriptBytes.add(opPushData(token));
        }
      }
    }

    return scriptBytes.toBytes();
  } finally {
    scriptBytes.close();
  }
}