Completed
Pull Request — master (#403)
by thomas
73:13 queued 70:38
created

Signer::signer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 1
b 0
f 0
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\Transaction\SignatureHash\SigHash;
9
use BitWasp\Bitcoin\Transaction\TransactionFactory;
10
use BitWasp\Bitcoin\Transaction\TransactionInterface;
11
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
12
13
class Signer
14
{
15
    /**
16
     * @var EcAdapterInterface
17
     */
18
    private $ecAdapter;
19
20
    /**
21
     * @var TransactionInterface
22
     */
23
    private $tx;
24
25
    /**
26
     * @var InputSigner[]
27
     */
28
    private $signatureCreator = [];
29
30
    /**
31
     * TxWitnessSigner constructor.
32
     * @param TransactionInterface $tx
33
     * @param EcAdapterInterface $ecAdapter
34
     */
35 168
    public function __construct(TransactionInterface $tx, EcAdapterInterface $ecAdapter = null)
36
    {
37 168
        $this->tx = $tx;
38 168
        $this->ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
39 168
    }
40
41
    /**
42
     * @param int $nIn
43
     * @param PrivateKeyInterface $key
44
     * @param TransactionOutputInterface $txOut
45
     * @param SignData $signData
46
     * @param int $sigHashType
47
     * @return $this
48
     */
49 168
    public function sign($nIn, PrivateKeyInterface $key, TransactionOutputInterface $txOut, SignData $signData = null, $sigHashType = SigHash::ALL)
50
    {
51 168
        if (null === $signData) {
52
            $signData = new SignData();
53
        }
54 168
        if (!$this->signer($nIn, $txOut, $signData)->sign($key, $sigHashType)) {
55
            throw new \RuntimeException('Unsignable script');
56
        }
57
58 168
        return $this;
59
    }
60
61
    /**
62
     * @param int $nIn
63
     * @param TransactionOutputInterface $txOut
64
     * @param SignData $signData
65
     * @return InputSigner
66
     */
67 168
    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...
68
    {
69 168
        if (!isset($this->signatureCreator[$nIn])) {
70 168
            $this->signatureCreator[$nIn] = new InputSigner($this->ecAdapter, $this->tx, $nIn, $txOut, $signData);
71 56
        }
72
73 168
        return $this->signatureCreator[$nIn];
74
    }
75
76
    /**
77
     * @return TransactionInterface
78
     */
79 168
    public function get()
80
    {
81 168
        $mutable = TransactionFactory::mutate($this->tx);
82 168
        $witnesses = [];
83 168
        foreach ($mutable->inputsMutator() as $idx => $input) {
84 168
            $sig = $this->signatureCreator[$idx]->serializeSignatures();
85 168
            $input->script($sig->getScriptSig());
86 168
            $witnesses[$idx] = $sig->getScriptWitness();
87 56
        }
88
89 168
        if (count($witnesses) > 0) {
90 168
            $mutable->witness($witnesses);
91 56
        }
92
93 168
        $new = $mutable->done();
94 168
        return $new;
95
    }
96
}
97