Completed
Pull Request — master (#759)
by thomas
25:10
created

UpdatableInput::addWitnessTxOut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Transaction\PSBT;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Bitcoin\Transaction\TransactionInterface;
10
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
11
12
class UpdatableInput
13
{
14
    private $psbt;
15
    private $nIn;
16
    private $input;
17
18 10
    public function __construct(
19
        PSBT $psbt,
20
        int $nIn,
21
        PSBTInput $input
22
    ) {
23 10
        $this->psbt = $psbt;
24 10
        $this->nIn = $nIn;
25 10
        $this->input = $input;
26 10
    }
27
28 8
    public function input(): PSBTInput
29
    {
30 8
        return $this->input;
31
    }
32
33 4
    public function addNonWitnessTx(TransactionInterface $tx)
34
    {
35 4
        $outPoint = $this->psbt->getTransaction()->getInputs()[$this->nIn]->getOutPoint();
36 4
        if (!$outPoint->getTxId()->equals($tx->getTxId())) {
37 1
            throw new \RuntimeException("Non-witness txid differs from unsigned tx input {$this->nIn}");
38
        }
39 3
        if ($outPoint->getVout() > count($tx->getOutputs()) - 1) {
40 1
            throw new \RuntimeException("unsigned tx outpoint does not exist in this transaction");
41
        }
42 2
        $this->input = $this->input->withNonWitnessTx($tx);
43 2
    }
44
45 3
    public function addWitnessTxOut(TransactionOutputInterface $txOut)
46
    {
47 3
        $this->input = $this->input->withWitnessTxOut($txOut);
48 3
    }
49
50 2
    public function addRedeemScript(ScriptInterface $script)
51
    {
52 2
        $this->input = $this->input->withRedeemScript($script);
53 2
    }
54
55 2
    public function addWitnessScript(ScriptInterface $script)
56
    {
57 2
        $this->input = $this->input->withWitnessScript($script);
58 2
    }
59
60 1
    public function addDerivation(PublicKeyInterface $key, PSBTBip32Derivation $derivation)
61
    {
62 1
        $this->input = $this->input->withDerivation($key, $derivation);
63 1
    }
64
}
65