Completed
Pull Request — master (#248)
by thomas
58:32 queued 33:01
created

PayToPubkeyHash::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 3.0175
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\ScriptInfo;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
6
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier;
7
use BitWasp\Bitcoin\Script\Script;
8
use BitWasp\Bitcoin\Script\ScriptFactory;
9
use BitWasp\Bitcoin\Script\ScriptInterface;
10
use BitWasp\Bitcoin\Signature\TransactionSignatureInterface;
11
use BitWasp\Buffertools\BufferInterface;
12
13
class PayToPubkeyHash implements ScriptInfoInterface
14
{
15
    /**
16
     * @var ScriptInterface
17
     */
18
    private $script;
19
20
    /**
21
     * @var BufferInterface
22
     */
23
    private $hash;
24
25
    /**
26
     * @param ScriptInterface $script
27
     */
28 57
    public function __construct(ScriptInterface $script)
29
    {
30 57
        $this->script = $script;
31 57
        $chunks = $this->script->getScriptParser()->decode();
32 57
        if (count($chunks) < 5 || !$chunks[2]->isPush()) {
33
            throw new \RuntimeException('Malformed pay-to-pubkey-hash script');
34
        }
35
36
        /** @var BufferInterface $hash */
37 57
        $hash = $chunks[2]->getData();
38 57
        $this->hash = $hash;
39 57
    }
40
41
    /**
42
     * @return string
43
     */
44 57
    public function classification()
45
    {
46 57
        return OutputClassifier::PAYTOPUBKEYHASH;
47
    }
48
49
    /**
50
     * @return int
51
     */
52 45
    public function getRequiredSigCount()
53
    {
54 45
        return 1;
55
    }
56
57
    /**
58
     * @return int
59
     */
60
    public function getKeyCount()
61
    {
62
        return 1;
63
    }
64
65
    /**
66
     * @param PublicKeyInterface $publicKey
67
     * @return bool
68
     */
69
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
70
    {
71
        return $publicKey->getPubKeyHash()->getBinary() === $this->hash->getBinary();
72
    }
73
74
    /**
75
     * @return PublicKeyInterface[]
76
     */
77 57
    public function getKeys()
78
    {
79 57
        return [];
80
    }
81
82
    /**
83
     * @param TransactionSignatureInterface[] $signatures
84
     * @param PublicKeyInterface[] $publicKeys
85
     * @return Script|ScriptInterface
86
     */
87 33
    public function makeScriptSig(array $signatures = [], array $publicKeys = [])
88
    {
89 33
        $newScript = new Script();
90 33
        if (count($publicKeys) > 0 && count($signatures) === $this->getRequiredSigCount()) {
91 33
            $newScript = ScriptFactory::sequence([$signatures[0]->getBuffer(), $publicKeys[0]->getBuffer()]);
92 33
        }
93
94 33
        return $newScript;
95
    }
96
}
97