Completed
Pull Request — master (#403)
by thomas
86:27 queued 84:05
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 216
    public function __construct(TransactionInterface $tx, EcAdapterInterface $ecAdapter = null)
36
    {
37 216
        $this->tx = $tx;
38 216
        $this->ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
39 216
    }
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 162
    public function sign($nIn, PrivateKeyInterface $key, TransactionOutputInterface $txOut, SignData $signData = null, $sigHashType = SigHash::ALL)
50
    {
51 162
        if (!$this->input($nIn, $txOut, $signData)->sign($key, $sigHashType)) {
52
            throw new \RuntimeException('Unsignable script');
53
        }
54
55 162
        return $this;
56
    }
57
58
    /**
59
     * @param int $nIn
60
     * @param TransactionOutputInterface $txOut
61
     * @param SignData $signData
62
     * @return InputSigner
63
     */
64 216
    public function input($nIn, TransactionOutputInterface $txOut, SignData $signData = null)
65
    {
66 216
        if (null === $signData) {
67
            $signData = new SignData();
68
        }
69
70 216
        if (!isset($this->signatureCreator[$nIn])) {
71 216
            $this->signatureCreator[$nIn] = new InputSigner($this->ecAdapter, $this->tx, $nIn, $txOut, $signData);
72 72
        }
73
74 216
        return $this->signatureCreator[$nIn];
75
    }
76
77
    /**
78
     * @return TransactionInterface
79
     */
80 216
    public function get()
81
    {
82 216
        $mutable = TransactionFactory::mutate($this->tx);
83 216
        $witnesses = [];
84 216
        foreach ($mutable->inputsMutator() as $idx => $input) {
85 216
            $sig = $this->signatureCreator[$idx]->serializeSignatures();
86 216
            $input->script($sig->getScriptSig());
87 216
            $witnesses[$idx] = $sig->getScriptWitness();
88 72
        }
89
90 216
        if (count($witnesses) > 0) {
91 216
            $mutable->witness($witnesses);
92 72
        }
93
94 216
        $new = $mutable->done();
95 216
        return $new;
96
    }
97
}
98