Completed
Pull Request — master (#276)
by thomas
40:21
created

Signer::sign()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 2
b 0
f 0
cc 3
eloc 6
nc 4
nop 6
crap 3.0261
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction\Factory;
4
5
use BitWasp\Bitcoin\Collection\Transaction\TransactionWitnessCollection;
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)
37
    {
38 84
        $this->tx = $tx;
39 84
        $this->ecAdapter = $ecAdapter;
40 84
    }
41
42
    /**
43
     * @param int $nIn
44
     * @param PrivateKeyInterface $key
45
     * @param TransactionOutputInterface $txOut
46
     * @param ScriptInterface|null $redeemScript
47
     * @param ScriptInterface|null $witnessScript
48
     * @param int $sigHashType
49
     * @return $this
50
     */
51 84
    public function sign($nIn, PrivateKeyInterface $key, TransactionOutputInterface $txOut, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null, $sigHashType = SigHash::ALL)
52
    {
53 84
        if (!isset($this->signatureCreator[$nIn])) {
54 84
            $this->signatureCreator[$nIn] = new InputSigner($this->ecAdapter, $this->tx, $nIn, $txOut, $sigHashType);
55 84
        }
56
57 84
        if (!$this->signatureCreator[$nIn]->sign($key, $redeemScript, $witnessScript)) {
58
            throw new \RuntimeException('Unsignable script');
59
        }
60
61 84
        return $this;
62
    }
63
64
    /**
65
     * @return TransactionInterface
66
     */
67 84
    public function get()
68
    {
69 84
        $mutable = TransactionFactory::mutate($this->tx);
70 84
        $witnesses = [];
71 84
        foreach ($mutable->inputsMutator() as $idx => $input) {
72 84
            $sig = $this->signatureCreator[$idx]->serializeSignatures();
73 84
            $input->script($sig->getScriptSig());
74 84
            $witnesses[$idx] = $sig->getScriptWitness();
75 84
        }
76
77 84
        if (count($witnesses) > 0) {
78 84
            $mutable->witness(new TransactionWitnessCollection($witnesses));
79 84
        }
80
81 84
        $new = $mutable->done();
82 84
        return $new;
83
    }
84
}
85