Completed
Pull Request — master (#403)
by thomas
75:49 queued 72:57
created

Signer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 89.66%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 84
ccs 26
cts 29
cp 0.8966
rs 10
c 3
b 0
f 0
wmc 10
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A sign() 0 11 3
A signer() 0 8 2
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\Key\PrivateKeyInterface;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Bitcoin\Transaction\SignatureHash\SigHash;
10
use BitWasp\Bitcoin\Transaction\TransactionFactory;
11
use BitWasp\Bitcoin\Transaction\TransactionInterface;
12
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
13
14
class Signer
15
{
16
    /**
17
     * @var EcAdapterInterface
18
     */
19
    private $ecAdapter;
20
21
    /**
22
     * @var TransactionInterface
23
     */
24
    private $tx;
25
26
    /**
27
     * @var InputSigner[]
28
     */
29
    private $signatureCreator = [];
30
31
    /**
32
     * TxWitnessSigner constructor.
33
     * @param TransactionInterface $tx
34
     * @param EcAdapterInterface $ecAdapter
35
     */
36 84
    public function __construct(TransactionInterface $tx, EcAdapterInterface $ecAdapter = null)
37
    {
38 84
        $this->tx = $tx;
39 84
        $this->ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
40 84
    }
41
42
    /**
43
     * @param int $nIn
44
     * @param PrivateKeyInterface $key
45
     * @param TransactionOutputInterface $txOut
46
     * @param SignData $signData
47
     * @param int $sigHashType
48
     * @return $this
49
     */
50 84
    public function sign($nIn, PrivateKeyInterface $key, TransactionOutputInterface $txOut, SignData $signData = null, $sigHashType = SigHash::ALL)
51
    {
52 84
        if (null === $signData) {
53
            $signData = new SignData();
54
        }
55 84
        if (!$this->signer($nIn, $txOut, $signData)->sign($key, $sigHashType)) {
56
            throw new \RuntimeException('Unsignable script');
57
        }
58
59 84
        return $this;
60
    }
61
62
    /**
63
     * @param int $nIn
64
     * @param TransactionOutputInterface $txOut
65
     * @param SignData $signData
66
     * @return InputSigner
67
     */
68 84
    public function signer($nIn, TransactionOutputInterface $txOut, SignData $signData)
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
69
    {
70 84
        if (!isset($this->signatureCreator[$nIn])) {
71 84
            $this->signatureCreator[$nIn] = new InputSigner($this->ecAdapter, $this->tx, $nIn, $txOut, $signData);
72 42
        }
73
74 84
        return $this->signatureCreator[$nIn];
75
    }
76
77
    /**
78
     * @return TransactionInterface
79
     */
80 84
    public function get()
81
    {
82 84
        $mutable = TransactionFactory::mutate($this->tx);
83 84
        $witnesses = [];
84 84
        foreach ($mutable->inputsMutator() as $idx => $input) {
85 84
            $sig = $this->signatureCreator[$idx]->serializeSignatures();
86 84
            $input->script($sig->getScriptSig());
87 84
            $witnesses[$idx] = $sig->getScriptWitness();
88 42
        }
89
90 84
        if (count($witnesses) > 0) {
91 84
            $mutable->witness($witnesses);
92 42
        }
93
94 84
        $new = $mutable->done();
95 84
        return $new;
96
    }
97
}
98