Completed
Push — master ( 4c9dce...c05f7f )
by thomas
56:25
created

PayToPubkey   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 78.26%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 81
ccs 18
cts 23
cp 0.7826
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A classification() 0 4 1
A getRequiredSigCount() 0 4 1
A getKeyCount() 0 4 1
A checkInvolvesKey() 0 4 1
A getKeys() 0 4 1
A makeScriptSig() 0 9 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\ScriptInfo;
4
5
use BitWasp\Bitcoin\Key\PublicKeyFactory;
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\Crypto\EcAdapter\Key\PublicKeyInterface;
11
use BitWasp\Bitcoin\Signature\TransactionSignatureInterface;
12
13
class PayToPubkey implements ScriptInfoInterface
14
{
15
    /**
16
     * @var ScriptInterface
17
     */
18
    private $script;
19
20
    /**
21
     * @var PublicKeyInterface
22
     */
23
    private $publicKey;
24
25
    /**
26
     * @param ScriptInterface $script
27
     */
28 24
    public function __construct(ScriptInterface $script)
29
    {
30 24
        $this->script = $script;
31 24
        $chunks = $script->getScriptParser()->decode();
32 24
        if (count($chunks) < 1 || !$chunks[0]->isPush()) {
33
            throw new \InvalidArgumentException('Malformed pay-to-pubkey script');
34
        }
35 24
        $this->publicKey = PublicKeyFactory::fromHex($chunks[0]->getData());
0 ignored issues
show
Documentation introduced by
$chunks[0]->getData() is of type object<BitWasp\Buffertools\BufferInterface>, but the function expects a object<BitWasp\Buffertools\Buffer>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36 24
    }
37
38
    /**
39
     * @return string
40
     */
41 24
    public function classification()
42
    {
43 24
        return OutputClassifier::PAYTOPUBKEY;
44
    }
45
46
    /**
47
     * @return int
48
     */
49 24
    public function getRequiredSigCount()
50
    {
51 24
        return 1;
52
    }
53
54
    /**
55
     * @return int
56
     */
57
    public function getKeyCount()
58
    {
59
        return 1;
60
    }
61
62
    /**
63
     * @param PublicKeyInterface $publicKey
64
     * @return bool
65
     */
66
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
67
    {
68
        return $publicKey->getBinary() === $this->publicKey->getBinary();
69
    }
70
71
    /**
72
     * @return PublicKeyInterface[]
73
     */
74 24
    public function getKeys()
75
    {
76 24
        return [$this->publicKey];
77
    }
78
79
    /**
80
     * @param TransactionSignatureInterface[] $signatures
81
     * @param PublicKeyInterface[] $publicKeys
82
     * @return Script|ScriptInterface
83
     */
84 12
    public function makeScriptSig(array $signatures = [], array $publicKeys = [])
85
    {
86 12
        $newScript = new Script();
87 12
        if (count($signatures) === $this->getRequiredSigCount()) {
88 12
            $newScript = ScriptFactory::sequence([$signatures[0]->getBuffer()]);
89 12
        }
90
91 12
        return $newScript;
92
    }
93
}
94