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
|
|
|
|