def serialize(script_string):
    '''
    str -> bytearray
    '''
    string_tokens = script_string.split()
    serialized_script = bytearray()

    for token in string_tokens:
        if token == 'OP_CODESEPARATOR' or token == 'OP_PUSHDATA4':
            raise NotImplementedError('{} is a bad idea.'.format(token))

        if token in riemann.network.CODE_TO_INT_OVERWRITE:
            serialized_script.extend(
                [riemann.network.CODE_TO_INT_OVERWRITE[token]])

        elif token in CODE_TO_INT:
            serialized_script.extend([CODE_TO_INT[token]])

        else:
            token_bytes = bytes.fromhex(token)

            if len(token_bytes) <= 75:
                op = 'OP_PUSH_{}'.format(len(token_bytes))
                serialized_script.extend([CODE_TO_INT[op]])
                serialized_script.extend(token_bytes)

            elif len(token_bytes) > 75 and len(token_bytes) <= 255:
                op = 'OP_PUSHDATA1'
                serialized_script.extend([CODE_TO_INT[op]])
                serialized_script.extend(utils.i2le(len(token_bytes)))
                serialized_script.extend(token_bytes)

            elif len(token_bytes) > 255 and len(token_bytes) <= 1000:
                op = 'OP_PUSHDATA2'
                serialized_script.extend([CODE_TO_INT[op]])
                serialized_script.extend(
                    utils.i2le_padded(len(token_bytes), 2))
                serialized_script.extend(token_bytes)

            else:
                raise NotImplementedError(
                    'Hex string too long to serialize.')

    return serialized_script
str -> bytearray