1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Chain\Params; |
6
|
|
|
use BitWasp\Bitcoin\Chain\ParamsInterface; |
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory; |
9
|
|
|
use BitWasp\Bitcoin\Math\Math; |
10
|
|
|
use BitWasp\Bitcoin\Network\Network; |
11
|
|
|
use BitWasp\Bitcoin\Network\NetworkFactory; |
12
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
13
|
|
|
use Mdanter\Ecc\EccFactory; |
14
|
|
|
use Mdanter\Ecc\Primitives\GeneratorPoint; |
15
|
|
|
|
16
|
|
|
class Bitcoin |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var NetworkInterface |
20
|
|
|
*/ |
21
|
|
|
private static $network; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var EcAdapterInterface |
25
|
|
|
*/ |
26
|
|
|
private static $adapter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ParamsInterface |
30
|
|
|
*/ |
31
|
|
|
private static $params; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Math |
35
|
|
|
*/ |
36
|
2928 |
|
public static function getMath() |
37
|
|
|
{ |
38
|
2928 |
|
return new Math(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Load the generator to be used throughout |
43
|
|
|
*/ |
44
|
4 |
|
public static function getGenerator() |
45
|
|
|
{ |
46
|
4 |
|
return EccFactory::getSecgCurves(self::getMath())->generator256k1(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Math $math |
51
|
|
|
* @param GeneratorPoint $generator |
52
|
|
|
* @return EcAdapterInterface |
53
|
|
|
*/ |
54
|
296 |
|
public static function getEcAdapter(Math $math = null, GeneratorPoint $generator = null) |
55
|
|
|
{ |
56
|
296 |
|
if (null === self::$adapter) { |
57
|
|
|
self::$adapter = EcAdapterFactory::getAdapter( |
58
|
|
|
($math ?: self::getMath()), |
59
|
|
|
($generator ?: self::getGenerator()) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
296 |
|
return self::$adapter; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ParamsInterface $params |
68
|
|
|
*/ |
69
|
|
|
public static function setParams(ParamsInterface $params) |
70
|
|
|
{ |
71
|
|
|
self::$params = $params; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return ParamsInterface |
76
|
|
|
*/ |
77
|
|
|
public static function getParams() |
78
|
|
|
{ |
79
|
|
|
if (null === self::$params) { |
80
|
|
|
self::$params = self::getDefaultParams(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return self::$params; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Math|null $math |
88
|
|
|
* @return ParamsInterface |
89
|
|
|
*/ |
90
|
|
|
public static function getDefaultParams(Math $math = null) |
91
|
|
|
{ |
92
|
|
|
return new Params($math ?: self::getMath()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param EcAdapterInterface $adapter |
97
|
|
|
*/ |
98
|
|
|
public static function setAdapter(EcAdapterInterface $adapter) |
99
|
|
|
{ |
100
|
|
|
self::$adapter = $adapter; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param NetworkInterface $network |
105
|
|
|
*/ |
106
|
2 |
|
public static function setNetwork(NetworkInterface $network) |
107
|
|
|
{ |
108
|
2 |
|
self::$network = $network; |
109
|
2 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Network |
113
|
|
|
*/ |
114
|
54 |
|
public static function getNetwork() |
115
|
|
|
{ |
116
|
54 |
|
if (null === self::$network) { |
117
|
|
|
self::$network = self::getDefaultNetwork(); |
118
|
|
|
} |
119
|
|
|
|
120
|
54 |
|
return self::$network; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return NetworkInterface |
125
|
|
|
*/ |
126
|
4 |
|
public static function getDefaultNetwork() |
127
|
|
|
{ |
128
|
4 |
|
return NetworkFactory::bitcoin(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|