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

PayToPubkey   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 105
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDecodedScript() 0 8 4
A fromScript() 0 4 1
A getType() 0 4 1
A getRequiredSigCount() 0 4 1
A getKeyCount() 0 4 1
A isChecksigVerify() 0 4 1
A checkInvolvesKey() 0 4 1
A getKeyBuffer() 0 4 1
A __construct() 0 14 4
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