1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Key\Deterministic; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer; |
10
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface; |
11
|
|
|
use BitWasp\Bitcoin\Crypto\Hash; |
12
|
|
|
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2pkhScriptDataFactory; |
13
|
|
|
use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactory; |
14
|
|
|
use BitWasp\Bitcoin\Key\PrivateKeyFactory; |
15
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
16
|
|
|
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\Base58ExtendedKeySerializer; |
17
|
|
|
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer; |
18
|
|
|
use BitWasp\Buffertools\Buffer; |
19
|
|
|
use BitWasp\Buffertools\BufferInterface; |
20
|
|
|
|
21
|
|
|
class HierarchicalKeyFactory |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param EcAdapterInterface $ecAdapter |
25
|
|
|
* @return Base58ExtendedKeySerializer |
26
|
|
|
*/ |
27
|
|
|
public static function getSerializer(EcAdapterInterface $ecAdapter): Base58ExtendedKeySerializer |
28
|
|
|
{ |
29
|
|
|
$extSerializer = new Base58ExtendedKeySerializer(new ExtendedKeySerializer($ecAdapter)); |
30
|
|
|
return $extSerializer; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
35
|
|
|
* @param ScriptDataFactory|null $scriptDataFactory |
36
|
|
|
* @return HierarchicalKey |
37
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public static function generateMasterKey(EcAdapterInterface $ecAdapter = null, ScriptDataFactory $scriptDataFactory = null): HierarchicalKey |
41
|
|
|
{ |
42
|
|
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
43
|
|
|
$buffer = PrivateKeyFactory::create(true, $ecAdapter); |
44
|
|
|
return self::fromEntropy($buffer->getBuffer(), $ecAdapter, $scriptDataFactory); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param BufferInterface $entropy |
49
|
|
|
* @param ScriptDataFactory|null $scriptFactory |
50
|
|
|
* @return HierarchicalKey |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
|
|
public static function fromEntropy(BufferInterface $entropy, EcAdapterInterface $ecAdapter = null, ScriptDataFactory $scriptFactory = null): HierarchicalKey |
54
|
|
|
{ |
55
|
|
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
56
|
|
|
$scriptFactory = $scriptFactory ?: new P2pkhScriptDataFactory(EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter)); |
57
|
|
|
$seed = Hash::hmac('sha512', $entropy, new Buffer('Bitcoin seed', null, $ecAdapter->getMath())); |
|
|
|
|
58
|
|
|
$privateKey = PrivateKeyFactory::fromBuffer($seed->slice(0, 32), true, $ecAdapter); |
59
|
|
|
return new HierarchicalKey($ecAdapter, $scriptFactory, 0, 0, 0, $seed->slice(32, 32), $privateKey); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $extendedKey |
64
|
|
|
* @param NetworkInterface $network |
65
|
|
|
* @param EcAdapterInterface $ecAdapter |
66
|
|
|
* @return HierarchicalKey |
67
|
|
|
*/ |
68
|
|
|
public static function fromExtended($extendedKey, NetworkInterface $network = null, EcAdapterInterface $ecAdapter = null): HierarchicalKey |
69
|
|
|
{ |
70
|
|
|
$extSerializer = self::getSerializer($ecAdapter ?: Bitcoin::getEcAdapter()); |
71
|
|
|
return $extSerializer->parse($network ?: Bitcoin::getNetwork(), $extendedKey); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.