Completed
Pull Request — master (#287)
by thomas
22:09
created

OutputScriptFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 82.14%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 12
c 7
b 0
f 0
lcom 0
cbo 9
dl 0
loc 89
ccs 23
cts 28
cp 0.8214
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A classify() 0 4 1
A payToAddress() 0 6 2
A payToPubKey() 0 4 1
A payToPubKeyHash() 0 4 1
A payToScriptHash() 0 4 1
B multisig() 0 28 6
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\Buffertools;
15
16
class OutputScriptFactory
17
{
18
    /**
19
     * @return OutputClassifier
20
     */
21
    public function classify()
22
    {
23
        return new OutputClassifier();
24
    }
25
26
    /**
27
     * @param AddressInterface $address
28
     * @return ScriptInterface
29
     */
30 108
    public function payToAddress(AddressInterface $address)
31
    {
32
        return $address instanceof ScriptHashAddress
33 108
            ? ScriptFactory::sequence([Opcodes::OP_HASH160, Buffer::hex($address->getHash(), 20), Opcodes::OP_EQUAL])
34 108
            : ScriptFactory::sequence([Opcodes::OP_DUP, Opcodes::OP_HASH160, Buffer::hex($address->getHash(), 20), Opcodes::OP_EQUALVERIFY, Opcodes::OP_CHECKSIG]);
35
    }
36
37
    /**
38
     * Create a Pay to pubkey output
39
     *
40
     * @param PublicKeyInterface  $publicKey
41
     * @return ScriptInterface
42
     */
43 36
    public function payToPubKey(PublicKeyInterface $publicKey)
44
    {
45 36
        return ScriptFactory::sequence([$publicKey->getBuffer(), Opcodes::OP_CHECKSIG]);
46
    }
47
48
    /**
49
     * Create a P2PKH output script
50
     *
51
     * @param PublicKeyInterface $public_key
52
     * @return ScriptInterface
53
     */
54 18
    public function payToPubKeyHash(PublicKeyInterface $public_key)
55
    {
56 18
        return ScriptFactory::sequence([Opcodes::OP_DUP, Opcodes::OP_HASH160, $public_key->getPubKeyHash(), Opcodes::OP_EQUALVERIFY, Opcodes::OP_CHECKSIG]);
57
    }
58
59
    /**
60
     * Create a P2SH output script
61
     *
62
     * @param ScriptInterface $p2shScript
63
     * @return ScriptInterface
64
     */
65 96
    public function payToScriptHash(ScriptInterface $p2shScript)
66
    {
67 96
        return ScriptFactory::sequence([Opcodes::OP_HASH160, $p2shScript->getScriptHash(), Opcodes::OP_EQUAL]);
68
    }
69
70
    /**
71
     * @param int $m
72
     * @param PublicKeyInterface[] $keys
73
     * @param bool|true $sort
74
     * @return ScriptCreator|Script
75
     */
76 78
    public function multisig($m, array $keys = [], $sort = true)
77
    {
78 78
        $n = count($keys);
79 78
        if ($m > $n) {
80
            throw new \LogicException('Required number of sigs exceeds number of public keys');
81
        }
82
83 78
        if ($n > 16) {
84
            throw new \LogicException('Number of public keys is greater than 16');
85
        }
86
87 78
        if ($sort) {
88 78
            $keys = Buffertools::sort($keys);
89 78
        }
90
91 78
        $new = ScriptFactory::create()->int($m);
92 78
        foreach ($keys as $key) {
93 78
            if (!$key instanceof PublicKeyInterface) {
94
                throw new \LogicException('Values in $keys[] must be a PublicKey');
95
            }
96
97 78
            $new->push($key->getBuffer());
98 78
        }
99
100 78
        $new->int($n)->op('OP_CHECKMULTISIG');
101
102 78
        return $new->getScript();
103
    }
104
}
105