Completed
Push — master ( d885db...3f1305 )
by thomas
30:43
created

Signer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 96.3%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 97
ccs 26
cts 27
cp 0.963
rs 10
c 2
b 0
f 0
wmc 10
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A sign() 0 8 2
A input() 0 12 3
A get() 0 17 3
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction\Factory;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface;
11
use BitWasp\Bitcoin\Serializer\Signature\TransactionSignatureSerializer;
12
use BitWasp\Bitcoin\Transaction\SignatureHash\SigHash;
13
use BitWasp\Bitcoin\Transaction\TransactionFactory;
14
use BitWasp\Bitcoin\Transaction\TransactionInterface;
15
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
16
17
class Signer
18
{
19
    /**
20
     * @var EcAdapterInterface
21
     */
22
    private $ecAdapter;
23
24
    /**
25
     * @var TransactionInterface
26
     */
27
    private $tx;
28
29
    /**
30
     * @var TransactionSignatureSerializer
31
     */
32
    private $sigSerializer;
33
34
    /**
35
     * @var PublicKeySerializerInterface
36
     */
37
    private $pubKeySerializer;
38
39
    /**
40
     * @var InputSigner[]
41
     */
42
    private $signatureCreator = [];
43
44
    /**
45
     * TxWitnessSigner constructor.
46
     * @param TransactionInterface $tx
47
     * @param EcAdapterInterface $ecAdapter
48
     */
49 58
    public function __construct(TransactionInterface $tx, EcAdapterInterface $ecAdapter = null)
50
    {
51 58
        $this->tx = $tx;
52 58
        $this->ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
53 58
        $this->sigSerializer = new TransactionSignatureSerializer(EcSerializer::getSerializer(DerSignatureSerializerInterface::class, $this->ecAdapter));
0 ignored issues
show
Documentation introduced by
$this->ecAdapter is of type object<BitWasp\Bitcoin\C...ter\EcAdapterInterface>, but the function expects a boolean|object<BitWasp\B...\Crypto\EcAdapter\true>.

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...
54 58
        $this->pubKeySerializer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, $this->ecAdapter);
0 ignored issues
show
Documentation introduced by
$this->ecAdapter is of type object<BitWasp\Bitcoin\C...ter\EcAdapterInterface>, but the function expects a boolean|object<BitWasp\B...\Crypto\EcAdapter\true>.

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...
55 58
    }
56
57
    /**
58
     * @param int $nIn
59
     * @param PrivateKeyInterface $key
60
     * @param TransactionOutputInterface $txOut
61
     * @param SignData $signData
62
     * @param int $sigHashType
63
     * @return $this
64
     */
65 50
    public function sign($nIn, PrivateKeyInterface $key, TransactionOutputInterface $txOut, SignData $signData = null, $sigHashType = SigHash::ALL)
66
    {
67 50
        if (!$this->input($nIn, $txOut, $signData)->sign($key, $sigHashType)) {
68
            throw new \RuntimeException('Unsignable script');
69
        }
70
71 50
        return $this;
72
    }
73
74
    /**
75
     * @param int $nIn
76
     * @param TransactionOutputInterface $txOut
77
     * @param SignData $signData
78
     * @return InputSigner
79
     */
80 58
    public function input($nIn, TransactionOutputInterface $txOut, SignData $signData = null)
81
    {
82 58
        if (null === $signData) {
83 8
            $signData = new SignData();
84
        }
85
86 58
        if (!isset($this->signatureCreator[$nIn])) {
87 58
            $this->signatureCreator[$nIn] = new InputSigner($this->ecAdapter, $this->tx, $nIn, $txOut, $signData, $this->sigSerializer, $this->pubKeySerializer);
88
        }
89
90 58
        return $this->signatureCreator[$nIn];
91
    }
92
93
    /**
94
     * @return TransactionInterface
95
     */
96 50
    public function get()
97
    {
98 50
        $mutable = TransactionFactory::mutate($this->tx);
99 50
        $witnesses = [];
100 50
        foreach ($mutable->inputsMutator() as $idx => $input) {
101 50
            $sig = $this->signatureCreator[$idx]->serializeSignatures();
102 50
            $input->script($sig->getScriptSig());
103 50
            $witnesses[$idx] = $sig->getScriptWitness();
104
        }
105
106 50
        if (count($witnesses) > 0) {
107 50
            $mutable->witness($witnesses);
108
        }
109
110 50
        $new = $mutable->done();
111 50
        return $new;
112
    }
113
}
114