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