<commit_before><?php

namespace BitWasp\Bitcoin\Script\Classifier;

use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Key\PublicKey;
use BitWasp\Bitcoin\Script\Script;
use BitWasp\Bitcoin\Script\ScriptInterface;

class InputClassifier implements ScriptClassifierInterface
{

    /**
     * @var ScriptInterface
     */
    private $script;

    /**
     * @var array
     */
    private $evalScript;

    /**
     * @param ScriptInterface $script
     */
    public function __construct(ScriptInterface $script)
    {
        $this->script = $script;
        $this->evalScript = $script->getScriptParser()->parse();
    }

    /**
     * @return bool
     */
    public function isPayToPublicKey()
    {
        return (
            count($this->evalScript) == 1
            && (strlen($this->evalScript) <= 0x47)
        );
    }

    /**
     * @return bool
     */
    public function isPayToPublicKeyHash()
    {
        return (
            count($this->evalScript) == 2
            && (strlen($this->evalScript[0]) <= 0x47)
            && PublicKey::isCompressedOrUncompressed(Buffer::hex($this->evalScript[1]))
        );
    }

    /**
     * @return bool
     */
    public function isPayToScriptHash()
    {
        if (count($this->evalScript) == 0) {
            return false;
        }

        $final = end($this->evalScript);
        if (!$final) {
            return false;
        }

        $script = new Script(Buffer::hex($final));
        $type = new self($script);
        // todo.......
        return $type->classify() !== false;
    }

    /**
     * @return bool
     */
    public function isMultisig()
    {
        return false;
    }

    /**
     * @return string
     */
    public function classify()
    {
        if ($this->isPayToPublicKey()) {
            return self::PAYTOPUBKEY;
        } elseif ($this->isPayToPublicKeyHash()) {
            return self::PAYTOPUBKEYHASH;
        } elseif ($this->isPayToScriptHash()) {
            return self::PAYTOSCRIPTHASH;
        } elseif ($this->isMultisig()) {
            return self::MULTISIG;
        }

        return self::UNKNOWN;
    }
}<commit_msg>Add more thorough classification tests to InputClassifier<commit_after><?php

namespace BitWasp\Bitcoin\Script\Classifier;

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Key\PublicKey;
use BitWasp\Bitcoin\Script\Script;
use BitWasp\Bitcoin\Script\ScriptInterface;

class InputClassifier implements ScriptClassifierInterface
{

    /**
     * @var ScriptInterface
     */
    private $script;

    /**
     * @var array
     */
    private $evalScript;

    const MAXSIGLEN = 0x48;

    /**
     * @param ScriptInterface $script
     */
    public function __construct(ScriptInterface $script)
    {
        $this->script = $script;
        $this->evalScript = $script->getScriptParser()->parse();
    }

    /**
     * @return bool
     */
    public function isPayToPublicKey()
    {
        return (
            count($this->evalScript) == 1
            && ($this->evalScript[0] instanceof Buffer)
            && ($this->evalScript[0]->getSize() <= self::MAXSIGLEN)
        );
    }

    /**
     * @return bool
     */
    public function isPayToPublicKeyHash()
    {
        return (
            count($this->evalScript) == 2
            && ($this->evalScript[0] instanceof Buffer && $this->evalScript[1] instanceof Buffer)
            && ($this->evalScript[0]->getSize() <= self::MAXSIGLEN)
            && PublicKey::isCompressedOrUncompressed($this->evalScript[1])
        );
    }

    /**
     * @return bool
     */
    public function isPayToScriptHash()
    {
        if (count($this->evalScript) == 0) {
            return false;
        }

        $final = end($this->evalScript);
        if (!$final || !$final instanceof Buffer) {
            return false;
        }

        $type = new self(new Script($final));
        return false === in_array($type->classify(), [
            InputClassifier::UNKNOWN,
            InputClassifier::PAYTOSCRIPTHASH
        ]);
    }

    /**
     * @return bool
     */
    public function isMultisig()
    {
        if (count($this->evalScript) == 0) {
            return false;
        }

        $final = end($this->evalScript);
        if (!$final || !$final instanceof Buffer) {
            return false;
        }

        $script = new Script($final);
        $parsed = $script->getScriptParser()->parse();
        $count = count($parsed);
        $opCodes = $script->getOpCodes();

        $mOp = $parsed[0];
        $nOp = $parsed[$count - 2];
        $keys = array_slice($parsed, 1, -2);
        $keysValid = function() use ($keys) {
            $valid = true;
            foreach ($keys as $key) {
                $valid &= ($key instanceof Buffer) && PublicKey::isCompressedOrUncompressed($key);
            }
            return $valid;
        };

        return (
            $opCodes->cmp($mOp, 'OP_0') >= 0
            && $opCodes->cmp($nOp, 'OP_16') <= 0
            && $keysValid()
        );
    }

    /**
     * @return string
     */
    public function classify()
    {
        if ($this->isPayToPublicKey()) {
            return self::PAYTOPUBKEY;
        } elseif ($this->isPayToPublicKeyHash()) {
            return self::PAYTOPUBKEYHASH;
        } elseif ($this->isMultisig()) {
            return self::MULTISIG;
        } elseif ($this->isPayToScriptHash()) {
            return self::PAYTOSCRIPTHASH;
        }

        return self::UNKNOWN;
    }
}
{'fraction_non_alphanumeric': 0.14402470032691608, 'fraction_numerical': 0.00599346167816927, 'mean_word_length': 1.8862683438155137, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6974842', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1631, 'n_words': 299}