Completed
Pull Request — master (#338)
by thomas
33:29 queued 12:36
created

OutputScriptFactory::payToScriptHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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