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