<commit_before><?php

namespace BitWasp\Bitcoin\Tests\Key\Deterministic;

use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeySequence;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Key\Deterministic\MultisigHD;
use BitWasp\Bitcoin\Tests\AbstractTestCase;
use BitWasp\Buffertools\Buffer;

class MultisigHDTest extends AbstractTestCase
{

    /**
     * @expectedException \RuntimeException
     * @expectedExceptionMessage Must have at least one HierarchicalKey for Multisig HD Script
     */
    public function testAlwaysProvidesKeys()
    {
        $sequences = new HierarchicalKeySequence();
        new \BitWasp\Bitcoin\Key\Deterministic\MultisigHD(2, 'm', [], $sequences, true);
    }

    public function testCreateRootWhenAlreadySorted()
    {
        $keys[0] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('01'));
        $keys[1] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('02'));

        $sequences = new HierarchicalKeySequence();

        $hd = new MultisigHD(2, 'm', $keys, $sequences, true);

        $this->assertEquals('m', $hd->getPath(), 'confirm path set via constructor');
        $this->assertEquals($keys, $hd->getKeys(), 'confirm keys has same order');
    }

    public function testSortedKeysAsSideEffect()
    {
        $keys[0] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('02'));
        $keys[1] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('01'));

        $sequences = new HierarchicalKeySequence();
        $hd = new MultisigHD(2, 'm', $keys, $sequences, true);
        
        $this->assertNotEquals($keys, $hd->getKeys(), 'these cases should not match input since they will be sorted');
    }

    public function testNoSideEffectWhenNotSorting()
    {
        $keys[0] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('02'));
        $keys[1] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('01'));

        $sequences = new HierarchicalKeySequence();

        $hd = new \BitWasp\Bitcoin\Key\Deterministic\MultisigHD(2, 'm', $keys, $sequences, false);

        $this->assertEquals($keys, $hd->getKeys(), 'keys should match input when not sorting');
    }

    public function testGetRedeemScript()
    {
        $keys[0] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('02'));
        $keys[1] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('01'));
        $sequences = new HierarchicalKeySequence();
        $hd = new MultisigHD(2, 'm', $keys, $sequences, true);
        $script = $hd->getRedeemScript();

        // note the indexes - we know these keys will be of reversed order.
        $expected = '5221' . $keys[1]->getPublicKey()->getHex() . '21' . $keys[0]->getPublicKey()->getHex() . '52ae';

        $this->assertEquals($expected, $script->getHex());
    }

    public function testDeriveChild()
    {<commit_msg>MultisigHDTest: use static xpubs for redeem script test<commit_after><?php

namespace BitWasp\Bitcoin\Tests\Key\Deterministic;

use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeySequence;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Key\Deterministic\MultisigHD;
use BitWasp\Bitcoin\Tests\AbstractTestCase;
use BitWasp\Buffertools\Buffer;

class MultisigHDTest extends AbstractTestCase
{

    /**
     * @expectedException \RuntimeException
     * @expectedExceptionMessage Must have at least one HierarchicalKey for Multisig HD Script
     */
    public function testAlwaysProvidesKeys()
    {
        $sequences = new HierarchicalKeySequence();
        new MultisigHD(2, 'm', [], $sequences, true);
    }

    public function testCreateRootWhenAlreadySorted()
    {
        $keys[0] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('01'));
        $keys[1] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('02'));

        $sequences = new HierarchicalKeySequence();

        $hd = new MultisigHD(2, 'm', $keys, $sequences, true);

        $this->assertEquals('m', $hd->getPath(), 'confirm path set via constructor');
        $this->assertEquals($keys, $hd->getKeys(), 'confirm keys has same order');
    }

    public function testSortedKeysAsSideEffect()
    {
        $keys[0] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('02'));
        $keys[1] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('01'));

        $sequences = new HierarchicalKeySequence();
        $hd = new MultisigHD(2, 'm', $keys, $sequences, true);
        
        $this->assertNotEquals($keys, $hd->getKeys(), 'these cases should not match input since they will be sorted');
    }

    public function testNoSideEffectWhenNotSorting()
    {
        $keys[0] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('02'));
        $keys[1] = HierarchicalKeyFactory::fromEntropy(Buffer::hex('01'));

        $sequences = new HierarchicalKeySequence();

        $hd = new \BitWasp\Bitcoin\Key\Deterministic\MultisigHD(2, 'm', $keys, $sequences, false);

        $this->assertEquals($keys, $hd->getKeys(), 'keys should match input when not sorting');
    }

    public function testGetRedeemScript()
    {
        $keys[0] = HierarchicalKeyFactory::fromExtended('<KEY>');
        $keys[1] = HierarchicalKeyFactory::fromExtended('<KEY>MEznufcz');
        $sequences = new HierarchicalKeySequence();
        $hd = new MultisigHD(2, 'm', $keys, $sequences, true);
        $script = $hd->getRedeemScript();

        // note the indexes - we know these keys will be of reversed order.
        $expected = '5221' . $keys[1]->getPublicKey()->getHex() . '21' . $keys[0]->getPublicKey()->getHex() . '52ae';

        $this->assertEquals($expected, $script->getHex());
    }

    public function testDeriveChild()
    {
{'fraction_non_alphanumeric': 0.14320728291316526, 'fraction_numerical': 0.012955182072829132, 'mean_word_length': 3.8008403361344536, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, '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': '5925675', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1725, 'n_words': 349}