Completed
Push — master ( 7ac9fb...3b1821 )
by thomas
28:43
created

PayToPubkey::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 3
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\ScriptInfo;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
6
use BitWasp\Bitcoin\Script\Opcodes;
7
use BitWasp\Bitcoin\Script\Parser\Operation;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Bitcoin\Script\ScriptType;
10
use BitWasp\Buffertools\BufferInterface;
11
12
class PayToPubkey
13
{
14
    /**
15
     * @var BufferInterface
16
     */
17
    private $publicKey;
18
19
    /**
20
     * @var bool
21
     */
22
    private $verify;
23
24
    /**
25
     * @var int
26
     */
27
    private $opcode;
28
29
    /**
30
     * PayToPubkey constructor.
31
     * @param int $opcode
32
     * @param BufferInterface $publicKey
33
     * @param bool $allowVerify
34
     */
35 82
    public function __construct($opcode, BufferInterface $publicKey, $allowVerify = false)
36
    {
37 82
        if ($opcode === Opcodes::OP_CHECKSIG) {
38 78
            $verify = false;
39 38
        } else if ($allowVerify && $opcode === Opcodes::OP_CHECKSIGVERIFY) {
40 8
            $verify = true;
41
        } else {
42 30
            throw new \InvalidArgumentException('Malformed pay-to-pubkey script - invalid opcode');
43
        }
44
45 82
        $this->verify = $verify;
46 82
        $this->opcode = $opcode;
47 82
        $this->publicKey = $publicKey;
48 82
    }
49
50
    /**
51
     * @param Operation[] $chunks
52
     * @param bool $allowVerify
53
     * @return static
54
     */
55 168
    public static function fromDecodedScript(array $chunks, $allowVerify = false)
56
    {
57 168
        if (count($chunks) !== 2 || !$chunks[0]->isPush() || $chunks[1]->isPush()) {
58 166
            throw new \InvalidArgumentException('Malformed pay-to-pubkey script');
59
        }
60
61 82
        return new static($chunks[1]->getOp(), $chunks[0]->getData(), $allowVerify);
62
    }
63
64
    /**
65
     * @param ScriptInterface $script
66
     * @param bool $allowVerify
67
     * @return PayToPubkey
68
     */
69 2
    public static function fromScript(ScriptInterface $script, $allowVerify = false)
70
    {
71 2
        return static::fromDecodedScript($script->getScriptParser()->decode(), $allowVerify);
72
    }
73
74 80
    public function getType()
75
    {
76 80
        return ScriptType::P2PK;
77
    }
78
79
    /**
80
     * @return int
81
     */
82 82
    public function getRequiredSigCount()
83
    {
84 82
        return 1;
85
    }
86
87
    /**
88
     * @return int
89
     */
90 2
    public function getKeyCount()
91
    {
92 2
        return 1;
93
    }
94
95 44
    public function isChecksigVerify()
96
    {
97 44
        return $this->verify;
98
    }
99
100
    /**
101
     * @param PublicKeyInterface $publicKey
102
     * @return bool
103
     */
104 2
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
105
    {
106 2
        return $publicKey->getBuffer()->equals($this->publicKey);
107
    }
108
109
    /**
110
     * @return BufferInterface
111
     */
112 82
    public function getKeyBuffer()
113
    {
114 82
        return $this->publicKey;
115
    }
116
}
117