Completed
Pull Request — master (#359)
by Ruben de
14:27
created

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