Completed
Pull Request — master (#256)
by thomas
29:41 queued 10:14
created

OutputScriptFactory::payToLightningChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1
Metric Value
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 9.3142
cc 1
eloc 19
nc 1
nop 5
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Factory;
4
5
use BitWasp\Bitcoin\Address\AddressInterface;
6
use BitWasp\Bitcoin\Address\ScriptHashAddress;
7
use BitWasp\Bitcoin\Crypto\Hash;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
9
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier;
10
use BitWasp\Bitcoin\Script\Opcodes;
11
use BitWasp\Bitcoin\Script\Script;
12
use BitWasp\Bitcoin\Script\ScriptFactory;
13
use BitWasp\Bitcoin\Script\ScriptInterface;
14
use BitWasp\Buffertools\Buffer;
15
use BitWasp\Buffertools\BufferInterface;
16
use BitWasp\Buffertools\Buffertools;
17
18
class OutputScriptFactory
19
{
20
    /**
21
     * @param ScriptInterface $script
22
     * @return OutputClassifier
23
     */
24 201
    public function classify(ScriptInterface $script)
25
    {
26 201
        return new OutputClassifier($script);
27
    }
28
29
    /**
30
     * @param AddressInterface $address
31
     * @return ScriptInterface
32
     */
33 138
    public function payToAddress(AddressInterface $address)
34
    {
35
        $script = ($address instanceof ScriptHashAddress
36 138
            ? ScriptFactory::create()
37 12
                ->op('OP_HASH160')
38 12
                ->push(Buffer::hex($address->getHash(), 20))
39 12
                ->op('OP_EQUAL')
40 138
            : ScriptFactory::create()
41 132
                ->op('OP_DUP')
42 132
                ->op('OP_HASH160')
43 132
                ->push(Buffer::hex($address->getHash(), 20))
44 132
                ->op('OP_EQUALVERIFY')
45 138
                ->op('OP_CHECKSIG'));
46
47 138
        return $script->getScript();
48
    }
49
50
    /**
51
     * Create a Pay to pubkey output
52
     *
53
     * @param PublicKeyInterface  $public_key
54
     * @return ScriptInterface
55
     */
56 48
    public function payToPubKey(PublicKeyInterface $public_key)
57
    {
58 48
        return ScriptFactory::create()
59 48
            ->push($public_key->getBuffer())
60 48
            ->op('OP_CHECKSIG')
61 48
            ->getScript();
62
    }
63
64
    /**
65
     * Create a P2PKH output script
66
     *
67
     * @param PublicKeyInterface $public_key
68
     * @return ScriptInterface
69
     */
70 33
    public function payToPubKeyHash(PublicKeyInterface $public_key)
71
    {
72 33
        return ScriptFactory::create()
73 33
            ->op('OP_DUP')
74 33
            ->op('OP_HASH160')
75 33
            ->push($public_key->getPubKeyHash())
76 33
            ->op('OP_EQUALVERIFY')
77 33
            ->op('OP_CHECKSIG')
78 33
            ->getScript();
79
    }
80
81
    /**
82
     * Create a P2SH output script
83
     *
84
     * @param ScriptInterface $p2shScript
85
     * @return ScriptInterface
86
     */
87 144
    public function payToScriptHash(ScriptInterface $p2shScript)
88
    {
89 144
        return ScriptFactory::create()
90 144
            ->op('OP_HASH160')
91 144
            ->push($p2shScript->getScriptHash())
92 144
            ->op('OP_EQUAL')
93 144
            ->getScript();
94
    }
95
96
    /**
97
     * @param int $m
98
     * @param PublicKeyInterface[] $keys
99
     * @param bool|true $sort
100
     * @return ScriptCreator|Script
101
     */
102 102
    public function multisig($m, array $keys = [], $sort = true)
103
    {
104 102
        $n = count($keys);
105 102
        if ($m > $n) {
106
            throw new \LogicException('Required number of sigs exceeds number of public keys');
107
        }
108
109 102
        if ($n > 16) {
110
            throw new \LogicException('Number of public keys is greater than 16');
111
        }
112
113 102
        if ($sort) {
114 102
            $keys = Buffertools::sort($keys);
115 102
        }
116
117 102
        $opM = \BitWasp\Bitcoin\Script\encodeOpN($m);
118 102
        $opN = \BitWasp\Bitcoin\Script\encodeOpN($n);
119
120 102
        $script = ScriptFactory::create();
121 102
        foreach ($keys as $key) {
122 102
            if (!$key instanceof PublicKeyInterface) {
123
                throw new \LogicException('Values in $keys[] must be a PublicKey');
124
            }
125
126 102
            $script->push($key->getBuffer());
127 102
        }
128 102
        $keyBuf = $script->getScript()->getBuffer();
129
130 102
        $script = new Script(new Buffer(chr($opM) . $keyBuf->getBinary() . chr($opN) . chr(Opcodes::OP_CHECKMULTISIG)));
131 102
        return $script;
132
    }
133
}
134