Completed
Push — master ( 407a4a...aa6067 )
by thomas
69:21
created

Signer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 70
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A sign() 0 12 3
A get() 0 17 3
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\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
    public function __construct(TransactionInterface $tx, EcAdapterInterface $ecAdapter)
36
    {
37
        $this->tx = $tx;
38
        $this->ecAdapter = $ecAdapter;
39
    }
40
41
    /**
42
     * @param int $nIn
43
     * @param PrivateKeyInterface $key
44
     * @param TransactionOutputInterface $txOut
45
     * @param ScriptInterface|null $redeemScript
46
     * @param ScriptInterface|null $witnessScript
47
     * @return $this
48
     */
49
    public function sign($nIn, PrivateKeyInterface $key, TransactionOutputInterface $txOut, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null)
50
    {
51
        if (!isset($this->signatureCreator[$nIn])) {
52
            $this->signatureCreator[$nIn] = new InputSigner($this->ecAdapter, $this->tx, $nIn, $txOut);
53
        }
54
55
        if (!$this->signatureCreator[$nIn]->sign($key, $redeemScript, $witnessScript)) {
56
            throw new \RuntimeException('Unsignable script');
57
        }
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return TransactionInterface
64
     */
65
    public function get()
66
    {
67
        $mutable = TransactionFactory::mutate($this->tx);
68
        $witnesses = [];
69
        foreach ($mutable->inputsMutator() as $idx => $input) {
70
            $sig = $this->signatureCreator[$idx]->serializeSignatures();
71
            $input->script($sig->getScriptSig());
72
            $witnesses[$idx] = $sig->getScriptWitness();
73
        }
74
75
        if (count($witnesses) > 0) {
76
            $mutable->witness(new TransactionWitnessCollection($witnesses));
77
        }
78
79
        $new = $mutable->done();
80
        return $new;
81
    }
82
}
83